Raprize.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\exception\DbException;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. use think\response\Json;
  9. /**
  10. * 活动奖品管理
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Raprize extends Backend
  15. {
  16. /**
  17. * Raprize模型对象
  18. * @var \app\admin\model\Raprize
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\Raprize;
  25. $this->view->assign("statusList", $this->model->getStatusList());
  26. }
  27. /**
  28. * 查看
  29. *
  30. * @return string|Json
  31. * @throws \think\Exception
  32. * @throws DbException
  33. */
  34. public function index()
  35. {
  36. $ra_id=input('ids');
  37. if(empty($ra_id)){
  38. $ra_id=session('ra_id');
  39. }else{
  40. session('ra_id',$ra_id);
  41. }
  42. //设置过滤方法
  43. $this->request->filter(['strip_tags', 'trim']);
  44. if (false === $this->request->isAjax()) {
  45. return $this->view->fetch();
  46. }
  47. //如果发送的来源是 Selectpage,则转发到 Selectpage
  48. if ($this->request->request('keyField')) {
  49. return $this->selectpage();
  50. }
  51. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  52. $dts['r_id'] =$ra_id;
  53. $list = $this->model
  54. ->where($where)
  55. ->where($dts)
  56. ->order($sort, $order)
  57. ->paginate($limit);
  58. $result = ['total' => $list->total(), 'rows' => $list->items()];
  59. return json($result);
  60. }
  61. /**
  62. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  63. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  64. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  65. */
  66. /**
  67. * 添加
  68. *
  69. * @return string
  70. * @throws \think\Exception
  71. */
  72. public function add()
  73. {
  74. if (false === $this->request->isPost()) {
  75. return $this->view->fetch();
  76. }
  77. $params = $this->request->post('row/a');
  78. if (empty($params)) {
  79. $this->error(__('Parameter %s can not be empty', ''));
  80. }
  81. $params = $this->preExcludeFields($params);
  82. $ra_id=session('ra_id');
  83. $params['r_id']=$ra_id;
  84. $raffle = \app\admin\model\Raffle::getById($ra_id);
  85. $params['r_name']=$raffle->name;
  86. $category=\app\common\model\Category::getById($params['t_type']);
  87. $params['t_type_name']=$category->name;
  88. $prize=\app\admin\model\Prize::getById($params['p_id']);
  89. $params['p_name']=$prize->name;
  90. $params['price']=$prize->price;
  91. $params['money']=$prize->money;
  92. $params['description']=$prize->code;
  93. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  94. $params[$this->dataLimitField] = $this->auth->id;
  95. }
  96. $result = false;
  97. Db::startTrans();
  98. try {
  99. //是否采用模型验证
  100. if ($this->modelValidate) {
  101. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  102. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  103. $this->model->validateFailException()->validate($validate);
  104. }
  105. $result = $this->model->allowField(true)->save($params);
  106. Db::commit();
  107. } catch (ValidateException|PDOException|Exception $e) {
  108. Db::rollback();
  109. $this->error($e->getMessage());
  110. }
  111. if ($result === false) {
  112. $this->error(__('No rows were inserted'));
  113. }
  114. $this->success();
  115. }
  116. public function edit($ids = null)
  117. {
  118. $row = $this->model->get($ids);
  119. if (!$row) {
  120. $this->error(__('No Results were found'));
  121. }
  122. $adminIds = $this->getDataLimitAdminIds();
  123. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  124. $this->error(__('You have no permission'));
  125. }
  126. if (false === $this->request->isPost()) {
  127. $this->view->assign('row', $row);
  128. return $this->view->fetch();
  129. }
  130. $params = $this->request->post('row/a');
  131. if (empty($params)) {
  132. $this->error(__('Parameter %s can not be empty', ''));
  133. }
  134. $params = $this->preExcludeFields($params);
  135. $ra_id=session('ra_id');
  136. $params['r_id']=$ra_id;
  137. $raffle = \app\admin\model\Raffle::getById($ra_id);
  138. $params['r_name']=$raffle->name;
  139. $category=\app\common\model\Category::getById($params['t_type']);
  140. $params['t_type_name']=$category->name;
  141. $prize=\app\admin\model\Prize::getById($params['p_id']);
  142. $params['p_name']=$prize->name;
  143. $params['price']=$prize->price;
  144. $params['money']=$prize->money;
  145. $params['description']=$prize->code;
  146. $result = false;
  147. Db::startTrans();
  148. try {
  149. //是否采用模型验证
  150. if ($this->modelValidate) {
  151. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  152. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  153. $row->validateFailException()->validate($validate);
  154. }
  155. $result = $row->allowField(true)->save($params);
  156. Db::commit();
  157. } catch (ValidateException|PDOException|Exception $e) {
  158. Db::rollback();
  159. $this->error($e->getMessage());
  160. }
  161. if (false === $result) {
  162. $this->error(__('No rows were updated'));
  163. }
  164. $this->success();
  165. }
  166. }