StoreController.php 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  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\Store\backend\controllers;
  11. use addons\Agent\common\models\AgentLevel;
  12. use addons\BusinessBonus\common\events\user\UpgradeUserEvent;
  13. use addons\Distribution\common\enums\DistributionEnum;
  14. use addons\Distribution\common\models\DistributionLevel;
  15. use addons\HuifuPay\common\service\HuifuBillShareService;
  16. use addons\Store\common\enums\StoreEnum;
  17. use addons\Store\common\models\Store;
  18. use addons\Store\common\models\StoreAdmin;
  19. use addons\Store\common\models\StoreCate;
  20. use addons\Store\common\models\StoreClerk;
  21. use addons\Store\common\models\StoreCommunity;
  22. use addons\Store\common\models\StoreGoods;
  23. use addons\Store\common\models\StoreMiniCode;
  24. use addons\Store\common\models\StoreModel;
  25. use addons\Store\common\models\StoreOrder;
  26. use addons\Store\common\models\StoreSettings;
  27. use common\enums\AddonsEnum;
  28. use common\enums\GoodsTypeEnum;
  29. use common\enums\OrderStatusEnum;
  30. use common\enums\StatusEnum;
  31. use common\globals\GlobalInvoke;
  32. use common\helpers\MiniQrCode;
  33. use common\helpers\QrCode;
  34. use common\helpers\RegularHelper;
  35. use common\logic\common\AddonsLimit;
  36. use common\models\common\MallSetting;
  37. use common\models\common\Region;
  38. use common\models\mall\MallAddons;
  39. use common\models\mall\MallHost;
  40. use common\models\user\User;
  41. use common\models\user\UserLevel;
  42. use Yii;
  43. use common\models\user\Town;
  44. use addons\Store\common\logic\StoreCreateSync;
  45. use addons\Store\common\services\StoreService;
  46. class StoreController extends BaseController
  47. {
  48. public function actionIndex()
  49. {
  50. if (!\Yii::$app->request->isAjax) {
  51. return $this->render('/store/index');
  52. }
  53. if (\Yii::$app->request->isGet) {
  54. $page = \Yii::$app->request->get('page', 1);
  55. $limit = \Yii::$app->request->get('limit', $this->pageSize);
  56. $store_name = \Yii::$app->request->get('store_name');
  57. $cate_id = \Yii::$app->request->get('cate_id');
  58. $storeowner = \Yii::$app->request->get('storeowner');
  59. $nickname = \Yii::$app->request->get('nickname');
  60. // 门店状态,1:正常;2:过期;3:禁用
  61. $status = \Yii::$app->request->get('status');
  62. $time_sort = \Yii::$app->request->get('time_sort');
  63. $time = time();
  64. $wheres[] = ['mall_id' => $this->mall_id, 'check_status' => StoreEnum::CHECK_ADOPT];
  65. if (empty($status)) {
  66. $wheres[] = ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]];
  67. } else {
  68. if (1 == $status) {
  69. $wheres[] = ['status' => StatusEnum::ENABLED];
  70. $wheres[] = ['or', ['>=', 'expire_time', $time], ['expire_time' => 0]];
  71. } elseif (2 == $status) {
  72. $wheres[] = ['status' => StatusEnum::ENABLED];
  73. $wheres[] = ['<=', 'expire_time', $time];
  74. $wheres[] = ['!=', 'expire_time', 0];
  75. } elseif (3 == $status) {
  76. $wheres[] = ['status' => StatusEnum::DISABLED];
  77. } else {
  78. return $this->error('参数错误');
  79. }
  80. }
  81. if (!empty($cate_id)) {
  82. $wheres[] = ['c_id' => $cate_id];
  83. }
  84. if (!empty($store_name)) {
  85. $store_name = trim($store_name);
  86. $wheres[] = ['or',['like', 'store_name', $store_name], ['id' => $store_name]];
  87. }
  88. if (!empty($storeowner)) {
  89. $wheres[] = ['like', 'shop_owner', $storeowner];
  90. }
  91. if (!empty($nickname)) {
  92. $users = User::find()
  93. ->select('id,nickname,mobile,avatar_url')
  94. ->where(['like', 'nickname', $nickname])
  95. ->andWhere(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
  96. ->asArray()
  97. ->indexBy('id')
  98. ->all();
  99. $user_ids = array_column($users, 'id');
  100. $wheres[] = ['user_id' => $user_ids];
  101. }
  102. $orderBy = 'created_at desc';
  103. if (isset($time_sort) && $time_sort > 0) {
  104. switch ($time_sort) {
  105. case 1:
  106. $orderBy = 'expire_time asc';
  107. $wheres[] = ['>', 'expire_time', time()];
  108. break;
  109. case 2:
  110. $orderBy = 'expire_time desc';
  111. break;
  112. }
  113. }
  114. $params = [
  115. 'select' => 'id,mall_id,user_id,shop_owner,shop_mobile,store_name,c_id,province_id,check_status,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,street_id,community_id,is_sync,sync_status',
  116. 'where' => $wheres,
  117. 'order' => $orderBy,
  118. 'page' => $page,
  119. 'limit' => $limit,
  120. ];
  121. $store_list = Store::listPage($params);
  122. if (false === $store_list) {
  123. return $this->error(Store::getStaticError());
  124. }
  125. if (!empty($store_list['list'])) {
  126. if (empty($users)) {
  127. $user_ids = array_unique(array_column($store_list['list'], 'user_id'));
  128. $users = User::find()
  129. ->select('id,nickname,mobile,avatar_url')
  130. ->where(['id' => $user_ids, 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
  131. ->asArray()
  132. ->indexBy('id')
  133. ->all();
  134. }
  135. $cate_ids = array_unique(array_column($store_list['list'], 'c_id'));
  136. $categorys = StoreCate::find()
  137. ->select('id,name')
  138. ->where(['id' => $cate_ids, 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
  139. ->asArray()
  140. ->indexBy('id')
  141. ->all();
  142. $region_ids = [];
  143. $province_ids = array_unique(array_column($store_list['list'], 'province_id'));
  144. $city_ids = array_unique(array_column($store_list['list'], 'city_id'));
  145. $district_ids = array_unique(array_column($store_list['list'], 'district_id'));
  146. $street_ids = array_unique(array_column($store_list['list'], 'street_id'));
  147. $community_ids = array_unique(array_column($store_list['list'], 'community_id'));
  148. $region_ids = array_merge($province_ids, $city_ids, $district_ids);
  149. $regions = Region::find()
  150. ->select('id,name')
  151. ->where(['id' => $region_ids])
  152. ->asArray()
  153. ->indexBy('id')
  154. ->all();
  155. $streets = town::find()
  156. ->select('id,name')
  157. ->where(['id'=>$street_ids])
  158. ->asArray()
  159. ->indexBy('id')
  160. ->all();
  161. $communitys = StoreCommunity::find()
  162. ->select('id,community_name')
  163. ->where(['mall_id'=>$this->mall_id,'id'=>$community_ids])
  164. ->asArray()
  165. ->indexBy('id')
  166. ->all();
  167. $H5Url = MallHost::getHost($this->mall_id,'h5_url');
  168. foreach ($store_list['list'] as &$list) {
  169. $h5_domain = $H5Url . '/#/plugins2/Store/pay/cashRegister?sign=' . $this->mall['mall_sign'];
  170. $user = $users[$list['user_id']] ?? [];
  171. $category = $categorys[$list['c_id']] ?? [];
  172. $province = $regions[$list['province_id']] ?? [];
  173. $city = $regions[$list['city_id']] ?? [];
  174. $district = $regions[$list['district_id']] ?? [];
  175. $streetInfo = $streets[$list['street_id']] ?? [];
  176. if($list['community_id'] == '-1'){
  177. $CommunityInfo = '其它社区';
  178. }else{
  179. if(isset($communitys[$list['community_id']])){
  180. $CommunityInfo = $communitys[$list['community_id']]['community_name'];
  181. }else{
  182. $CommunityInfo = '';
  183. }
  184. }
  185. $list['nickname'] = $user['nickname'] ?? '';
  186. $list['avatar_url'] = $user['avatar_url'] ?: \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png';
  187. $list['category'] = $category['name'] ?? '';
  188. //判断address字段是否只是详细
  189. preg_match('/(.*?(?:省|自治区|北京市|天津市|上海市|重庆市))+(.*?(?:市|自治州|地区|区划|县))+(.*?(?:市|区|县|镇|乡|街道))/', $list['address'], $matches);
  190. if(count($matches) > 1){
  191. $list['store_address'] = $list['address'];
  192. }else{
  193. $list['store_address'] = ($province['name'] ?? '') . ($city['name'] ?? '') . ($district['name'] ?? ''). ($streetInfo['name'] ?? ''). ($CommunityInfo ?? '') . $list['address'];
  194. }
  195. // 门店状态,1:正常;2:过期;3:禁用
  196. if ($list['status'] == StatusEnum::ENABLED && $list['check_status'] == StoreEnum::CHECK_ADOPT && ($list['expire_time'] > $time || $list['expire_time'] == 0) && $list['is_freeze'] == StoreEnum::FREEZE_NO) {
  197. $list['store_status'] = 1;
  198. } elseif ($list['status'] == StatusEnum::ENABLED && $list['check_status'] == StoreEnum::CHECK_ADOPT && $list['expire_time'] > 0 && $list['expire_time'] <= $time) {
  199. $list['store_status'] = 2;
  200. } elseif ($list['status'] == StatusEnum::DISABLED) {
  201. $list['store_status'] = 3;
  202. }elseif($list['is_freeze'] == StoreEnum::FREEZE_YES) {
  203. $list['store_status'] = 4;
  204. } else {
  205. $list['store_status'] = 1;
  206. }
  207. if($list['is_sync'] && $list['sync_status'] == StoreEnum::SYNC_STATUS_IN_SYNC) {
  208. $list['store_status'] = 5;
  209. }
  210. $h5_domain .= '&store_id=' . $list['id'];
  211. $list['store_collection_url'] = $h5_domain;
  212. $list['promotion_code'] = $H5Url . '/#/plugins2/Store/store/index?store_id='.$list['id']."&sign=" . $this->mall['mall_sign']."&pid=" . $list['user_id'];
  213. }
  214. }
  215. $store_list['store_login_url'] = \Yii::$app->request->hostInfo . '?r=store/client/auth/login&sign=' . $this->mall['mall_sign'];
  216. $cate_list = StoreCate::find()
  217. ->select('id,name')
  218. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
  219. ->asArray()
  220. ->all();
  221. $store_list['cate_list'] = $cate_list;
  222. return $this->success('success', $store_list);
  223. }
  224. }
  225. /**
  226. * @name:
  227. * @msg: 切换门店状态
  228. * @param {*}
  229. * @return {*}
  230. */
  231. public function actionSwitchStatus()
  232. {
  233. if (!\Yii::$app->request->isPost) {
  234. return $this->error('请求方式错误');
  235. }
  236. $post = \Yii::$app->request->post();
  237. $rules = [
  238. [['store_id', 'status'], 'required'],
  239. [['store_id', 'status'], 'integer'],
  240. ['status', 'in', 'range' => [1, 3]],
  241. ];
  242. $res = \Yii::$app->services->validate->validate($post, $rules);
  243. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  244. if (1 == $post['status']) {
  245. $status = StatusEnum::ENABLED;
  246. } else {
  247. $status = StatusEnum::DISABLED;
  248. }
  249. $params = [
  250. 'id' => $post['store_id'],
  251. 'status' => $status,
  252. 'mall_id' => $this->mall_id,
  253. ];
  254. $res = Store::setData($params);
  255. if (false === $res) {
  256. return $this->error(Store::getStaticError());
  257. }
  258. return $this->success('操作成功');
  259. }
  260. /**
  261. * @name:
  262. * @msg: 根据地区id获取第一层子级地区信息列表
  263. * @param {*}
  264. * @return {*}
  265. */
  266. public function actionGetRegions()
  267. {
  268. if (!\Yii::$app->request->isGet) {
  269. return $this->error('请求方式错误');
  270. }
  271. $region_id = \Yii::$app->request->get('region_id');
  272. $level = \Yii::$app->request->get('level');
  273. if (empty($region_id)) {
  274. return $this->error('参数错误');
  275. }
  276. $region = Region::findOne($region_id);
  277. if (empty($region) && $level <= 3) {
  278. return $this->error('地区信息未找到');
  279. }
  280. if(isset($level) && $level >= 3){
  281. if($level == 3){
  282. $region_list = Town::find()
  283. ->select('id,name')
  284. ->where(['district_id' => $region->id])
  285. ->asArray()
  286. ->indexBy('id')
  287. ->all();
  288. }else{
  289. $region_list = StoreCommunity::find()
  290. ->select('id,community_name')
  291. ->where(['street_id' => $region_id,'mall_id'=>$this->mall_id,'status'=>0])
  292. ->asArray()
  293. ->indexBy('id')
  294. ->all();
  295. $data = ['id'=>'-1','community_name'=>'其它社区'];
  296. $region_list[] = $data;
  297. }
  298. }else{
  299. $region_list = Region::find()
  300. ->select('id,name,lng,lat')
  301. ->where(['parent_id' => $region->id])
  302. ->asArray()
  303. ->indexBy('id')
  304. ->all();
  305. }
  306. return $this->success('success', $region_list);
  307. }
  308. /**
  309. * @name:
  310. * @msg: 获取用户列表
  311. * @param {*}
  312. * @return {*}
  313. */
  314. public function actionGetUsers()
  315. {
  316. if (!\Yii::$app->request->isGet) {
  317. return $this->error('请求方式错误');
  318. }
  319. $user_id = \Yii::$app->request->get('user_id');
  320. $keyword = \Yii::$app->request->get('keyword');
  321. $page = \Yii::$app->request->get('page', 1);
  322. $limit = \Yii::$app->request->get('limit', $this->pageSize);
  323. $store_user_ids = Store::find()->select('user_id')->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])->asArray()->column();
  324. $wheres[] = ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED];
  325. $wheres[] = ['not in', 'id', $store_user_ids];
  326. if (!empty($user_id)) {
  327. $wheres[] = ['id' => $user_id];
  328. }
  329. if (!empty($keyword)) {
  330. $wheres[] = ['or', ['like', 'nickname', $keyword], ['like', 'mobile', $keyword]];
  331. }
  332. $params = [
  333. 'select' => 'id,nickname,avatar_url,mobile,level,parent_id',
  334. 'where' => $wheres,
  335. 'order' => 'created_at desc',
  336. 'page' => $page,
  337. 'limit' => $limit,
  338. ];
  339. $user_list = User::listPage($params);
  340. if (empty($user_list['list'])) {
  341. if (false === $user_list['list']) {
  342. return $this->error(User::getStaticError());
  343. }
  344. } else {
  345. $levels = UserLevel::find()
  346. ->select('id,level,name')
  347. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
  348. ->asArray()
  349. ->indexBy('level')
  350. ->all();
  351. $parent_ids = array_column($user_list['list'], 'parent_id');
  352. $parent_users = User::find()
  353. ->select('id,nickname,avatar_url,mobile,level,parent_id')
  354. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $parent_ids])
  355. ->asArray()
  356. ->indexBy('id')
  357. ->all();
  358. foreach ($user_list['list'] as &$user) {
  359. if (empty($user['avatar_url'])) {
  360. $user['avatar_url'] = \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png';
  361. }
  362. $level = $levels[$user['level']] ?? [];
  363. $parent_user = $parent_users[$user['parent_id']] ?? [];
  364. $user['level_name'] = $level['name'] ?? '';
  365. $user['parent_nickname'] = $parent_user['nickname'] ?? '';
  366. }
  367. }
  368. return $this->success('success', $user_list);
  369. }
  370. /**
  371. * @name:
  372. * @msg: 门店添加/编辑
  373. * @param {*}
  374. * @return {*}
  375. */
  376. public function actionStorage()
  377. {
  378. if (!\Yii::$app->request->isAjax) {
  379. return $this->render('/store/add-edit');
  380. }
  381. if (\Yii::$app->request->isGet) {
  382. $cate_list = StoreCate::find()
  383. ->select('id,name')
  384. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
  385. ->asArray()
  386. ->all();
  387. $regions = Region::find()
  388. ->select('id,name,lng,lat')
  389. ->where(['level' => 1])
  390. ->asArray()
  391. ->indexBy('id')
  392. ->all();
  393. $setting = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'community_set');
  394. $community_status = $setting['community_status']??0;
  395. $enter = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'enter');
  396. $enter['template_store_id'] = 0;
  397. if (empty($enter['template_store'])) {
  398. $enter['template_store'] = '';
  399. } else {
  400. $enter['template_store'] = json_decode($enter['template_store'], true);
  401. $template_store = end($enter['template_store']);
  402. $enter['template_store_id'] = $template_store['id'] ?? 0;
  403. }
  404. return $this->success('success', ['list' => $cate_list, 'provinces' => $regions,'community_status'=>$community_status, 'enter' => $enter]);
  405. } else {
  406. $post = \Yii::$app->request->post();
  407. $rules = [
  408. [['user_id', 'shop_mobile', 'shop_owner', 'store_name', 'c_id', 'logo_img', 'license_img', 'province_id', 'city_id', 'district_id', 'address', 'longitude', 'latitude'], 'required'],
  409. [['user_id', 'shop_mobile', 'c_id', 'province_id', 'city_id', 'district_id', 'street_id', 'community_id', 'is_set_template', 'template_store_id'], 'integer'],
  410. [['longitude', 'latitude'], 'number'],
  411. [['shop_owner', 'store_name', 'store_desc', 'logo_img', 'license_img', 'address'], 'string', 'max' => 255],
  412. [['logo_img', 'license_img'], 'url'],
  413. ['shop_mobile', function ($attribute, $params) {
  414. if (!RegularHelper::verify('mobile', $this->$attribute)) {
  415. $this->addError($attribute, '手机号格式错误');
  416. }
  417. }],
  418. ['store_desc', 'default', 'value' => ''],
  419. ['store_desc', 'safe']
  420. ];
  421. $res = \Yii::$app->services->validate->validate($post, $rules);
  422. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  423. // dd($post);
  424. $post['mall_id'] = $this->mall_id;
  425. $config_enter = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'enter');
  426. // dd($config_enter);
  427. if (1 == $config_enter['time_status']) {
  428. $days = $config_enter['expired_days'];
  429. $post['expire_time'] = time() + $days * 24 * 60 * 60;
  430. } else {
  431. $post['expire_time'] = 0;
  432. }
  433. $post['check_status'] = StoreEnum::CHECK_ADOPT;
  434. if($post['is_set_template']) {
  435. $post['is_sync'] = 1;
  436. }
  437. $post['is_examine'] = -1;
  438. $post['is_admin'] = 1;
  439. $store = Store::addStore($post);
  440. if (false === $store) {
  441. return $this->error(Store::getStaticError());
  442. }
  443. if($post['check_status']==StoreEnum::CHECK_ADOPT){
  444. if(!empty($store)){
  445. $params = [
  446. 'mall_id'=>$store['mall_id'],
  447. 'user_id'=>$store['user_id'],
  448. 'store_id'=>$store['id'],
  449. 'remark'=>'后台新增门店添加',
  450. ];
  451. $res = StoreClerk::setData($params);
  452. //触发招商分红用户条件升级
  453. if (MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_BUSINESS_BONUS)) {
  454. $event = new UpgradeUserEvent($this->mall_id);
  455. $data = ['mall_id' => $store['mall_id'], 'user_id' => $store['user_id']];
  456. \Yii::$app->services->events->dispatch($event, $data, $event->listeners);
  457. }
  458. }
  459. }
  460. //处理复制数据
  461. if ($post['is_set_template']) {
  462. \Yii::$app->services->rabbitMq->delay(10, [
  463. 'mall_id' => $this->mall_id,
  464. 'self_store_id' => $store['id'],
  465. 'source_store_id' => $post['template_store_id']
  466. ], StoreCreateSync::class, 'synStoreData');
  467. }
  468. return $this->success('添加成功');
  469. }
  470. }
  471. /**
  472. * @name:
  473. * @msg: 门店详情
  474. * @param {*}
  475. * @return {*}
  476. */
  477. public function actionDetail()
  478. {
  479. if (!\Yii::$app->request->isAjax) {
  480. // 获取组件配置
  481. $addons_setting = AddonsLimit::getGlobalSetting($this->mall_id);
  482. $addons_arr = [AddonsEnum::ADDONS_NAME_DISTRIBUTION, AddonsEnum::ADDONS_NAME_AGENT, AddonsEnum::ADDONS_NAME_AREA,AddonsEnum::ADDONS_NAME_DOUBLE_COPY,AddonsEnum::ADDONS_NAME_REBATE];
  483. if (!empty($addons_setting)) {
  484. foreach ($addons_setting as $key => $val) {
  485. if (!in_array($key, $addons_arr)) unset($addons_setting[$key]);
  486. }
  487. }
  488. $huifu_addons = StatusEnum::DISABLED;
  489. if (MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_HUIFU_PAY)) {
  490. $huifu_addons = HuifuBillShareService::isEnableShare($this->mall_id);
  491. }
  492. return $this->render('/store/detail', ['addons_settings' => $addons_setting,
  493. 'huifu_addons' => $huifu_addons]);
  494. }
  495. if (\Yii::$app->request->isGet) {
  496. $store_id = \Yii::$app->request->get('store_id');
  497. if (empty($store_id)) {
  498. return $this->error('参数错误');
  499. }
  500. $categorys = StoreCate::find()
  501. ->select('id,name,parent_id')
  502. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
  503. ->asArray()
  504. ->all();
  505. $store = Store::find()
  506. ->where(['mall_id' => $this->mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED], 'id' => $store_id])
  507. ->asArray()
  508. ->one();
  509. if (empty($store)) {
  510. return $this->error('门店不存在');
  511. }
  512. $store['cate_name'] = StoreCate::getAllParentName($store['c_id'],$categorys,$this->mall_id);
  513. $region_ids = [$store['province_id'], $store['city_id'], $store['district_id']];
  514. $regions = Region::find()
  515. ->select('id,name')
  516. ->where(['id' => $region_ids])
  517. ->asArray()
  518. ->indexBy('id')
  519. ->all();
  520. $store['addr'] = $regions[$store['province_id']]['name'] . '/' . $regions[$store['city_id']]['name'] . '/' . $regions[$store['district_id']]['name'];
  521. $good_counts = StoreGoods::find()
  522. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'store_id' => $store_id])
  523. ->count();
  524. $store['good_counts'] = $good_counts;
  525. $store['total_order_number'] = $store['total_num'];
  526. $store['unsettled_amount'] = $store['total_expenses'];
  527. $streets = Town::find()
  528. ->where(['district_id'=>$store['district_id']])
  529. ->select('id,name')
  530. ->asArray()
  531. ->indexBy('id')
  532. ->all();
  533. $street_name = $streets[$store['street_id']]['name']??'';
  534. if($store['community_id'] == '-1'){
  535. $communitys_name = '其它社区';
  536. }else{
  537. $communitys = StoreCommunity::find()
  538. ->select('id,community_name')
  539. ->where(['street_id' => $store['street_id'],'status'=>0])
  540. ->asArray()
  541. ->indexBy('id')
  542. ->all();
  543. $communitys_name = $communitys[$store['community_id']]['community_name'] ?? '';
  544. }
  545. $store['show_address'] = $regions[$store['province_id']]['name'].$regions[$store['city_id']]['name'].$regions[$store['district_id']]['name'].$street_name.$communitys_name.$store['address'];
  546. $store['address_names'] = $regions[$store['province_id']]['name'].'-'.$regions[$store['city_id']]['name'].'-'.$regions[$store['district_id']]['name'].'-'.$street_name.'-'.$communitys_name.'-'.$store['address'];
  547. $basic_setting = $store;
  548. $store_admin = StoreAdmin::findOne(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'store_id' =>$store_id]);
  549. $settings = StoreSettings::find()
  550. ->where(['mall_id' => $this->mall_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED], 'store_id' => $store_id])
  551. ->asArray()
  552. ->one();
  553. $default_store_service_ratio = 10;
  554. $basic = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_STORE, 'basic');
  555. if ($basic['service_fee_type'] == 1) {
  556. $default_store_service_ratio = $basic['max_platform_ratio'];
  557. }
  558. $store_setting = [
  559. 'shop_status' => $store['shop_status'] ?? 0,
  560. 'business_time_start' => $store['business_time_start'] ?? '',
  561. 'business_time_end' => $store['business_time_end'] ?? '',
  562. 'store_background' => $store['store_background'] ?? '',
  563. 'device_id' => $store['device_id'] ?? '',
  564. 'username' => $store_admin->username,
  565. 'store_service_fee' => $settings['store_service_fee'] ?? 0,
  566. 'store_service_fee_type' => $settings['store_service_fee_type'] ?? 0,
  567. 'store_service_ratio' => $settings['store_service_ratio'] ?? $default_store_service_ratio,
  568. 'huifu_mch_no' => $settings['huifu_mch_no'] ?? '',
  569. 'store_settle_type' => $settings['store_settle_type'] ?? 1,
  570. 'store_service_fee_settle_type' => $settings['store_service_fee_settle_type'] ?? 1,
  571. 'store_settle_scene' => $settings['store_settle_scene'] ?? 1,
  572. 'store_commission_authority' => json_decode($settings['store_commission_authority'], true) ?? [],
  573. 'show_sync_goods' => $store['show_sync_goods'] ?? 0,
  574. 'is_enable_withdraw_setting' => $settings['is_enable_withdraw_setting'] ?? 0,
  575. 'balance_cash_service_fee_limit' => $settings['balance_cash_service_fee_limit'] ?? 0,
  576. 'balance_cash_service_fee' => $settings['balance_cash_service_fee'] ?? 0,
  577. 'balance_cash_service_fee_diff' => $settings['balance_cash_service_fee_diff'] ?? 0,
  578. 'balance_cash_service_fee_month' => $settings['balance_cash_service_fee_month'] ?? 0,
  579. 'is_allow_editing' => $settings['is_allow_editing'] ?? 0,
  580. //平台设置的服务费类型
  581. 'platform_service_fee_type' => \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'basic.service_fee_type')
  582. ];
  583. /* ---------- 用户权重相关配置处理 start ------ */
  584. $cashier_setting = [
  585. 'cachier_service_ratio' => $settings['cachier_service_ratio'] ?? 10,
  586. 'cashier_member_status' => $settings['cashier_member_status'] ?? 0,
  587. 'cashier_member_settings' => $settings['cashier_member_settings'],
  588. 'cashier_distribution_is_enable' => $settings['cashier_distribution_is_enable'] ?? 0,
  589. 'cashier_distribution_is_enable_score' => $settings['cashier_distribution_is_enable_score'] ?? 0,
  590. 'cashier_distribution_settings' => $settings['cashier_distribution_settings'],
  591. 'cashier_distribution_settings_score' => $settings['cashier_distribution_settings_score']??[],
  592. 'cashier_agent_is_enable' => $settings['cashier_agent_is_enable'] ?? 0,
  593. 'cashier_agent_settings' => $settings['cashier_agent_settings'],
  594. 'preferential_undertaking' => $settings['preferential_undertaking']??'1',
  595. 'is_preferential_undertaking' => $settings['is_preferential_undertaking']??0,
  596. 'is_preferential_gift' => $settings['is_preferential_gift']??0,
  597. 'cashier_goods_complete_direct' => $settings['cashier_goods_complete_direct']??0,
  598. 'deepNames' => [],
  599. 'enable_discount_price' => $settings['enable_discount_price'] ?? 0,
  600. 'discount_price_scale' => $settings['discount_price_scale'] ?? 0,
  601. 'cashier_is_open_discount' => $settings['cashier_is_open_discount'] ?? 0,
  602. 'is_area_enable' => $settings['is_area_enable'] ?? 0,
  603. ];
  604. $level = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_DISTRIBUTION, 'basic.level');
  605. if(empty($level)) $level = 0;
  606. if($level>0) $cashier_setting['deepNames'][]='一级';
  607. if($level>1) $cashier_setting['deepNames'][]='二级';
  608. if($level>2) $cashier_setting['deepNames'][]='三级';
  609. $member_levels = UserLevel::find()
  610. ->select('level,name as level_name')
  611. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
  612. ->asArray()
  613. ->all();
  614. if (empty($member_levels)) {
  615. $member_levels = [
  616. // ['level' => 0, 'level_name' => '默认权重'],
  617. ];
  618. }
  619. if (!empty($cashier_setting['cashier_member_settings'])) {
  620. $setting_members = json_decode($cashier_setting['cashier_member_settings'], true);
  621. $mem_levels = array_column($setting_members, 'level');
  622. $mem_level_discount_map = array_column($setting_members, 'discount', 'level');
  623. } else {
  624. $mem_levels = [];
  625. $mem_level_discount_map = [];
  626. }
  627. foreach ($member_levels as &$mlevel) {
  628. if (!in_array($mlevel['level'], $mem_levels)) {
  629. $mlevel['discount'] = 0;
  630. } else {
  631. $mlevel['discount'] = floatval($mem_level_discount_map[$mlevel['level']] ?? 0);
  632. }
  633. }
  634. // dd($member_levels);
  635. $cashier_setting['cashier_member_settings'] = $member_levels;
  636. /* ---------- 用户权重相关配置处理 end ------ */
  637. /* ---------- 分销权重相关配置处理 start ------ */
  638. $is_install_distribution = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_DISTRIBUTION);
  639. if (!$is_install_distribution) {
  640. $distribution_settings = [];
  641. $is_distribution_enable = 0;
  642. $is_distribution_enable_score = 0;
  643. } else {
  644. $is_distribution_enable = 1;
  645. $is_distribution_enable_score = 1;
  646. $setting = \Yii::$app->services->setting->addons->get($this->mall_id, DistributionEnum::ADDONS_NAME, 'basic');
  647. $default_level_name = '默认等级';
  648. if(!empty($setting['default_level_name'])){
  649. $default_level_name = $setting['default_level_name'];
  650. }
  651. $distribution_levels = [['level' => 0, 'level_name' => $default_level_name]];
  652. $distribution_levels_temp = DistributionLevel::find()
  653. ->select('level,name as level_name')
  654. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
  655. ->asArray()
  656. ->all();
  657. $distribution_levels = array_merge($distribution_levels, $distribution_levels_temp);
  658. if (!empty($cashier_setting['cashier_distribution_settings'])) {
  659. $distribution_settings = json_decode($cashier_setting['cashier_distribution_settings'], true);
  660. $distr_levels = array_column($distribution_settings, 'level');
  661. $distr_level_discount_map = array_column($distribution_settings, 'commission_rate', 'level');
  662. } else {
  663. $distr_levels = $distr_level_discount_map = [];
  664. }
  665. if (!empty($cashier_setting['cashier_distribution_settings_score'])) {
  666. $distribution_settings = json_decode($cashier_setting['cashier_distribution_settings_score'], true);
  667. $distr_levels_score = array_column($distribution_settings, 'level');
  668. $distr_level_discount_map_score = array_column($distribution_settings, 'commission_rate', 'level');
  669. } else {
  670. $distr_levels_score = $distr_level_discount_map_score = [];
  671. }
  672. $distribution_levels_score = $distribution_levels;
  673. foreach ($distribution_levels as &$dlevel) {
  674. if (!in_array($dlevel['level'], $distr_levels)) {
  675. $dlevel['commission_rate'] = [0, 0,0];
  676. } else {
  677. $dlevel['commission_rate'] = $distr_level_discount_map[$dlevel['level']] ?? [0, 0,0];
  678. }
  679. }
  680. foreach ($distribution_levels_score as &$dlevel) {
  681. if (!in_array($dlevel['level'], $distr_levels_score)) {
  682. $dlevel['commission_rate'] = [0, 0,0];
  683. } else {
  684. $dlevel['commission_rate'] = $distr_level_discount_map_score[$dlevel['level']] ?? [0, 0,0];
  685. }
  686. }
  687. $cashier_setting['cashier_distribution_settings'] = $distribution_levels;
  688. $cashier_setting['cashier_distribution_settings_score'] = $distribution_levels_score;
  689. $cashier_setting['is_distribution_enable'] = $is_distribution_enable;
  690. $cashier_setting['is_distribution_enable_score'] = $is_distribution_enable_score;
  691. }
  692. /* ---------- 分销权重相关配置处理 end ------ */
  693. /****收银台区域奖励配置 ----start----****/
  694. $is_install_area = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_AREA);
  695. $area_currency_arr = ['detail' => '佣金', 'detail_score' => '积分'];
  696. $cashier_setting['area_currency'] = $area_currency_arr;
  697. if ($is_install_area) {
  698. $area_levels = [
  699. 6 => ['level' => 6, 'level_name' => '大区代'],
  700. 1 => ['level' => 1, 'level_name' => '省代'],
  701. 2 => ['level' => 2, 'level_name' => '市代'],
  702. 3 => ['level' => 3, 'level_name' => '区代'],
  703. 4 => ['level' => 4, 'level_name' => '镇代'],
  704. 5 => ['level' => 5, 'level_name' => '小区代']
  705. ];
  706. $areaSystem = json_decode($settings['cashier_area_settings'], true) ?? [];
  707. if (empty($areaSystem)) {
  708. // 默认配置
  709. foreach ($area_currency_arr as $area => $item) {
  710. foreach ($area_levels as $value) {
  711. $areaSystem[$area][] = [
  712. 'level' => $value['level'],
  713. 'level_name' => $value['level_name'],
  714. 'prize' => 0,
  715. 'equal_prize' => 0
  716. ];
  717. }
  718. }
  719. $areaSystem['detail_remaining_ratio'] = 0;
  720. $areaSystem['detail_score_remaining_ratio'] = 0;
  721. $areaSystem['is_enable'] = 0;
  722. }
  723. // 获取配置信息
  724. $cashier_setting['is_area_enable_cashier'] = 1;
  725. $cashier_setting['cashier_area_settings'] = $areaSystem;
  726. } else {
  727. $cashier_setting['is_area_enable_cashier'] = 0;
  728. $cashier_setting['cashier_area_settings'] = null;
  729. }
  730. /* ---------- 经销等级相关配置处理 start ------ */
  731. $is_install_agent = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_AGENT);
  732. if (!$is_install_agent) {
  733. $agent_settings = [
  734. 'team_rewards' => [
  735. // ['level' => 0, 'level_name' => '默认权重', 'commission_rate' => 0],
  736. ],
  737. 'equal_rewards' => [
  738. // ['level' => 0, 'level_name' => '默认权重', 'commission_rate' => 0],
  739. ],
  740. 'over_rewards' => [
  741. // ['level' => 0, 'level_name' => '默认权重', 'commission_rate' => 0],
  742. ],
  743. ];
  744. $is_agent_enable = 0;
  745. } else {
  746. $is_agent_enable = 1;
  747. $agent_levels = AgentLevel::find()
  748. ->select('level,name as level_name')
  749. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
  750. ->asArray()
  751. ->all();
  752. if (empty($agent_levels)) {
  753. $agent_levels = [
  754. // ['level' => 0, 'level_name' => '默认权重'],
  755. ];
  756. }
  757. if (!empty($cashier_setting['cashier_agent_settings'])) {
  758. $agent_settings = json_decode($cashier_setting['cashier_agent_settings'], true);
  759. } else {
  760. $agent_settings = [
  761. 'team_rewards' => [
  762. // ['level' => 0, 'level_name' => '默认权重', 'commission_rate' => 0],
  763. ],
  764. 'equal_rewards' => [
  765. // ['level' => 0, 'level_name' => '默认权重', 'commission_rate' => 0],
  766. ],
  767. 'over_rewards' => [
  768. // ['level' => 0, 'level_name' => '默认权重', 'commission_rate' => 0],
  769. ],
  770. ];
  771. }
  772. foreach ($agent_settings as $reward_name => $setting) {
  773. foreach ($setting as $val) {
  774. $agent_settings[$reward_name][$val['level']] = $val;
  775. }
  776. }
  777. $ag_levels = array_column($agent_settings['team_rewards'], 'level');
  778. $agent_level_settings = [];
  779. foreach ($agent_levels as $alevel) {
  780. if (!in_array($alevel['level'], $ag_levels)) {
  781. $arr = $alevel;
  782. $arr['commission_rate'] = 0;
  783. $agent_level_settings['team_rewards'][] = $arr;
  784. $agent_level_settings['equal_rewards'][] = $arr;
  785. $agent_level_settings['over_rewards'][] = $arr;
  786. } else {
  787. $arr = [
  788. 'level' => $alevel['level'],
  789. 'level_name' => $alevel['level_name'],
  790. 'commission_rate' => $agent_settings['team_rewards'][$alevel['level']]['commission_rate'] ?? 0,
  791. ];
  792. $agent_level_settings['team_rewards'][] = $arr;
  793. $arr['commission_rate'] = $agent_settings['equal_rewards'][$alevel['level']]['commission_rate'] ?? 0;
  794. $agent_level_settings['equal_rewards'][] = $arr;
  795. $arr['commission_rate'] = $agent_settings['over_rewards'][$alevel['level']]['commission_rate'] ?? 0;
  796. $agent_level_settings['over_rewards'][] = $arr;
  797. }
  798. }
  799. $cashier_setting['cashier_agent_settings'] = $agent_level_settings;
  800. $cashier_setting['is_agent_enable'] = $is_agent_enable;
  801. /* ---------- 经销权重相关配置处理 end ------ */
  802. }
  803. if(!isset($store_setting['device_id']))$store_setting['device_id'] = '';
  804. $setting = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'community_set');
  805. $community_status = $setting['community_status']??0;
  806. //赋值省市区街道等详情信息
  807. $regions_provinces = Region::find()
  808. ->select('id,name')
  809. ->where(['level' => 1])
  810. ->asArray()
  811. ->indexBy('id')
  812. ->all();
  813. return $this->success('success', ['basic_setting' => $basic_setting, 'store_setting' => $store_setting, 'cashier_setting' => $cashier_setting, 'categorys' => $categorys,'provinces'=>$regions_provinces,'community_status'=>$community_status]);
  814. } else {
  815. $post = \Yii::$app->request->post();
  816. $group = $post['group'];
  817. $store_id = $post['store_id'];
  818. if (empty($group) || !in_array($group, ['store_setting', 'cashier_setting', 'basic_setting']) || empty($store_id)) {
  819. return $this->error('参数错误');
  820. }
  821. // dd($post[$group]);
  822. $rules = [
  823. 'basic_setting' => [
  824. [['store_name', 'logo_img', 'license_img', 'share_title', 'share_desc', 'share_pic', 'longitude', 'latitude', 'address', 'share_title', 'share_desc', 'share_pic', 'addr'], 'string', 'max' => 255],
  825. [['shop_mobile', 'c_id', 'expire_time','payment_settlement_type','province_id','city_id','district_id','street_id','community_id'], 'integer'],
  826. ],
  827. 'store_setting' => [
  828. [['shop_status', 'business_time_start', 'business_time_end', 'username', 'store_service_fee', 'store_service_ratio', 'store_settle_type', 'store_settle_scene', 'store_service_fee_settle_type'], 'required'],
  829. [['shop_status', 'store_service_fee', 'store_settle_type', 'store_settle_scene', 'status', 'show_sync_goods', 'is_enable_withdraw_setting', 'balance_cash_service_fee_limit', 'store_service_fee_type','is_allow_editing', 'store_service_fee_settle_type'], 'integer'],
  830. [['store_service_ratio', 'balance_cash_service_fee', 'balance_cash_service_fee_diff', 'balance_cash_service_fee_month'], 'number'],
  831. [['business_time_start', 'business_time_end', 'store_background', 'Distribution', 'Agent', 'Area','DoubleCopy', 'huifu_mch_no','Rebate'], 'string'],
  832. [['password', 'store_commission_authority','device_id'], 'safe'],
  833. ],
  834. 'cashier_setting' => [
  835. [['cachier_service_ratio', 'cashier_member_status', 'cashier_distribution_is_enable', 'cashier_agent_is_enable','is_preferential_undertaking','preferential_undertaking'], 'required'],
  836. [['cashier_member_status', 'cashier_distribution_is_enable', 'cashier_distribution_is_enable_score', 'cashier_agent_is_enable','cashier_goods_complete_direct','enable_discount_price','cashier_is_open_discount'], 'integer'],
  837. [['id','cashier_member_settings','cashier_distribution_settings','cashier_distribution_settings_score','cashier_agent_settings','is_preferential_gift', 'cashier_area_settings'], 'safe'],
  838. [['discount_price_scale'], 'number']
  839. ],
  840. ];
  841. $res = \Yii::$app->services->validate->validate($post[$group], $rules[$group]);
  842. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  843. $post[$group]['store_id'] = $store_id;
  844. if ('store_setting' == $group) {
  845. $addons_settings = [];
  846. if (!empty($post[$group]['Distribution'])) {
  847. $addons_settings['Distribution'] = json_decode($post[$group]['Distribution'], true);
  848. if (!empty($addons_settings['Distribution']['is_enable'])) {
  849. $post[$group]['store_commission_authority']['distribution'] = 1;
  850. } else {
  851. $post[$group]['store_commission_authority']['distribution'] = 0;
  852. }
  853. }
  854. if (!empty($post[$group]['Area'])) {
  855. $addons_settings['Area'] = json_decode($post[$group]['Area'], true);
  856. if (!empty($addons_settings['Area']['is_enable'])) {
  857. $post[$group]['store_commission_authority']['area'] = 1;
  858. } else {
  859. $post[$group]['store_commission_authority']['area'] = 0;
  860. }
  861. }
  862. if (!empty($post[$group]['Agent'])) {
  863. $addons_settings['Agent'] = json_decode($post[$group]['Agent'], true);
  864. if (!empty($addons_settings['Agent']['is_enable'])) {
  865. $post[$group]['store_commission_authority']['agent'] = 1;
  866. } else {
  867. $post[$group]['store_commission_authority']['agent'] = 0;
  868. }
  869. }
  870. if (!empty($post[$group]['DoubleCopy'])) {
  871. $addons_settings['DoubleCopy'] = json_decode($post[$group]['DoubleCopy'], true);
  872. if (!empty($addons_settings['DoubleCopy']['is_enable'])) {
  873. $post[$group]['store_commission_authority']['doubleCopy'] = 1;
  874. } else {
  875. $post[$group]['store_commission_authority']['doubleCopy'] = 0;
  876. }
  877. }
  878. if (!empty($post[$group]['Rebate'])) {
  879. $addons_settings['Rebate'] = json_decode($post[$group]['Rebate'], true);
  880. }
  881. $res = StoreSettings::saveStoreSetting($this->mall_id, $post[$group], $addons_settings);
  882. if (false === $res) {
  883. return $this->error(StoreSettings::getStaticError());
  884. }
  885. return $this->success('保存成功');
  886. } elseif ('cashier_setting' == $group) {
  887. $res = StoreSettings::setData($this->mall_id, $post[$group]);
  888. if (false === $res) {
  889. return $this->error(StoreSettings::getStaticError());
  890. }
  891. return $this->success('保存成功');
  892. } elseif ('basic_setting' == $group) {
  893. $post[$group]['mall_id'] = $this->mall_id;
  894. $post[$group]['id'] = $store_id;
  895. unset($post[$group]['store_id']);
  896. // dd($post);
  897. $addr = $post[$group]['addr'] ?? '';
  898. if (!empty($addr)) {
  899. $addrs = explode('/', $addr);
  900. if(!empty($addrs[0])&&!empty( $addrs[1])&&!empty( $addrs[2])){
  901. $addrs[0] = str_replace('中国','',$addrs[0]);
  902. $regions = Region::find()
  903. ->select('id,name,level')
  904. ->where(['or', ['name' => $addrs[0]], ['name' => $addrs[1]], ['name' => $addrs[2]]])
  905. ->asArray()
  906. ->indexBy('level')
  907. ->all();
  908. $post[$group]['province_id'] = $regions[1]['id'];
  909. $post[$group]['city_id'] = $regions[2]['id'];
  910. $post[$group]['district_id'] = $regions[3]['id'];
  911. }
  912. }
  913. $post[$group]['community_id'] = !empty($post[$group]['community_id']) ? $post[$group]['community_id'] : 0;
  914. $res = Store::setData($post[$group]);
  915. if (false === $res) {
  916. return $this->error(Store::getStaticError());
  917. }
  918. return $this->success('保存成功');
  919. }
  920. }
  921. }
  922. public function actionModelDetail()
  923. {
  924. if (!\Yii::$app->request->isGet) {
  925. return $this->error('请求方式错误');
  926. }
  927. $store_id = \Yii::$app->request->get('store_id');
  928. if (empty($store_id)) {
  929. return $this->error('参数错误');
  930. }
  931. $store_model = StoreModel::getOne([['mall_id' => $this->mall_id, 'store_id' => $store_id]], true);
  932. $content = $setting = [];
  933. if (!empty($store_model['content'])) {
  934. $content = json_decode($store_model['content'], true);
  935. }
  936. // 获取配置
  937. foreach ($content as $key => &$item) {
  938. $setting[$key] = $item;
  939. }
  940. // 获取权重
  941. $level_params = ['where' => [['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED]], 'select' => 'level,name'];
  942. $level_params['mall_id'] = $this->mall_id;
  943. $level = GlobalInvoke::exec(GlobalInvoke::GOODS_MODEL_LEVEL, $this->mall_id, $level_params);
  944. $commission_type_map = GoodsTypeEnum::getCommissionTypeMap($this->mall_id);
  945. unset($commission_type_map['digital']);
  946. $status = $store_model['status'] ?? 0;
  947. $area_levels = [
  948. 1 => ['level' => 1, 'name' => '省代'],
  949. 2 => ['level' => 2, 'name' => '市代'],
  950. 3 => ['level' => 3, 'name' => '区代'],
  951. 4 => ['level' => 4, 'name' => '镇代'],
  952. ];
  953. $level['Area'] = $area_levels;
  954. return $this->success('操作成功', compact('status', 'level', 'setting', 'commission_type_map'));
  955. }
  956. /**
  957. * 组件获取店铺列表
  958. * @Author: lun
  959. * @DateTime: 2022/8/3 0003 18:40
  960. * @Copyright: copyright (c) 2021 广东七件事集团
  961. * @return mixed|void
  962. */
  963. public function actionStoreList()
  964. {
  965. $retuest = \Yii::$app->request;
  966. if ($retuest->isAjax) {
  967. if ($retuest->isGet) {
  968. $get = \Yii::$app->request->get();
  969. // 检查提交的参数
  970. $res = \Yii::$app->services->validate->validate($get,[
  971. [['id'], 'integer'],
  972. [['store_name'], 'string'],
  973. [['store_ids', 'exist_ids'], 'safe'],
  974. ]);
  975. if($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  976. $where = $list = [];
  977. !empty($get['id']) && $where[] = ['=', 'id', $get['id']];
  978. $where[] = ['=', 'mall_id', $this->mall_id];
  979. !empty($get['store_name']) && $where[] = ['like', 'store_name', $get['store_name']];
  980. !empty($get['store_ids']) && $where[] = ['id' => $get['store_ids']];
  981. !empty($get['exist_ids']) && $where[] = ['not in', 'id', $get['exist_ids']];
  982. $where[] = ['=', 'check_status', StatusEnum::ENABLED];
  983. $where[] = ['=', 'status', StatusEnum::ENABLED];
  984. $order = 'created_at desc';
  985. $select = 'id,c_id,mall_id,logo_img,store_name,shop_mobile,created_at';
  986. $params = array('where' => $where, 'select' => $select, 'order' => $order, 'limit' => $get['limit']);
  987. $params['with'] = ['cate' => function ($query) {
  988. $query->select('id, name');
  989. }];
  990. $list = Store::listPage($params, false, true);
  991. return $this->success('获取成功', $list);
  992. }
  993. }
  994. }
  995. /**
  996. * 小程序推广码/收银码
  997. *
  998. * @return mixed
  999. */
  1000. public function actionShowMiniPromotionCode()
  1001. {
  1002. $request = \Yii::$app->request;
  1003. if ($request->isAjax) {
  1004. if ($request->isPost) {
  1005. $params = \Yii::$app->request->post();
  1006. // 检查提交的参数
  1007. $res = \Yii::$app->services->validate->validate($params,[
  1008. [['id','type'], 'required'],
  1009. ]);
  1010. if($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  1011. $store = Store::findOne(['mall_id'=>$this->mall_id,'id'=>$params['id']]);
  1012. if(empty($store)) return $this->error('店铺不存在');
  1013. $model = StoreMiniCode::findOne(['store_id'=>$store['id'],'mall_id'=>$this->mall_id,'type'=>$params['type'], 'status' => StatusEnum::ENABLED]);
  1014. if(empty($model)){
  1015. $mall = $this->mall;
  1016. $content = [
  1017. 'mall_id'=>$this->mall_id,
  1018. 'store_id'=>$store['id'],
  1019. 'mall_sign'=>$mall['mall_sign'],
  1020. 'type'=>$params['type'],
  1021. 'pid'=>$store['user_id'],
  1022. ];
  1023. $content = json_encode($content);
  1024. if($params['type']==1){
  1025. $page = 'plugins2/Store/store/index';
  1026. }else{
  1027. $page = '/plugins2/Store/pay/cashRegister';
  1028. }
  1029. $code = md5($content);
  1030. $obj = new MiniQrCode();
  1031. $applet_code = $obj->getAppletQrCode($this->mall_id,$page,$code);
  1032. $params['mall_id'] = $this->mall_id;
  1033. $params['code'] = $code;
  1034. $params['store_id'] = $store['id'];
  1035. $params['content'] = $content;
  1036. $params['applet_code'] = $applet_code;
  1037. $model = StoreMiniCode::setData($params);
  1038. if($model==false){
  1039. return $this->error(StoreMiniCode::getStaticError());
  1040. }
  1041. } else if (empty($model->applet_code)) {
  1042. $mall = $this->mall;
  1043. $content = [
  1044. 'mall_id'=>$this->mall_id,
  1045. 'store_id'=>$store['id'],
  1046. 'mall_sign'=>$mall['mall_sign'],
  1047. 'type'=>$params['type'],
  1048. 'pid'=>$store['user_id'],
  1049. ];
  1050. $content = json_encode($content);
  1051. if($params['type']==1){
  1052. $page = 'plugins2/Store/store/index';
  1053. }else{
  1054. $page = '/plugins2/Store/pay/cashRegister';
  1055. }
  1056. $code = md5($content);
  1057. $obj = new MiniQrCode();
  1058. $applet_code = $obj->getAppletQrCode($this->mall_id,$page,$code);
  1059. $model->applet_code = $applet_code;
  1060. $model->save();
  1061. }
  1062. $applet_url = $model['applet_code'];
  1063. return $this->success('获取成功',['applet_url'=>$applet_url]);
  1064. }
  1065. }
  1066. }
  1067. /**
  1068. * 选择门店接口,外部调用
  1069. * @return mixed|void
  1070. */
  1071. public function actionSelectStore(){
  1072. if (Yii::$app->request->isAjax && Yii::$app->request->isGet) {
  1073. $params = Yii::$app->request->get();
  1074. $params['where'][] = ['mall_id' => $this->mall_id];
  1075. $params['where'][] = ['status' => [StatusEnum::ENABLED]];
  1076. if (!empty($params['keyword'])) $params['where'][] = ['or', ['like', 'store_name', $params['keyword']], ['id' => $params['keyword']]];
  1077. $params['order'] = 'id desc';
  1078. $params['select'] = 'id,store_name,shop_owner,shop_mobile,logo_img,store_desc,shop_status,status';
  1079. $list = Store::listPage($params);
  1080. return $this->success('操作成功', $list);
  1081. }
  1082. }
  1083. //门店删除
  1084. public function actionDel(){
  1085. try {
  1086. $params = Yii::$app->request->post();
  1087. Store::delStore($this->mall_id,$params['id']);
  1088. return $this->success();
  1089. }catch (\Exception $e){
  1090. return $this->error($e->getMessage());
  1091. }
  1092. }
  1093. /**
  1094. * 门店列表(全部门店)
  1095. *
  1096. * @return string
  1097. */
  1098. public function actionList()
  1099. {
  1100. $service = new StoreService(['mall_id' => $this->mall_id]);
  1101. return $this->success('ok', $service->getStoreList());
  1102. }
  1103. /**
  1104. * 云喇叭测试
  1105. * @Author:lun
  1106. * @Date:2024-01-06 16:22
  1107. * @Copyright:copyright(c)2023 广东七件事集团
  1108. * @return mixed|void|null
  1109. */
  1110. public function actionSuonaTest()
  1111. {
  1112. $request = Yii::$app->request;
  1113. if ($request->isPost) {
  1114. $params = $request->post();
  1115. // 检查提交的参数
  1116. $res = Yii::$app->services->validate->validate($params,[
  1117. [['store_id','device_id'], 'required'],
  1118. ]);
  1119. if($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  1120. $basic = Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'basic');
  1121. if (empty($basic['suona_appId'])) return $this->error('appId不能为空');
  1122. if (empty($basic['suona_appSecret'])) return $this->error('appSecret不能为空');
  1123. if (empty($basic['suona_host'])) return $this->error('appSecret不能为空');
  1124. $url = $basic['suona_host'] . 'plugin.yunqian-api.api.push.sendSpeakerMessage';
  1125. try {
  1126. $obj = new \addons\Store\common\logic\order\SuonaLogic($basic['suona_appId'], $basic['suona_appSecret']);
  1127. $data = $obj->suona($url,100, $params['device_id'],6);
  1128. if ($data['response']['result'] == 0) return $this->error('测试失败:' .'请求参数:'. $data['params'] .',请求响应:'. $data['response']['msg']);
  1129. return $this->success('测试成功');
  1130. } catch (\Exception $e) {
  1131. return $this->error('请求报错:' . $e->getMessage());
  1132. }
  1133. }
  1134. }
  1135. /**
  1136. * 清除小程序码
  1137. * @return void
  1138. */
  1139. public function actionCleanMiniCode()
  1140. {
  1141. $service = new StoreService(['mall_id' => $this->mall_id]);
  1142. return $service->cleanMiniCode()
  1143. ? $this->success('清除成功')
  1144. : $this->error('清除失败');
  1145. }
  1146. public function actionStoreCateTree()
  1147. {
  1148. if (Yii::$app->request->isAjax) {
  1149. if (Yii::$app->request->isGet) {
  1150. $cond = [['mall_id' => $this->mall_id]];
  1151. $list = StoreCate::getCates($cond, [], true, 'id,name,parent_id');
  1152. $list = StoreCate::getTreeCate($list);
  1153. return $this->success('操作成功', compact('list'));
  1154. }
  1155. }
  1156. }
  1157. /**
  1158. * 选择门店店长接口,外部调用
  1159. * @return mixed|void
  1160. */
  1161. public function actionSelectStoreUser(){
  1162. if (Yii::$app->request->isAjax && Yii::$app->request->isGet) {
  1163. $params = Yii::$app->request->get();
  1164. $params['where'][] = ['mall_id' => $this->mall_id];
  1165. $params['where'][] = ['status' => [StatusEnum::ENABLED]];
  1166. if (!empty($params['keyword'])) {
  1167. $uids = User::find()
  1168. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
  1169. ->where(['or', ['like', 'nickname', $params['keyword']], ['like', 'mobile', $params['keyword']]])
  1170. ->select('id')
  1171. ->column();
  1172. $uids[] = -1; //防止$uid 空数组
  1173. $params['where'][] = ['in', 'user_id', $uids];
  1174. }
  1175. if (!empty($params['user_id'])) {
  1176. $params['where'][] = ['id' => $params['user_id']];
  1177. }
  1178. $params['order'] = 'id desc';
  1179. $params['select'] = 'id,user_id,store_name,shop_owner,shop_mobile,logo_img,store_desc,shop_status,status';
  1180. $list = Store::listPage($params);
  1181. if(empty($list['list']) == false){
  1182. $uids = array_column($list['list'],'user_id');
  1183. $u_where = [];
  1184. $u_where[] = ['in','id',$uids];
  1185. $userList = User::lists(['where' => $u_where],true);
  1186. if(empty($userList) == false){
  1187. $userList = array_column($userList, null, 'id');
  1188. }
  1189. $user_level_arr = UserLevel::getUserLevelArr($this->mall_id);
  1190. foreach ($list['list'] as &$item){
  1191. $user = $userList[$item['user_id']];
  1192. $item['avatar_url'] = $user['avatar_url'];
  1193. $item['nickname'] = $user['nickname'];
  1194. $item['mobile'] = $user['mobile'];
  1195. $item['level_name'] = !empty($user_level_arr[$user['level']]) ? $user_level_arr[$user['level']] : MallSetting::getDefaultLevelName($this->mall_id);
  1196. }
  1197. }
  1198. return $this->success('操作成功', $list);
  1199. }
  1200. }
  1201. }