| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- <?php
- namespace addons\Certificate\backend\controllers;
- use addons\Certificate\common\models\Certificate;
- use common\enums\StatusEnum;
- use common\models\user\UserLevel;
- use Yii;
- use yii\db\Exception;
- use common\controllers\AddonsController;
- /**
- * 默认控制器
- *
- * Class DefaultController
- * @package addons\Certificate\backend\controllers
- */
- class DefaultController extends BaseController
- {
- public $enableCsrfValidation = false;
- /**
- * 首页
- *
- * @return string
- */
- public function actionIndex()
- {
- $retuest = Yii::$app->request;
- if ($retuest->isAjax) {
- if ($retuest->isGet) {
- $params = Yii::$app->request->get();
- // 检查提交的参数
- $res = Yii::$app->services->validate->validate($params,[
- [['id','name','create_time'], 'safe'],
- [['limit'],'default','value' => '15'],
- ]);
- if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $where = [];
- if($params['create_time']) $where[] = ['between', 'created_at', strtotime($params['create_time'][0]),strtotime($params['create_time'][1])];
- if(!empty($params['name'])) $where[] = ['like', 'name', $params['name']];
- $where[] = ['=', 'mall_id', $this->mall_id];
- $where[] = ['>=', 'status', StatusEnum::DISABLED];
- $order = 'id desc';
- $params = array('where' => $where, 'order'=>$order, 'limit' => $params['limit']);
- $list = Certificate::listPage($params, false, true);
- $this->success('获取成功', $list);
- }
- } else {
- return $this->render($this->action->id);
- }
- }
- /**
- * 编辑
- * @Author: ltm
- * @DateTime: 2022/5/8 0021 11:29
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|string|void
- */
- public function actionEdit()
- {
- try {
- $request = Yii::$app->request;
- if ($request->isAjax) {
- // 提交内容
- if ($request->isPost) {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['name','id','content','status','member_level'], 'safe'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params = Certificate::exceptNullVal($params);
- $params['mall_id'] = $this->mall_id;
- if(!empty($params['content']))$params['content'] = json_encode($params['content']);
- if(isset($params['status']) && !in_array($params['status'],[StatusEnum::DISABLED,StatusEnum::ENABLED,StatusEnum::DELETE])) throw new Exception("参数错误");;
- !isset($params['member_level']) && $params['member_level'] = [];
- $res = Certificate::setData($params);
- if ($res === false) throw new Exception(Certificate::getStaticError());
- return $this->success('操作成功');
- }else{
- $params = Yii::$app->request->get();
- $res = Yii::$app->services->validate->validate($params, [
- [['id'], 'safe'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $where = [];
- $where[] = ['=' ,'mall_id',$this->mall_id];
- $where[] = ['=' ,'id',$params['id']];
- $detail = Certificate::getOne($where,true);
- $detail['member_level'] = $detail['member_level'] !== '' ? explode(',', $detail['member_level']) : [];
- $detail['content'] = json_decode($detail['content']);
- $detail['levels'] = UserLevel::getUserLevel([['mall_id' => $this->mall_id], ['status' => StatusEnum::ENABLED]], [], true, 'id,level,name');
- $detail['levels'] = array_merge([['level' => 0, 'name' => '普通用户']], $detail['levels']);
- return $this->success('操作成功',$detail);
- }
- }
- } catch (\Exception $e) {
- return $this->error($e->getMessage(), []);
- }
- return $this->render($this->action->id);
- }
- /**
- * 等级列表
- *
- * @return mixed|null
- */
- public function actionGetLevelList()
- {
- $levels = UserLevel::getUserLevel([['mall_id' => $this->mall_id], ['status' => StatusEnum::ENABLED]], [], true, 'id,level,name');
- return $this->success('success', array_merge([['level' => 0, 'name' => '普通用户']], $levels));
- }
- }
|