UserLabelController.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace backend\modules\mall\controllers;
  3. use addons\Xhs\common\models\AddonsXhsContent;
  4. use backend\controllers\MallController;
  5. use common\enums\StatusEnum;
  6. use common\enums\UserLabelEnum;
  7. use common\models\common\UpgradeCondition;
  8. use common\models\goods\Goods;
  9. use common\models\user\Tag;
  10. use common\models\user\User;
  11. use common\models\user\UserLabel;
  12. use common\models\user\UserLabelCond;
  13. use common\models\user\UserLabelRelationship;
  14. use common\models\user\UserLevel;
  15. use common\models\goods\GoodsService;
  16. use common\models\user\UserLevelUpgradeCondition;
  17. use PHPUnit\Util\Exception;
  18. use Yii;
  19. /**
  20. * 标签列表
  21. *
  22. * Class TagController
  23. * @package backend\modules\mall\TagController
  24. */
  25. class UserLabelController extends MallController
  26. {
  27. /**
  28. *
  29. * @return mixed|string|void
  30. */
  31. public function actionIndex()
  32. {
  33. $request = Yii::$app->request;
  34. if ($request->isAjax) {
  35. if ($request->isPost) {
  36. $post = Yii::$app->request->post();
  37. // 检查提交的参数
  38. $res = Yii::$app->services->validate->validate($post,[
  39. [['name','date_end','date_start','type'], 'safe'],
  40. ]);
  41. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  42. // 查询参数
  43. $where[] = ['=', 'mall_id', $this->mall_id];
  44. $where[] = ['in', 'status', [StatusEnum::DISABLED, StatusEnum::ENABLED]];
  45. if(isset($post['name'])) $where[] = ['like', 'name', $post['name']];
  46. if(!empty($post['type'])) $where[] = ['=', 'type', $post['type']];
  47. if(!empty($post['date_start']) && !empty($post['date_end'])) $where[] = ['between', 'created_at', strtotime($post['date_start']),strtotime($post['date_end'])];
  48. $params = array('where' => $where, 'order' => 'id desc', 'limit' => $request->get('limit', 12));
  49. $params['with'] = ['labelCond'];
  50. $page_data = UserLabel::listPage($params, false);
  51. if($page_data['list']){
  52. foreach ($page_data['list'] as &$v){
  53. $v['cond_data'] = UserLabel::getLabelConf($v['labelCond']);
  54. }
  55. }
  56. return $this->success('操作成功', $page_data);
  57. }
  58. }
  59. return $this->render($this->action->id);
  60. }
  61. /**
  62. * @Author: ltm
  63. * @DateTime: 2022/3/10 0010 15:24
  64. * @Copyright: copyright (c) 2021 广东七件事集团
  65. * @param $data
  66. * @return string
  67. */
  68. public function actionEdit()
  69. {
  70. try {
  71. $request = Yii::$app->request;
  72. if ($request->isAjax) {
  73. if ($request->isGet) {
  74. $list = [];
  75. $params = $request->get();
  76. if (!empty($params['id'])) {
  77. $where[] = ['=','id',$params['id']];
  78. $list = UserLabel::getOne($where,true);
  79. $conf_where[] = ['=','label_id',$params['id']];
  80. $conf = UserLabelCond::lists(['where' => $conf_where],true);
  81. $conf_data = [];
  82. if($conf){
  83. foreach ($conf as $k => $v){
  84. $conf_data[$v['cond_type']]['condition'] = json_decode($v['condition']);
  85. $conf_data[$v['cond_type']]['select_status'] = $v['select_status'];
  86. if($v['cond_type'] == UserLabelEnum::GOODS && $v['condition']){
  87. $goods_where[] = ['in','id',explode(',',json_decode($v['condition']))];
  88. $select = 'goods_name,price,cover_pic,stock';
  89. $goods_list = Goods::lists(['where' => $goods_where,'select' => $select],true);
  90. $list['goods_list'] = $goods_list;
  91. }
  92. }
  93. }
  94. $cond_enum = UserLabelEnum::getMap();
  95. $list['cond_enum'] = $cond_enum;
  96. $list['conf'] = $conf_data;
  97. }
  98. return $this->success('操作成功', $list);
  99. }
  100. //保存数据
  101. if ($request->isPost) {
  102. $params = Yii::$app->request->post('form',[]);
  103. if(empty($params)) return $this->error('参数不完整');
  104. $res = Yii::$app->services->validate->validate($params, [
  105. [['id','mall_id'], 'integer'],
  106. [['type'],'in','range'=>[UserLabelEnum::MANUAL,UserLabelEnum::AUTO]],
  107. [['satisfy_type'],'in','range'=>[UserLabelEnum::SATISFY_ONE,UserLabelEnum::SATISFY_ALL]],
  108. [['condition'],'safe'],
  109. [['color'],'safe'],
  110. [['name'],function($attr,$value){
  111. if(is_numeric($this->$attr)) $this->addError($attr,'标签名称不能为纯数字');
  112. }]
  113. ]);
  114. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  115. try {
  116. $params['mall_id'] = $this->mall_id;
  117. if(empty($params['color'])) $params['color'] = '';
  118. $label_model = UserLabel::setData($params);
  119. if ($label_model == false) throw new \Exception(UserLabel::getStaticError());
  120. return $this->success('操作成功');
  121. } catch (\Exception $e) {
  122. return $this->error($e->getMessage());
  123. }
  124. }
  125. }
  126. } catch (\Exception $e) {
  127. return $this->error($e->getMessage(), []);
  128. }
  129. return $this->render($this->action->id);
  130. }
  131. /**
  132. * 删除
  133. * @param null $keyword
  134. * @param int $is_recycle
  135. * @return mixed|void
  136. */
  137. public function actionDel()
  138. {
  139. $request = Yii::$app->request;
  140. if ($request->isAjax) {
  141. if ($request->isGet) {
  142. try {
  143. $params = Yii::$app->request->get();
  144. $res = Yii::$app->services->validate->validate($params, [
  145. [['id'], 'required'],
  146. ]);
  147. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  148. $params['status'] = StatusEnum::DELETE;
  149. $params['mall_id'] = $this->mall_id;
  150. $res = UserLabel::setData($params);
  151. if ($res === false) throw new Exception(Tag::getStaticCodeError()['error']);
  152. return $this->success('操作成功');
  153. } catch (\Exception $e) {
  154. return $this->error($e->getMessage(), []);
  155. }
  156. }
  157. }
  158. }
  159. /**
  160. * 获取标签
  161. * @Author: ltm
  162. * @DateTime: 2022/3/14 0014 11:43
  163. * @Copyright: copyright (c) 2021 广东七件事集团
  164. * @param $data
  165. * @return string
  166. */
  167. public function actionGetMallLabel(){
  168. $request = Yii::$app->request;
  169. if ($request->isAjax) {
  170. $tag_list = UserLabel::getUserLabel([['mall_id' => $this->mall_id,'status' => StatusEnum::ENABLED]], [], true, 'id tag_id,name');
  171. return $this->success('操作成功',$tag_list);
  172. }
  173. }
  174. /**
  175. *
  176. * @Author: ltm
  177. * @DateTime: 2022/3/15 0015 16:38
  178. * @Copyright: copyright (c) 2021 广东七件事集团
  179. * @return mixed|void
  180. */
  181. public function actionAddUserLabel(){
  182. $request = Yii::$app->request;
  183. if ($request->isAjax) {
  184. if ($request->isPost) {
  185. try {
  186. $params = Yii::$app->request->post();
  187. $res = Yii::$app->services->validate->validate($params, [
  188. [['label_id','user_id'], 'required'],
  189. ]);
  190. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  191. $user_data[$params['user_id']] = $params['label_id'];
  192. if ($request->isAjax) {
  193. $tag_list = UserLabelRelationship::batchSetData($this->mall_id,$user_data,false);
  194. return $this->success('操作成功',$tag_list);
  195. }
  196. } catch (\Exception $e) {
  197. return $this->error($e->getMessage(), []);
  198. }
  199. }
  200. }
  201. }
  202. }