ShopController.php 19 KB

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