Condition.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace app\admin\controller\lottery;
  3. use app\common\controller\Backend;
  4. /**
  5. * 抽奖条件管理
  6. *
  7. * @icon fa fa-tasks
  8. */
  9. class Condition extends Backend
  10. {
  11. /**
  12. * Condition模型对象
  13. * @var \app\admin\model\lottery\Condition
  14. */
  15. protected $model = null;
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. $this->model = new \app\admin\model\lottery\Condition;
  20. // 获取枚举值列表
  21. $this->view->assign("typeList", $this->model->getTypeList());
  22. $this->view->assign("goodsRuleList", $this->model->getGoodsRuleList());
  23. $this->view->assign("statusList", $this->model->getStatusList());
  24. }
  25. /**
  26. * 查看
  27. */
  28. public function index()
  29. {
  30. //当前是否为关联查询
  31. $this->relationSearch = true;
  32. //设置过滤方法
  33. $this->request->filter(['strip_tags', 'trim']);
  34. if ($this->request->isAjax()) {
  35. //如果发送的来源是Selectpage,则转发到Selectpage
  36. if ($this->request->request('keyField')) {
  37. return $this->selectpage();
  38. }
  39. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  40. $list = $this->model
  41. ->with(['activity'])
  42. ->where($where)
  43. ->order($sort, $order)
  44. ->paginate($limit);
  45. foreach ($list as $row) {
  46. $row->visible(['id','activity_id','type','condition_value','reward_times','is_repeatable','status']);
  47. $row->visible(['activity']);
  48. $row->getRelation('activity')->visible(['name']);
  49. }
  50. $result = array("total" => $list->total(), "rows" => $list->items());
  51. return json($result);
  52. }
  53. return $this->view->fetch();
  54. }
  55. /**
  56. * 添加
  57. */
  58. public function add()
  59. {
  60. if ($this->request->isPost()) {
  61. $params = $this->request->post("row/a");
  62. if ($params) {
  63. $params = $this->preExcludeFields($params);
  64. // 处理商品ID列表
  65. if (isset($params['goods_ids']) && is_array($params['goods_ids'])) {
  66. $params['goods_ids'] = json_encode($params['goods_ids']);
  67. }
  68. $result = false;
  69. \think\Db::startTrans();
  70. try {
  71. //是否采用模型验证
  72. if ($this->modelValidate) {
  73. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  74. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  75. $this->model->validateFailException(true)->validate($validate);
  76. }
  77. $result = $this->model->allowField(true)->save($params);
  78. \think\Db::commit();
  79. } catch (\think\exception\ValidateException $e) {
  80. \think\Db::rollback();
  81. $this->error($e->getMessage());
  82. } catch (\think\exception\PDOException $e) {
  83. \think\Db::rollback();
  84. $this->error($e->getMessage());
  85. } catch (\Exception $e) {
  86. \think\Db::rollback();
  87. $this->error($e->getMessage());
  88. }
  89. if ($result !== false) {
  90. $this->success();
  91. } else {
  92. $this->error(__('No rows were inserted'));
  93. }
  94. }
  95. $this->error(__('Parameter %s can not be empty', ''));
  96. }
  97. return $this->view->fetch();
  98. }
  99. /**
  100. * 编辑
  101. */
  102. public function edit($ids = null)
  103. {
  104. $row = $this->model->get($ids);
  105. if (!$row) {
  106. $this->error(__('No Results were found'));
  107. }
  108. $adminIds = $this->getDataLimitAdminIds();
  109. if (is_array($adminIds)) {
  110. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  111. $this->error(__('You have no permission'));
  112. }
  113. }
  114. if ($this->request->isPost()) {
  115. $params = $this->request->post("row/a");
  116. if ($params) {
  117. $params = $this->preExcludeFields($params);
  118. // 处理商品ID列表
  119. if (isset($params['goods_ids']) && is_array($params['goods_ids'])) {
  120. $params['goods_ids'] = json_encode($params['goods_ids']);
  121. }
  122. $result = false;
  123. \think\Db::startTrans();
  124. try {
  125. //是否采用模型验证
  126. if ($this->modelValidate) {
  127. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  128. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  129. $row->validateFailException(true)->validate($validate);
  130. }
  131. $result = $row->allowField(true)->save($params);
  132. \think\Db::commit();
  133. } catch (\think\exception\ValidateException $e) {
  134. \think\Db::rollback();
  135. $this->error($e->getMessage());
  136. } catch (\think\exception\PDOException $e) {
  137. \think\Db::rollback();
  138. $this->error($e->getMessage());
  139. } catch (\Exception $e) {
  140. \think\Db::rollback();
  141. $this->error($e->getMessage());
  142. }
  143. if ($result !== false) {
  144. $this->success();
  145. } else {
  146. $this->error(__('No rows were updated'));
  147. }
  148. }
  149. $this->error(__('Parameter %s can not be empty', ''));
  150. }
  151. // 处理商品ID列表显示
  152. if ($row->goods_ids) {
  153. $row->goods_ids = json_decode($row->goods_ids, true);
  154. }
  155. $this->view->assign("row", $row);
  156. return $this->view->fetch();
  157. }
  158. }