ShopController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\shop\classes\MainClass;
  4. use biz\shop\classes\ShopAdminClass;
  5. use biz\sj\services\MerchantExtendService;
  6. use bizGhs\admin\classes\AdminClass;
  7. use bizGhs\custom\classes\CustomClass;
  8. use bizGhs\ghs\classes\GhsClass;
  9. use bizGhs\merchant\classes\ShopClass;
  10. use bizGhs\merchant\services\ShopService;
  11. use bizHd\saas\services\RegionService;
  12. use common\components\arrayUtil;
  13. use common\components\dict;
  14. use common\components\dirUtil;
  15. use common\components\httpUtil;
  16. use common\components\imgUtil;
  17. use common\components\noticeUtil;
  18. use common\components\stringUtil;
  19. use common\components\util;
  20. use common\components\wxUtil;
  21. use Yii;
  22. use bizGhs\stat\classes\StatSaleClass;
  23. class ShopController extends BaseController
  24. {
  25. public $guestAccess = ['all'];
  26. //修改客户微信二维码 ssh 20240714
  27. public function actionChangeWx()
  28. {
  29. $get = Yii::$app->request->get();
  30. $url = $get['url'] ?? '';
  31. $shop = $this->shop;
  32. $shop->superWx = $url;
  33. $shop->save();
  34. util::complete('修改成功');
  35. }
  36. //我的可以进入的门店 ssh 20230424
  37. public function actionMyShop()
  38. {
  39. $staff = $this->shopAdmin;
  40. $shopList = [];
  41. if (isset($staff->super) && $staff->super == 1) {
  42. $sjId = $this->sjId ?? 0;
  43. $shopList = ShopClass::getAllByCondition(['sjId' => $sjId, 'ptStyle' => 2], null, 'id,shopName,merchantName,sjId,mainId,ptStyle', 'id');
  44. }
  45. $adminId = $this->adminId ?? 0;
  46. $staffList = ShopAdminClass::getAllByCondition(['adminId' => $adminId], null, '*', null, true);
  47. $mainIdList = [];
  48. if (!empty($staffList)) {
  49. foreach ($staffList as $staff) {
  50. $isPt = $staff->isPt ?? 0;
  51. $delStatus = $staff->delStatus ?? 0;
  52. //$super = $staff->super ?? 0;
  53. $mainId = $staff->mainId ?? 0;
  54. if ($isPt == 0 && $delStatus == 0) {
  55. $mainIdList[] = $mainId;
  56. }
  57. }
  58. }
  59. if (!empty($mainIdList)) {
  60. $addShopList = ShopClass::getAllByCondition(['mainId' => ['in', $mainIdList], 'ptStyle' => 2], null, 'id,shopName,merchantName,sjId,mainId,ptStyle', 'id');
  61. if (!empty($addShopList)) {
  62. foreach ($addShopList as $addShop) {
  63. $addShopId = $addShop['id'] ?? 0;
  64. if (isset($shopList[$addShopId]) == false) {
  65. $shopList[$addShopId] = $addShop;
  66. }
  67. }
  68. }
  69. }
  70. $list = array_values($shopList);
  71. util::success(['list' => $list]);
  72. }
  73. //绑定零售店 ssh 20220430
  74. public function actionBindLsShop()
  75. {
  76. $id = Yii::$app->request->get('id');
  77. $connection = Yii::$app->db;
  78. $transaction = $connection->beginTransaction();
  79. util::fail('此方法已停用,如需启用请确认有没问题');
  80. try {
  81. $shop = $this->shop ?? [];
  82. $lsShopId = $shop->lsShopId ?? 0;
  83. if (!empty($lsShopId)) {
  84. util::fail('您已经绑定过了');
  85. }
  86. ShopClass::bindLsShopByCustomId($shop, $id, $this->adminId);
  87. $transaction->commit();
  88. util::complete();
  89. } catch (\Exception $exception) {
  90. $transaction->rollBack();
  91. util::fail('绑定失败');
  92. }
  93. }
  94. //预订设置 ssh 20220324
  95. public function actionBookSet()
  96. {
  97. $get = Yii::$app->request->get();
  98. $presell = $get['presell'] ?? 0;
  99. $this->shop->presell = $presell;
  100. $this->shop->save();
  101. util::complete();
  102. }
  103. //当前门店信息
  104. public function actionInfo()
  105. {
  106. $shop = $this->shop ?? [];
  107. util::success($shop);
  108. }
  109. //获取当前门店信息 ssh 2021.3.27
  110. public function actionCurrentShop()
  111. {
  112. $main = $this->shop;
  113. util::success($main);
  114. }
  115. //门店列表 ssh 2021.1.14
  116. public function actionList()
  117. {
  118. $where = [];
  119. $where['sjId'] = $this->sjId;
  120. $where['delStatus'] = 0;
  121. $list = ShopClass::getShopList($where);
  122. util::success($list);
  123. }
  124. //更新门店 ssh 2020.2.29
  125. public function actionUpdate()
  126. {
  127. $post = Yii::$app->request->post();
  128. $post['sjId'] = $this->sjId;
  129. $id = $post['id'] ?? 0;
  130. $shop = ShopService::getById($id);
  131. if (empty($shop)) {
  132. util::fail('没有找到门店65');
  133. }
  134. $staff = $this->shopAdmin;
  135. if (isset($staff->finance) == false || $staff->finance == 0) {
  136. if ($this->adminId != 4) {
  137. util::fail('没有修改门店权限');
  138. }
  139. }
  140. if (isset($staff->super) == false || $staff->super == 0) {
  141. if ($this->adminId != 4) {
  142. util::fail('没有修改门店权限哦');
  143. }
  144. }
  145. //显示和隐藏库存,同时影响到老客户和新注册客户 ssh 20250309
  146. $showStock = $post['showStock'] ?? 0;
  147. $shopId = $this->shopId;
  148. $customList = CustomClass::getAllByCondition(['ownShopId' => $shopId], null, '*', null, true);
  149. if (!empty($customList)) {
  150. foreach ($customList as $key => $custom) {
  151. $ghsId = $custom->ghsId ?? 0;
  152. $ghs = GhsClass::getById($ghsId, true);
  153. if (!empty($custom) && !empty($ghs)) {
  154. $ghs->showStock = $showStock;
  155. $ghs->save();
  156. $custom->showStock = $showStock;
  157. $custom->save();
  158. }
  159. }
  160. }
  161. $post['meetNum'] = isset($post['meetNum']) && !empty($post['meetNum']) ? $post['meetNum'] : 0;
  162. $post['meetAmount'] = isset($post['meetAmount']) && !empty($post['meetAmount']) ? $post['meetAmount'] : 0;
  163. $post['cutAmount'] = isset($post['cutAmount']) && !empty($post['cutAmount']) ? $post['cutAmount'] : 0;
  164. ShopClass::valid($shop, $this->sjId);
  165. //手机号具有唯一性,暂时不允许修改
  166. unset($post['mobile']);
  167. $post['img'] = isset($post['img']) && is_array($post['img']) ? json_encode($post['img']) : '';
  168. \biz\shop\classes\ShopClass::updateShop($shop, $post);
  169. util::complete();
  170. }
  171. //添加门店 ssh 2021.1.14
  172. public function actionAdd()
  173. {
  174. $shop = $this->shop;
  175. $default = $shop->default ?? 0;
  176. if ($default == 0) {
  177. util::fail('当前直营分店,请切回总店添加');
  178. }
  179. $adminId = $this->adminId;
  180. $ptSuperAdminId = \biz\admin\classes\AdminClass::getPtSuperAdminId();
  181. if ($adminId != $shop->adminId && $adminId != $ptSuperAdminId) {
  182. util::fail('本店超管才能添加门店');
  183. }
  184. $mainId = $this->mainId;
  185. if (getenv('YII_ENV') == 'production') {
  186. if ($mainId == 42490) {
  187. util::fail('无法建分店,请联系石头');
  188. }
  189. } else {
  190. if ($mainId == 644) {
  191. util::fail('无法建分店,请联系石头');
  192. }
  193. }
  194. $data = Yii::$app->request->post();
  195. $data['sjId'] = $this->sjId;
  196. $data['shopId'] = $this->shopId;
  197. $data['adminId'] = $this->adminId;
  198. if (isset($data['shopName']) == false || empty($data['shopName'])) {
  199. util::fail('请填写门店名称');
  200. }
  201. $sj = $this->sj;
  202. $sjName = $sj->name ?? '';
  203. $ptStyle = $sj->style ?? dict::getDict('ptStyle', 'hd');
  204. $data['ptStyle'] = $ptStyle;
  205. $data['merchantName'] = $sjName;
  206. $data['default'] = 0;
  207. $connection = Yii::$app->db;
  208. $transaction = $connection->beginTransaction();
  209. try {
  210. $mobile = $data['mobile'] ?? 0;
  211. if (stringUtil::isMobile($mobile) == false) {
  212. util::fail('请填写正确手机号');
  213. }
  214. $adminInfo = ['name' => $mobile, 'mobile' => $mobile];
  215. $fromApp = dict::getDict('userSourceGetId', 'app', 'id');
  216. $admin = AdminClass::replaceAdmin($adminInfo, $fromApp);
  217. $adminId = $admin['id'] ?? 0;
  218. $data['adminId'] = $adminId;
  219. ShopClass::addMainShop($data, $this->shop, $this->sj);
  220. $transaction->commit();
  221. util::complete();
  222. } catch (\Exception $exception) {
  223. $transaction->rollBack();
  224. util::fail();
  225. }
  226. }
  227. //PC下载微信和支付宝收款码 ssh 2020.2.29
  228. public function actionGatheringCode()
  229. {
  230. $id = Yii::$app->request->get('id', 0);
  231. $shop = ShopService::getById($id);
  232. ShopClass::valid($shop, $this->sjId);
  233. $gatheringCode = ShopClass::generateGatheringCode($this->sjId, $id);
  234. $file = dirUtil::getImgDir() . $gatheringCode;
  235. if (file_exists($file) == false) {
  236. util::fail('没有找到收款码');
  237. }
  238. if (httpUtil::isMiniProgram()) {
  239. $imgUrl = imgUtil::getPrefix() . $gatheringCode;
  240. util::success(['imgUrl' => $imgUrl]);
  241. }
  242. $fileName = "门店({$id})收款码.jpg";
  243. $contentType = 'image/jpeg';
  244. header("Cache-control: private");
  245. header("Content-type: $contentType"); //设置要下载的文件类型
  246. header("Content-Length:" . filesize($file)); //设置要下载文件的文件大小
  247. header("Content-Disposition: attachment; filename=" . urldecode($fileName)); //设置要下载文件的文件名
  248. readfile($file);
  249. }
  250. //PC获取小程序的收款码 ssh
  251. public function actionGatheringMiniCode()
  252. {
  253. $id = Yii::$app->request->get('id', 0);
  254. $extend = MerchantExtendService::getBySjId($this->sjId);
  255. if ($extend['miniAuth'] == 0) {
  256. util::fail('您的小程序还没有授权');
  257. }
  258. $shop = ShopService::getById($id);
  259. ShopClass::valid($shop, $this->sjId);
  260. $applyMemberCode = wxUtil::generateGatheringMiniCode($this->sj->attributes, $id);
  261. $file = dirUtil::getImgDir() . $applyMemberCode;
  262. if (file_exists($file) == false) {
  263. util::fail('没有找到注册会员码');
  264. }
  265. if (httpUtil::isMiniProgram()) {
  266. $imgUrl = imgUtil::getPrefix() . $applyMemberCode;
  267. util::success(['imgUrl' => $imgUrl]);
  268. }
  269. $fileName = "门店({$id})小程序收款码.jpg";
  270. $contentType = 'image/jpeg';
  271. header("Cache-control: private");
  272. header("Content-type: $contentType"); //设置要下载的文件类型
  273. header("Content-Length:" . filesize($file)); //设置要下载文件的文件大小
  274. header("Content-Disposition: attachment; filename=" . urldecode($fileName)); //设置要下载文件的文件名
  275. readfile($file);
  276. }
  277. //PC下载会员注册码 ssh 2020.2.29
  278. public function actionApplyMemberCode()
  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. ShopClass::valid($shop, $this->sjId);
  287. $applyMemberCode = wxUtil::generateMemberCode($this->sj->attributes, $id);
  288. $file = dirUtil::getImgDir() . $applyMemberCode;
  289. if (file_exists($file) == false) {
  290. util::fail('没有找到注册会员码');
  291. }
  292. if (httpUtil::isMiniProgram()) {
  293. $imgUrl = imgUtil::getPrefix() . $applyMemberCode;
  294. util::success(['imgUrl' => $imgUrl]);
  295. }
  296. $fileName = "注册会员码{$id}.jpg";
  297. $contentType = 'image/jpeg';
  298. header("Cache-control: private");
  299. header("Content-type: $contentType"); //设置要下载的文件类型
  300. header("Content-Length:" . filesize($file)); //设置要下载文件的文件大小
  301. header("Content-Disposition: attachment; filename=" . urldecode($fileName)); //设置要下载文件的文件名
  302. readfile($file);
  303. }
  304. //删除门店 ssh 2020.3.1
  305. public function actionDelete()
  306. {
  307. util::fail('禁止操作');
  308. $id = Yii::$app->request->get('id', 0);
  309. $shop = ShopService::getById($id);
  310. ShopClass::valid($shop, $this->sjId);
  311. ShopClass::deleteShop($shop);
  312. util::complete();
  313. }
  314. //获取所有门店lqh 2021.1.31
  315. public function actionAll()
  316. {
  317. $res = ShopClass::getAllShop($this->sjId);
  318. util::success($res);
  319. }
  320. //切换门店 lqh 2021.1.31
  321. public function actionToggleShop()
  322. {
  323. $shopAdmin = $this->shopAdmin;
  324. $switchShop = ShopAdminClass::hasSwitchShopRight($shopAdmin);
  325. if ($switchShop == 0) {
  326. util::fail('不能切换门店');
  327. }
  328. $newShopId = Yii::$app->request->post('shopId', 0);
  329. $newShop = ShopClass::getById($newShopId, true);
  330. if (empty($newShop)) {
  331. util::fail('没有找到门店66');
  332. }
  333. $res = AdminClass::toggleShop($this->admin, $newShop, $this->sjId, $this->shopAdmin);
  334. util::success($res);
  335. }
  336. //提现信息更新 ssh 2021.3.23
  337. public function actionCashUpdate()
  338. {
  339. $get = Yii::$app->request->get();
  340. $cashAccount = $get['cashAccount'] ?? '';
  341. $cashName = $get['cashName'] ?? '';
  342. $cashBank = $get['cashBank'] ?? '';
  343. if (empty($cashName)) {
  344. util::fail('请填写姓名');
  345. }
  346. if (empty($cashAccount)) {
  347. util::fail('请填写银行卡号');
  348. }
  349. if (empty($cashBank)) {
  350. util::fail('请输入开户行');
  351. }
  352. $shopAdmin = $this->shopAdmin;
  353. $mainId = $shopAdmin->mainId ?? 0;
  354. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  355. util::fail('超管才能操作');
  356. }
  357. MainClass::updateById($mainId, ['cashAccount' => $cashAccount, 'cashName' => $cashName, 'cashBank' => $cashBank]);
  358. $shop = $this->shop;
  359. $sjName = $shop->merchantName ?? '';
  360. $shopName = $shop->shopName ?? '';
  361. noticeUtil::push("{$sjName}-{$shopName} 修改提现账号,{$cashName} {$cashAccount} {$cashBank}", '15280215347');
  362. util::complete();
  363. }
  364. //获取门店详情 ssh 2021.4.23
  365. public function actionDetail()
  366. {
  367. $shopId = Yii::$app->request->get('shopId');
  368. if (empty($shopId)) {
  369. $shopId = $this->shopId;
  370. }
  371. $shop = ShopClass::getById($shopId, true);
  372. if ($shop->mainId != $this->mainId) {
  373. //这个注释不能打开,因为首店可能修改其它分店信息
  374. //util::fail('无法查看');
  375. }
  376. util::success(['info' => $shop]);
  377. }
  378. //获取平台所有的供货商门店 ssh 20210908
  379. public function actionGetPtGhsShop()
  380. {
  381. $hasSale = Yii::$app->request->get('hasSale', 0);
  382. $shopList = ShopClass::getAllByCondition(['ptStyle' => 2, 'live' => 1], 'id asc', '*');
  383. $totalSale = 0;
  384. $totalShopNum = 0;
  385. $totalBalance = 0;
  386. if (!empty($shopList)) {
  387. foreach ($shopList as $key => $shop) {
  388. $mainId = $shop['mainId'] ?? 0;
  389. $main = MainClass::getById($mainId, true);
  390. $balance = $main->balance ?? 0;
  391. $respond = StatSaleClass::profile($mainId);
  392. $incomeList = $respond['income'] ?? [];
  393. $todaySale = 0;
  394. if (!empty($incomeList)) {
  395. foreach ($incomeList as $item) {
  396. //调拨出库暂时不算到收入里去
  397. if (isset($item['id']) && $item['id'] == 'allot') {
  398. continue;
  399. }
  400. $todaySale = bcadd($todaySale, $item['amount'], 2);
  401. }
  402. }
  403. $shopList[$key]['balance'] = $balance;
  404. $shopList[$key]['todaySale'] = $todaySale;
  405. $count = CustomClass::getCount(['ownMainId' => $mainId]);
  406. $count = $count > 0 ? $count : 0;
  407. $shopList[$key]['shopNum'] = $count;
  408. if ($hasSale == 1 && $todaySale <= 0) {
  409. unset($shopList[$key]);
  410. }
  411. $totalSale = bcadd($totalSale, $todaySale, 2);
  412. $totalSale = floatval($totalSale);
  413. $totalBalance = bcadd($totalBalance, $balance, 2);
  414. $totalBalance = floatval($totalBalance);
  415. $totalShopNum = bcadd($totalShopNum, $count);
  416. }
  417. $shopList = arrayUtil::arraySort($shopList, 'todaySale');
  418. }
  419. util::success(['list' => $shopList, 'totalSale' => $totalSale, 'totalBalance' => $totalBalance, 'totalShopNum' => $totalShopNum]);
  420. }
  421. //切换门店 ssh 20220624
  422. public function actionSwitchGhsShop()
  423. {
  424. $shopId = Yii::$app->request->get('shopId', 0);
  425. $shop = ShopClass::getById($shopId, true);
  426. if (empty($shop)) {
  427. util::fail('没有找到门店67');
  428. }
  429. $shopAdmin = $this->shopAdmin;
  430. $admin = $this->admin;
  431. $adminId = $this->adminId ?? 0;
  432. if (getenv('YII_ENV') == 'production') {
  433. if (in_array($adminId, [4]) == false) {
  434. util::fail('开发中');
  435. }
  436. } else {
  437. if (in_array($adminId, [919]) == false) {
  438. util::fail('开发中');
  439. }
  440. }
  441. ShopAdminClass::changeShopAddRelate($shopAdmin, $shop, 1);
  442. $admin->currentGhsShopId = $shopId;
  443. $lsShopId = $shop->lsShopId ?? 0;
  444. $admin->currentShopId = $lsShopId;
  445. $admin->save();
  446. util::complete();
  447. }
  448. //散客门店详情 ssh 20211007
  449. public function actionSkCustom()
  450. {
  451. $skCustomId = $this->shop->skCustomId ?? 0;
  452. $custom = [];
  453. if (!empty($skCustomId)) {
  454. $custom = CustomClass::getById($skCustomId);
  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 actionSkCustomSet()
  462. {
  463. $id = Yii::$app->request->get('id', 0);
  464. $custom = CustomClass::getById($id, true);
  465. CustomClass::valid($custom, $this->shopId);
  466. $this->shop->skCustomId = $id;
  467. $this->shop->save();
  468. util::complete('修改成功');
  469. }
  470. //运费模式和运费成本设置 ssh 20211014
  471. public function actionUpdateCgModel()
  472. {
  473. $get = Yii::$app->request->get();
  474. $cgModel = $get['cgModel'] ?? 0;
  475. $cgPerCost = $get['cgPerCost'] ?? 0;
  476. if ($cgModel == 1) {
  477. if (is_numeric($cgPerCost) == false || $cgPerCost < 0) {
  478. util::fail('请输入运费成本');
  479. }
  480. $this->shop->cgPerCost = $cgPerCost;
  481. }
  482. $this->shop->cgModel = $cgModel;
  483. $this->shop->save();
  484. util::complete('修改成功');
  485. }
  486. //地区 ssh 20220606
  487. public function actionRegion()
  488. {
  489. $regionTree = RegionService::tree();
  490. util::success(['region' => $regionTree]);
  491. }
  492. }