NewCoupon.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\Exception;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. /**
  9. * 优惠券
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class NewCoupon extends Backend
  14. {
  15. /**
  16. * Coupon模型对象
  17. * @var \app\admin\model\Coupon
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\Coupon;
  24. $this->view->assign("couponTypeList", $this->model->getCouponTypeList());
  25. $this->view->assign("couponNameList", $this->model->getCouponNameList());
  26. $this->view->assign("couponStatusList", $this->model->getCouponStatusList());
  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->request->filter(['strip_tags', 'trim']);
  44. if ($this->request->isAjax()) {
  45. //如果发送的来源是Selectpage,则转发到Selectpage
  46. if ($this->request->request('keyField')) {
  47. return $this->selectpage();
  48. }
  49. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  50. $where2['coupon_name'] = 1;
  51. $list = $this->model
  52. ->where($where2)
  53. ->where($where)
  54. ->order($sort, $order)
  55. ->paginate($limit);
  56. $result = array("total" => $list->total(), "rows" => $list->items());
  57. return json($result);
  58. }
  59. return $this->view->fetch();
  60. }
  61. /**
  62. * 添加
  63. */
  64. public function add()
  65. {
  66. if ($this->request->isPost()) {
  67. $params = $this->request->post("row/a");
  68. if ($params) {
  69. $params = $this->preExcludeFields($params);
  70. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  71. $params[$this->dataLimitField] = $this->auth->id;
  72. }
  73. $result = false;
  74. Db::startTrans();
  75. try {
  76. //是否采用模型验证
  77. if ($this->modelValidate) {
  78. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  79. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  80. $this->model->validateFailException(true)->validate($validate);
  81. }
  82. //$params['expiration'] = $params['expiration']*86400;
  83. $result = $this->model->allowField(true)->save($params);
  84. Db::commit();
  85. } catch (ValidateException $e) {
  86. Db::rollback();
  87. $this->error($e->getMessage());
  88. } catch (PDOException $e) {
  89. Db::rollback();
  90. $this->error($e->getMessage());
  91. } catch (Exception $e) {
  92. Db::rollback();
  93. $this->error($e->getMessage());
  94. }
  95. if ($result !== false) {
  96. $this->success();
  97. } else {
  98. $this->error(__('No rows were inserted'));
  99. }
  100. }
  101. $this->error(__('Parameter %s can not be empty', ''));
  102. }
  103. return $this->view->fetch();
  104. }
  105. /**
  106. * 编辑
  107. */
  108. public function edit($ids = null)
  109. {
  110. $row = $this->model->get($ids);
  111. if (!$row) {
  112. $this->error(__('No Results were found'));
  113. }
  114. $adminIds = $this->getDataLimitAdminIds();
  115. if (is_array($adminIds)) {
  116. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  117. $this->error(__('You have no permission'));
  118. }
  119. }
  120. if ($this->request->isPost()) {
  121. $params = $this->request->post("row/a");
  122. if ($params) {
  123. $params = $this->preExcludeFields($params);
  124. $result = false;
  125. Db::startTrans();
  126. try {
  127. //是否采用模型验证
  128. if ($this->modelValidate) {
  129. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  130. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  131. $row->validateFailException(true)->validate($validate);
  132. }
  133. $result = $row->allowField(true)->save($params);
  134. Db::commit();
  135. } catch (ValidateException $e) {
  136. Db::rollback();
  137. $this->error($e->getMessage());
  138. } catch (PDOException $e) {
  139. Db::rollback();
  140. $this->error($e->getMessage());
  141. } catch (Exception $e) {
  142. Db::rollback();
  143. $this->error($e->getMessage());
  144. }
  145. if ($result !== false) {
  146. $this->success();
  147. } else {
  148. $this->error(__('No rows were updated'));
  149. }
  150. }
  151. $this->error(__('Parameter %s can not be empty', ''));
  152. }
  153. $this->view->assign("row", $row);
  154. return $this->view->fetch();
  155. }
  156. public function issue_user($ids){
  157. $coupon = $this->model->where('id',$ids)->find();
  158. $this->assign('coupon',$coupon);
  159. return $this->view->fetch();
  160. }
  161. public function post_user(){
  162. if($this->request->isPost()){
  163. $param = $this->request->post("row/a");
  164. $userAll = explode(',',$param['user_id']);
  165. foreach ($userAll as $k=>$v){
  166. $data[] = [
  167. 'user_id' => $v,
  168. 'coupon_id' => $param['coupon_id'],
  169. 'issuance_time' => strtotime($param['issuance_time']),
  170. 'expiration' => $param['expiration'],
  171. 'createtime' =>time()
  172. ];
  173. }
  174. $user_coupon = (new \app\admin\model\UserCoupon())->saveAll($data);
  175. $this->success('发放成功');
  176. }
  177. }
  178. }