Back.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace app\admin\controller\gift;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. * 礼物背包
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Back extends Backend
  11. {
  12. /**
  13. * Back模型对象
  14. * @var \app\admin\model\gift\Back
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\gift\Back;
  21. $typeList = [
  22. 'isUseList' => $this->model->getIsUseList(),
  23. 'getWayList' => $this->model->getGetWayList(),
  24. ];
  25. $this->view->assign($typeList);
  26. $this->assignconfig($typeList);
  27. }
  28. public function import()
  29. {
  30. parent::import();
  31. }
  32. /**
  33. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  34. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  35. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  36. */
  37. /**
  38. * 查看
  39. */
  40. public function index()
  41. {
  42. //当前是否为关联查询
  43. $this->relationSearch = true;
  44. //设置过滤方法
  45. $this->request->filter(['strip_tags', 'trim']);
  46. if ($this->request->isAjax()) {
  47. //如果发送的来源是Selectpage,则转发到Selectpage
  48. if ($this->request->request('keyField')) {
  49. return $this->selectpage();
  50. }
  51. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  52. $list = $this->model
  53. ->with(['user'])
  54. ->where($where)
  55. ->order($sort, $order)
  56. ->paginate($limit);
  57. foreach ($list as $row) {
  58. // $row->visible(['id','image','special','value','is_use','use_time','get_way','createtime']);
  59. // $row->visible(['user']);
  60. $row->getRelation('user')->visible(['u_id','nickname']);
  61. }
  62. $result = array("total" => $list->total(), "rows" => $list->items());
  63. return json($result);
  64. }
  65. return $this->view->fetch();
  66. }
  67. /**
  68. * 添加
  69. */
  70. public function add()
  71. {
  72. if ($this->request->isPost()) {
  73. $params = $this->request->post("row/a");
  74. if ($params) {
  75. $params = $this->preExcludeFields($params);
  76. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  77. $params[$this->dataLimitField] = $this->auth->id;
  78. }
  79. $result = false;
  80. Db::startTrans();
  81. try {
  82. //是否采用模型验证
  83. if ($this->modelValidate) {
  84. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  85. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  86. $this->model->validateFailException(true)->validate($validate);
  87. }
  88. //多加几个
  89. $number = intval(input('number',1));
  90. if($number < 1) {$number = 1;}
  91. $gift = Db::name('gift')->where('id',$params['gift_id'])->find();
  92. $params['name'] = $gift['name'];
  93. $params['image'] = $gift['image'];
  94. $params['gif_image'] = $gift['special'];
  95. $params['value'] = $gift['price'];
  96. $params['number'] = 1;
  97. $params['is_use'] = 0;
  98. $saveall = [];
  99. for($i=1;$i<=$number;$i++){
  100. $saveall[] = $params;
  101. }
  102. $result = Db::name('gift_back')->insertAll($saveall);
  103. Db::commit();
  104. } catch (ValidateException $e) {
  105. Db::rollback();
  106. $this->error($e->getMessage());
  107. } catch (PDOException $e) {
  108. Db::rollback();
  109. $this->error($e->getMessage());
  110. } catch (Exception $e) {
  111. Db::rollback();
  112. $this->error($e->getMessage());
  113. }
  114. if ($result !== false) {
  115. $this->success();
  116. } else {
  117. $this->error(__('No rows were inserted'));
  118. }
  119. }
  120. $this->error(__('Parameter %s can not be empty', ''));
  121. }
  122. return $this->view->fetch();
  123. }
  124. }