ShopController.php 16 KB

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