StoreController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. namespace addons\Happiness\agent\controllers;
  3. use addons\Happiness\common\enums\HappinessEnum;
  4. use addons\Happiness\common\models\HappinessStore;
  5. use addons\Happiness\common\models\HappinessStoreAdmin;
  6. use addons\Happiness\common\models\HappinessStoreGoods;
  7. use addons\Store\common\models\StoreCate;
  8. use addons\Store\common\models\StoreCommunity;
  9. use common\enums\StatusEnum;
  10. use common\models\common\Region;
  11. use common\models\mall\Mall;
  12. use common\models\mall\MallHost;
  13. use common\models\user\Town;
  14. use common\models\user\User;
  15. use Yii;
  16. class StoreController extends BaseController
  17. {
  18. /**
  19. * 门店首页(概况)
  20. * @Author bing
  21. * @DateTime 2022-04-26 18:06:27
  22. * @return void
  23. * @copyright: Copyright (c) 2022 广东七件事集团
  24. */
  25. public function actionIndex()
  26. {
  27. if (!\Yii::$app->request->isAjax) {
  28. return $this->render('/store/index');
  29. }
  30. if (\Yii::$app->request->isGet) {
  31. $page = \Yii::$app->request->get('page', 1);
  32. $limit = \Yii::$app->request->get('limit', $this->pageSize);
  33. $store_name = \Yii::$app->request->get('store_name');
  34. $storeowner = \Yii::$app->request->get('storeowner');
  35. $nickname = \Yii::$app->request->get('nickname');
  36. // 门店状态,1:正常;2:过期;3:禁用
  37. $status = \Yii::$app->request->get('status');
  38. $time = time();
  39. $wheres[] = ['mall_id' => $this->mall_id];
  40. $wheres[] = ['check_status' => StatusEnum::ENABLED];
  41. //or条件
  42. $wheres[] = ['or', ['in', 'city_id', $this->city_ids], ['in', 'district_id', $this->district_ids], ['in', 'province_id', $this->province_ids]];
  43. if (empty($status)) {
  44. $wheres[] = ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]];
  45. } else {
  46. if (1 == $status) {
  47. $wheres[] = ['status' => StatusEnum::ENABLED];
  48. $wheres[] = ['or', ['>=', 'expire_time', $time], ['expire_time' => 0]];
  49. } elseif (2 == $status) {
  50. $wheres[] = ['status' => StatusEnum::ENABLED];
  51. $wheres[] = ['<=', 'expire_time', $time];
  52. $wheres[] = ['!=', 'expire_time', 0];
  53. } elseif (3 == $status) {
  54. $wheres[] = ['status' => StatusEnum::DISABLED];
  55. } else {
  56. return $this->error('参数错误');
  57. }
  58. }
  59. if (!empty($store_name)) {
  60. $store_name = trim($store_name);
  61. $wheres[] = ['or', ['like', 'store_name', $store_name], ['id' => $store_name]];
  62. }
  63. if (!empty($storeowner)) {
  64. $wheres[] = ['like', 'shop_owner', $storeowner];
  65. }
  66. if (!empty($nickname)) {
  67. $users = User::find()
  68. ->select('id,nickname,mobile,avatar_url')
  69. ->where(['like', 'nickname', $nickname])
  70. ->andWhere(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
  71. ->asArray()
  72. ->indexBy('id')
  73. ->all();
  74. $user_ids = array_column($users, 'id');
  75. $wheres[] = ['user_id' => $user_ids];
  76. }
  77. $params = [
  78. 'select' => 'id,mall_id,user_id,shop_owner,business_id,shop_mobile,store_name,province_id,city_id,district_id,address,logo_img,total_money,balance,expire_time,user_nums,status,created_at,is_freeze,total_order_money,total_settled_amount,unsettled_amount,total_num',
  79. 'where' => $wheres,
  80. 'order' => 'created_at desc',
  81. 'page' => $page,
  82. 'limit' => $limit,
  83. 'with' => ['business' => function ($query) {
  84. $query->select('id,nickname,mobile');
  85. }],
  86. ];
  87. $store_list = HappinessStore::listPage($params);
  88. if (false === $store_list) {
  89. return $this->error(HappinessStore::getStaticError());
  90. }
  91. if (!empty($store_list['list'])) {
  92. if (empty($users)) {
  93. $user_ids = array_unique(array_column($store_list['list'], 'user_id'));
  94. $users = User::find()
  95. ->select('id,nickname,mobile,avatar_url')
  96. ->where(['id' => $user_ids, 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
  97. ->asArray()
  98. ->indexBy('id')
  99. ->all();
  100. }
  101. $region_ids = [];
  102. $province_ids = array_unique(array_column($store_list['list'], 'province_id'));
  103. $city_ids = array_unique(array_column($store_list['list'], 'city_id'));
  104. $district_ids = array_unique(array_column($store_list['list'], 'district_id'));
  105. $street_ids = array_unique(array_column($store_list['list'], 'street_id'));
  106. $community_ids = array_unique(array_column($store_list['list'], 'community_id'));
  107. $region_ids = array_merge($province_ids, $city_ids, $district_ids);
  108. $regions = Region::find()
  109. ->select('id,name')
  110. ->where(['id' => $region_ids])
  111. ->asArray()
  112. ->indexBy('id')
  113. ->all();
  114. $streets = town::find()
  115. ->select('id,name')
  116. ->where(['id' => $street_ids])
  117. ->asArray()
  118. ->indexBy('id')
  119. ->all();
  120. $communitys = StoreCommunity::find()
  121. ->select('id,community_name')
  122. ->where(['mall_id' => $this->mall_id, 'id' => $community_ids])
  123. ->asArray()
  124. ->indexBy('id')
  125. ->all();
  126. $H5Url = MallHost::getHost($this->mall_id, 'h5_url');
  127. foreach ($store_list['list'] as &$list) {
  128. $user = $users[$list['user_id']] ?? [];
  129. $province = $regions[$list['province_id']] ?? [];
  130. $city = $regions[$list['city_id']] ?? [];
  131. $district = $regions[$list['district_id']] ?? [];
  132. $streetInfo = $streets[$list['street_id']] ?? [];
  133. if ($list['community_id'] == '-1') {
  134. $CommunityInfo = '其它社区';
  135. } else {
  136. if (isset($communitys[$list['community_id']])) {
  137. $CommunityInfo = $communitys[$list['community_id']]['community_name'];
  138. } else {
  139. $CommunityInfo = '';
  140. }
  141. }
  142. $list['nickname'] = $user['nickname'] ?? '';
  143. $list['user_mobile'] = $user['mobile'] ?? '';
  144. $list['avatar_url'] = $user['avatar_url'] ?: \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png';
  145. $list['business']['avatar_url'] = $list['business']['avatar_url'] ?: \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png';
  146. //判断address字段是否只是详细
  147. preg_match('/(.*?(?:省|自治区|北京市|天津市|上海市|重庆市))+(.*?(?:市|自治州|地区|区划|县))+(.*?(?:市|区|县|镇|乡|街道))/', $list['address'], $matches);
  148. if (count($matches) > 1) {
  149. $list['store_address'] = $list['address'];
  150. } else {
  151. $list['store_address'] = ($province['name'] ?? '') . ($city['name'] ?? '') . ($district['name'] ?? '') . ($streetInfo['name'] ?? '') . ($CommunityInfo ?? '') . $list['address'];
  152. }
  153. // 门店状态,1:正常;2:过期;3:禁用
  154. if ($list['status'] == StatusEnum::ENABLED) {
  155. $list['store_status'] = 1;
  156. } elseif ($list['status'] == StatusEnum::DISABLED) {
  157. $list['store_status'] = 3;
  158. } else {
  159. $list['store_status'] = 1;
  160. }
  161. }
  162. }
  163. return $this->success('success', $store_list);
  164. }
  165. }
  166. public function actionDetail()
  167. {
  168. if (!\Yii::$app->request->isAjax) {
  169. return $this->render('/store/detail');
  170. }
  171. if (\Yii::$app->request->isGet) {
  172. $store_id = \Yii::$app->request->get('store_id');
  173. if (empty($store_id)) {
  174. return $this->error('参数错误');
  175. }
  176. $store = HappinessStore::find()
  177. ->where(['mall_id' => $this->mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED], 'id' => $store_id])
  178. ->asArray()
  179. ->one();
  180. if (empty($store)) {
  181. return $this->error('门店不存在');
  182. }
  183. $streets = Town::find()
  184. ->where(['district_id'=>$store['district_id']])
  185. ->select('id,name')
  186. ->asArray()
  187. ->indexBy('id')
  188. ->all();
  189. $street_name = $streets[$store['street_id']]['name']??'';
  190. $region_ids = [$store['province_id'], $store['city_id'], $store['district_id']];
  191. $regions = Region::find()
  192. ->select('id,name')
  193. ->where(['id' => $region_ids])
  194. ->asArray()
  195. ->indexBy('id')
  196. ->all();
  197. $store['addr'] = $regions[$store['province_id']]['name'] . '/' . $regions[$store['city_id']]['name'] . '/' . $regions[$store['district_id']]['name'].'/' . $street_name;
  198. $good_counts = HappinessStoreGoods::find()
  199. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'store_id' => $store_id])
  200. ->count();
  201. $store['good_counts'] = $good_counts;
  202. $store['total_order_number'] = $store['total_num'];
  203. $store['unsettled_amount'] = $store['total_expenses'];
  204. $store['show_address'] = $regions[$store['province_id']]['name'] . $regions[$store['city_id']]['name'] .
  205. $regions[$store['district_id']]['name'] .$street_name.$store['address'];
  206. $basic_setting = $store;
  207. $store_admin = HappinessStoreAdmin::findOne(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'store_id' => $store_id]);
  208. $store_setting = [
  209. 'shop_status' => $store['shop_status'] ?? 0,
  210. 'business_time_start' => $store['business_time_start'] ?? '',
  211. 'business_time_end' => $store['business_time_end'] ?? '',
  212. 'username' => $store_admin->username,
  213. 'merchant_distribution' => $store['merchant_distribution'] ?? 1,
  214. 'self_mention' => $store['self_mention'] ?? 1,
  215. ];
  216. // if (!empty($basic_setting['freight_type'])) $store_setting['freight_type'] = json_decode($basic_setting['freight_type'], true);
  217. if (!empty($basic_setting['intra_city_distribution'])) $store_setting['intra_city_distribution'] = json_decode($basic_setting['intra_city_distribution'], true);
  218. $setting = \Yii::$app->services->setting->addons->get($this->mall_id, HappinessEnum::ADDONS_NAME, 'basic');
  219. $configs = [
  220. 'delivery_amount' => $setting['delivery_amount'], //起送金额
  221. 'delivery_fee' => $setting['delivery_fee'], //配送费设置
  222. 'start_limit' => $setting['start_limit'], //初始公里数
  223. 'start_amount' => $setting['start_amount'], //初始配送费
  224. 'add_limit' => $setting['add_limit'], //每次增加配送费公里数
  225. 'add_amount' => $setting['add_amount'], //每次增加配送费数量
  226. ];
  227. if ($setting['is_location_limit'] == 1) {
  228. $configs['localtion_limit'] = $setting['localtion_limit'];
  229. } else {
  230. $configs['localtion_limit'] = '';
  231. }
  232. //赋值省市区街道等详情信息
  233. $regions_provinces = Region::find()
  234. ->select('id,name')
  235. ->where(['level' => 1])
  236. ->asArray()
  237. ->indexBy('id')
  238. ->all();
  239. return $this->success('success', ['basic_setting' => $basic_setting, 'store_setting' => $store_setting, 'provinces' => $regions_provinces, 'configs' => $configs]);
  240. } else {
  241. }
  242. }
  243. //修改密码
  244. public function actionUpdatePassword()
  245. {
  246. $params = Yii::$app->request->post();
  247. $res = Yii::$app->services->validate->validate($params, [
  248. [['old_pass', 'new_pass', 'check_pass'], 'required'],
  249. ]);
  250. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  251. if ($params['new_pass'] != $params['check_pass']) return $this->error('新密码和确认密码不一致');
  252. if (!$this->admin->validatePassword($params['old_pass'])) {
  253. return $this->error('旧密码不正确');
  254. }
  255. $this->admin->password = Yii::$app->security->generatePasswordHash($params['new_pass']);
  256. $res = $this->admin->save();
  257. if ($res === false) return $this->error('修改密码失败');
  258. return $this->success('操作成功');
  259. }
  260. }