ShopController.php 18 KB

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