StoreAgentController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. <?php
  2. namespace addons\Happiness\backend\controllers;
  3. use addons\Happiness\common\enums\HappinessEnum;
  4. use addons\Happiness\common\models\HappinessAgent;
  5. use addons\Happiness\common\models\HappinessBoss;
  6. use addons\Happiness\common\models\HappinessBusiness;
  7. use common\enums\StatusEnum;
  8. use common\models\common\Region;
  9. use common\models\user\User;
  10. use common\models\user\UserLevel;
  11. use Yii;
  12. /**
  13. * 默认控制器
  14. *
  15. * Class StoreAgentController
  16. * @package addons\Happiness\backend\controllers
  17. */
  18. class StoreAgentController extends BaseController
  19. {
  20. /**
  21. * 首页
  22. *
  23. * @return string
  24. * @throws \yii\web\NotFoundHttpException
  25. */
  26. public function actionIndex()
  27. {
  28. $request = \Yii::$app->request;
  29. if ($request->isAjax) {
  30. if ($request->isGet) {
  31. $get = \Yii::$app->request->get();
  32. // 检查提交的参数
  33. $res = \Yii::$app->services->validate->validate($get, [
  34. [['level'], 'integer'],
  35. [['user_keyword'], 'string'],
  36. [['parent_keyword'], 'string'],
  37. [['region'], 'string'],
  38. ]);
  39. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  40. !empty($get['level']) && $where[] = ['=', 'level', $get['level']];
  41. $where[] = ['=', 'mall_id', $this->mall_id];
  42. $where[] = ['in', 'status', [StatusEnum::ENABLED, StatusEnum::DISABLED]];
  43. if(!empty($get['user_keyword']))
  44. {
  45. $user_list = User::lists(['where' => [['mall_id' => $this->mall_id], [
  46. 'or',
  47. ['=', 'id', $get['user_keyword']],
  48. ['like', 'nickname', $get['user_keyword'] . '%', false],
  49. ['like', 'mobile', $get['user_keyword'] . '%', false]
  50. ]], 'select' => 'id']);
  51. $user_ids = array_column($user_list, 'id');
  52. $where[]=['in', 'user_id', $user_ids];
  53. }
  54. if(!empty($get['parent_keyword']))
  55. {
  56. $user_list = User::lists(['where' => [['mall_id' => $this->mall_id], [
  57. 'or',
  58. ['=', 'id', $get['parent_keyword']],
  59. ['like', 'nickname', $get['parent_keyword'] . '%', false],
  60. ['like', 'mobile', $get['parent_keyword'] . '%', false]
  61. ]], 'select' => 'id']);
  62. $parent_ids = array_column($user_list, 'id');
  63. $where[]=['in', 'parent_id', $parent_ids];
  64. }
  65. if(!empty($get['region']))
  66. {
  67. $where[]=['like', 'region_name', $get['region']];
  68. }
  69. $order = 'created_at desc';
  70. $select = '*';
  71. $params = array('where' => $where, 'select' => $select, 'order' => $order, 'limit' => $get['limit']);
  72. $params['with'] = ['user' => function ($query) {
  73. $query->select('id, nickname, mobile,avatar_url');
  74. }, 'parent' => function ($query) {
  75. $query->select('id, nickname, mobile,avatar_url');
  76. }];
  77. $list = HappinessAgent::listPage($params, false, true);
  78. if($list==false)
  79. {
  80. return $this->error(HappinessAgent::getStaticError(), []);
  81. }
  82. $config= \Yii::$app->services->setting->addons->get($this->mall_id, HappinessEnum::ADDONS_NAME, 'basic');
  83. $agent_list=!empty($config['agent_list'])?json_decode($config['agent_list'],true):[];
  84. if(!empty($list['list']))
  85. {
  86. if(!empty($agent_list))
  87. {
  88. $agent_list=array_column($agent_list,null,'level');
  89. }
  90. foreach ($list['list'] as &$item)
  91. {
  92. $item['level_name']=isset($agent_list[$item['level']])?$agent_list[$item['level']]['name']:'';
  93. }
  94. }
  95. $list['agent_list'] = $agent_list;
  96. $list['regions'] = Region::find()
  97. ->select('id,name,lng,lat')
  98. ->where(['level' => 1])
  99. ->asArray()
  100. ->indexBy('id')
  101. ->all();
  102. $list['agent_login_url']= \Yii::$app->request->hostInfo . '?r=happiness/agent/auth/login&sign=' . $this->mall['mall_sign'];
  103. return $this->success('获取成功', $list);
  104. }
  105. if ($request->isPost) {
  106. $post = \Yii::$app->request->post();
  107. $res = \Yii::$app->services->validate->validate($post, [
  108. [['level', 'user_id', 'province_id','role'], 'required'],
  109. [['level', 'user_id', 'parent_id', 'province_id', 'city_id', 'district_id', 'role'], 'integer'],
  110. ]);
  111. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  112. $region_ids=[];
  113. if(!empty($post['province_id']))
  114. {
  115. $region_ids[]=$post['province_id'];
  116. }
  117. if(!empty($post['city_id']) && !empty($post['role']) && $post['role']>=HappinessEnum::AGENT_CITY)
  118. {
  119. $region_ids[]=$post['city_id'];
  120. }
  121. if(!empty($post['district_id']) && !empty($post['role']) && $post['role']==HappinessEnum::AGENT_DISTRICT)
  122. {
  123. $region_ids[]=$post['district_id'];
  124. }
  125. if(!$post['city_id'])
  126. {
  127. unset($post['city_id']);
  128. }
  129. if(!$post['district_id'])
  130. {
  131. unset($post['district_id']);
  132. }
  133. if(empty($post['parent_id']))
  134. {
  135. $post['parent_id']=0;
  136. }
  137. $region_name=Region::find()->where(['id'=>$region_ids])->select('name')->asArray()->all();
  138. $post['region_name']=implode('',array_column($region_name,'name'));
  139. $post['mall_id'] = $this->mall_id;
  140. $res=HappinessAgent::setData($post);
  141. if($res===false)
  142. {
  143. return $this->error(HappinessAgent::getStaticError());
  144. }
  145. return $this->success('操作成功');
  146. }
  147. }
  148. return $this->render('index');
  149. }
  150. /**
  151. * @name:
  152. * @msg: 根据地区id获取第一层子级地区信息列表
  153. * @param {*}
  154. * @return {*}
  155. */
  156. public function actionGetRegions()
  157. {
  158. if (!\Yii::$app->request->isGet) {
  159. return $this->error('请求方式错误');
  160. }
  161. $region_id = \Yii::$app->request->get('region_id');
  162. $level = \Yii::$app->request->get('level');
  163. if (empty($region_id)) {
  164. return $this->error('参数错误');
  165. }
  166. $region = Region::findOne($region_id);
  167. if (empty($region) && $level <= 3) {
  168. return $this->error('地区信息未找到');
  169. }
  170. if(isset($level) && $level < 3){
  171. $region_list = Region::find()
  172. ->select('id,name,lng,lat')
  173. ->where(['parent_id' => $region->id])
  174. ->asArray()
  175. ->indexBy('id')
  176. ->all();
  177. }
  178. return $this->success('success', $region_list);
  179. }
  180. /**
  181. * @name:
  182. * @msg: 获取会员列表
  183. * @param {*}
  184. * @return {*}
  185. */
  186. public function actionGetUsers()
  187. {
  188. if (!\Yii::$app->request->isGet) {
  189. return $this->error('请求方式错误');
  190. }
  191. $user_id = \Yii::$app->request->get('user_id');
  192. $keyword = \Yii::$app->request->get('keyword');
  193. $page = \Yii::$app->request->get('page', 1);
  194. $limit = \Yii::$app->request->get('limit', $this->pageSize);
  195. $wheres[] = ['=','mall_id', $this->mall_id];
  196. $wheres[] = ['status' => StatusEnum::ENABLED];
  197. if (!empty($user_id)) {
  198. $wheres[] = ['in','id',$user_id];
  199. }
  200. if (!empty($keyword)) {
  201. $wheres[] = ['or', ['like', 'nickname', $keyword], ['like', 'mobile', $keyword]];
  202. }
  203. $params = [
  204. 'select' => 'id,nickname,avatar_url,mobile,level,parent_id,mall_id',
  205. 'where' => $wheres,
  206. 'order' => 'created_at desc',
  207. 'page' => $page,
  208. 'limit' => $limit,
  209. ];
  210. $user_list = User::listPage($params);
  211. if (empty($user_list['list'])) {
  212. if (false === $user_list['list']) {
  213. return $this->error(User::getStaticError());
  214. }
  215. } else {
  216. $levels = UserLevel::find()
  217. ->select('id,level,name')
  218. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
  219. ->asArray()
  220. ->indexBy('level')
  221. ->all();
  222. $parent_ids = array_column($user_list['list'], 'parent_id');
  223. $parent_users = User::find()
  224. ->select('id,nickname,avatar_url,mobile,level,parent_id')
  225. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $parent_ids])
  226. ->asArray()
  227. ->indexBy('id')
  228. ->all();
  229. foreach ($user_list['list'] as &$user) {
  230. if (empty($user['avatar_url'])) {
  231. $user['avatar_url'] = \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png';
  232. }
  233. $level = $levels[$user['level']] ?? [];
  234. $parent_user = $parent_users[$user['parent_id']] ?? [];
  235. $user['level_name'] = $level['name'] ?? '';
  236. $user['parent_nickname'] = $parent_user['nickname'] ?? '';
  237. }
  238. }
  239. return $this->success('success', $user_list);
  240. }
  241. /**
  242. * @name:
  243. * @msg: 获取会员列表
  244. * @param {*}
  245. * @return {*}
  246. */
  247. public function actionGetAgentUsers()
  248. {
  249. if (!\Yii::$app->request->isGet) {
  250. return $this->error('请求方式错误');
  251. }
  252. $user_id = \Yii::$app->request->get('user_id');
  253. $keyword = \Yii::$app->request->get('keyword');
  254. $page = \Yii::$app->request->get('page', 1);
  255. $limit = \Yii::$app->request->get('limit', $this->pageSize);
  256. $wheres[] = ['=','o.mall_id', $this->mall_id];
  257. $wheres[] = ['o.status' => StatusEnum::ENABLED];
  258. if (!empty($user_id)) {
  259. $wheres[] = ['in','id',$user_id];
  260. }
  261. if (!empty($keyword)) {
  262. $wheres[] = ['or', ['o.like', 'o.nickname', $keyword], ['like', 'o.mobile', $keyword]];
  263. }
  264. $params = [
  265. 'select' => 'u.*',
  266. 'where' => $wheres,
  267. 'alias' => 'o',
  268. 'order' => 'o.created_at desc',
  269. 'page' => $page,
  270. 'join' =>[['INNER JOIN', User::tableName() . ' u', 'u.id = o.user_id']],
  271. 'limit' => $limit,
  272. ];
  273. $user_list = HappinessBusiness::listPage($params);
  274. if (empty($user_list['list'])) {
  275. if (false === $user_list['list']) {
  276. return $this->error(User::getStaticError());
  277. }
  278. } else {
  279. $levels = UserLevel::find()
  280. ->select('id,level,name')
  281. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
  282. ->asArray()
  283. ->indexBy('level')
  284. ->all();
  285. $parent_ids = array_column($user_list['list'], 'parent_id');
  286. $parent_users = User::find()
  287. ->select('id,nickname,avatar_url,mobile,level,parent_id')
  288. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $parent_ids])
  289. ->asArray()
  290. ->indexBy('id')
  291. ->all();
  292. foreach ($user_list['list'] as &$user) {
  293. if (empty($user['avatar_url'])) {
  294. $user['avatar_url'] = \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png';
  295. }
  296. $level = $levels[$user['level']] ?? [];
  297. $parent_user = $parent_users[$user['parent_id']] ?? [];
  298. $user['level_name'] = $level['name'] ?? '';
  299. $user['parent_nickname'] = $parent_user['nickname'] ?? '';
  300. }
  301. }
  302. return $this->success('success', $user_list);
  303. }
  304. public function actionSwitchStatus()
  305. {
  306. if (\Yii::$app->request->isAjax) {
  307. if (\Yii::$app->request->isPost) {
  308. $params = \Yii::$app->request->post();
  309. $res = Yii::$app->services->validate->validate($params, [
  310. [['id'], 'required'],
  311. [['status'], 'integer']
  312. ]);
  313. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  314. $params['mall_id'] = $this->mall_id;
  315. $res = HappinessAgent::setData($params);
  316. if ($res === false) return $this->error(HappinessAgent::getStaticError());
  317. return $this->success('操作成功');
  318. }
  319. }
  320. return $this->error();
  321. }
  322. public function actionDelete()
  323. {
  324. if (\Yii::$app->request->isAjax) {
  325. if (\Yii::$app->request->isPost) {
  326. $params = \Yii::$app->request->post();
  327. $res = Yii::$app->services->validate->validate($params, [
  328. [['id'], 'required'],
  329. ]);
  330. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  331. $params['mall_id'] = $this->mall_id;
  332. $params['status'] = StatusEnum::DELETE;
  333. $res = HappinessAgent::setData($params);
  334. if ($res === false) return $this->error(HappinessAgent::getStaticError());
  335. return $this->success('操作成功');
  336. }
  337. }
  338. return $this->error();
  339. }
  340. public function actionEdit()
  341. {
  342. if (\Yii::$app->request->isAjax) {
  343. if (\Yii::$app->request->isPost) {
  344. }
  345. if (\Yii::$app->request->isGet) {
  346. $id = \Yii::$app->request->get('id');
  347. $agent = HappinessAgent::find() ->with(['user'=>function ($query){
  348. $query->select('id,nickname,mobile,avatar_url');
  349. },'parent'=>function ($query) {
  350. $query->select('id,nickname,mobile,avatar_url');
  351. }])->where(['id' => $id, 'mall_id' =>
  352. $this->mall_id])
  353. ->asArray()
  354. ->one();
  355. if (empty($agent)) {
  356. return $this->error('数据未找到');
  357. }
  358. $citys = Region::find()
  359. ->select('id,name,lng,lat')
  360. ->where(['parent_id' => $agent['province_id']])
  361. ->asArray()
  362. ->indexBy('id')
  363. ->all();
  364. $districts = Region::find()
  365. ->select('id,name,lng,lat')
  366. ->where(['parent_id' => $agent['city_id']])
  367. ->asArray()
  368. ->indexBy('id')
  369. ->all();
  370. $agent['citys']= $citys;
  371. $agent['districts']= $districts;
  372. return $this->success('success', $agent);
  373. }
  374. }
  375. }
  376. }