ShopController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. util::complete();
  145. }
  146. public function actionAdd()
  147. {
  148. $data = Yii::$app->request->post();
  149. $data['sjId'] = $this->sjId;
  150. $data['shopId'] = $this->shopId;
  151. if (isset($data['shopName']) == false || empty($data['shopName'])) {
  152. util::fail('请填写门店名称');
  153. }
  154. $sj = $this->sj;
  155. $sjName = $sj->name ?? '';
  156. $data['merchantName'] = $sjName;
  157. $ptStyle = $sj->style ?? dict::getDict('ptStyle', 'hd');
  158. $data['ptStyle'] = $ptStyle;
  159. $connection = Yii::$app->db;
  160. $transaction = $connection->beginTransaction();
  161. try {
  162. $mobile = $data['mobile'] ?? 0;
  163. if (stringUtil::isMobile($mobile) == false) {
  164. util::fail('请填写正确手机号');
  165. }
  166. $adminInfo = ['name' => $mobile, 'mobile' => $mobile];
  167. $fromApp = dict::getDict('userSourceGetId', 'app', 'id');
  168. $admin = AdminClass::replaceAdmin($adminInfo, $fromApp);
  169. $adminId = $admin['id'] ?? 0;
  170. $data['adminId'] = $adminId;
  171. \bizHd\shop\classes\ShopClass::addMainShop($data, $this->shop, $this->sj);
  172. $transaction->commit();
  173. util::complete();
  174. } catch (\Exception $exception) {
  175. $transaction->rollBack();
  176. util::fail();
  177. }
  178. }
  179. //切换门店 lqh 2021.1.31
  180. public function actionToggleShop()
  181. {
  182. $adminId = $this->adminId;
  183. $mainId = $this->mainId;
  184. //多处要同步修改,关键词 no_allow_toggle_shop
  185. if (getenv('YII_ENV') == 'production') {
  186. //安海恋善不允许切换门店
  187. if (in_array($mainId, [44759, 56314, 56315, 56452])) {
  188. if (in_array($adminId, [54630, 54723])) {
  189. util::fail('专用账号,不允许切换门店');
  190. }
  191. }
  192. //惠雅南城不允许切换门店
  193. if ($mainId == 52 && $adminId == 3539) {
  194. util::fail('收银专用账号,不允许切换门店');
  195. }
  196. //惠雅莞城不允许切换门店
  197. if ($mainId == 1459 && $adminId == 2812) {
  198. util::fail('收银专用账号,不允许切换门店');
  199. }
  200. //淘花里中山店
  201. if ($mainId == 16948 && $adminId == 17908) {
  202. util::fail('收银专用账号,不允许切换门店');
  203. }
  204. //淘花里小榄店
  205. if ($mainId == 72057 && $adminId == 69792) {
  206. util::fail('收银专用账号,不允许切换门店');
  207. }
  208. }
  209. $shopAdmin = $this->shopAdmin;
  210. $switchShop = \biz\shop\classes\ShopAdminClass::hasSwitchShopRight($shopAdmin);
  211. if ($switchShop == 0) {
  212. util::fail('不能切换门店');
  213. }
  214. $newShopId = Yii::$app->request->post('shopId', 0);
  215. $newShop = ShopClass::getById($newShopId, true);
  216. if (empty($newShop)) {
  217. util::fail('没有找到门店30');
  218. }
  219. $res = \bizHd\admin\classes\AdminClass::toggleShop($this->admin, $newShop, $this->sjId, $this->shopAdmin);
  220. util::success($res);
  221. }
  222. //网页端使用,下载微信和支付宝收款码 ssh 20250821
  223. public function actionGatheringCode()
  224. {
  225. $id = Yii::$app->request->get('id', 0);
  226. $shop = ShopService::getById($id, true);
  227. if (empty($shop)) {
  228. util::fail('没有找到门店31');
  229. }
  230. ShopClass::valid($shop->attributes, $this->sjId);
  231. $imgUrl = ShopClass::generateGatheringCode($this->sjId, $id);
  232. $shopName = $shop->shopName ?? '';
  233. $file = fopen($imgUrl, "rb");
  234. Header("Content-type: application/octet-stream ");
  235. Header("Accept-Ranges: bytes ");
  236. Header("Content-Disposition: attachment;filename={$shopName}收款码.jpg");
  237. $contents = "";
  238. while (!feof($file)) {
  239. $contents .= fread($file, 8192);
  240. }
  241. echo $contents;
  242. fclose($file);
  243. }
  244. //小程序端使用,下载收款码 ssh 20250821
  245. public function actionMiniGatheringCode()
  246. {
  247. $shop = $this->shop;
  248. $shopId = $shop->id;
  249. $imgUrl = ShopClass::generateGatheringCode($this->sjId, $shopId);
  250. util::success(['imgUrl' => $imgUrl]);
  251. }
  252. //获取二维码
  253. public function actionGetGatheringCodeUrl()
  254. {
  255. $id = Yii::$app->request->get('id', 0);
  256. $shop = ShopService::getById($id, true);
  257. if (empty($shop)) {
  258. util::fail('没有找到门店32');
  259. }
  260. ShopClass::valid($shop->attributes, $this->sjId);
  261. $imgUrl = Yii::$app->params['hdHost'] . '/auth/gathering-img-url?id=' . $id;
  262. util::success(['url' => $imgUrl]);
  263. }
  264. public function actionGatheringImgUrl()
  265. {
  266. $id = Yii::$app->request->get('id', 0);
  267. $shop = ShopService::getById($id, true);
  268. if (empty($shop)) {
  269. util::fail('没有找到门店33');
  270. }
  271. $sjId = $shop->sjId;
  272. $imgUrl = ShopClass::generateGatheringCode($sjId, $id);
  273. $fileResource = file_get_contents($imgUrl);
  274. header('Content-type: image/jpeg');
  275. echo $fileResource;
  276. }
  277. //获取小程序的收款码 ssh
  278. public function actionGatheringMiniCode()
  279. {
  280. $id = Yii::$app->request->get('id', 0);
  281. $extend = MerchantExtendService::getBySjId($this->sjId);
  282. if ($extend['miniAuth'] == 0) {
  283. util::fail('您的小程序还没有授权');
  284. }
  285. $shop = ShopService::getById($id);
  286. ShopService::valid($shop, $this->sjId);
  287. $applyMemberCode = wxUtil::generateGatheringMiniCode($this->sj->attributes, $id);
  288. $file = dirUtil::getImgDir() . $applyMemberCode;
  289. if (file_exists($file) == false) {
  290. util::fail('没有找到注册会员码');
  291. }
  292. $fileName = "门店({$id})小程序收款码.jpg";
  293. $contentType = 'image/jpeg';
  294. header("Cache-control: private");
  295. header("Content-type: $contentType"); //设置要下载的文件类型
  296. header("Content-Length:" . filesize($file)); //设置要下载文件的文件大小
  297. header("Content-Disposition: attachment; filename=" . urldecode($fileName)); //设置要下载文件的文件名
  298. readfile($file);
  299. }
  300. //下载会员注册码 ssh 2020.2.29
  301. public function actionApplyMemberCode()
  302. {
  303. $id = Yii::$app->request->get('id', 0);
  304. $extend = MerchantExtendService::getBySjId($this->sjId);
  305. if ($extend['miniAuth'] == 0) {
  306. util::fail('您的小程序还没有授权');
  307. }
  308. $shop = ShopService::getById($id);
  309. ShopService::valid($shop, $this->sjId);
  310. $applyMemberCode = wxUtil::generateMemberCode($this->sj->attributes, $id);
  311. $file = dirUtil::getImgDir() . $applyMemberCode;
  312. if (file_exists($file) == false) {
  313. util::fail('没有找到注册会员码');
  314. }
  315. $fileName = "注册会员码{$id}.jpg";
  316. $contentType = 'image/jpeg';
  317. header("Cache-control: private");
  318. header("Content-type: $contentType"); //设置要下载的文件类型
  319. header("Content-Length:" . filesize($file)); //设置要下载文件的文件大小
  320. header("Content-Disposition: attachment; filename=" . urldecode($fileName)); //设置要下载文件的文件名
  321. readfile($file);
  322. }
  323. //删除门店 ssh 2020.3.1
  324. public function actionDelete()
  325. {
  326. util::fail('禁止操作');
  327. $id = Yii::$app->request->get('id', 0);
  328. util::fail('功能开发中');
  329. $shop = ShopService::getById($id);
  330. ShopService::valid($shop, $this->sjId);
  331. ShopService::deleteShop($shop);
  332. util::complete();
  333. }
  334. //获取门店详情 ssh 2021.4.23
  335. public function actionDetail()
  336. {
  337. $shopId = Yii::$app->request->get('shopId');
  338. if (empty($shopId)) {
  339. $shopId = $this->shopId;
  340. }
  341. $shop = ShopClass::getShopInfo($shopId);
  342. $shop['hasMap'] = dict::getDict('hasMap'); //添加 hasMap 属性(字典中的 hasMap)
  343. util::success(['info' => $shop]);
  344. }
  345. //提现信息更新 ssh 2021.3.23
  346. public function actionCashUpdate()
  347. {
  348. $get = Yii::$app->request->get();
  349. $cashAccount = $get['cashAccount'] ?? '';
  350. $cashName = $get['cashName'] ?? '';
  351. $cashBank = $get['cashBank'] ?? '';
  352. if (empty($cashName)) {
  353. util::fail('请填写姓名');
  354. }
  355. if (empty($cashAccount)) {
  356. util::fail('请填写银行卡号');
  357. }
  358. if (empty($cashBank)) {
  359. util::fail('请输入开户行');
  360. }
  361. $shopAdmin = $this->shopAdmin;
  362. $shopId = $shopAdmin->shopId ?? 0;
  363. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  364. util::fail('超管才能操作');
  365. }
  366. ShopClass::updateById($shopId, ['cashAccount' => $cashAccount, 'cashName' => $cashName, 'cashBank' => $cashBank]);
  367. $shop = $this->shop;
  368. $sjName = $shop->merchantName ?? '';
  369. $shopName = $shop->shopName ?? '';
  370. //noticeUtil::push("{$sjName}-{$shopName} 修改提现账号,{$cashName} {$cashAccount} {$cashBank}", '15280215347');
  371. util::complete();
  372. }
  373. //获取所有门店lqh 2021.1.31
  374. public function actionAll()
  375. {
  376. $res = [];
  377. $shopAdmin = isset($this->shopAdmin) && !empty($this->shopAdmin) ? $this->shopAdmin->attributes : [];
  378. if (isset($shopAdmin['super']) && $shopAdmin['super'] == 1) {
  379. $res = ShopClass::getAllShop($this->sjId);
  380. }
  381. util::success($res);
  382. }
  383. //获取采购时的支付方式 ssh 20211004
  384. public function actionGetCgPayWay()
  385. {
  386. $get = Yii::$app->request->get();
  387. $id = $get['id'] ?? 0;
  388. $cg = PurchaseClass::getById($id, true);
  389. PurchaseClass::valid($cg, $this->shopId);
  390. $customId = $cg->customId ?? 0;
  391. $custom = CustomClass::getById($customId);
  392. $hasDebtPay = $custom['debt'] ?? 0;
  393. //预订单不能欠款
  394. if ($cg->book == 1) {
  395. $hasDebtPay = 0;
  396. }
  397. $current = time();
  398. $cgDeadTime = strtotime($cg->deadline);
  399. //让客户在失效前$aheadTime秒要支付,这样回调通知时订单才不会过期状态
  400. $aheadTime = dict::getDict('order_online_pay_has_ahead_time');
  401. $count = $cgDeadTime - $current - $aheadTime;
  402. $data = [];
  403. $data['balance'] = $this->shop->balance ?? 0;
  404. $data['count'] = $count;
  405. $data['hasDebtPay'] = $hasDebtPay;
  406. util::success($data);
  407. }
  408. //地区 ssh 20220606
  409. public function actionRegion()
  410. {
  411. $regionTree = RegionService::tree();
  412. util::success(['region' => $regionTree]);
  413. }
  414. //散客门店详情 ssh 20211007
  415. public function actionKjCustom()
  416. {
  417. $defaultCustomId = $this->shop->defaultCustomId ?? 0;
  418. $custom = [];
  419. if (!empty($defaultCustomId)) {
  420. $custom = HdCustomClass::getById($defaultCustomId);
  421. $avatar = $custom['avatar'] ?? '';
  422. $custom['avatar'] = imgUtil::groupImg($avatar) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  423. }
  424. util::success(['custom' => $custom]);
  425. }
  426. //散客门店设置
  427. public function actionKjCustomSet()
  428. {
  429. $id = Yii::$app->request->get('id', 0);
  430. $custom = HdCustomClass::getById($id, true);
  431. if (empty($custom)) {
  432. util::fail('没有找到客户');
  433. }
  434. if ($custom->shopId != $this->shopId) {
  435. util::fail('客户不属于当前门店');
  436. }
  437. $shop = $this->shop;
  438. $shop->defaultCustomId = $id;
  439. $shop->save();
  440. util::complete('修改成功');
  441. }
  442. public function actionUpdateShopAvatar()
  443. {
  444. $data = Yii::$app->request->post();
  445. $id = isset($data['id']) ? $data['id'] : 0;
  446. $shop = ShopClass::getById($id, true, 'id, sjId, avatar');
  447. ShopService::valid($shop, $this->sjId);
  448. if (isset($data['avatar']) && $data['avatar'] != '') {
  449. $shop->avatar = $data['avatar'];
  450. $re = $shop->save();
  451. if ($re) {
  452. \bizGhs\custom\classes\CustomClass::updateByCondition(['shopId' => $id], [
  453. 'avatar' => $data['avatar']
  454. ]);
  455. util::complete('头像修改完成');
  456. }
  457. util::fail('头像修改失败');
  458. }
  459. util::fail('头像参数出错');
  460. }
  461. }