Coupon.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace app\admin\controller\service\shop;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. * 优惠券管理
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Coupon extends Backend
  11. {
  12. /**
  13. * Coupon模型对象
  14. * @var \app\admin\model\service\shop\Coupon
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\service\shop\Coupon;
  21. $this->view->assign("typeList", $this->model->getTypeList());
  22. $this->view->assign("isCheckList", $this->model->getIsCheckList());
  23. }
  24. public function index()
  25. {
  26. //设置过滤方法
  27. $this->request->filter(['strip_tags', 'trim']);
  28. if (false === $this->request->isAjax()) {
  29. return $this->view->fetch();
  30. }
  31. //如果发送的来源是 Selectpage,则转发到 Selectpage
  32. if ($this->request->request('keyField')) {
  33. return $this->selectpage();
  34. }
  35. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  36. $list = $this->model
  37. ->where($where)
  38. ->where('shop_id','>',0)
  39. ->order($sort, $order)
  40. ->paginate($limit);
  41. foreach ($list as &$value)
  42. {
  43. $value->shopname = \app\admin\model\service\shop\Shop::where('id',$value->shop_id)->value('name');
  44. }
  45. $result = ['total' => $list->total(), 'rows' => $list->items()];
  46. return json($result);
  47. }
  48. public function edit($ids = null)
  49. {
  50. $row = $this->model->get($ids);
  51. if (!$row) {
  52. $this->error(__('No Results were found'));
  53. }
  54. $adminIds = $this->getDataLimitAdminIds();
  55. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  56. $this->error(__('You have no permission'));
  57. }
  58. if (false === $this->request->isPost()) {
  59. $this->view->assign('row', $row);
  60. return $this->view->fetch();
  61. }
  62. $params = $this->request->post('row/a');
  63. if (empty($params)) {
  64. $this->error(__('Parameter %s can not be empty', ''));
  65. }
  66. ($params['type'] == 4 && !$params['code']) && $this->error('口令优惠券请添加口令');
  67. ($params['type'] == 1 && !$params['goods_id']) && $this->error('项目券请选择项目');
  68. $params = $this->preExcludeFields($params);
  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 . '.edit' : $name) : $this->modelValidate;
  76. $row->validateFailException()->validate($validate);
  77. }
  78. $result = $row->allowField(true)->save($params);
  79. Db::commit();
  80. } catch (ValidateException|PDOException|Exception $e) {
  81. Db::rollback();
  82. $this->error($e->getMessage());
  83. }
  84. if (false === $result) {
  85. $this->error(__('No rows were updated'));
  86. }
  87. $this->success();
  88. }
  89. }