ShopController.php 15 KB

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