ShopController.php 19 KB

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