| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- <?php
- namespace backend\modules\mall\controllers;
- use addons\Xhs\common\models\AddonsXhsContent;
- use backend\controllers\MallController;
- use common\enums\StatusEnum;
- use common\enums\UserLabelEnum;
- use common\models\common\UpgradeCondition;
- use common\models\goods\Goods;
- use common\models\user\Tag;
- use common\models\user\User;
- use common\models\user\UserLabel;
- use common\models\user\UserLabelCond;
- use common\models\user\UserLabelRelationship;
- use common\models\user\UserLevel;
- use common\models\goods\GoodsService;
- use common\models\user\UserLevelUpgradeCondition;
- use PHPUnit\Util\Exception;
- use Yii;
- /**
- * 标签列表
- *
- * Class TagController
- * @package backend\modules\mall\TagController
- */
- class UserLabelController extends MallController
- {
- /**
- *
- * @return mixed|string|void
- */
- public function actionIndex()
- {
- $request = Yii::$app->request;
- if ($request->isAjax) {
- if ($request->isPost) {
- $post = Yii::$app->request->post();
- // 检查提交的参数
- $res = Yii::$app->services->validate->validate($post,[
- [['name','date_end','date_start','type'], 'safe'],
- ]);
- if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- // 查询参数
- $where[] = ['=', 'mall_id', $this->mall_id];
- $where[] = ['in', 'status', [StatusEnum::DISABLED, StatusEnum::ENABLED]];
- if(isset($post['name'])) $where[] = ['like', 'name', $post['name']];
- if(!empty($post['type'])) $where[] = ['=', 'type', $post['type']];
- if(!empty($post['date_start']) && !empty($post['date_end'])) $where[] = ['between', 'created_at', strtotime($post['date_start']),strtotime($post['date_end'])];
- $params = array('where' => $where, 'order' => 'id desc', 'limit' => $request->get('limit', 12));
- $params['with'] = ['labelCond'];
- $page_data = UserLabel::listPage($params, false);
- if($page_data['list']){
- foreach ($page_data['list'] as &$v){
- $v['cond_data'] = UserLabel::getLabelConf($v['labelCond']);
- }
- }
- return $this->success('操作成功', $page_data);
- }
- }
- return $this->render($this->action->id);
- }
- /**
- * @Author: ltm
- * @DateTime: 2022/3/10 0010 15:24
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $data
- * @return string
- */
- public function actionEdit()
- {
- try {
- $request = Yii::$app->request;
- if ($request->isAjax) {
- if ($request->isGet) {
- $list = [];
- $params = $request->get();
- if (!empty($params['id'])) {
- $where[] = ['=','id',$params['id']];
- $list = UserLabel::getOne($where,true);
- $conf_where[] = ['=','label_id',$params['id']];
- $conf = UserLabelCond::lists(['where' => $conf_where],true);
- $conf_data = [];
- if($conf){
- foreach ($conf as $k => $v){
- $conf_data[$v['cond_type']]['condition'] = json_decode($v['condition']);
- $conf_data[$v['cond_type']]['select_status'] = $v['select_status'];
- if($v['cond_type'] == UserLabelEnum::GOODS && $v['condition']){
- $goods_where[] = ['in','id',explode(',',json_decode($v['condition']))];
- $select = 'goods_name,price,cover_pic,stock';
- $goods_list = Goods::lists(['where' => $goods_where,'select' => $select],true);
- $list['goods_list'] = $goods_list;
- }
- }
- }
- $cond_enum = UserLabelEnum::getMap();
- $list['cond_enum'] = $cond_enum;
- $list['conf'] = $conf_data;
- }
- return $this->success('操作成功', $list);
- }
- //保存数据
- if ($request->isPost) {
- $params = Yii::$app->request->post('form',[]);
- if(empty($params)) return $this->error('参数不完整');
- $res = Yii::$app->services->validate->validate($params, [
- [['id','mall_id'], 'integer'],
- [['type'],'in','range'=>[UserLabelEnum::MANUAL,UserLabelEnum::AUTO]],
- [['satisfy_type'],'in','range'=>[UserLabelEnum::SATISFY_ONE,UserLabelEnum::SATISFY_ALL]],
- [['condition'],'safe'],
- [['color'],'safe'],
- [['name'],function($attr,$value){
- if(is_numeric($this->$attr)) $this->addError($attr,'标签名称不能为纯数字');
- }]
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- try {
- $params['mall_id'] = $this->mall_id;
- if(empty($params['color'])) $params['color'] = '';
- $label_model = UserLabel::setData($params);
- if ($label_model == false) throw new \Exception(UserLabel::getStaticError());
- return $this->success('操作成功');
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- }
- } catch (\Exception $e) {
- return $this->error($e->getMessage(), []);
- }
- return $this->render($this->action->id);
- }
- /**
- * 删除
- * @param null $keyword
- * @param int $is_recycle
- * @return mixed|void
- */
- public function actionDel()
- {
- $request = Yii::$app->request;
- if ($request->isAjax) {
- if ($request->isGet) {
- try {
- $params = Yii::$app->request->get();
- $res = Yii::$app->services->validate->validate($params, [
- [['id'], 'required'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['status'] = StatusEnum::DELETE;
- $params['mall_id'] = $this->mall_id;
- $res = UserLabel::setData($params);
- if ($res === false) throw new Exception(Tag::getStaticCodeError()['error']);
- return $this->success('操作成功');
- } catch (\Exception $e) {
- return $this->error($e->getMessage(), []);
- }
- }
- }
- }
- /**
- * 获取标签
- * @Author: ltm
- * @DateTime: 2022/3/14 0014 11:43
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $data
- * @return string
- */
- public function actionGetMallLabel(){
- $request = Yii::$app->request;
- if ($request->isAjax) {
- $tag_list = UserLabel::getUserLabel([['mall_id' => $this->mall_id,'status' => StatusEnum::ENABLED]], [], true, 'id tag_id,name');
- return $this->success('操作成功',$tag_list);
- }
- }
- /**
- *
- * @Author: ltm
- * @DateTime: 2022/3/15 0015 16:38
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionAddUserLabel(){
- $request = Yii::$app->request;
- if ($request->isAjax) {
- if ($request->isPost) {
- try {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['label_id','user_id'], 'required'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $user_data[$params['user_id']] = $params['label_id'];
- if ($request->isAjax) {
- $tag_list = UserLabelRelationship::batchSetData($this->mall_id,$user_data,false);
- return $this->success('操作成功',$tag_list);
- }
- } catch (\Exception $e) {
- return $this->error($e->getMessage(), []);
- }
- }
- }
- }
- }
|