ShopController.php 15 KB

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