ShopController.php 19 KB

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