LevelController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace addons\BossWeight\backend\controllers;
  3. use addons\BossWeight\common\enums\BossEnum;
  4. use addons\BossWeight\common\logic\RewardLog;
  5. use addons\BossWeight\common\models\AddonsBoss;
  6. use addons\BossWeight\common\models\AddonsBossLevel;
  7. use common\enums\StatusEnum;
  8. use common\models\common\UpgradeCondition;
  9. use common\models\order\Order;
  10. use Yii;
  11. use yii\db\Exception;
  12. use yii\helpers\Json;
  13. use common\models\goods\Goods;
  14. use common\models\user\UserLevel;
  15. /**
  16. * 股东等级控制器
  17. *
  18. * Class LevelController
  19. * @package addons\BossWeight\backend\controllers
  20. */
  21. class LevelController extends BaseController
  22. {
  23. public $enableCsrfValidation = false;
  24. /**
  25. * 首页
  26. *
  27. * @return string
  28. */
  29. public function actionIndex()
  30. {
  31. $request = Yii::$app->request;
  32. if ($request->isAjax) {
  33. if ($request->isGet) {
  34. $params = Yii::$app->request->get();
  35. // 检查提交的参数
  36. $res = Yii::$app->services->validate->validate($params,[
  37. [['id'], 'integer'],
  38. [['id','keyword'], 'safe'],
  39. [['keyword'], 'string'],
  40. ]);
  41. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  42. $where = [];
  43. $where[] = ['=', 'mall_id', $this->mall_id];
  44. $where[] = ['<>', 'status', StatusEnum::DELETE];
  45. isset($params['keyword']) && $where[] = ['like', 'name', $params['keyword']];
  46. $with = ['setting' => function($query) {
  47. $query->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]);
  48. }];
  49. $order = 'level asc';
  50. $params = array('where' => $where, 'with' => $with, 'order'=>$order, 'limit' => $params['limit']);
  51. $list = AddonsBossLevel::listPage($params, false, true);
  52. if (empty($list)) $this->error('获取失败');
  53. $this->success('获取成功', $list);
  54. }
  55. }
  56. return $this->render($this->action->id);
  57. }
  58. /**
  59. * 等级数据处理
  60. * @Author: lun
  61. * @DateTime: 2021/10/20 0020 18:03
  62. * @Copyright: copyright (c) 2021 广东七件事集团
  63. * @return mixed|void
  64. */
  65. public function actionHandle()
  66. {
  67. $request = Yii::$app->request;
  68. if ($request->isAjax) {
  69. if ($request->isGet) {
  70. $params = Yii::$app->request->get();
  71. // 检查提交的参数
  72. $res = Yii::$app->services->validate->validate($params,[
  73. [['id'], 'required'],
  74. [['id','status'], 'integer'],
  75. [['setting','status'], 'safe'],
  76. ]);
  77. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  78. if (!isset($params['setting'])) unset($params['setting']);
  79. if (!isset($params['status'])) unset($params['status']);
  80. $params['mall_id'] = $this->mall_id;
  81. $res = AddonsBossLevel::setData($params);
  82. if (!$res) return $this->error('修改失败:msg=' . AddonsBossLevel::getStaticError());
  83. $this->success('获取成功');
  84. }
  85. }
  86. }
  87. /**
  88. * 等级编辑
  89. * @Author: lun
  90. * @DateTime: 2021/10/21 0021 11:29
  91. * @Copyright: copyright (c) 2021 广东七件事集团
  92. * @return mixed|string|void
  93. */
  94. public function actionEdit()
  95. {
  96. $upgradeCondition = \Yii::$app->services->setting->addons->get($this->mall_id, BossEnum::ADDONS_NAME, 'upgrade_condition');
  97. // 获取等级升级的条件
  98. $list = [];
  99. if(!empty($upgradeCondition['level_upgrade_condition'])){
  100. $ids = Json::decode($upgradeCondition['level_upgrade_condition'],true);
  101. if(!empty($ids)) {
  102. $list = UpgradeCondition::getData($ids);
  103. }
  104. }
  105. $upgrade_condition_data = UpgradeCondition::getUpgradeConditionData($this, $this->mall_id, $list);
  106. try {
  107. $request = Yii::$app->request;
  108. if ($request->isAjax) {
  109. // 查看等级详情
  110. if ($request->isGet) {
  111. $id = Yii::$app->request->get('id', '');
  112. $data = [];
  113. if(!empty($id)){
  114. // 获取等级信息
  115. $model = AddonsBossLevel::getOne([['id' => $id, 'mall_id' => $this->mall_id]], true, ['setting' => function($query) {
  116. $query->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]);
  117. }]);
  118. // 解析选中的条件跟条件值
  119. $model['checked_condition_values'] = Json::decode($model['checked_condition_values'],true) ?? [];
  120. $model['checked_condition_keys'] = Json::decode($model['checked_condition_keys'],true) ?? [];
  121. //处理key可能不存在 或者 数组为 null 的情况
  122. $key_arr = json_decode($upgrade_condition_data['key_arr'],true);
  123. foreach ($key_arr as $key=>$arr){
  124. if(isset($model['checked_condition_values'][$key]) == false){
  125. $model['checked_condition_values'][$key] = $arr;
  126. }
  127. }
  128. foreach ($model['checked_condition_values'] as $key=>&$value){
  129. if($value['key'] == null){
  130. $value = $key_arr[$key];
  131. }
  132. }
  133. unset($value);
  134. // 解析被选中的商品
  135. $model['goods_ids'] = Json::decode($model['goods_ids'],true) ?? [];
  136. $model['goods_list'] = Json::decode($model['goods_list'],true) ?? [];
  137. unset($model['updated_at'], $model['created_at']);
  138. $data['detail'] = $model;
  139. $data['goods'] = [];
  140. if (!empty($data['detail']['setting'])){
  141. unset($data['detail']['setting']['level']);// 删除配置的等级防止修改等级报错
  142. if($data['detail']['setting']['checked_weight_values']){
  143. $data['detail']['setting']['checked_weight_values'] = json_decode($data['detail']['setting']['checked_weight_values'],true);
  144. }
  145. if($data['detail']['setting']['checked_weight_values'][0]['value']['val1']){
  146. $data['goods'] = Goods::find()->andWhere(['in','id',$data['detail']['setting']['checked_weight_values'][0]['value']['val1']])->asArray()->all();
  147. }
  148. }
  149. }
  150. $data['weights'] = AddonsBossLevel::getLevelWeights($this->mall_id);
  151. $data['memberLevels'] = UserLevel::find()->select('name,level')->where(['mall_id' => $this->mall_id,'status' => 1])->asArray()->all();
  152. return $this->success('操作成功', $data);
  153. }
  154. // 提交等级内容
  155. if ($request->isPost) {
  156. $params = Yii::$app->request->post()['form'];
  157. $res = Yii::$app->services->validate->validate($params, [
  158. [['level', 'name', 'explain', 'status'], 'required'],
  159. [['id', 'is_auto_upgrade', 'checked_condition_values', 'setting',
  160. 'checked_condition_keys', 'condition_type', 'upgrade_type_goods', 'goods_list', 'upgrade_type_condition', 'goods_ids', 'goods_type',
  161. 'buy_goods_type', 'extra_limit_price','goods_list'], 'safe']
  162. ]);
  163. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  164. $params['mall_id'] = $this->mall_id;
  165. // 将数组转为json字符串
  166. foreach ($params as &$item) {
  167. if (is_array($item)) $item = Json::encode($item);
  168. }
  169. $res = AddonsBossLevel::setData($params);
  170. if ($res === false) throw new Exception(AddonsBossLevel::getStaticCodeError()['error']);
  171. return $this->success('操作成功');
  172. }
  173. }
  174. } catch (\Exception $e) {
  175. return $this->error($e->getMessage(), []);
  176. }
  177. return $this->render('edit', $upgrade_condition_data);
  178. }
  179. /**
  180. * 设置升级条件
  181. * @Author: lun
  182. * @DateTime: 2021/10/21 0021 11:28
  183. * @Copyright: copyright (c) 2021 广东七件事集团
  184. * @return mixed|string|void
  185. */
  186. public function actionCondition() {
  187. if (\Yii::$app->request->isAjax) {
  188. try {
  189. if (\Yii::$app->request->isGet) {
  190. $data['upgrade_condition'] = UpgradeCondition::getData(UpgradeCondition::$boss_weight_condition_ids);
  191. $upgradeCondition = Yii::$app->services->setting->addons->get($this->mall_id, BossEnum::ADDONS_NAME, 'upgrade_condition');
  192. $data['checked_condition_keys'] = !empty($upgradeCondition['level_upgrade_condition']) ? Json::decode($upgradeCondition['level_upgrade_condition'], true) : [];
  193. return $this->success('操作成功', $data);
  194. } else {
  195. $postData = Yii::$app->request->post();
  196. $res = Yii::$app->services->validate->validate($postData, [
  197. [['checked_condition_keys'], 'required'],
  198. ]);
  199. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  200. $data['upgrade_condition'] = ['level_upgrade_condition' => Json::encode($postData['checked_condition_keys'])];
  201. Yii::$app->services->setting->addons->set($this->mall_id, BossEnum::ADDONS_NAME, $data);
  202. UpgradeCondition::clear($postData['checked_condition_keys'],'addons\BossWeight\common\models\AddonsBossLevel',$this->mall_id);
  203. return $this->success('操作成功');
  204. }
  205. } catch (\Exception $e) {
  206. return $this->error($e->getMessage());
  207. }
  208. }
  209. return $this->render('condition');
  210. }
  211. }