ShopController.php 18 KB

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