Coupon.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace app\admin\controller\service;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use fast\Random;
  6. /**
  7. * 优惠券管理
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class Coupon extends Backend
  12. {
  13. /**
  14. * Coupon模型对象
  15. * @var \app\admin\model\service\Coupon
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new \app\admin\model\service\Coupon;
  22. $this->view->assign("typeList", $this->model->getTypeList());
  23. $this->view->assign("isCheckList", $this->model->getIsCheckList());
  24. }
  25. public function index()
  26. {
  27. //设置过滤方法
  28. $this->request->filter(['strip_tags', 'trim']);
  29. if (false === $this->request->isAjax()) {
  30. return $this->view->fetch();
  31. }
  32. //如果发送的来源是 Selectpage,则转发到 Selectpage
  33. if ($this->request->request('keyField')) {
  34. return $this->selectpage();
  35. }
  36. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  37. $list = $this->model
  38. ->where($where)
  39. ->where('shop_id',0)
  40. ->order($sort, $order)
  41. ->paginate($limit);
  42. $result = ['total' => $list->total(), 'rows' => $list->items()];
  43. return json($result);
  44. }
  45. /**
  46. * 添加
  47. * @return string|void
  48. * @throws \think\Exception
  49. */
  50. public function add()
  51. {
  52. if (false === $this->request->isPost()) {
  53. return $this->view->fetch();
  54. }
  55. $params = $this->request->post('row/a');
  56. if (empty($params)) {
  57. $this->error(__('Parameter %s can not be empty', ''));
  58. }
  59. $params = $this->preExcludeFields($params);
  60. $params['is_check'] = 1;
  61. ($params['type'] == 1 && !$params['goods_id']) && $this->error('项目券请选择项目');
  62. if($params['type'] == 4)
  63. {
  64. $params['code'] = Random::alnum(12);
  65. }
  66. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  67. $params[$this->dataLimitField] = $this->auth->id;
  68. }
  69. $result = false;
  70. Db::startTrans();
  71. try {
  72. //是否采用模型验证
  73. if ($this->modelValidate) {
  74. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  75. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  76. $this->model->validateFailException()->validate($validate);
  77. }
  78. $result = $this->model->allowField(true)->save($params);
  79. Db::commit();
  80. } catch (ValidateException|PDOException|Exception $e) {
  81. Db::rollback();
  82. $this->error($e->getMessage());
  83. }
  84. if ($result === false) {
  85. $this->error(__('No rows were inserted'));
  86. }
  87. $this->success();
  88. }
  89. public function edit($ids = null)
  90. {
  91. $row = $this->model->get($ids);
  92. if (!$row) {
  93. $this->error(__('No Results were found'));
  94. }
  95. $adminIds = $this->getDataLimitAdminIds();
  96. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  97. $this->error(__('You have no permission'));
  98. }
  99. if (false === $this->request->isPost()) {
  100. $this->view->assign('row', $row);
  101. return $this->view->fetch();
  102. }
  103. $params = $this->request->post('row/a');
  104. if (empty($params)) {
  105. $this->error(__('Parameter %s can not be empty', ''));
  106. }
  107. ($params['type'] == 1 && !$params['goods_id']) && $this->error('项目券请选择项目');
  108. if($params['type'] == 4 && $row->type != 4)
  109. {
  110. $params['code'] = Random::alnum(12);
  111. }
  112. $params = $this->preExcludeFields($params);
  113. $result = false;
  114. Db::startTrans();
  115. try {
  116. //是否采用模型验证
  117. if ($this->modelValidate) {
  118. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  119. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  120. $row->validateFailException()->validate($validate);
  121. }
  122. $result = $row->allowField(true)->save($params);
  123. Db::commit();
  124. } catch (ValidateException|PDOException|Exception $e) {
  125. Db::rollback();
  126. $this->error($e->getMessage());
  127. }
  128. if (false === $result) {
  129. $this->error(__('No rows were updated'));
  130. }
  131. $this->success();
  132. }
  133. }