LevelController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. namespace addons\WechatVideoShop\backend\controllers;
  3. use addons\WechatVideoShop\common\enums\WechatVideoShopEnums;
  4. use addons\WechatVideoShop\common\event\WechatVideoShopLevelEvent;
  5. use addons\WechatVideoShop\common\models\WechatVideoShopLevel;
  6. use addons\WechatVideoShop\common\models\WechatVideoShopSharer;
  7. use addons\WechatVideoShop\common\models\WechatVideoShopUpgradeCondition;
  8. use common\enums\StatusEnum;
  9. use common\models\goods\Goods;
  10. use Exception;
  11. use Yii;
  12. use yii\helpers\Json;
  13. class LevelController extends BaseController
  14. {
  15. public $enableCsrfValidation = false;
  16. /**
  17. * @desc:首页
  18. * @Author: hua
  19. * @Date: 2021/10/20
  20. * @Time: 16:26
  21. * @Copyright: copyright (c) 2021 广东七件事集团
  22. * @return mixed|string|void
  23. */
  24. public function actionIndex()
  25. {
  26. $request = Yii::$app->request;
  27. if ($request->isAjax) {
  28. // 查询参数
  29. $get = \Yii::$app->request->get();
  30. $where[] = ['=', 'mall_id', $this->mall_id];
  31. $where[] = ['in', 'status', [StatusEnum::DISABLED, StatusEnum::ENABLED]];
  32. $where[] = ['or', ['like', 'name', $get['keyword']], ['like', 'level', $get['keyword']]];
  33. $params = array('where' => $where, 'limit' => $request->get('limit', 12));
  34. $params['order'] = 'level asc';
  35. $page_data = WechatVideoShopLevel::listPage($params, false);
  36. return $this->success('操作成功', $page_data);
  37. }
  38. return $this->render($this->action->id);
  39. }
  40. /**
  41. * @desc:修改状态
  42. * @Author: hua
  43. * @Date: 2021/10/23
  44. * @Time: 16:31
  45. * @Copyright: copyright (c) 2021 广东七件事集团
  46. * @return mixed|void
  47. * @throws Exception
  48. */
  49. public function actionSwitchStatus()
  50. {
  51. if (\Yii::$app->request->isAjax) {
  52. if (\Yii::$app->request->isPost) {
  53. $params = \Yii::$app->request->post();
  54. $res = Yii::$app->services->validate->validate($params, [
  55. [['id'], 'required'],
  56. [['status'], 'integer']
  57. ]);
  58. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  59. $params['mall_id'] = $this->mall_id;
  60. $res = WechatVideoShopLevel::setData($params);
  61. if ($res === false) return $this->error(WechatVideoShopLevel::getStaticError());
  62. $event = new WechatVideoShopLevelEvent($this->mall_id);
  63. $data = [
  64. 'mall_id'=>$this->mall_id,
  65. 'level'=>$res['level'],
  66. 'level_name'=>$res['name'],
  67. 'status'=>$res['status'],
  68. 'id'=>$res['id'],
  69. 'type'=>'edit',
  70. ];
  71. \Yii::$app->services->events->dispatch($event, $data , $event->listeners);
  72. return $this->success('操作成功');
  73. }
  74. }
  75. return $this->error();
  76. }
  77. /**
  78. * @desc:删除等级
  79. * @Author: hua
  80. * @Date: 2021/10/23
  81. * @Time: 16:43
  82. * @Copyright: copyright (c) 2021 广东七件事集团
  83. * @return mixed|void
  84. * @throws Exception
  85. */
  86. public function actionDelete()
  87. {
  88. if (\Yii::$app->request->isAjax) {
  89. if (\Yii::$app->request->isPost) {
  90. $params = \Yii::$app->request->post();
  91. $res = Yii::$app->services->validate->validate($params, [
  92. [['id'], 'required'],
  93. ]);
  94. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  95. $params['mall_id'] = $this->mall_id;
  96. $params['status'] = StatusEnum::DELETE;
  97. $res = WechatVideoShopLevel::del($params);
  98. if ($res === false) return $this->error(WechatVideoShopLevel::getStaticError());
  99. return $this->success('操作成功');
  100. }
  101. }
  102. return $this->error();
  103. }
  104. /**
  105. * @desc:设置升级条件
  106. * @Author: hua
  107. * @Date: 2021/10/20
  108. * @Time: 18:50
  109. * @Copyright: copyright (c) 2021 广东七件事集团
  110. * @return mixed|string|void
  111. */
  112. public function actionCondition()
  113. {
  114. if (\Yii::$app->request->isAjax) {
  115. try {
  116. if (\Yii::$app->request->isGet) {
  117. $data['upgrade_condition'] = WechatVideoShopUpgradeCondition::getData(WechatVideoShopUpgradeCondition::$wechat_video_shop_condition_ids);
  118. $agentUpgradeCondition = Yii::$app->services->setting->addons->get($this->mall_id, WechatVideoShopEnums::ADDONS_NAME, 'upgrade_condition');
  119. $data['checked_condition_keys'] = !empty($agentUpgradeCondition['wechat_video_shop_level_upgrade_condition']) ?
  120. Json::decode($agentUpgradeCondition['wechat_video_shop_level_upgrade_condition']) : [];
  121. return $this->success('操作成功', $data);
  122. } else {
  123. $postData = Yii::$app->request->post();
  124. $res = Yii::$app->services->validate->validate($postData, [
  125. [['checked_condition_keys'], 'required'],
  126. ]);
  127. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  128. $data['upgrade_condition'] = ['wechat_video_shop_level_upgrade_condition' => Json::encode($postData['checked_condition_keys'])];
  129. Yii::$app->services->setting->addons->set($this->mall_id, WechatVideoShopEnums::ADDONS_NAME, $data);
  130. WechatVideoShopUpgradeCondition::clear($postData['checked_condition_keys'], WechatVideoShopLevel::class, $this->mall_id);
  131. return $this->success('操作成功');
  132. }
  133. } catch (\Exception $e) {
  134. return $this->error($e->getMessage());
  135. }
  136. }
  137. return $this->render('condition');
  138. }
  139. /**
  140. * @desc:编辑等级
  141. * @Author: hua
  142. * @Date: 2021/10/20
  143. * @Time: 15:51
  144. * @Copyright: copyright (c) 2021 广东七件事集团
  145. * @return mixed|string|void
  146. */
  147. public function actionEdit()
  148. {
  149. try {
  150. $request = Yii::$app->request;
  151. if ($request->isAjax) {
  152. if ($request->isGet) {
  153. $id = Yii::$app->request->get('id', '');
  154. $mall_id = $this->mall_id;
  155. $data = [];
  156. if (!empty($id)) {
  157. $model = WechatVideoShopLevel::getOne([['id' => $id]], true);
  158. $model['checked_condition_values'] = Json::decode($model['checked_condition_values']) ?? [];
  159. if (empty($model['checked_condition_values'])) unset($model['checked_condition_values']);
  160. $model['checked_condition_keys'] = Json::decode($model['checked_condition_keys']) ?? [];
  161. $model['goods_ids'] = Json::decode($model['goods_ids']) ?? [];
  162. $model['goods_list'] = Json::decode($model['goods_list']) ?? [];
  163. if(!empty($model['goods_list'])){
  164. $goods_ids = array_column($model['goods_list'],'id');
  165. $goods_list = Goods::lists(['where'=>[['id'=>$goods_ids]],'select'=>'id,cover_pic,goods_name','index'=>'id']);
  166. foreach ($model['goods_list'] as&$item){
  167. if(isset($goods_list[$item['id']])){
  168. $item['cover_pic'] = $goods_list[$item['id']]['cover_pic'];
  169. $item['name'] = $goods_list[$item['id']]['goods_name'];
  170. }
  171. }
  172. }
  173. unset($model['updated_at'], $model['created_at']);
  174. $model['score'] = [
  175. 'is_percent_team' => 0,
  176. 'is_percent_equal' => 0,
  177. 'wechat_video_shop_team_prize' => 0,
  178. 'wechat_video_shop_equal_prize' => 0,
  179. 'wechat_video_shop_over_prize' => 0,
  180. ];
  181. $model['commission'] = [
  182. 'is_percent_team' => 0,
  183. 'is_percent_equal' => 0,
  184. 'wechat_video_shop_team_prize' => 0,
  185. 'wechat_video_shop_equal_prize' => 0,
  186. 'wechat_video_shop_over_prize' => 0,
  187. ];
  188. $data['detail'] = $model;
  189. }
  190. $data['weights'] = WechatVideoShopLevel::getLevelWeights($this->mall_id);
  191. return $this->success('操作成功', $data);
  192. }
  193. if ($request->isPost) {
  194. $params = Yii::$app->request->post()['form'];
  195. $res = Yii::$app->services->validate->validate($params, [
  196. [['level', 'name'], 'required'],
  197. [['id', 'is_auto_upgrade', 'score', 'commission', 'checked_condition_values',
  198. 'checked_condition_keys', 'condition_type', 'upgrade_type_goods', 'goods_list', 'upgrade_type_condition', 'goods_ids', 'goods_type',
  199. 'buy_goods_type','explain','reward_commission_type','reward_commission', 'reward_score_type', 'reward_score'], 'safe']
  200. ]);
  201. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  202. if(!empty($params['id'])){
  203. $distribution_level = WechatVideoShopLevel::findOne(['id'=>$params['id'],'mall_id'=>$this->mall_id,'status'=>[StatusEnum::DISABLED,StatusEnum::ENABLED]]);
  204. if($distribution_level&&$distribution_level->level!=$params['level']){
  205. $res = WechatVideoShopSharer::findOne(['mall_id'=>$this->mall_id,
  206. 'level'=>$distribution_level->level,
  207. 'status'=>[StatusEnum::DISABLED,StatusEnum::ENABLED]]);
  208. if($res) return $this->error('该等级关联了经销商,等级不能修改');
  209. }
  210. }
  211. $params['mall_id'] = $this->mall_id;
  212. $res = WechatVideoShopLevel::setData($params);
  213. if ($res === false) throw new Exception(WechatVideoShopLevel::getStaticCodeError()['error']);
  214. $event = new WechatVideoShopLevelEvent($this->mall_id);
  215. $data = [
  216. 'mall_id'=>$this->mall_id,
  217. 'level'=>$params['level'],
  218. 'level_name'=>$params['name'],
  219. 'status'=>$res['status'],
  220. 'id'=>$res['id'],
  221. 'type'=>!empty($params['id'])?'edit':'add',
  222. ];
  223. \Yii::$app->services->events->dispatch($event,$data , $event->listeners);
  224. return $this->success('操作成功');
  225. }
  226. }
  227. } catch (\Exception $e) {
  228. return $this->error($e->getMessage(), []);
  229. }
  230. $agentUpgradeCondition = \Yii::$app->services->setting->addons->get($this->mall_id, WechatVideoShopEnums::ADDONS_NAME, 'upgrade_condition');
  231. $list = [];
  232. if (!empty($agentUpgradeCondition['wechat_video_shop_level_upgrade_condition'])) {
  233. $ids = Json::decode($agentUpgradeCondition['wechat_video_shop_level_upgrade_condition']);
  234. $list = WechatVideoShopUpgradeCondition::getData($ids);
  235. }
  236. $upgrade_condition_data = WechatVideoShopUpgradeCondition::getUpgradeConditionData($this, $this->mall_id, $list);
  237. return $this->render('edit', $upgrade_condition_data);
  238. }
  239. /**
  240. * @desc:权重
  241. * @Author: hua
  242. * @Date: 2021/10/23
  243. * @Time: 15:56
  244. * @Copyright: copyright (c) 2021 广东七件事集团
  245. * @return mixed|void
  246. */
  247. public function actionSetting()
  248. {
  249. if (\Yii::$app->request->isAjax) {
  250. $weights = WechatVideoShopLevel::getLevelWeights($this->mall_id);
  251. $enable_level_list = [];
  252. foreach ($weights as $k) {
  253. if ($k['disabled']) {
  254. unset($k['disabled']);
  255. $enable_level_list[] = $k;
  256. }
  257. }
  258. return $this->success('操作成功', ['weights' => $weights, 'enable_level_list' => $enable_level_list]);
  259. }
  260. return $this->error();
  261. }
  262. }