ShopController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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 common\components\dict;
  15. use common\components\dirUtil;
  16. use common\components\imgUtil;
  17. use common\components\stringUtil;
  18. use common\components\util;
  19. use common\components\wxUtil;
  20. use Yii;
  21. class ShopController extends BaseController
  22. {
  23. public $guestAccess = ['all', 'gathering-img-url', 'check-shop'];
  24. public function actionModifySjName()
  25. {
  26. $get = Yii::$app->request->get();
  27. $adminId = $this->adminId;
  28. $staff = $this->shopAdmin;
  29. if ($staff->founder != 2 && $adminId != 4) {
  30. util::fail('老板才能改店名');
  31. }
  32. $name = $get['name'] ?? '';
  33. if (empty($name)) {
  34. util::fail('请填写名称');
  35. }
  36. //开启事务
  37. $connection = Yii::$app->db;
  38. $transaction = $connection->beginTransaction();
  39. try {
  40. $shop = $this->shop;
  41. $shop->merchantName = $name;
  42. $shop->save();
  43. $sj = SjClass::getById($shop->sjId, true);
  44. if (empty($sj)) {
  45. util::fail('门店信息缺失呢');
  46. }
  47. $sj->name = $name;
  48. $sj->save();
  49. $fullName = $shop->shopName == '首店' ? $name : $name . ' ' . $shop->shopName;
  50. $shopId = $shop->id;
  51. HdClass::updateByCondition(['shopId' => $shopId], ['name' => $fullName]);
  52. CustomClass::updateByCondition(['shopId' => $shopId], ['originName' => $fullName]);
  53. $transaction->commit();
  54. util::complete('修改成功');
  55. } catch (\Exception $exception) {
  56. $transaction->rollBack();
  57. Yii::error("修改门店名称失败:" . $exception->getMessage());
  58. util::fail('修改门店名称失败');
  59. }
  60. }
  61. //我的可以进入的门店 ssh 20230424
  62. public function actionMyShop()
  63. {
  64. $staff = $this->shopAdmin;
  65. $shopList = [];
  66. if (isset($staff->super) && $staff->super == 1) {
  67. $sjId = $this->sjId ?? 0;
  68. $shopList = ShopClass::getAllByCondition(['sjId' => $sjId, 'ptStyle' => 1], null, 'id,shopName,merchantName,sjId,mainId,ptStyle', 'id');
  69. }
  70. $adminId = $this->adminId ?? 0;
  71. $staffList = ShopAdminClass::getAllByCondition(['adminId' => $adminId], null, '*', null, true);
  72. $mainIdList = [];
  73. if (!empty($staffList)) {
  74. foreach ($staffList as $staff) {
  75. $isPt = $staff->isPt ?? 0;
  76. $delStatus = $staff->delStatus ?? 0;
  77. //$super = $staff->super ?? 0;
  78. $mainId = $staff->mainId ?? 0;
  79. if ($isPt == 0 && $delStatus == 0) {
  80. $mainIdList[] = $mainId;
  81. }
  82. }
  83. }
  84. if (!empty($mainIdList)) {
  85. $addShopList = ShopClass::getAllByCondition(['mainId' => ['in', $mainIdList], 'ptStyle' => 1], null, 'id,shopName,merchantName,sjId,mainId,ptStyle', 'id');
  86. if (!empty($addShopList)) {
  87. foreach ($addShopList as $addShop) {
  88. $addShopId = $addShop['id'] ?? 0;
  89. if (isset($shopList[$addShopId]) == false) {
  90. $shopList[$addShopId] = $addShop;
  91. }
  92. }
  93. }
  94. }
  95. $list = array_values($shopList);
  96. util::success(['list' => $list]);
  97. }
  98. //是否开启完整功能 ssh 20230226
  99. public function actionChangeOnlyCg()
  100. {
  101. $shop = $this->shop;
  102. $get = Yii::$app->request->get();
  103. $onlyCg = $get['onlyCg'] ?? 1;
  104. $shop->onlyCg = $onlyCg;
  105. $shop->save();
  106. util::complete('操作成功');
  107. }
  108. public function actionCheckShop()
  109. {
  110. $get = Yii::$app->request->get();
  111. $mobile = $get['mobile'] ?? '';
  112. $shop = ShopClass::getByCondition(['mobile' => $mobile], true);
  113. if (!empty($shop)) {
  114. util::success(['has' => 1]);
  115. }
  116. util::success(['has' => 0]);
  117. }
  118. //获取当前门店信息 ssh 2021.3.27
  119. public function actionCurrentShop()
  120. {
  121. util::success($this->shop);
  122. }
  123. //门店列表 ssh 20202.2.29
  124. public function actionList()
  125. {
  126. $where = [];
  127. $where['sjId'] = $this->sjId;
  128. $where['delStatus'] = 0;
  129. $list = \bizHd\shop\classes\ShopClass::getShopList($where);
  130. util::success($list);
  131. }
  132. //更新门店 ssh 2020.2.29
  133. public function actionUpdate()
  134. {
  135. $data = Yii::$app->request->post();
  136. $data['sjId'] = $this->sjId;
  137. $id = isset($data['id']) ? $data['id'] : 0;
  138. unset($data['id']);
  139. $shop = ShopService::getById($id);
  140. ShopService::valid($shop, $this->sjId);
  141. $data['img'] = isset($data['img']) && is_array($data['img']) ? json_encode($data['img']) : '';
  142. $mobile = $data['mobile'];
  143. //手机号具有唯一性,暂时不允许修改 --- 同批发端 app-ghs/controllers/ShopController.php 保持一致
  144. unset($data['mobile']);
  145. // 添加事务处理
  146. $connection = Yii::$app->db;
  147. $transaction = $connection->beginTransaction();
  148. try {
  149. ShopClass::updateShop($shop, $data);
  150. // 找出附近批发店,之后自动给建立关系,然后补充二个店之间的距离(app-hd GhsController actionInfo),计算距离使用高德接口
  151. if(isset($data['openLocation']) && $data['openLocation'] == 1){
  152. $toLat = $data['lat'] ?? null;
  153. $toLnt = $data['long'] ?? null;
  154. $province = $data['province'] ?? '';
  155. $city = $data['city'] ?? '';
  156. \biz\ghs\classes\GhsClass::buildNearbyGhsRelations($id, $this->shop->pfShopId, $province, $city, $toLat, $toLnt);
  157. }
  158. $transaction->commit();
  159. util::complete();
  160. } catch (\Exception $exception) {
  161. $transaction->rollBack();
  162. Yii::error("更新门店失败:" . $exception->getMessage());
  163. util::fail('更新门店失败');
  164. }
  165. }
  166. public function actionAdd()
  167. {
  168. $data = Yii::$app->request->post();
  169. $data['sjId'] = $this->sjId;
  170. $data['shopId'] = $this->shopId;
  171. if (isset($data['shopName']) == false || empty($data['shopName'])) {
  172. util::fail('请填写门店名称');
  173. }
  174. $sj = $this->sj;
  175. $sjName = $sj->name ?? '';
  176. $data['merchantName'] = $sjName;
  177. $ptStyle = $sj->style ?? dict::getDict('ptStyle', 'hd');
  178. $data['ptStyle'] = $ptStyle;
  179. $connection = Yii::$app->db;
  180. $transaction = $connection->beginTransaction();
  181. try {
  182. $mobile = $data['mobile'] ?? 0;
  183. if (stringUtil::isMobile($mobile) == false) {
  184. util::fail('请填写正确手机号');
  185. }
  186. $adminInfo = ['name' => $mobile, 'mobile' => $mobile];
  187. $fromApp = dict::getDict('userSourceGetId', 'app', 'id');
  188. $admin = AdminClass::replaceAdmin($adminInfo, $fromApp);
  189. $adminId = $admin['id'] ?? 0;
  190. $data['adminId'] = $adminId;
  191. \bizHd\shop\classes\ShopClass::addMainShop($data, $this->shop, $this->sj);
  192. $transaction->commit();
  193. util::complete();
  194. } catch (\Exception $exception) {
  195. $transaction->rollBack();
  196. util::fail();
  197. }
  198. }
  199. //切换门店 lqh 2021.1.31
  200. public function actionToggleShop()
  201. {
  202. $adminId = $this->adminId;
  203. $mainId = $this->mainId;
  204. //多处要同步修改,关键词 no_allow_toggle_shop
  205. if (getenv('YII_ENV') == 'production') {
  206. //安海恋善不允许切换门店
  207. if (in_array($mainId, [44759, 56314, 56315, 56452])) {
  208. if (in_array($adminId, [54630, 54723])) {
  209. util::fail('专用账号,不允许切换门店');
  210. }
  211. }
  212. //惠雅南城不允许切换门店
  213. if ($mainId == 52 && $adminId == 3539) {
  214. util::fail('收银专用账号,不允许切换门店');
  215. }
  216. //惠雅莞城不允许切换门店
  217. if ($mainId == 1459 && $adminId == 2812) {
  218. util::fail('收银专用账号,不允许切换门店');
  219. }
  220. //淘花里中山店
  221. if ($mainId == 16948 && $adminId == 17908) {
  222. util::fail('收银专用账号,不允许切换门店');
  223. }
  224. //淘花里小榄店
  225. if ($mainId == 72057 && $adminId == 69792) {
  226. util::fail('收银专用账号,不允许切换门店');
  227. }
  228. }
  229. $shopAdmin = $this->shopAdmin;
  230. $switchShop = \biz\shop\classes\ShopAdminClass::hasSwitchShopRight($shopAdmin);
  231. if ($switchShop == 0) {
  232. util::fail('不能切换门店');
  233. }
  234. $newShopId = Yii::$app->request->post('shopId', 0);
  235. $deviceId = Yii::$app->request->post('deviceId', '');
  236. $newShop = ShopClass::getById($newShopId, true);
  237. if (empty($newShop)) {
  238. util::fail('没有找到门店30');
  239. }
  240. $res = \bizHd\admin\classes\AdminClass::toggleShop($this->admin, $newShop, $this->sjId, $this->shopAdmin);
  241. if ($deviceId != '') {
  242. //设备登录状态更新
  243. $oldDevice = \bizHd\device\classes\HdDeviceClass::getByCondition(['shopId' => $this->shopId, 'deviceId' => $deviceId], true);
  244. if ($oldDevice) {
  245. $oldDevice->status = 0;
  246. $oldDevice->save();
  247. }
  248. $newLoginDevice = \bizHd\device\classes\HdDeviceClass::getByCondition(['shopId' => $newShopId, 'deviceId' => $deviceId], true);
  249. if ($newLoginDevice) {
  250. $newLoginDevice->status = 1;
  251. $newLoginDevice->save();
  252. }
  253. }
  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. }