ShopController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. <?php
  2. namespace hd\controllers;
  3. use biz\shop\classes\ShopClass;
  4. use biz\sj\services\MerchantExtendService;
  5. use bizGhs\admin\classes\AdminClass;
  6. use bizGhs\custom\classes\CustomClass;
  7. use bizHd\admin\classes\ShopAdminClass;
  8. use bizHd\custom\classes\CustomClass as HdCustomClass;
  9. use bizHd\custom\classes\HdClass;
  10. use bizHd\merchant\classes\SjClass;
  11. use bizHd\merchant\services\ShopService;
  12. use bizHd\purchase\classes\PurchaseClass;
  13. use bizHd\saas\services\RegionService;
  14. use bizHd\shop\classes\MainClass;
  15. use common\components\dict;
  16. use common\components\dirUtil;
  17. use common\components\imgUtil;
  18. use common\components\IntraCityExpress;
  19. use common\components\stringUtil;
  20. use common\components\util;
  21. use common\components\wxUtil;
  22. use Yii;
  23. class ShopController extends BaseController
  24. {
  25. public $guestAccess = ['all', 'gathering-img-url', 'check-shop'];
  26. public function actionModifySjName()
  27. {
  28. $get = Yii::$app->request->get();
  29. $adminId = $this->adminId;
  30. if (getenv('YII_ENV') == 'production') {
  31. if ($adminId != 4) {
  32. util::fail('请联系管理员修改');
  33. }
  34. } else {
  35. if ($adminId != 919) {
  36. util::fail('请联系管理员修改');
  37. }
  38. }
  39. $name = $get['name'] ?? '';
  40. if (empty($name)) {
  41. util::fail('请填写名称');
  42. }
  43. $shop = $this->shop;
  44. $shop->merchantName = $name;
  45. $shop->save();
  46. $sjId = $shop->sjId;
  47. $sj = SjClass::getById($sjId, true);
  48. if (empty($sj)) {
  49. util::fail('门店信息缺失呢');
  50. }
  51. $sj->name = $name;
  52. $sj->save();
  53. $fullName = $shop->shopName == '首店' ? $name : $name . ' ' . $shop->shopName;
  54. $shopId = $shop->id;
  55. HdClass::updateByCondition(['shopId' => $shopId], ['name' => $fullName]);
  56. util::complete('修改成功');
  57. }
  58. //我的可以进入的门店 ssh 20230424
  59. public function actionMyShop()
  60. {
  61. $staff = $this->shopAdmin;
  62. $shopList = [];
  63. if (isset($staff->super) && $staff->super == 1) {
  64. $sjId = $this->sjId ?? 0;
  65. $shopList = ShopClass::getAllByCondition(['sjId' => $sjId, 'ptStyle' => 1], null, 'id,shopName,merchantName,sjId,mainId,ptStyle', 'id');
  66. }
  67. $adminId = $this->adminId ?? 0;
  68. $staffList = ShopAdminClass::getAllByCondition(['adminId' => $adminId], null, '*', null, true);
  69. $mainIdList = [];
  70. if (!empty($staffList)) {
  71. foreach ($staffList as $staff) {
  72. $isPt = $staff->isPt ?? 0;
  73. $delStatus = $staff->delStatus ?? 0;
  74. //$super = $staff->super ?? 0;
  75. $mainId = $staff->mainId ?? 0;
  76. if ($isPt == 0 && $delStatus == 0) {
  77. $mainIdList[] = $mainId;
  78. }
  79. }
  80. }
  81. if (!empty($mainIdList)) {
  82. $addShopList = ShopClass::getAllByCondition(['mainId' => ['in', $mainIdList], 'ptStyle' => 1], null, 'id,shopName,merchantName,sjId,mainId,ptStyle', 'id');
  83. if (!empty($addShopList)) {
  84. foreach ($addShopList as $addShop) {
  85. $addShopId = $addShop['id'] ?? 0;
  86. if (isset($shopList[$addShopId]) == false) {
  87. $shopList[$addShopId] = $addShop;
  88. }
  89. }
  90. }
  91. }
  92. $list = array_values($shopList);
  93. util::success(['list' => $list]);
  94. }
  95. //是否开启完整功能 ssh 20230226
  96. public function actionChangeOnlyCg()
  97. {
  98. $shop = $this->shop;
  99. $get = Yii::$app->request->get();
  100. $onlyCg = $get['onlyCg'] ?? 1;
  101. $shop->onlyCg = $onlyCg;
  102. $shop->save();
  103. util::complete('操作成功');
  104. }
  105. public function actionCheckShop()
  106. {
  107. $get = Yii::$app->request->get();
  108. $mobile = $get['mobile'] ?? '';
  109. $shop = ShopClass::getByCondition(['mobile' => $mobile], true);
  110. if (!empty($shop)) {
  111. util::success(['has' => 1]);
  112. }
  113. util::success(['has' => 0]);
  114. }
  115. //获取当前门店信息 ssh 2021.3.27
  116. public function actionCurrentShop()
  117. {
  118. util::success($this->shop);
  119. }
  120. //门店列表 ssh 20202.2.29
  121. public function actionList()
  122. {
  123. $where = [];
  124. $where['sjId'] = $this->sjId;
  125. $where['delStatus'] = 0;
  126. $list = \bizHd\shop\classes\ShopClass::getShopList($where);
  127. util::success($list);
  128. }
  129. //更新门店 ssh 2020.2.29
  130. public function actionUpdate()
  131. {
  132. $data = Yii::$app->request->post();
  133. $data['sjId'] = $this->sjId;
  134. $id = isset($data['id']) ? $data['id'] : 0;
  135. unset($data['id']);
  136. $shop = ShopService::getById($id);
  137. ShopService::valid($shop, $this->sjId);
  138. $data['img'] = isset($data['img']) && is_array($data['img']) ? json_encode($data['img']) : '';
  139. $mobile = $data['mobile'];
  140. //手机号具有唯一性,暂时不允许修改 --- 同批发端 app-ghs/controllers/ShopController.php 保持一致
  141. unset($data['mobile']);
  142. ShopClass::updateShop($shop, $data);
  143. //同步更新同城配送微信门店
  144. // 校验有无经纬度数据,没有不更新(说明地图定位功能不可用了)
  145. if ($data['lat'] != '' && $data['long'] != '') {
  146. $main = MainClass::getById($this->mainId, false, false, 'id, wxStoreId');
  147. if ($main['wxStoreId'] == '') {
  148. Yii::error(__CLASS__ . __METHOD__ . ' - 未创建门店,请先创建');
  149. util::complete();
  150. }
  151. $shopName = isset($data['shopName']) ? $data['shopName'] : '';
  152. $sjName = $shop['merchantName'] ?? '';
  153. $storeName = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
  154. $storeData = [
  155. 'keys' => ['wx_store_id' => $main['wxStoreId']],
  156. 'content' => [
  157. 'store_name' => $storeName,
  158. 'address_info' => [
  159. 'province' => $data['province'],
  160. 'city' => $data['city'],
  161. 'area' => $data['dist'],
  162. 'street' => '',
  163. 'house' => $data['floor'] ? $data['address'] . $data['floor'] : $data['address'],
  164. 'lat' => $data['lat'],
  165. 'lng' => $data['long'],
  166. 'phone' => $mobile
  167. ],
  168. //"order_pattern" => 2,
  169. //"service_trans_prefer" => "SFTC" //order_pattern = 2时必填
  170. ]
  171. ];
  172. $result = IntraCityExpress::updateStore($storeData);
  173. if ($result['errcode'] == 0) {
  174. Yii::info("同步更新同城配送微信门店成功");
  175. } else {
  176. Yii::error("同步更新同城配送微信门店失败:" . $result['errmsg']);
  177. }
  178. }
  179. util::complete();
  180. }
  181. public function actionAdd()
  182. {
  183. $data = Yii::$app->request->post();
  184. $data['sjId'] = $this->sjId;
  185. $data['shopId'] = $this->shopId;
  186. if (isset($data['shopName']) == false || empty($data['shopName'])) {
  187. util::fail('请填写门店名称');
  188. }
  189. $sj = $this->sj;
  190. $sjName = $sj->name ?? '';
  191. $data['merchantName'] = $sjName;
  192. $ptStyle = $sj->style ?? dict::getDict('ptStyle', 'hd');
  193. $data['ptStyle'] = $ptStyle;
  194. $connection = Yii::$app->db;
  195. $transaction = $connection->beginTransaction();
  196. try {
  197. $mobile = $data['mobile'] ?? 0;
  198. if (stringUtil::isMobile($mobile) == false) {
  199. util::fail('请填写正确手机号');
  200. }
  201. $adminInfo = ['name' => $mobile, 'mobile' => $mobile];
  202. $fromApp = dict::getDict('userSourceGetId', 'app', 'id');
  203. $admin = AdminClass::replaceAdmin($adminInfo, $fromApp);
  204. $adminId = $admin['id'] ?? 0;
  205. $data['adminId'] = $adminId;
  206. \bizHd\shop\classes\ShopClass::addMainShop($data, $this->shop, $this->sj);
  207. $transaction->commit();
  208. util::complete();
  209. } catch (\Exception $exception) {
  210. $transaction->rollBack();
  211. util::fail();
  212. }
  213. }
  214. //切换门店 lqh 2021.1.31
  215. public function actionToggleShop()
  216. {
  217. $adminId = $this->adminId;
  218. $mainId = $this->mainId;
  219. //多处要同步修改,关键词 no_allow_toggle_shop
  220. if (getenv('YII_ENV') == 'production') {
  221. //安海恋善不允许切换门店
  222. if (in_array($mainId, [44759, 56314, 56315, 56452])) {
  223. if (in_array($adminId, [54630, 54723])) {
  224. util::fail('专用账号,不允许切换门店');
  225. }
  226. }
  227. //惠雅南城不允许切换门店
  228. if ($mainId == 52 && $adminId == 3539) {
  229. util::fail('收银专用账号,不允许切换门店');
  230. }
  231. //惠雅莞城不允许切换门店
  232. if ($mainId == 1459 && $adminId == 2812) {
  233. util::fail('收银专用账号,不允许切换门店');
  234. }
  235. //淘花里中山店
  236. if ($mainId == 16948 && $adminId == 17908) {
  237. util::fail('收银专用账号,不允许切换门店');
  238. }
  239. //淘花里小榄店
  240. if ($mainId == 72057 && $adminId == 69792) {
  241. util::fail('收银专用账号,不允许切换门店');
  242. }
  243. }
  244. $shopAdmin = $this->shopAdmin;
  245. $switchShop = \biz\shop\classes\ShopAdminClass::hasSwitchShopRight($shopAdmin);
  246. if ($switchShop == 0) {
  247. util::fail('不能切换门店');
  248. }
  249. $newShopId = Yii::$app->request->post('shopId', 0);
  250. $newShop = ShopClass::getById($newShopId, true);
  251. if (empty($newShop)) {
  252. util::fail('没有找到门店30');
  253. }
  254. $res = \bizHd\admin\classes\AdminClass::toggleShop($this->admin, $newShop, $this->sjId, $this->shopAdmin);
  255. util::success($res);
  256. }
  257. //网页端使用,下载微信和支付宝收款码 ssh 20250821
  258. public function actionGatheringCode()
  259. {
  260. $id = Yii::$app->request->get('id', 0);
  261. $shop = ShopService::getById($id, true);
  262. if (empty($shop)) {
  263. util::fail('没有找到门店31');
  264. }
  265. ShopClass::valid($shop->attributes, $this->sjId);
  266. $imgUrl = ShopClass::generateGatheringCode($this->sjId, $id);
  267. $shopName = $shop->shopName ?? '';
  268. $file = fopen($imgUrl, "rb");
  269. Header("Content-type: application/octet-stream ");
  270. Header("Accept-Ranges: bytes ");
  271. Header("Content-Disposition: attachment;filename={$shopName}收款码.jpg");
  272. $contents = "";
  273. while (!feof($file)) {
  274. $contents .= fread($file, 8192);
  275. }
  276. echo $contents;
  277. fclose($file);
  278. }
  279. //小程序端使用,下载收款码 ssh 20250821
  280. public function actionMiniGatheringCode()
  281. {
  282. $shop = $this->shop;
  283. $shopId = $shop->id;
  284. $imgUrl = ShopClass::generateGatheringCode($this->sjId, $shopId);
  285. util::success(['imgUrl' => $imgUrl]);
  286. }
  287. //获取二维码
  288. public function actionGetGatheringCodeUrl()
  289. {
  290. $id = Yii::$app->request->get('id', 0);
  291. $shop = ShopService::getById($id, true);
  292. if (empty($shop)) {
  293. util::fail('没有找到门店32');
  294. }
  295. ShopClass::valid($shop->attributes, $this->sjId);
  296. $imgUrl = Yii::$app->params['hdHost'] . '/auth/gathering-img-url?id=' . $id;
  297. util::success(['url' => $imgUrl]);
  298. }
  299. public function actionGatheringImgUrl()
  300. {
  301. $id = Yii::$app->request->get('id', 0);
  302. $shop = ShopService::getById($id, true);
  303. if (empty($shop)) {
  304. util::fail('没有找到门店33');
  305. }
  306. $sjId = $shop->sjId;
  307. $imgUrl = ShopClass::generateGatheringCode($sjId, $id);
  308. $fileResource = file_get_contents($imgUrl);
  309. header('Content-type: image/jpeg');
  310. echo $fileResource;
  311. }
  312. //获取小程序的收款码 ssh
  313. public function actionGatheringMiniCode()
  314. {
  315. $id = Yii::$app->request->get('id', 0);
  316. $extend = MerchantExtendService::getBySjId($this->sjId);
  317. if ($extend['miniAuth'] == 0) {
  318. util::fail('您的小程序还没有授权');
  319. }
  320. $shop = ShopService::getById($id);
  321. ShopService::valid($shop, $this->sjId);
  322. $applyMemberCode = wxUtil::generateGatheringMiniCode($this->sj->attributes, $id);
  323. $file = dirUtil::getImgDir() . $applyMemberCode;
  324. if (file_exists($file) == false) {
  325. util::fail('没有找到注册会员码');
  326. }
  327. $fileName = "门店({$id})小程序收款码.jpg";
  328. $contentType = 'image/jpeg';
  329. header("Cache-control: private");
  330. header("Content-type: $contentType"); //设置要下载的文件类型
  331. header("Content-Length:" . filesize($file)); //设置要下载文件的文件大小
  332. header("Content-Disposition: attachment; filename=" . urldecode($fileName)); //设置要下载文件的文件名
  333. readfile($file);
  334. }
  335. //下载会员注册码 ssh 2020.2.29
  336. public function actionApplyMemberCode()
  337. {
  338. $id = Yii::$app->request->get('id', 0);
  339. $extend = MerchantExtendService::getBySjId($this->sjId);
  340. if ($extend['miniAuth'] == 0) {
  341. util::fail('您的小程序还没有授权');
  342. }
  343. $shop = ShopService::getById($id);
  344. ShopService::valid($shop, $this->sjId);
  345. $applyMemberCode = wxUtil::generateMemberCode($this->sj->attributes, $id);
  346. $file = dirUtil::getImgDir() . $applyMemberCode;
  347. if (file_exists($file) == false) {
  348. util::fail('没有找到注册会员码');
  349. }
  350. $fileName = "注册会员码{$id}.jpg";
  351. $contentType = 'image/jpeg';
  352. header("Cache-control: private");
  353. header("Content-type: $contentType"); //设置要下载的文件类型
  354. header("Content-Length:" . filesize($file)); //设置要下载文件的文件大小
  355. header("Content-Disposition: attachment; filename=" . urldecode($fileName)); //设置要下载文件的文件名
  356. readfile($file);
  357. }
  358. //删除门店 ssh 2020.3.1
  359. public function actionDelete()
  360. {
  361. util::fail('禁止操作');
  362. $id = Yii::$app->request->get('id', 0);
  363. util::fail('功能开发中');
  364. $shop = ShopService::getById($id);
  365. ShopService::valid($shop, $this->sjId);
  366. ShopService::deleteShop($shop);
  367. util::complete();
  368. }
  369. //获取门店详情 ssh 2021.4.23
  370. public function actionDetail()
  371. {
  372. $shopId = Yii::$app->request->get('shopId');
  373. if (empty($shopId)) {
  374. $shopId = $this->shopId;
  375. }
  376. $shop = ShopClass::getShopInfo($shopId);
  377. $shop['hasMap'] = dict::getDict('hasMap'); //添加 hasMap 属性(字典中的 hasMap)
  378. util::success(['info' => $shop]);
  379. }
  380. //提现信息更新 ssh 2021.3.23
  381. public function actionCashUpdate()
  382. {
  383. $get = Yii::$app->request->get();
  384. $cashAccount = $get['cashAccount'] ?? '';
  385. $cashName = $get['cashName'] ?? '';
  386. $cashBank = $get['cashBank'] ?? '';
  387. if (empty($cashName)) {
  388. util::fail('请填写姓名');
  389. }
  390. if (empty($cashAccount)) {
  391. util::fail('请填写银行卡号');
  392. }
  393. if (empty($cashBank)) {
  394. util::fail('请输入开户行');
  395. }
  396. $shopAdmin = $this->shopAdmin;
  397. $shopId = $shopAdmin->shopId ?? 0;
  398. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  399. util::fail('超管才能操作');
  400. }
  401. ShopClass::updateById($shopId, ['cashAccount' => $cashAccount, 'cashName' => $cashName, 'cashBank' => $cashBank]);
  402. $shop = $this->shop;
  403. $sjName = $shop->merchantName ?? '';
  404. $shopName = $shop->shopName ?? '';
  405. //noticeUtil::push("{$sjName}-{$shopName} 修改提现账号,{$cashName} {$cashAccount} {$cashBank}", '15280215347');
  406. util::complete();
  407. }
  408. //获取所有门店lqh 2021.1.31
  409. public function actionAll()
  410. {
  411. $res = [];
  412. $shopAdmin = isset($this->shopAdmin) && !empty($this->shopAdmin) ? $this->shopAdmin->attributes : [];
  413. if (isset($shopAdmin['super']) && $shopAdmin['super'] == 1) {
  414. $res = ShopClass::getAllShop($this->sjId);
  415. }
  416. util::success($res);
  417. }
  418. //获取采购时的支付方式 ssh 20211004
  419. public function actionGetCgPayWay()
  420. {
  421. $get = Yii::$app->request->get();
  422. $id = $get['id'] ?? 0;
  423. $cg = PurchaseClass::getById($id, true);
  424. PurchaseClass::valid($cg, $this->shopId);
  425. $customId = $cg->customId ?? 0;
  426. $custom = CustomClass::getById($customId);
  427. $hasDebtPay = $custom['debt'] ?? 0;
  428. //预订单不能欠款
  429. if ($cg->book == 1) {
  430. $hasDebtPay = 0;
  431. }
  432. $current = time();
  433. $cgDeadTime = strtotime($cg->deadline);
  434. //让客户在失效前$aheadTime秒要支付,这样回调通知时订单才不会过期状态
  435. $aheadTime = dict::getDict('order_online_pay_has_ahead_time');
  436. $count = $cgDeadTime - $current - $aheadTime;
  437. $data = [];
  438. $data['balance'] = $this->shop->balance ?? 0;
  439. $data['count'] = $count;
  440. $data['hasDebtPay'] = $hasDebtPay;
  441. util::success($data);
  442. }
  443. //地区 ssh 20220606
  444. public function actionRegion()
  445. {
  446. $regionTree = RegionService::tree();
  447. util::success(['region' => $regionTree]);
  448. }
  449. //散客门店详情 ssh 20211007
  450. public function actionKjCustom()
  451. {
  452. $defaultCustomId = $this->shop->defaultCustomId ?? 0;
  453. $custom = [];
  454. if (!empty($defaultCustomId)) {
  455. $custom = HdCustomClass::getById($defaultCustomId);
  456. $avatar = $custom['avatar'] ?? '';
  457. $custom['avatar'] = imgUtil::groupImg($avatar) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  458. }
  459. util::success(['custom' => $custom]);
  460. }
  461. //散客门店设置
  462. public function actionKjCustomSet()
  463. {
  464. $id = Yii::$app->request->get('id', 0);
  465. $custom = HdCustomClass::getById($id, true);
  466. if (empty($custom)) {
  467. util::fail('没有找到客户');
  468. }
  469. if ($custom->shopId != $this->shopId) {
  470. util::fail('客户不属于当前门店');
  471. }
  472. $shop = $this->shop;
  473. $shop->defaultCustomId = $id;
  474. $shop->save();
  475. util::complete('修改成功');
  476. }
  477. public function actionUpdateShopAvatar()
  478. {
  479. $data = Yii::$app->request->post();
  480. $id = isset($data['id']) ? $data['id'] : 0;
  481. $shop = ShopClass::getById($id, true, 'id, sjId, avatar');
  482. ShopService::valid($shop, $this->sjId);
  483. if (isset($data['avatar']) && $data['avatar'] != '') {
  484. $shop->avatar = $data['avatar'];
  485. $re = $shop->save();
  486. if ($re) {
  487. \bizGhs\custom\classes\CustomClass::updateByCondition(['shopId' => $id], [
  488. 'avatar' => $data['avatar']
  489. ]);
  490. util::complete('头像修改完成');
  491. }
  492. util::fail('头像修改失败');
  493. }
  494. util::fail('头像参数出错');
  495. }
  496. }