StoreBossController.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace addons\Happiness\backend\controllers;
  3. use addons\Happiness\common\enums\HappinessEnum;
  4. use addons\Happiness\common\models\HappinessBoss;
  5. use common\enums\StatusEnum;
  6. use common\models\user\User;
  7. use Yii;
  8. /**
  9. * 默认控制器
  10. *
  11. * Class StoreAgentController
  12. * @package addons\Happiness\backend\controllers
  13. */
  14. class StoreBossController extends BaseController
  15. {
  16. /**
  17. * 首页
  18. *
  19. * @return string
  20. * @throws \yii\web\NotFoundHttpException
  21. */
  22. public function actionIndex()
  23. {
  24. $request = \Yii::$app->request;
  25. if ($request->isAjax) {
  26. if ($request->isGet) {
  27. $get = \Yii::$app->request->get();
  28. // 检查提交的参数
  29. $res = \Yii::$app->services->validate->validate($get, [
  30. [['level'], 'integer'],
  31. [['user_keyword'], 'string'],
  32. [['parent_keyword'], 'string'],
  33. ]);
  34. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  35. !empty($get['level']) && $where[] = ['=', 'level', $get['level']];
  36. $where[] = ['=', 'mall_id', $this->mall_id];
  37. $where[] = ['in', 'status', [StatusEnum::ENABLED, StatusEnum::DISABLED]];
  38. if(!empty($get['user_keyword']))
  39. {
  40. $user_list = User::lists(['where' => [['mall_id' => $this->mall_id], [
  41. 'or',
  42. ['=', 'id', $get['user_keyword']],
  43. ['like', 'nickname', $get['user_keyword'] . '%', false],
  44. ['like', 'mobile', $get['user_keyword'] . '%', false]
  45. ]], 'select' => 'id']);
  46. $user_ids = array_column($user_list, 'id');
  47. $where[]=['in', 'user_id', $user_ids];
  48. }
  49. if(!empty($get['parent_keyword']))
  50. {
  51. $user_list = User::lists(['where' => [['mall_id' => $this->mall_id], [
  52. 'or',
  53. ['=', 'id', $get['parent_keyword']],
  54. ['like', 'nickname', $get['parent_keyword'] . '%', false],
  55. ['like', 'mobile', $get['parent_keyword'] . '%', false]
  56. ]], 'select' => 'id']);
  57. $parent_ids = array_column($user_list, 'id');
  58. $where[]=['in', 'parent_id', $parent_ids];
  59. }
  60. $order = 'created_at desc';
  61. $select = '*';
  62. $params = array('where' => $where, 'select' => $select, 'order' => $order, 'limit' => $get['limit']);
  63. $params['with'] = ['user' => function ($query) {
  64. $query->select('id, nickname, mobile,avatar_url');
  65. }, 'parent' => function ($query) {
  66. $query->select('id, nickname, mobile,avatar_url');
  67. }];
  68. $list = HappinessBoss::listPage($params, false, true);
  69. if($list==false)
  70. {
  71. return $this->error(HappinessBoss::getStaticError(), []);
  72. }
  73. $config= \Yii::$app->services->setting->addons->get($this->mall_id, HappinessEnum::ADDONS_NAME, 'basic');
  74. $boss_list=!empty($config['boss_list'])?json_decode($config['boss_list'],true):[];
  75. if(!empty($list['list']))
  76. {
  77. if(!empty($boss_list))
  78. {
  79. $boss_list=array_column($boss_list,null,'level');
  80. }
  81. foreach ($list['list'] as &$item)
  82. {
  83. $item['level_name']=isset($boss_list[$item['level']])?$boss_list[$item['level']]['name']:'';
  84. }
  85. }
  86. $list['boss_list'] = $boss_list;
  87. return $this->success('获取成功', $list);
  88. }
  89. if ($request->isPost) {
  90. $post = \Yii::$app->request->post();
  91. $res = \Yii::$app->services->validate->validate($post, [
  92. [['level', 'user_id'], 'required'],
  93. [['level', 'user_id', 'parent_id'], 'integer'],
  94. ]);
  95. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  96. if(empty($post['parent_id']))
  97. {
  98. $post['parent_id']=0;
  99. }
  100. $post['mall_id'] = $this->mall_id;
  101. $res=HappinessBoss::setData($post);
  102. if($res===false)
  103. {
  104. return $this->error(HappinessBoss::getStaticError());
  105. }
  106. return $this->success('操作成功');
  107. }
  108. }
  109. return $this->render('index');
  110. }
  111. public function actionSwitchStatus()
  112. {
  113. if (\Yii::$app->request->isAjax) {
  114. if (\Yii::$app->request->isPost) {
  115. $params = \Yii::$app->request->post();
  116. $res = Yii::$app->services->validate->validate($params, [
  117. [['id'], 'required'],
  118. [['status'], 'integer']
  119. ]);
  120. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  121. $params['mall_id'] = $this->mall_id;
  122. $res = HappinessBoss::setData($params);
  123. if ($res === false) return $this->error(HappinessBoss::getStaticError());
  124. return $this->success('操作成功');
  125. }
  126. }
  127. return $this->error();
  128. }
  129. public function actionDelete()
  130. {
  131. if (\Yii::$app->request->isAjax) {
  132. if (\Yii::$app->request->isPost) {
  133. $params = \Yii::$app->request->post();
  134. $res = Yii::$app->services->validate->validate($params, [
  135. [['id'], 'required'],
  136. ]);
  137. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  138. $params['mall_id'] = $this->mall_id;
  139. $params['status'] = StatusEnum::DELETE;
  140. $res = HappinessBoss::setData($params);
  141. if ($res === false) return $this->error(HappinessBoss::getStaticError());
  142. return $this->success('操作成功');
  143. }
  144. }
  145. return $this->error();
  146. }
  147. public function actionEdit()
  148. {
  149. if (\Yii::$app->request->isAjax) {
  150. if (\Yii::$app->request->isPost) {
  151. }
  152. if (\Yii::$app->request->isGet) {
  153. $id = \Yii::$app->request->get('id');
  154. $boss = HappinessBoss::find() ->with(['user'=>function ($query){
  155. $query->select('id,nickname,mobile,avatar_url');
  156. },'parent'=>function ($query) {
  157. $query->select('id,nickname,mobile,avatar_url');
  158. }])->where(['id' => $id, 'mall_id' =>
  159. $this->mall_id])
  160. ->asArray()
  161. ->one();
  162. if (empty($boss)) {
  163. return $this->error('数据未找到');
  164. }
  165. return $this->success('success', $boss);
  166. }
  167. }
  168. }
  169. }