ShopController.php 18 KB

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