123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php
- namespace app\admin\controller;
- use app\common\controller\Backend;
- use Think\Db;
- /**
- * 优惠券管理
- *
- * @icon fa fa-circle-o
- */
- class Coupon extends Backend
- {
- /**
- * Coupon模型对象
- * @var \app\admin\model\Coupon
- */
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\Coupon;
- $this->view->assign("typeList", $this->model->getTypeList());
- $this->view->assign("purposeList", $this->model->getPurposeList());
- $this->view->assign("statusList", $this->model->getStatusList());
- }
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
- * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
- /**
- * 添加
- */
- public function add() {
- if ($this->request->isPost()) {
- $params = $this->request->post("row/a");
- if ($params) {
- $params = $this->preExcludeFields($params);
- if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
- $params[$this->dataLimitField] = $this->auth->id;
- }
- if ($params['type'] == 1) { //折扣券
- $params['money'] = (int)$params['money'];
- if ($params['money'] <= 0 || $params['money'] >= 100) {
- $this->error('输入正确打折券(百分比)折数');
- }
- }
- if ($params['type'] == 2) { //抵扣券
- if (!preg_match('/^[0-9]+(.[0-9]{1,2})?$/', $params['money']) || $params['money'] <= 0) {
- $this->error('请输入正确抵扣券面值金额');
- }
- if (!preg_match('/^[0-9]+(.[0-9]{1,2})?$/', $params['minmoney']) || $params['minmoney'] <= 0) {
- $this->error('请输入正确抵扣券最低消费金额');
- }
- if ($params['minmoney'] <= $params['money']) {
- $this->error('抵扣券最低消费金额必须大于抵扣金额');
- }
- }
- $params['effectiveday'] = (int)$params['effectiveday']; //有效天数
- if ($params['effectiveday'] <= 0) {
- $this->error('请输入正确有效天数');
- }
- $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 . '.add' : $name) : $this->modelValidate;
- $this->model->validateFailException(true)->validate($validate);
- }
- $result = $this->model->allowField(true)->save($params);
- Db::commit();
- } catch (ValidateException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (PDOException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result !== false) {
- $this->success();
- } else {
- $this->error(__('No rows were inserted'));
- }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- return $this->view->fetch();
- }
- /**
- * 修改优惠券状态
- */
- public function chanagestatus() {
- $ids = input('ids', 0, 'intval'); //优惠券id
- $status = input('status', 0, 'intval'); //状态:1=发布,2=下架
- if (!$ids) {
- $this->error('参数缺失');
- }
- if (!in_array($status, [1, 2])) {
- $this->error('参数错误');
- }
- $info = Db::name('coupon')->find($ids);
- if (!$info) {
- $this->error('优惠券不存在');
- }
- if ($status == 1 && $info['status'] != 0) {
- $this->error('优惠券状态已改变');
- }
- if ($status == 2) {
- if ($info['status'] != 1) {
- $this->error('优惠券状态已改变');
- }
- if ($info['purpose'] == 0) { //获得方式:0=充值,1=注册,2=推广,3=后台发放,4=轮播图,5=报名活动
- //判断是否有绑定充值
- $count = Db::name('recharge')->where(['coupon_id' => $ids])->find();
- if ($count) {
- $this->error('请先取消该优惠券与充值列表的绑定关系');
- }
- }
- }
- $rs = Db::name('coupon')->where(['id' => $ids, 'status' => $info['status']])->setField('status', $status);
- if (!$rs) {
- $this->error('操作失败');
- }
- $this->success('操作成功');
- }
- /**
- * 删除
- */
- public function delcoupon($ids = "")
- {
- if (!$this->request->isPost()) {
- $this->error(__("Invalid parameters"));
- }
- $ids = $ids ? $ids : $this->request->post("ids");
- $info = Db::name('coupon')->find($ids);
- if (!$info) {
- $this->error('操作成功');
- }
- if ($info['status'] != 2) {
- $this->error('请先下架优惠券');
- }
- Db::name('coupon')->delete($ids);
- $this->success('操作成功');
- }
- }
|