CommunityHeadController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. namespace addons\Community\backend\controllers;
  3. use addons\Community\common\enums\CommunityEnum;
  4. use addons\Community\common\models\CommunityGoods;
  5. use addons\Community\common\models\CommunityHead;
  6. use addons\Community\common\models\CommunityHeadApply;
  7. use addons\Community\common\service\SettingService;
  8. use common\enums\StatusEnum;
  9. use common\helpers\RegularHelper;
  10. use common\models\common\Region;
  11. use common\models\user\User;
  12. use common\models\user\UserLevel;
  13. use yii\helpers\Json;
  14. /**
  15. * 默认控制器
  16. *
  17. * Class CommunityHeadController
  18. * @package addons\Community\backend\controllers
  19. */
  20. class CommunityHeadController extends BaseController
  21. {
  22. public $enableCsrfValidation = false;
  23. /**
  24. * 首页
  25. *
  26. * @return string
  27. */
  28. public function actionIndex()
  29. {
  30. if (!\Yii::$app->request->isAjax) {
  31. return $this->render($this->action->id);
  32. }
  33. if (\Yii::$app->request->isGet) {
  34. $page = \Yii::$app->request->get('page', 1);
  35. $limit = \Yii::$app->request->get('limit', $this->pageSize);
  36. $name = \Yii::$app->request->get('name');
  37. $address = \Yii::$app->request->get('address');
  38. $user_id = \Yii::$app->request->get('user_id');
  39. $start_at = \Yii::$app->request->get('start_at');
  40. $end_at = \Yii::$app->request->get('end_at');
  41. $nickname = \Yii::$app->request->get('nickname');
  42. $mobile = \Yii::$app->request->get('mobile');
  43. $where = [];
  44. $where[] = ['mall_id' => $this->mall_id];
  45. $where[] = ['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]];
  46. $where[] = ['check_status' => CommunityEnum::CHECK_ADOPT];
  47. if (!empty($name)) $where[] = ['like', 'name', $name];
  48. if (!empty($address)) $where[] = ['like', 'address', $address];
  49. if (!empty($user_id)) $where[] = ['user_id' => $user_id];
  50. if (!empty($start_at)) $where[] = ['>=', 'created_at', strtotime($start_at)];
  51. if (!empty($end_at)) $where[] = ['<=', 'created_at', strtotime($end_at)];
  52. if (!empty($mobile)) $where[] = ['mobile' => $mobile];
  53. if (!empty($nickname)) {
  54. $n_where = [
  55. 'or', ['like', 'nickname', $nickname]
  56. ];
  57. $users = User::find()
  58. ->select('id')
  59. ->where($n_where)
  60. ->andWhere(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
  61. ->asArray()
  62. ->indexBy('id')
  63. ->all();
  64. $user_ids = array_column($users, 'id');
  65. $where[] = ['user_id' => $user_ids];
  66. }
  67. $params = [
  68. 'select' => 'id,mall_id,user_id,mobile,name,province_id,check_status,city_id,district_id,address,logo,idcard_img,license_img,status,created_at',
  69. 'where' => $where,
  70. 'order' => 'created_at desc',
  71. 'page' => $page,
  72. 'limit' => $limit,
  73. 'with' => ['user' => function ($query) {
  74. return $query->select('avatar_url,id,nickname');
  75. }]
  76. ];
  77. $store_list = CommunityHead::listPage($params);
  78. if (false === $store_list) {
  79. return $this->error(CommunityHead::getStaticError());
  80. }
  81. if (!empty($store_list['list'])) {
  82. foreach ($store_list['list'] as &$item) {
  83. $item['idcard_img'] = Json::decode($item['idcard_img']);
  84. }
  85. }
  86. return $this->success('success', $store_list);
  87. }
  88. }
  89. public function actionDelete()
  90. {
  91. if (\Yii::$app->request->isAjax) {
  92. if (\Yii::$app->request->isPost) {
  93. $param = \Yii::$app->request->post();
  94. $t = \Yii::$app->db->beginTransaction();
  95. try {
  96. $one = CommunityHead::findOne(['mall_id' => $this->mall_id, 'id' => $param['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  97. if (!$one) throw new \Exception('数据id错误');
  98. $one->status = StatusEnum::DELETE;
  99. if (!$one->save()) throw new \Exception($one->getErrorMessage());
  100. $apply = CommunityHeadApply::findOne(['mall_id' => $this->mall_id, 'user_id' => $one->user_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  101. if (!empty($apply)) {
  102. $apply->status = StatusEnum::DELETE;
  103. if (!$apply->save()) throw new \Exception($one->getErrorMessage());
  104. }
  105. // 删除商品
  106. CommunityGoods::find()->createCommand()->update(CommunityGoods::tableName(), [
  107. 'status' => StatusEnum::DELETE
  108. ], ['and', ['mall_id' => $this->mall_id], ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]], ['community_id' => $one->id]])->execute();
  109. $t->commit();
  110. return $this->success('删除成功');
  111. } catch (\Exception $e) {
  112. $t->rollBack();
  113. return $this->error($e->getMessage());
  114. }
  115. }
  116. }
  117. }
  118. public function actionStorage()
  119. {
  120. if (!\Yii::$app->request->isAjax) {
  121. return $this->render($this->action->id);
  122. }
  123. if (\Yii::$app->request->isGet) {
  124. $param = \Yii::$app->request->get();
  125. $setting_service = new SettingService();
  126. $area = $setting_service->getFieldSetting($this->mall_id, 'area', true);
  127. $area = $area ? json_decode($area, true) : $area;
  128. $regions = Region::find()
  129. ->select('id,name,lng,lat')
  130. ->where(['level' => 1])
  131. ->andWhere(['id' => $area])
  132. ->asArray()
  133. ->indexBy('id')
  134. ->all();
  135. $community_head = [];
  136. $id = $param['id'] ?? 0;
  137. $provinceInfo = [];
  138. $cityInfo = [];
  139. $districtInfo = [];
  140. $citys = [];
  141. $districts = [];
  142. if ($id > 0) {
  143. $where = [
  144. ['mall_id' => $this->mall_id],
  145. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
  146. ['id' => $param['id']]
  147. ];
  148. $community_head = CommunityHead::getOne($where, true, ['user']);
  149. $city_regions = [
  150. 'where' => [['level' => 2], ['id' => $area], ['parent_id' => $community_head['province_id']]],
  151. 'select' => 'id,name,lng,lat',
  152. 'index' => 'id',
  153. ];
  154. $district_regions = [
  155. 'where' => [['level' => 3], ['id' => $area], ['parent_id' => $community_head['city_id']]],
  156. 'select' => 'id,name,lng,lat',
  157. 'index' => 'id',
  158. ];
  159. if (!empty($community_head)) {
  160. $community_head['idcard_img'] = Json::decode($community_head['idcard_img']);
  161. }
  162. if (!empty($community_head['setting'])) {
  163. $community_head['setting'] = json_decode($community_head['setting'], true);
  164. } else {
  165. $community_head['setting'] = [
  166. "is_enable_reward" => 0,
  167. "settlement_type" => 1,
  168. "calculate_type" => 0,
  169. "head_commission_type" => 0,
  170. "commission" => 0,
  171. ];
  172. }
  173. $citys = Region::lists($city_regions, true);
  174. $districts = Region::lists($district_regions, true);
  175. $provinceInfo = $regions[$community_head['province_id']] ?? [];
  176. $cityInfo = $citys[$community_head['city_id']] ?? [];
  177. $districtInfo = $districts[$community_head['district_id']] ?? [];
  178. }
  179. return $this->success('success', ['provinces' => $regions, 'citys' => $citys, 'districts' => $districts, 'community_head' => $community_head, 'provinceInfo' => $provinceInfo, 'cityInfo' => $cityInfo, 'districtInfo' => $districtInfo]);
  180. } else {
  181. $post = \Yii::$app->request->post();
  182. $rules = [
  183. [['user_id', 'mobile', 'contacts', 'name', 'logo', 'license_img', 'province_id', 'city_id', 'district_id', 'address', 'longitude', 'latitude'], 'required'],
  184. [['user_id', 'mobile', 'province_id', 'city_id', 'district_id', 'id', 'is_home_delivery'], 'integer'],
  185. [['longitude', 'latitude'], 'number'],
  186. [['contacts', 'name', 'desc', 'logo', 'license_img', 'address'], 'string', 'max' => 255],
  187. ['mobile', function ($attribute, $params) {
  188. if (!RegularHelper::verify('mobile', $this->$attribute)) {
  189. $this->addError($attribute, '手机号格式错误');
  190. }
  191. }],
  192. ['desc', 'default', 'value' => ''],
  193. [['desc', 'idcard_img', 'setting'], 'safe']
  194. ];
  195. $res = \Yii::$app->services->validate->validate($post, $rules, [
  196. 'province_id' => '省份',
  197. 'city_id' => '城市',
  198. 'district_id' => '地区',
  199. 'mobile' => '手机号码',
  200. ]);
  201. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  202. if (!empty($post['setting'])) {
  203. $post['setting'] = json_encode($post['setting']);
  204. }
  205. $post['mall_id'] = $this->mall_id;
  206. $post['check_status'] = CommunityEnum::CHECK_ADOPT;
  207. $post['idcard_img'] = Json::encode($post['idcard_img']);
  208. $res = CommunityHead::setData($post, $this->admin->id);
  209. if (false === $res) {
  210. return $this->error(CommunityHead::getStaticError());
  211. }
  212. return $this->success('添加成功');
  213. }
  214. }
  215. /**
  216. * @Description: 根据地区id获取第一层子级地区信息列表
  217. * @Author: zhngsan
  218. * @DateTime 2023-02-08 11:22:11
  219. * @return mixed
  220. */
  221. public function actionGetRegions()
  222. {
  223. if (!\Yii::$app->request->isGet) {
  224. return $this->error('请求方式错误');
  225. }
  226. $region_id = \Yii::$app->request->get('region_id');
  227. if (empty($region_id)) {
  228. return $this->error('参数错误');
  229. }
  230. // 获取基础设置中包含的地区
  231. $setting_service = new SettingService();
  232. $area = $setting_service->getFieldSetting($this->mall_id, 'area', true);
  233. $area = $area ? json_decode($area, true) : $area;
  234. $region = Region::findOne($region_id);
  235. if (empty($region)) {
  236. return $this->error('地区信息未找到');
  237. }
  238. $region_list = Region::find()
  239. ->select('id,name,lng,lat')
  240. ->where(['parent_id' => $region->id, 'id' => $area])
  241. ->asArray()
  242. ->all();
  243. return $this->success('success', $region_list);
  244. }
  245. /**
  246. * @Description: 更改状态
  247. * @Author: zhngsan
  248. * @DateTime 2023-02-08 15:43:13
  249. * @return mixed
  250. */
  251. public function actionSwitchStatus()
  252. {
  253. if (\Yii::$app->request->isAjax) {
  254. if (\Yii::$app->request->isPost) {
  255. try {
  256. $param = \Yii::$app->request->post();
  257. $one = CommunityHead::findOne(['mall_id' => $this->mall_id, 'id' => $param['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  258. if (!$one) throw new \Exception('数据id错误');
  259. $one->status = $param['status'];
  260. if (!$one->save()) throw new \Exception($one->getErrorMessage());
  261. return $this->success('操作成功');
  262. } catch (\Exception $e) {
  263. return $this->error($e->getMessage());
  264. }
  265. }
  266. }
  267. }
  268. /**
  269. * @name:
  270. * @msg: 获取会员列表
  271. * @param {*}
  272. * @return {*}
  273. */
  274. public function actionGetUsers()
  275. {
  276. if (!\Yii::$app->request->isGet) {
  277. return $this->error('请求方式错误');
  278. }
  279. $user_id = \Yii::$app->request->get('user_id');
  280. $keyword = \Yii::$app->request->get('keyword');
  281. $page = \Yii::$app->request->get('page', 1);
  282. $limit = \Yii::$app->request->get('limit', $this->pageSize);
  283. $store_user_ids = CommunityHead::find()->select('user_id')->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])->asArray()->column();
  284. $wheres[] = ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED];
  285. $wheres[] = ['not in', 'id', $store_user_ids];
  286. if (!empty($user_id)) {
  287. $wheres[] = ['id' => $user_id];
  288. }
  289. if (!empty($keyword)) {
  290. $wheres[] = ['or', ['like', 'nickname', $keyword], ['like', 'mobile', $keyword]];
  291. }
  292. $params = [
  293. 'select' => 'id,nickname,avatar_url,mobile,level,parent_id',
  294. 'where' => $wheres,
  295. 'order' => 'created_at desc',
  296. 'page' => $page,
  297. 'limit' => $limit,
  298. ];
  299. $user_list = User::listPage($params);
  300. if (empty($user_list['list'])) {
  301. if (false === $user_list['list']) {
  302. return $this->error(User::getStaticError());
  303. }
  304. } else {
  305. $levels = UserLevel::find()
  306. ->select('id,level,name')
  307. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
  308. ->asArray()
  309. ->indexBy('level')
  310. ->all();
  311. $parent_ids = array_column($user_list['list'], 'parent_id');
  312. $parent_users = User::find()
  313. ->select('id,nickname,avatar_url,mobile,level,parent_id')
  314. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $parent_ids])
  315. ->asArray()
  316. ->indexBy('id')
  317. ->all();
  318. foreach ($user_list['list'] as &$user) {
  319. if (empty($user['avatar_url'])) {
  320. $user['avatar_url'] = \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png';
  321. }
  322. $level = $levels[$user['level']] ?? [];
  323. $parent_user = $parent_users[$user['parent_id']] ?? [];
  324. $user['level_name'] = $level['name'] ?? '';
  325. $user['parent_nickname'] = $parent_user['nickname'] ?? '';
  326. }
  327. }
  328. return $this->success('success', $user_list);
  329. }
  330. }