StoreController.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. <?php
  2. /*
  3. * @Descripttion:
  4. * @version:
  5. * @Author: ewgof
  6. * @Date: 2022-04-29 10:31:21
  7. * @LastEditors: Please set LastEditors
  8. * @LastEditTime: 2024-07-15 15:26:41
  9. */
  10. namespace addons\Happiness\backend\controllers;
  11. use addons\Happiness\common\enums\HappinessVoucherEnum;
  12. use addons\Happiness\common\logic\voucher\WalletLogic;
  13. use addons\Happiness\common\models\HappinessStore;
  14. use addons\Happiness\common\models\HappinessStoreAdmin;
  15. use addons\Happiness\common\enums\HappinessEnum;
  16. use addons\Happiness\common\models\HappinessClerk;
  17. use addons\Happiness\common\models\HappinessStoreGoods;
  18. use addons\Happiness\common\models\HappinessVoucherWallet;
  19. use addons\Store\common\models\StoreCate;
  20. use addons\Store\common\models\StoreCommunity;
  21. use addons\WeightBonus\common\enums\WeightBonusEnum;
  22. use common\enums\StatusEnum;
  23. use common\helpers\RegularHelper;
  24. use common\models\common\CapitalLog;
  25. use common\models\common\Region;
  26. use common\models\mall\MallHost;
  27. use common\models\user\User;
  28. use common\models\user\UserLevel;
  29. use Yii;
  30. use common\models\user\Town;
  31. class StoreController extends BaseController
  32. {
  33. public function actionIndex()
  34. {
  35. if (!\Yii::$app->request->isAjax) {
  36. return $this->render('/store/index');
  37. }
  38. if (\Yii::$app->request->isGet) {
  39. $page = \Yii::$app->request->get('page', 1);
  40. $limit = \Yii::$app->request->get('limit', $this->pageSize);
  41. $store_name = \Yii::$app->request->get('store_name');
  42. $storeowner = \Yii::$app->request->get('storeowner');
  43. $nickname = \Yii::$app->request->get('nickname');
  44. // 门店状态,1:正常;2:过期;3:禁用
  45. $status = \Yii::$app->request->get('status');
  46. $time = time();
  47. $wheres[] = ['mall_id' => $this->mall_id];
  48. $wheres[] = ['check_status' => StatusEnum::ENABLED];
  49. if (empty($status)) {
  50. $wheres[] = ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]];
  51. } else {
  52. if (1 == $status) {
  53. $wheres[] = ['status' => StatusEnum::ENABLED];
  54. $wheres[] = ['or', ['>=', 'expire_time', $time], ['expire_time' => 0]];
  55. } elseif (2 == $status) {
  56. $wheres[] = ['status' => StatusEnum::ENABLED];
  57. $wheres[] = ['<=', 'expire_time', $time];
  58. $wheres[] = ['!=', 'expire_time', 0];
  59. } elseif (3 == $status) {
  60. $wheres[] = ['status' => StatusEnum::DISABLED];
  61. } else {
  62. return $this->error('参数错误');
  63. }
  64. }
  65. if (!empty($store_name)) {
  66. $store_name = trim($store_name);
  67. $wheres[] = ['or', ['like', 'store_name', $store_name], ['id' => $store_name]];
  68. }
  69. if (!empty($storeowner)) {
  70. $wheres[] = ['like', 'shop_owner', $storeowner];
  71. }
  72. if (!empty($nickname)) {
  73. $users = User::find()
  74. ->select('id,nickname,mobile,avatar_url')
  75. ->where(['like', 'nickname', $nickname])
  76. ->andWhere(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
  77. ->asArray()
  78. ->indexBy('id')
  79. ->all();
  80. $user_ids = array_column($users, 'id');
  81. $wheres[] = ['user_id' => $user_ids];
  82. }
  83. $params = [
  84. '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',
  85. 'where' => $wheres,
  86. 'order' => 'created_at desc',
  87. 'page' => $page,
  88. 'limit' => $limit,
  89. 'with' => ['business' => function ($query) {
  90. $query->select('id,nickname,mobile');
  91. }],
  92. ];
  93. $store_list = HappinessStore::listPage($params);
  94. if (false === $store_list) {
  95. return $this->error(HappinessStore::getStaticError());
  96. }
  97. if (!empty($store_list['list'])) {
  98. if (empty($users)) {
  99. $user_ids = array_unique(array_column($store_list['list'], 'user_id'));
  100. $users = User::find()
  101. ->select('id,nickname,mobile,avatar_url')
  102. ->where(['id' => $user_ids, 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
  103. ->asArray()
  104. ->indexBy('id')
  105. ->all();
  106. }
  107. $region_ids = [];
  108. $province_ids = array_unique(array_column($store_list['list'], 'province_id'));
  109. $city_ids = array_unique(array_column($store_list['list'], 'city_id'));
  110. $district_ids = array_unique(array_column($store_list['list'], 'district_id'));
  111. $street_ids = array_unique(array_column($store_list['list'], 'street_id'));
  112. $community_ids = array_unique(array_column($store_list['list'], 'community_id'));
  113. $region_ids = array_merge($province_ids, $city_ids, $district_ids);
  114. $regions = Region::find()
  115. ->select('id,name')
  116. ->where(['id' => $region_ids])
  117. ->asArray()
  118. ->indexBy('id')
  119. ->all();
  120. $streets = town::find()
  121. ->select('id,name')
  122. ->where(['id' => $street_ids])
  123. ->asArray()
  124. ->indexBy('id')
  125. ->all();
  126. $communitys = StoreCommunity::find()
  127. ->select('id,community_name')
  128. ->where(['mall_id' => $this->mall_id, 'id' => $community_ids])
  129. ->asArray()
  130. ->indexBy('id')
  131. ->all();
  132. $H5Url = MallHost::getHost($this->mall_id, 'h5_url');
  133. foreach ($store_list['list'] as &$list) {
  134. $h5_domain = $H5Url . '/#/plugins2/Store/pay/cashRegister?sign=' . $this->mall['mall_sign'];
  135. $user = $users[$list['user_id']] ?? [];
  136. $category = $categorys[$list['c_id']] ?? [];
  137. $province = $regions[$list['province_id']] ?? [];
  138. $city = $regions[$list['city_id']] ?? [];
  139. $district = $regions[$list['district_id']] ?? [];
  140. $streetInfo = $streets[$list['street_id']] ?? [];
  141. if ($list['community_id'] == '-1') {
  142. $CommunityInfo = '其它社区';
  143. } else {
  144. if (isset($communitys[$list['community_id']])) {
  145. $CommunityInfo = $communitys[$list['community_id']]['community_name'];
  146. } else {
  147. $CommunityInfo = '';
  148. }
  149. }
  150. $list['nickname'] = $user['nickname'] ?? '';
  151. $list['user_mobile'] = $user['mobile'] ?? '';
  152. $list['avatar_url'] = $user['avatar_url'] ?: \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png';
  153. $list['business']['avatar_url'] = $list['business']['avatar_url'] ?: \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png';
  154. //判断address字段是否只是详细
  155. preg_match('/(.*?(?:省|自治区|北京市|天津市|上海市|重庆市))+(.*?(?:市|自治州|地区|区划|县))+(.*?(?:市|区|县|镇|乡|街道))/', $list['address'], $matches);
  156. if (count($matches) > 1) {
  157. $list['store_address'] = $list['address'];
  158. } else {
  159. $list['store_address'] = ($province['name'] ?? '') . ($city['name'] ?? '') . ($district['name'] ?? '') . ($streetInfo['name'] ?? '') . ($CommunityInfo ?? '') . $list['address'];
  160. }
  161. // 门店状态,1:正常;2:过期;3:禁用
  162. if ($list['status'] == StatusEnum::ENABLED) {
  163. $list['store_status'] = 1;
  164. } elseif ($list['status'] == StatusEnum::DISABLED) {
  165. $list['store_status'] = 3;
  166. } else {
  167. $list['store_status'] = 1;
  168. }
  169. $h5_domain .= '&store_id=' . $list['id'];
  170. $list['store_collection_url'] = $h5_domain;
  171. $list['promotion_code'] = $H5Url . '/#/plugins2/Store/store/index?store_id=' . $list['id'] . "&sign=" . $this->mall['mall_sign'];
  172. }
  173. }
  174. $store_list['store_login_url'] = \Yii::$app->request->hostInfo . '?r=happiness/client/auth/login&sign=' . $this->mall['mall_sign'];
  175. $cate_list = StoreCate::find()
  176. ->select('id,name')
  177. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
  178. ->asArray()
  179. ->all();
  180. $store_list['cate_list'] = $cate_list;
  181. $recharge_list = \Yii::$app->services->setting->addons->get($this->mall_id, HappinessEnum::ADDONS_NAME, 'basic.recharge_list');
  182. $recharge_list = isset($recharge_list) ? json_decode($recharge_list, true) : [];
  183. $store_list['recharge_list'] = $recharge_list;
  184. return $this->success('success', $store_list);
  185. }
  186. }
  187. /**
  188. * @name:
  189. * @msg: 切换门店状态
  190. * @param {*}
  191. * @return {*}
  192. */
  193. public function actionSwitchStatus()
  194. {
  195. if (!\Yii::$app->request->isPost) {
  196. return $this->error('请求方式错误');
  197. }
  198. $post = \Yii::$app->request->post();
  199. $rules = [
  200. [['store_id', 'status'], 'required'],
  201. [['store_id', 'status'], 'integer'],
  202. ['status', 'in', 'range' => [1, 3]],
  203. ];
  204. $res = \Yii::$app->services->validate->validate($post, $rules);
  205. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  206. if (1 == $post['status']) {
  207. $status = StatusEnum::ENABLED;
  208. } else {
  209. $status = StatusEnum::DISABLED;
  210. }
  211. $params = [
  212. 'id' => $post['store_id'],
  213. 'status' => $status,
  214. 'mall_id' => $this->mall_id,
  215. ];
  216. $res = HappinessStore::setData($params);
  217. if (false === $res) {
  218. return $this->error(HappinessStore::getStaticError());
  219. }
  220. return $this->success('操作成功');
  221. }
  222. /**
  223. * @name:
  224. * @msg: 根据地区id获取第一层子级地区信息列表
  225. * @param {*}
  226. * @return {*}
  227. */
  228. public function actionGetRegions()
  229. {
  230. if (!\Yii::$app->request->isGet) {
  231. return $this->error('请求方式错误');
  232. }
  233. $region_id = \Yii::$app->request->get('region_id');
  234. $level = \Yii::$app->request->get('level');
  235. if (empty($region_id)) {
  236. return $this->error('参数错误');
  237. }
  238. $region = Region::findOne($region_id);
  239. if (empty($region) && $level <= 3) {
  240. return $this->error('地区信息未找到');
  241. }
  242. if (isset($level) && $level >= 3) {
  243. if ($level == 3) {
  244. $region_list = Town::find()
  245. ->select('id,name')
  246. ->where(['district_id' => $region->id])
  247. ->asArray()
  248. ->indexBy('id')
  249. ->all();
  250. } else {
  251. $region_list = StoreCommunity::find()
  252. ->select('id,community_name')
  253. ->where(['street_id' => $region_id, 'mall_id' => $this->mall_id, 'status' => 0])
  254. ->asArray()
  255. ->indexBy('id')
  256. ->all();
  257. $data = ['id' => '-1', 'community_name' => '其它社区'];
  258. $region_list[] = $data;
  259. }
  260. } else {
  261. $region_list = Region::find()
  262. ->select('id,name,lng,lat')
  263. ->where(['parent_id' => $region->id])
  264. ->asArray()
  265. ->indexBy('id')
  266. ->all();
  267. }
  268. return $this->success('success', $region_list);
  269. }
  270. /**
  271. * @name:
  272. * @msg: 获取用户列表
  273. * @param {*}
  274. * @return {*}
  275. */
  276. public function actionGetUsers()
  277. {
  278. if (!\Yii::$app->request->isGet) {
  279. return $this->error('请求方式错误');
  280. }
  281. $user_id = \Yii::$app->request->get('user_id');
  282. $keyword = \Yii::$app->request->get('keyword');
  283. $page = \Yii::$app->request->get('page', 1);
  284. $limit = \Yii::$app->request->get('limit', $this->pageSize);
  285. $store_user_ids = HappinessStore::find()->select('user_id')->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])->asArray()->column();
  286. $wheres[] = ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED];
  287. $wheres[] = ['not in', 'id', $store_user_ids];
  288. if (!empty($user_id)) {
  289. $wheres[] = ['id' => $user_id];
  290. }
  291. if (!empty($keyword)) {
  292. $wheres[] = ['or', ['like', 'nickname', $keyword], ['like', 'mobile', $keyword]];
  293. }
  294. $params = [
  295. 'select' => 'id,nickname,avatar_url,mobile,level,parent_id',
  296. 'where' => $wheres,
  297. 'order' => 'created_at desc',
  298. 'page' => $page,
  299. 'limit' => $limit,
  300. ];
  301. $user_list = User::listPage($params);
  302. if (empty($user_list['list'])) {
  303. if (false === $user_list['list']) {
  304. return $this->error(User::getStaticError());
  305. }
  306. } else {
  307. $levels = UserLevel::find()
  308. ->select('id,level,name')
  309. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
  310. ->asArray()
  311. ->indexBy('level')
  312. ->all();
  313. $parent_ids = array_column($user_list['list'], 'parent_id');
  314. $parent_users = User::find()
  315. ->select('id,nickname,avatar_url,mobile,level,parent_id')
  316. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $parent_ids])
  317. ->asArray()
  318. ->indexBy('id')
  319. ->all();
  320. foreach ($user_list['list'] as &$user) {
  321. if (empty($user['avatar_url'])) {
  322. $user['avatar_url'] = \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png';
  323. }
  324. $level = $levels[$user['level']] ?? [];
  325. $parent_user = $parent_users[$user['parent_id']] ?? [];
  326. $user['level_name'] = $level['name'] ?? '';
  327. $user['parent_nickname'] = $parent_user['nickname'] ?? '';
  328. }
  329. }
  330. return $this->success('success', $user_list);
  331. }
  332. /**
  333. * @name:
  334. * @msg: 门店添加/编辑
  335. * @param {*}
  336. * @return {*}
  337. */
  338. public function actionStorage()
  339. {
  340. if (!\Yii::$app->request->isAjax) {
  341. return $this->render('/store/add-edit');
  342. }
  343. if (\Yii::$app->request->isGet) {
  344. $regions = Region::find()
  345. ->select('id,name,lng,lat')
  346. ->where(['level' => 1])
  347. ->asArray()
  348. ->indexBy('id')
  349. ->all();
  350. $setting = \Yii::$app->services->setting->addons->get($this->mall_id, HappinessEnum::ADDONS_NAME, 'basic');
  351. $configs = [
  352. 'delivery_amount' => $setting['delivery_amount'], //起送金额
  353. 'delivery_fee' => $setting['delivery_fee'], //配送费设置
  354. 'start_limit' => $setting['start_limit'], //初始公里数
  355. 'start_amount' => $setting['start_amount'], //初始配送费
  356. 'add_limit' => $setting['add_limit'], //每次增加配送费公里数
  357. 'add_amount' => $setting['add_amount'], //每次增加配送费数量
  358. ];
  359. if ($setting['is_location_limit'] == 1) {
  360. $configs['localtion_limit'] = $setting['localtion_limit'];
  361. } else {
  362. $configs['localtion_limit'] = '';
  363. }
  364. return $this->success('success', ['provinces' => $regions, 'configs' => $configs]);
  365. } else {
  366. $post = \Yii::$app->request->post();
  367. $rules = [
  368. [['user_id', 'shop_mobile', 'shop_owner', 'store_name', 'logo_img', 'license_img', 'province_id', 'city_id', 'district_id', 'address', 'longitude', 'latitude'], 'required'],
  369. [['user_id', 'shop_mobile', 'province_id', 'city_id', 'district_id','street_id', 'merchant_distribution', 'self_mention', 'shop_status','business_id'], 'integer'],
  370. [['longitude', 'latitude'], 'number'],
  371. [['shop_owner', 'store_name', 'store_desc', 'logo_img', 'license_img', 'address', 'business_time_start', 'business_time_end'], 'string', 'max' => 255],
  372. [['logo_img', 'license_img'], 'url'],
  373. ['shop_mobile', function ($attribute, $params) {
  374. if (!RegularHelper::verify('mobile', $this->$attribute)) {
  375. $this->addError($attribute, '手机号格式错误');
  376. }
  377. }],
  378. [['freight_type', 'intra_city_distribution'], 'safe']
  379. ];
  380. $res = \Yii::$app->services->validate->validate($post, $rules);
  381. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  382. $post['mall_id'] = $this->mall_id;
  383. // $post['expire_time'] = strtotime("+1 year");
  384. if (empty($post['store_desc'])) $post['store_desc'] = '';
  385. $setting = \Yii::$app->services->setting->addons->get($this->mall_id, HappinessEnum::ADDONS_NAME, 'basic');
  386. $post['is_alone'] = $post['intra_city_distribution']['is_alone'];
  387. if ($post['is_alone'] == 1) {
  388. $post['distribution_scope'] = $post['intra_city_distribution']['distribution_scope'];
  389. } else if ($post['is_alone'] == 0) {
  390. $post['distribution_scope'] = $setting['localtion_limit'] ? $setting['localtion_limit'] : 100000;
  391. if ($setting['is_location_limit'] == 0) $post['distribution_scope'] = 100000;
  392. }
  393. $post['check_status']=HappinessEnum::CHECK_ADOPT;
  394. $streets = Town::find()
  395. ->where(['district_id'=>$post['district_id']])
  396. ->select('id,name')
  397. ->asArray()
  398. ->indexBy('id')
  399. ->all();
  400. $street_name = $streets[$post['street_id']]['name']??'';
  401. $region_ids = [$post['province_id'], $post['city_id'], $post['district_id']];
  402. $regions = Region::find()
  403. ->select('id,name')
  404. ->where(['id' => $region_ids])
  405. ->asArray()
  406. ->indexBy('id')
  407. ->all();
  408. $name_rule=!empty($setting['name_rule'])?json_decode($setting['name_rule'],true):[];
  409. $store_name='';
  410. if(!empty($name_rule))
  411. {
  412. if(in_array('province',$name_rule))
  413. {
  414. $store_name.=$regions[$post['province_id']]['name'].'-';
  415. }
  416. if(in_array('city',$name_rule))
  417. {
  418. $store_name.=$regions[$post['city_id']]['name'].'-';
  419. }
  420. if(in_array('district',$name_rule))
  421. {
  422. $store_name.=$regions[$post['district_id']]['name'].'-';
  423. }
  424. if(in_array('street',$name_rule))
  425. {
  426. $store_name.=$street_name.'-';
  427. }
  428. if(in_array('no',$name_rule))
  429. {
  430. //取最新的编号
  431. $no=$setting['name_rule_no'];
  432. $has=HappinessStore::find()
  433. ->where(['mall_id'=>$this->mall_id])
  434. ->orderBy('id desc')
  435. ->asArray()
  436. ->one();
  437. if(!empty($has))
  438. {
  439. $no=$has['no'];
  440. }
  441. $number = intval($no) + 1;
  442. $post['no'] = str_pad($number, strlen($no), '0', STR_PAD_LEFT);
  443. $store_name.=$post['no'].'-';
  444. }
  445. $post['store_name'] = $store_name.$post['store_name'];
  446. }
  447. else
  448. {
  449. return $this->error('请设置小店命名规则');
  450. }
  451. $store = HappinessStore::addStore($post);
  452. if (false === $store) {
  453. return $this->error(HappinessStore::getStaticError());
  454. }
  455. if (!empty($store)) {
  456. $params = [
  457. 'mall_id' => $store['mall_id'],
  458. 'user_id' => $store['user_id'],
  459. 'store_id' => $store['id'],
  460. 'remark' => '后台新增门店添加',
  461. ];
  462. $res = HappinessClerk::setData($params);
  463. }
  464. // }
  465. return $this->success('添加成功');
  466. }
  467. }
  468. /**
  469. * @name:
  470. * @msg: 门店详情
  471. * @param {*}
  472. * @return {*}
  473. */
  474. public function actionDetail()
  475. {
  476. if (!\Yii::$app->request->isAjax) {
  477. return $this->render('/store/detail');
  478. }
  479. if (\Yii::$app->request->isGet) {
  480. $store_id = \Yii::$app->request->get('store_id');
  481. if (empty($store_id)) {
  482. return $this->error('参数错误');
  483. }
  484. $store = HappinessStore::find()
  485. ->where(['mall_id' => $this->mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED], 'id' => $store_id])
  486. ->asArray()
  487. ->one();
  488. if (empty($store)) {
  489. return $this->error('门店不存在');
  490. }
  491. $streets = Town::find()
  492. ->where(['district_id'=>$store['district_id']])
  493. ->select('id,name')
  494. ->asArray()
  495. ->indexBy('id')
  496. ->all();
  497. $street_name = $streets[$store['street_id']]['name']??'';
  498. $region_ids = [$store['province_id'], $store['city_id'], $store['district_id']];
  499. $regions = Region::find()
  500. ->select('id,name')
  501. ->where(['id' => $region_ids])
  502. ->asArray()
  503. ->indexBy('id')
  504. ->all();
  505. $store['addr'] = $regions[$store['province_id']]['name'] . '/' . $regions[$store['city_id']]['name'] . '/' . $regions[$store['district_id']]['name'].'/' . $street_name;
  506. $good_counts = HappinessStoreGoods::find()
  507. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'store_id' => $store_id])
  508. ->count();
  509. $store['good_counts'] = $good_counts;
  510. $store['total_order_number'] = $store['total_num'];
  511. $store['unsettled_amount'] = $store['total_expenses'];
  512. $store['show_address'] = $regions[$store['province_id']]['name'] . $regions[$store['city_id']]['name'] .
  513. $regions[$store['district_id']]['name'] .$street_name.$store['address'];
  514. $basic_setting = $store;
  515. $store_admin = HappinessStoreAdmin::findOne(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'store_id' => $store_id]);
  516. $store_setting = [
  517. 'shop_status' => $store['shop_status'] ?? 0,
  518. 'business_time_start' => $store['business_time_start'] ?? '',
  519. 'business_time_end' => $store['business_time_end'] ?? '',
  520. 'username' => $store_admin->username,
  521. 'merchant_distribution' => $store['merchant_distribution'] ?? 1,
  522. 'self_mention' => $store['self_mention'] ?? 1,
  523. ];
  524. // if (!empty($basic_setting['freight_type'])) $store_setting['freight_type'] = json_decode($basic_setting['freight_type'], true);
  525. if (!empty($basic_setting['intra_city_distribution'])) $store_setting['intra_city_distribution'] = json_decode($basic_setting['intra_city_distribution'], true);
  526. $setting = \Yii::$app->services->setting->addons->get($this->mall_id, HappinessEnum::ADDONS_NAME, 'basic');
  527. $configs = [
  528. 'delivery_amount' => $setting['delivery_amount'], //起送金额
  529. 'delivery_fee' => $setting['delivery_fee'], //配送费设置
  530. 'start_limit' => $setting['start_limit'], //初始公里数
  531. 'start_amount' => $setting['start_amount'], //初始配送费
  532. 'add_limit' => $setting['add_limit'], //每次增加配送费公里数
  533. 'add_amount' => $setting['add_amount'], //每次增加配送费数量
  534. ];
  535. if ($setting['is_location_limit'] == 1) {
  536. $configs['localtion_limit'] = $setting['localtion_limit'];
  537. } else {
  538. $configs['localtion_limit'] = '';
  539. }
  540. //赋值省市区街道等详情信息
  541. $regions_provinces = Region::find()
  542. ->select('id,name')
  543. ->where(['level' => 1])
  544. ->asArray()
  545. ->indexBy('id')
  546. ->all();
  547. return $this->success('success', ['basic_setting' => $basic_setting, 'store_setting' => $store_setting, 'provinces' => $regions_provinces, 'configs' => $configs]);
  548. } else {
  549. $post = \Yii::$app->request->post();
  550. $group = $post['group'];
  551. $store_id = $post['store_id'];
  552. if (empty($group) || !in_array($group, ['store_setting', 'cashier_setting', 'basic_setting']) || empty($store_id)) {
  553. return $this->error('参数错误');
  554. }
  555. // dd($post[$group]);
  556. $rules = [
  557. 'basic_setting' => [
  558. [['store_name', 'logo_img', 'license_img', 'longitude', 'latitude', 'address'], 'string', 'max' => 255],
  559. [['shop_mobile', 'expire_time', 'province_id', 'city_id', 'district_id','street_id'], 'integer'],
  560. ],
  561. 'store_setting' => [
  562. [['shop_status', 'business_time_start', 'business_time_end', 'username'], 'required'],
  563. [['shop_status', 'merchant_distribution', 'self_mention'], 'integer'],
  564. [['business_time_start', 'business_time_end', 'username'], 'string'],
  565. [['password', 'repassword', 'freight_type', 'intra_city_distribution'], 'safe'],
  566. ],
  567. ];
  568. $res = \Yii::$app->services->validate->validate($post[$group], $rules[$group]);
  569. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  570. $post[$group]['store_id'] = $store_id;
  571. if ('store_setting' == $group) {
  572. $setting = \Yii::$app->services->setting->addons->get($this->mall_id, HappinessEnum::ADDONS_NAME, 'basic');
  573. $post[$group]['is_alone'] = $post[$group]['intra_city_distribution']['is_alone'];
  574. if ($post[$group]['is_alone'] == 1) {
  575. $post[$group]['distribution_scope'] = $post[$group]['intra_city_distribution']['distribution_scope'];
  576. } else if ($post[$group]['is_alone'] == 0) {
  577. $post[$group]['distribution_scope'] = $setting['localtion_limit'] ? $setting['localtion_limit'] : 100000;
  578. if ($setting['is_location_limit'] == 0) $post[$group]['distribution_scope'] = 100000;
  579. }
  580. $res = HappinessStore::saveStoreSetting($this->mall_id, $post[$group]);
  581. if (false === $res) {
  582. return $this->error(HappinessStore::getStaticError());
  583. }
  584. return $this->success('保存成功');
  585. } elseif ('basic_setting' == $group) {
  586. $post[$group]['mall_id'] = $this->mall_id;
  587. $post[$group]['id'] = $store_id;
  588. unset($post[$group]['store_id']);
  589. // dd($post);
  590. // $addr = $post[$group]['address'] ?? '';
  591. // if (!empty($addr)) {
  592. // $addrs = explode('/', $addr);
  593. // if(!empty($addrs[0])&&!empty( $addrs[1])&&!empty( $addrs[2])){
  594. // $addrs[0] = str_replace('中国','',$addrs[0]);
  595. // $regions = Region::find()
  596. // ->select('id,name,level')
  597. // ->where(['or', ['name' => $addrs[0]], ['name' => $addrs[1]], ['name' => $addrs[2]]])
  598. // ->asArray()
  599. // ->indexBy('level')
  600. // ->all();
  601. // $post[$group]['province_id'] = $regions[1]['id'];
  602. // $post[$group]['city_id'] = $regions[2]['id'];
  603. // $post[$group]['district_id'] = $regions[3]['id'];
  604. // }
  605. // }
  606. $res = HappinessStore::setData($post[$group]);
  607. if (false === $res) {
  608. return $this->error(HappinessStore::getStaticError());
  609. }
  610. return $this->success('保存成功');
  611. }
  612. }
  613. }
  614. /**
  615. * 组件获取店铺列表
  616. * @Author: lun
  617. * @DateTime: 2022/8/3 0003 18:40
  618. * @Copyright: copyright (c) 2021 广东七件事集团
  619. * @return mixed|void
  620. */
  621. public function actionStoreList()
  622. {
  623. $retuest = \Yii::$app->request;
  624. if ($retuest->isAjax) {
  625. if ($retuest->isGet) {
  626. $get = \Yii::$app->request->get();
  627. // 检查提交的参数
  628. $res = \Yii::$app->services->validate->validate($get, [
  629. [['id'], 'integer'],
  630. [['store_name'], 'string'],
  631. [['store_ids', 'exist_ids'], 'safe'],
  632. ]);
  633. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  634. $where = $list = [];
  635. !empty($get['id']) && $where[] = ['=', 'id', $get['id']];
  636. $where[] = ['=', 'mall_id', $this->mall_id];
  637. !empty($get['store_name']) && $where[] = ['like', 'store_name', $get['store_name']];
  638. !empty($get['store_ids']) && $where[] = ['id' => $get['store_ids']];
  639. !empty($get['exist_ids']) && $where[] = ['not in', 'id', $get['exist_ids']];
  640. $where[] = ['=', 'check_status', StatusEnum::ENABLED];
  641. $where[] = ['=', 'status', StatusEnum::ENABLED];
  642. $order = 'created_at desc';
  643. $select = 'id,c_id,mall_id,logo_img,store_name,shop_mobile,created_at';
  644. $params = array('where' => $where, 'select' => $select, 'order' => $order, 'limit' => $get['limit']);
  645. $params['with'] = ['cate' => function ($query) {
  646. $query->select('id, name');
  647. }];
  648. $list = HappinessStore::listPage($params, false, true);
  649. return $this->success('获取成功', $list);
  650. }
  651. }
  652. }
  653. /**
  654. * 选择门店接口,外部调用
  655. * @return mixed|void
  656. */
  657. public function actionSelectStore()
  658. {
  659. if (Yii::$app->request->isAjax && Yii::$app->request->isGet) {
  660. $params = Yii::$app->request->get();
  661. $params['where'][] = ['mall_id' => $this->mall_id];
  662. $params['where'][] = ['status' => [StatusEnum::ENABLED]];
  663. if (!empty($params['keyword'])) $params['where'][] = ['or', ['like', 'store_name', $params['keyword']], ['id' => $params['keyword']]];
  664. $params['order'] = 'id desc';
  665. $params['select'] = 'id,store_name,shop_owner,shop_mobile,logo_img,store_desc,shop_status,status';
  666. $list = HappinessStore::listPage($params);
  667. return $this->success('操作成功', $list);
  668. }
  669. }
  670. //门店删除
  671. public function actionDel()
  672. {
  673. try {
  674. $params = Yii::$app->request->post();
  675. HappinessStore::delStore($this->mall_id, $params['id']);
  676. return $this->success();
  677. } catch (\Exception $e) {
  678. return $this->error($e->getMessage());
  679. }
  680. }
  681. //发放提货券
  682. public function actionRecharge()
  683. {
  684. try {
  685. $params = Yii::$app->request->post();
  686. $number = $params['number'];//充值的金额
  687. $store_id = $params['store_id'];//门店id
  688. $recharge_list = \Yii::$app->services->setting->addons->get($this->mall_id, HappinessEnum::ADDONS_NAME, 'basic.recharge_list');
  689. $recharge_list = !empty($recharge_list) ? json_decode($recharge_list, true) : [];
  690. if (empty($recharge_list)) {
  691. throw new \Exception('请先配置充值金额');
  692. }
  693. $itemSetting = [];
  694. foreach ($recharge_list as $item) {
  695. if ($item['number'] == $number) {
  696. $itemSetting = $item;
  697. break;
  698. }
  699. }
  700. if (empty($itemSetting) || empty($itemSetting['items'])) {
  701. throw new \Exception('请先配置充值金额');
  702. }
  703. $wallet_log = [];
  704. foreach ($itemSetting['items'] as $item) {
  705. $wallet_log[] = [
  706. 'mall_id' => $this->mall_id,
  707. 'store_id' => $store_id,
  708. 'level' => $item['level'],
  709. 'change_type' => CapitalLog::CHANGE_TYPE_ADD,
  710. 'change_num' => $item['price'],
  711. 'desc' => '管理员后台充值',
  712. 'source_table' => HappinessVoucherWallet::tableName(),
  713. 'source_table_id' => $store_id,
  714. 'from_type' => HappinessVoucherEnum::SYSTEM_ADD,
  715. 'wallet_type' => HappinessVoucherEnum::WALLET_VALUE,
  716. 'status' => 1,
  717. 'order_no' => ''
  718. ];
  719. }
  720. if(!empty($wallet_log))
  721. {
  722. foreach ($wallet_log as $item)
  723. {
  724. try {
  725. $res=WalletLogic::handle($item);
  726. }
  727. catch (\Exception $e)
  728. {
  729. \Yii::error($e->getMessage());
  730. }
  731. if($res===false)
  732. {
  733. throw new \Exception(WalletLogic::getStaticError());
  734. }
  735. }
  736. }
  737. return $this->success('操作成功');
  738. } catch (\Exception $e) {
  739. return $this->error($e->getMessage());
  740. }
  741. }
  742. }