Recharge.php 6.2 KB

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