| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php
- namespace app\admin\controller\service\shop;
- use app\common\controller\Backend;
- use think\Db;
- /**
- * 优惠券管理
- *
- * @icon fa fa-circle-o
- */
- class Coupon extends Backend
- {
- /**
- * Coupon模型对象
- * @var \app\admin\model\service\shop\Coupon
- */
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\service\shop\Coupon;
- $this->view->assign("typeList", $this->model->getTypeList());
- $this->view->assign("isCheckList", $this->model->getIsCheckList());
- }
- public function index()
- {
- //设置过滤方法
- $this->request->filter(['strip_tags', 'trim']);
- if (false === $this->request->isAjax()) {
- return $this->view->fetch();
- }
- //如果发送的来源是 Selectpage,则转发到 Selectpage
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- [$where, $sort, $order, $offset, $limit] = $this->buildparams();
- $list = $this->model
- ->where($where)
- ->where('shop_id','>',0)
- ->order($sort, $order)
- ->paginate($limit);
- foreach ($list as &$value)
- {
- $value->shopname = \app\admin\model\service\shop\Shop::where('id',$value->shop_id)->value('name');
- }
- $result = ['total' => $list->total(), 'rows' => $list->items()];
- return json($result);
- }
- public function edit($ids = null)
- {
- $row = $this->model->get($ids);
- if (!$row) {
- $this->error(__('No Results were found'));
- }
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
- $this->error(__('You have no permission'));
- }
- if (false === $this->request->isPost()) {
- $this->view->assign('row', $row);
- return $this->view->fetch();
- }
- $params = $this->request->post('row/a');
- if (empty($params)) {
- $this->error(__('Parameter %s can not be empty', ''));
- }
- ($params['type'] == 4 && !$params['code']) && $this->error('口令优惠券请添加口令');
- ($params['type'] == 1 && !$params['goods_id']) && $this->error('项目券请选择项目');
- $params = $this->preExcludeFields($params);
- $result = false;
- Db::startTrans();
- try {
- //是否采用模型验证
- if ($this->modelValidate) {
- $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
- $row->validateFailException()->validate($validate);
- }
- $result = $row->allowField(true)->save($params);
- Db::commit();
- } catch (ValidateException|PDOException|Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if (false === $result) {
- $this->error(__('No rows were updated'));
- }
- $this->success();
- }
- }
|