UserCoupon.php 6.1 KB

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