ShopController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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\stringUtil;
  17. use common\components\util;
  18. use common\components\wxUtil;
  19. use Yii;
  20. use bizGhs\stat\classes\StatSaleClass;
  21. class ShopController extends BaseController
  22. {
  23. public $guestAccess = ['all'];
  24. //绑定零售店 ssh 20220430
  25. public function actionBindLsShop()
  26. {
  27. $id = Yii::$app->request->get('id');
  28. $connection = Yii::$app->db;
  29. $transaction = $connection->beginTransaction();
  30. util::fail('此方法已停用,如需启用请确认有没问题');
  31. try {
  32. $shop = $this->shop ?? [];
  33. $lsShopId = $shop->lsShopId ?? 0;
  34. if (!empty($lsShopId)) {
  35. util::fail('您已经绑定过了');
  36. }
  37. ShopClass::bindLsShopByCustomId($shop, $id, $this->adminId);
  38. $transaction->commit();
  39. util::complete();
  40. } catch (\Exception $exception) {
  41. $transaction->rollBack();
  42. util::fail('绑定失败');
  43. }
  44. }
  45. //预订设置 ssh 20220324
  46. public function actionBookSet()
  47. {
  48. $get = Yii::$app->request->get();
  49. $kiloFee = $get['kiloFee'] ?? 0;
  50. $miniKilo = $get['miniKilo'] ?? 0;
  51. $this->shop->kiloFee = $kiloFee;
  52. $this->shop->miniKilo = $miniKilo;
  53. $this->shop->save();
  54. util::complete();
  55. }
  56. //当前门店信息
  57. public function actionInfo()
  58. {
  59. $shop = $this->shop ?? [];
  60. util::success($shop);
  61. }
  62. //获取当前门店信息 ssh 2021.3.27
  63. public function actionCurrentShop()
  64. {
  65. $main = $this->shop;
  66. util::success($main);
  67. }
  68. //门店列表 ssh 2021.1.14
  69. public function actionList()
  70. {
  71. $where = [];
  72. $where['sjId'] = $this->sjId;
  73. $where['delStatus'] = 0;
  74. $list = ShopClass::getShopList($where);
  75. util::success($list);
  76. }
  77. //更新门店 ssh 2020.2.29
  78. public function actionUpdate()
  79. {
  80. $shop = $this->shop;
  81. $join = $shop->join ?? 0;
  82. $default = $shop->default ?? 0;
  83. if ($join == 0 && $default == 0) {
  84. util::fail('请在总店操作');
  85. }
  86. $data = Yii::$app->request->post();
  87. $data['sjId'] = $this->sjId;
  88. $id = $data['id'] ?? 0;
  89. $shop = ShopService::getById($id);
  90. if (empty($shop)) {
  91. util::fail('没有找到门店');
  92. }
  93. ShopClass::valid($shop, $this->sjId);
  94. $data['img'] = isset($data['img']) && is_array($data['img']) ? json_encode($data['img']) : '';
  95. \biz\shop\classes\ShopClass::updateShop($shop, $data);
  96. util::complete();
  97. }
  98. //添加门店 ssh 2021.1.14
  99. public function actionAdd()
  100. {
  101. $shop = $this->shop;
  102. $join = $shop->join ?? 0;
  103. $default = $shop->default ?? 0;
  104. if ($join == 0 && $default == 0) {
  105. util::fail('请在总店操作');
  106. }
  107. $data = Yii::$app->request->post();
  108. $data['sjId'] = $this->sjId;
  109. $data['shopId'] = $this->shopId;
  110. $data['adminId'] = $this->adminId;
  111. if (isset($data['shopName']) == false || empty($data['shopName'])) {
  112. util::fail('请填写门店名称');
  113. }
  114. $sj = $this->sj;
  115. $sjName = $sj->name ?? '';
  116. $ptStyle = $sj->style ?? dict::getDict('ptStyle', 'hd');
  117. $data['ptStyle'] = $ptStyle;
  118. $data['merchantName'] = $sjName;
  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. util::complete();
  266. }
  267. //获取门店详情 ssh 2021.4.23
  268. public function actionDetail()
  269. {
  270. $shopId = Yii::$app->request->get('shopId');
  271. if (empty($shopId)) {
  272. $shopId = $this->shopId;
  273. }
  274. $shop = ShopClass::getShopInfo($shopId);
  275. util::success(['info' => $shop]);
  276. }
  277. //获取平台所有的供货商门店 ssh 20210908
  278. public function actionGetPtGhsShop()
  279. {
  280. $hasSale = Yii::$app->request->get('hasSale', 0);
  281. $shopList = ShopClass::getAllByCondition(['ptStyle' => 2], 'id asc', '*');
  282. $totalSale = 0;
  283. $totalShopNum = 0;
  284. $totalBalance = 0;
  285. if (!empty($shopList)) {
  286. foreach ($shopList as $key => $shop) {
  287. $mainId = $shop['mainId'] ?? 0;
  288. $main = MainClass::getById($mainId, true);
  289. $balance = $main->balance ?? 0;
  290. $respond = StatSaleClass::profile($mainId);
  291. $incomeList = $respond['income'] ?? [];
  292. $todaySale = 0;
  293. if (!empty($incomeList)) {
  294. foreach ($incomeList as $item) {
  295. $todaySale = bcadd($todaySale, $item['amount'], 2);
  296. }
  297. }
  298. $shopList[$key]['balance'] = $balance;
  299. $shopList[$key]['todaySale'] = $todaySale;
  300. $count = CustomClass::getCount(['ownMainId' => $mainId]);
  301. $count = $count > 0 ? $count : 0;
  302. $shopList[$key]['shopNum'] = $count;
  303. if ($hasSale == 1 && $todaySale <= 0) {
  304. unset($shopList[$key]);
  305. }
  306. $totalSale = bcadd($totalSale, $todaySale, 2);
  307. $totalSale = floatval($totalSale);
  308. $totalBalance = bcadd($totalBalance, $balance, 2);
  309. $totalBalance = floatval($totalBalance);
  310. $totalShopNum = bcadd($totalShopNum, $count);
  311. }
  312. $shopList = arrayUtil::arraySort($shopList, 'todaySale');
  313. }
  314. util::success(['list' => $shopList, 'totalSale' => $totalSale, 'totalBalance' => $totalBalance, 'totalShopNum' => $totalShopNum]);
  315. }
  316. //切换门店 ssh 20220624
  317. public function actionSwitchGhsShop()
  318. {
  319. $shopId = Yii::$app->request->get('shopId', 0);
  320. $shop = ShopClass::getById($shopId, true);
  321. if (empty($shop)) {
  322. util::fail('没有找到门店');
  323. }
  324. $shopAdmin = $this->shopAdmin;
  325. $admin = $this->admin;
  326. $adminId = $this->adminId ?? 0;
  327. if (getenv('YII_ENV') == 'production') {
  328. if (in_array($adminId, [4]) == false) {
  329. util::fail('没有权限切换,请联系管理员');
  330. }
  331. } else {
  332. if (in_array($adminId, [919]) == false) {
  333. util::fail('没有权限切换,请联系管理员');
  334. }
  335. }
  336. ShopAdminClass::changeShopAddRelate($shopAdmin, $shop, 1);
  337. $admin->currentGhsShopId = $shopId;
  338. $lsShopId = $shop->lsShopId ?? 0;
  339. $admin->currentShopId = $lsShopId;
  340. $admin->save();
  341. util::complete();
  342. }
  343. //散客门店详情 ssh 20211007
  344. public function actionSkCustom()
  345. {
  346. $skCustomId = $this->shop->skCustomId ?? 0;
  347. $custom = [];
  348. if (!empty($skCustomId)) {
  349. $custom = CustomClass::getById($skCustomId);
  350. $avatar = $custom['avatar'] ?? '';
  351. $custom['avatar'] = imgUtil::groupImg($avatar) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  352. }
  353. util::success(['custom' => $custom]);
  354. }
  355. //散客门店设置
  356. public function actionSkCustomSet()
  357. {
  358. $id = Yii::$app->request->get('id', 0);
  359. $custom = CustomClass::getById($id, true);
  360. CustomClass::valid($custom, $this->shopId);
  361. $this->shop->skCustomId = $id;
  362. $this->shop->save();
  363. util::complete('修改成功');
  364. }
  365. //运费模式和运费成本设置 ssh 20211014
  366. public function actionUpdateCgModel()
  367. {
  368. $get = Yii::$app->request->get();
  369. $cgModel = $get['cgModel'] ?? 0;
  370. $cgPerCost = $get['cgPerCost'] ?? 0;
  371. if ($cgModel == 1) {
  372. if (is_numeric($cgPerCost) == false || $cgPerCost < 0) {
  373. util::fail('请输入运费成本');
  374. }
  375. $this->shop->cgPerCost = $cgPerCost;
  376. }
  377. $this->shop->cgModel = $cgModel;
  378. $this->shop->save();
  379. util::complete('修改成功');
  380. }
  381. //地区 ssh 20220606
  382. public function actionRegion()
  383. {
  384. $regionTree = RegionService::tree();
  385. util::success(['region' => $regionTree]);
  386. }
  387. }