ShopController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. $data = Yii::$app->request->post();
  168. $data['sjId'] = $this->sjId;
  169. $data['shopId'] = $this->shopId;
  170. $data['adminId'] = $this->adminId;
  171. if (isset($data['shopName']) == false || empty($data['shopName'])) {
  172. util::fail('请填写门店名称');
  173. }
  174. $sj = $this->sj;
  175. $sjName = $sj->name ?? '';
  176. $ptStyle = $sj->style ?? dict::getDict('ptStyle', 'hd');
  177. $data['ptStyle'] = $ptStyle;
  178. $data['merchantName'] = $sjName;
  179. $data['default'] = 0;
  180. $connection = Yii::$app->db;
  181. $transaction = $connection->beginTransaction();
  182. try {
  183. $mobile = $data['mobile'] ?? 0;
  184. if (stringUtil::isMobile($mobile) == false) {
  185. util::fail('请填写正确手机号');
  186. }
  187. $adminInfo = ['name' => $mobile, 'mobile' => $mobile];
  188. $fromApp = dict::getDict('userSourceGetId', 'app', 'id');
  189. $admin = AdminClass::replaceAdmin($adminInfo, $fromApp);
  190. $adminId = $admin['id'] ?? 0;
  191. $data['adminId'] = $adminId;
  192. ShopClass::addMainShop($data, $this->shop, $this->sj);
  193. $transaction->commit();
  194. util::complete();
  195. } catch (\Exception $exception) {
  196. $transaction->rollBack();
  197. util::fail();
  198. }
  199. }
  200. //PC下载微信和支付宝收款码 ssh 2020.2.29
  201. public function actionGatheringCode()
  202. {
  203. $id = Yii::$app->request->get('id', 0);
  204. $shop = ShopService::getById($id);
  205. ShopClass::valid($shop, $this->sjId);
  206. $gatheringCode = ShopClass::generateGatheringCode($this->sjId, $id);
  207. $file = dirUtil::getImgDir() . $gatheringCode;
  208. if (file_exists($file) == false) {
  209. util::fail('没有找到收款码');
  210. }
  211. if (httpUtil::isMiniProgram()) {
  212. $imgUrl = imgUtil::getPrefix() . $gatheringCode;
  213. util::success(['imgUrl' => $imgUrl]);
  214. }
  215. $fileName = "门店({$id})收款码.jpg";
  216. $contentType = 'image/jpeg';
  217. header("Cache-control: private");
  218. header("Content-type: $contentType"); //设置要下载的文件类型
  219. header("Content-Length:" . filesize($file)); //设置要下载文件的文件大小
  220. header("Content-Disposition: attachment; filename=" . urldecode($fileName)); //设置要下载文件的文件名
  221. readfile($file);
  222. }
  223. //PC获取小程序的收款码 ssh
  224. public function actionGatheringMiniCode()
  225. {
  226. $id = Yii::$app->request->get('id', 0);
  227. $extend = MerchantExtendService::getBySjId($this->sjId);
  228. if ($extend['miniAuth'] == 0) {
  229. util::fail('您的小程序还没有授权');
  230. }
  231. $shop = ShopService::getById($id);
  232. ShopClass::valid($shop, $this->sjId);
  233. $applyMemberCode = wxUtil::generateGatheringMiniCode($this->sj->attributes, $id);
  234. $file = dirUtil::getImgDir() . $applyMemberCode;
  235. if (file_exists($file) == false) {
  236. util::fail('没有找到注册会员码');
  237. }
  238. if (httpUtil::isMiniProgram()) {
  239. $imgUrl = imgUtil::getPrefix() . $applyMemberCode;
  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 2020.2.29
  251. public function actionApplyMemberCode()
  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::generateMemberCode($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. //删除门店 ssh 2020.3.1
  278. public function actionDelete()
  279. {
  280. util::fail('禁止操作');
  281. $id = Yii::$app->request->get('id', 0);
  282. $shop = ShopService::getById($id);
  283. ShopClass::valid($shop, $this->sjId);
  284. ShopClass::deleteShop($shop);
  285. util::complete();
  286. }
  287. //获取所有门店lqh 2021.1.31
  288. public function actionAll()
  289. {
  290. $res = ShopClass::getAllShop($this->sjId);
  291. util::success($res);
  292. }
  293. //切换门店 lqh 2021.1.31
  294. public function actionToggleShop()
  295. {
  296. $shopAdmin = $this->shopAdmin;
  297. $switchShop = ShopAdminClass::hasSwitchShopRight($shopAdmin);
  298. if ($switchShop == 0) {
  299. util::fail('不能切换门店');
  300. }
  301. $newShopId = Yii::$app->request->post('shopId', 0);
  302. $newShop = ShopClass::getById($newShopId, true);
  303. if (empty($newShop)) {
  304. util::fail('没有找到门店66');
  305. }
  306. $res = AdminClass::toggleShop($this->admin, $newShop, $this->sjId, $this->shopAdmin);
  307. util::success($res);
  308. }
  309. //提现信息更新 ssh 2021.3.23
  310. public function actionCashUpdate()
  311. {
  312. $get = Yii::$app->request->get();
  313. $cashAccount = $get['cashAccount'] ?? '';
  314. $cashName = $get['cashName'] ?? '';
  315. $cashBank = $get['cashBank'] ?? '';
  316. if (empty($cashName)) {
  317. util::fail('请填写姓名');
  318. }
  319. if (empty($cashAccount)) {
  320. util::fail('请填写银行卡号');
  321. }
  322. if (empty($cashBank)) {
  323. util::fail('请输入开户行');
  324. }
  325. $shopAdmin = $this->shopAdmin;
  326. $mainId = $shopAdmin->mainId ?? 0;
  327. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  328. util::fail('超管才能操作');
  329. }
  330. MainClass::updateById($mainId, ['cashAccount' => $cashAccount, 'cashName' => $cashName, 'cashBank' => $cashBank]);
  331. $shop = $this->shop;
  332. $sjName = $shop->merchantName ?? '';
  333. $shopName = $shop->shopName ?? '';
  334. noticeUtil::push("{$sjName}-{$shopName} 修改提现账号,{$cashName} {$cashAccount} {$cashBank}", '15280215347');
  335. util::complete();
  336. }
  337. //获取门店详情 ssh 2021.4.23
  338. public function actionDetail()
  339. {
  340. $shopId = Yii::$app->request->get('shopId');
  341. if (empty($shopId)) {
  342. $shopId = $this->shopId;
  343. }
  344. $shop = ShopClass::getById($shopId, true);
  345. if ($shop->mainId != $this->mainId) {
  346. //这个注释不能打开,因为首店可能修改其它分店信息
  347. //util::fail('无法查看');
  348. }
  349. util::success(['info' => $shop]);
  350. }
  351. //获取平台所有的供货商门店 ssh 20210908
  352. public function actionGetPtGhsShop()
  353. {
  354. $hasSale = Yii::$app->request->get('hasSale', 0);
  355. $shopList = ShopClass::getAllByCondition(['ptStyle' => 2, 'live' => 1], 'id asc', '*');
  356. $totalSale = 0;
  357. $totalShopNum = 0;
  358. $totalBalance = 0;
  359. if (!empty($shopList)) {
  360. foreach ($shopList as $key => $shop) {
  361. $mainId = $shop['mainId'] ?? 0;
  362. $main = MainClass::getById($mainId, true);
  363. $balance = $main->balance ?? 0;
  364. $respond = StatSaleClass::profile($mainId);
  365. $incomeList = $respond['income'] ?? [];
  366. $todaySale = 0;
  367. if (!empty($incomeList)) {
  368. foreach ($incomeList as $item) {
  369. //调拨出库暂时不算到收入里去
  370. if (isset($item['id']) && $item['id'] == 'allot') {
  371. continue;
  372. }
  373. $todaySale = bcadd($todaySale, $item['amount'], 2);
  374. }
  375. }
  376. $shopList[$key]['balance'] = $balance;
  377. $shopList[$key]['todaySale'] = $todaySale;
  378. $count = CustomClass::getCount(['ownMainId' => $mainId]);
  379. $count = $count > 0 ? $count : 0;
  380. $shopList[$key]['shopNum'] = $count;
  381. if ($hasSale == 1 && $todaySale <= 0) {
  382. unset($shopList[$key]);
  383. }
  384. $totalSale = bcadd($totalSale, $todaySale, 2);
  385. $totalSale = floatval($totalSale);
  386. $totalBalance = bcadd($totalBalance, $balance, 2);
  387. $totalBalance = floatval($totalBalance);
  388. $totalShopNum = bcadd($totalShopNum, $count);
  389. }
  390. $shopList = arrayUtil::arraySort($shopList, 'todaySale');
  391. }
  392. util::success(['list' => $shopList, 'totalSale' => $totalSale, 'totalBalance' => $totalBalance, 'totalShopNum' => $totalShopNum]);
  393. }
  394. //切换门店 ssh 20220624
  395. public function actionSwitchGhsShop()
  396. {
  397. $shopId = Yii::$app->request->get('shopId', 0);
  398. $shop = ShopClass::getById($shopId, true);
  399. if (empty($shop)) {
  400. util::fail('没有找到门店67');
  401. }
  402. $shopAdmin = $this->shopAdmin;
  403. $admin = $this->admin;
  404. $adminId = $this->adminId ?? 0;
  405. if (getenv('YII_ENV') == 'production') {
  406. if (in_array($adminId, [4]) == false) {
  407. util::fail('开发中');
  408. }
  409. } else {
  410. if (in_array($adminId, [919]) == false) {
  411. util::fail('开发中');
  412. }
  413. }
  414. ShopAdminClass::changeShopAddRelate($shopAdmin, $shop, 1);
  415. $admin->currentGhsShopId = $shopId;
  416. $lsShopId = $shop->lsShopId ?? 0;
  417. $admin->currentShopId = $lsShopId;
  418. $admin->save();
  419. util::complete();
  420. }
  421. //散客门店详情 ssh 20211007
  422. public function actionSkCustom()
  423. {
  424. $skCustomId = $this->shop->skCustomId ?? 0;
  425. $custom = [];
  426. if (!empty($skCustomId)) {
  427. $custom = CustomClass::getById($skCustomId);
  428. $avatar = $custom['avatar'] ?? '';
  429. $custom['avatar'] = imgUtil::groupImg($avatar) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  430. }
  431. util::success(['custom' => $custom]);
  432. }
  433. //散客门店设置
  434. public function actionSkCustomSet()
  435. {
  436. $id = Yii::$app->request->get('id', 0);
  437. $custom = CustomClass::getById($id, true);
  438. CustomClass::valid($custom, $this->shopId);
  439. $this->shop->skCustomId = $id;
  440. $this->shop->save();
  441. util::complete('修改成功');
  442. }
  443. //运费模式和运费成本设置 ssh 20211014
  444. public function actionUpdateCgModel()
  445. {
  446. $get = Yii::$app->request->get();
  447. $cgModel = $get['cgModel'] ?? 0;
  448. $cgPerCost = $get['cgPerCost'] ?? 0;
  449. if ($cgModel == 1) {
  450. if (is_numeric($cgPerCost) == false || $cgPerCost < 0) {
  451. util::fail('请输入运费成本');
  452. }
  453. $this->shop->cgPerCost = $cgPerCost;
  454. }
  455. $this->shop->cgModel = $cgModel;
  456. $this->shop->save();
  457. util::complete('修改成功');
  458. }
  459. //地区 ssh 20220606
  460. public function actionRegion()
  461. {
  462. $regionTree = RegionService::tree();
  463. util::success(['region' => $regionTree]);
  464. }
  465. }