ShopController.php 18 KB

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