ShopController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. $newShopId = Yii::$app->request->post('shopId', 0);
  236. $newShop = ShopClass::getById($newShopId, true);
  237. if (empty($newShop)) {
  238. util::fail('没有找到门店');
  239. }
  240. $res = AdminClass::toggleShop($this->admin, $newShop, $this->sjId, $this->shopAdmin);
  241. util::success($res);
  242. }
  243. //提现信息更新 ssh 2021.3.23
  244. public function actionCashUpdate()
  245. {
  246. $get = Yii::$app->request->get();
  247. $cashAccount = $get['cashAccount'] ?? '';
  248. $cashName = $get['cashName'] ?? '';
  249. $cashBank = $get['cashBank'] ?? '';
  250. if (empty($cashName)) {
  251. util::fail('请填写姓名');
  252. }
  253. if (empty($cashAccount)) {
  254. util::fail('请填写银行卡号');
  255. }
  256. if (empty($cashBank)) {
  257. util::fail('请输入开户行');
  258. }
  259. $shopAdmin = $this->shopAdmin;
  260. $mainId = $shopAdmin->mainId ?? 0;
  261. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  262. util::fail('超管才能操作');
  263. }
  264. MainClass::updateById($mainId, ['cashAccount' => $cashAccount, 'cashName' => $cashName, 'cashBank' => $cashBank]);
  265. $shop = $this->shop;
  266. $sjName = $shop->merchantName ?? '';
  267. $shopName = $shop->shopName ?? '';
  268. noticeUtil::push("{$sjName}-{$shopName} 修改提现账号,{$cashName} {$cashAccount} {$cashBank}", '15280215347');
  269. util::complete();
  270. }
  271. //获取门店详情 ssh 2021.4.23
  272. public function actionDetail()
  273. {
  274. $shopId = Yii::$app->request->get('shopId');
  275. if (empty($shopId)) {
  276. $shopId = $this->shopId;
  277. }
  278. $shop = ShopClass::getById($shopId, true);
  279. if ($shop->mainId != $this->mainId) {
  280. //这个注释不能打开,因为首店可能修改其它分店信息
  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. }