ShopController.php 21 KB

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