ShopController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. <?php
  2. namespace hd\controllers;
  3. use biz\shop\classes\ShopClass;
  4. use biz\sj\services\MerchantExtendService;
  5. use bizGhs\admin\classes\AdminClass;
  6. use bizGhs\custom\classes\CustomClass;
  7. use bizHd\admin\classes\ShopAdminClass;
  8. use bizHd\custom\classes\CustomClass as HdCustomClass;
  9. use bizHd\merchant\services\ShopService;
  10. use bizHd\purchase\classes\PurchaseClass;
  11. use bizHd\saas\services\RegionService;
  12. use bizHd\shop\classes\MainClass;
  13. use common\components\dict;
  14. use common\components\dirUtil;
  15. use common\components\imgUtil;
  16. use common\components\IntraCityExpress;
  17. use common\components\stringUtil;
  18. use common\components\util;
  19. use common\components\wxUtil;
  20. use Yii;
  21. class ShopController extends BaseController
  22. {
  23. public $guestAccess = ['all', 'gathering-img-url', 'check-shop'];
  24. //我的可以进入的门店 ssh 20230424
  25. public function actionMyShop()
  26. {
  27. $staff = $this->shopAdmin;
  28. $shopList = [];
  29. if (isset($staff->super) && $staff->super == 1) {
  30. $sjId = $this->sjId ?? 0;
  31. $shopList = ShopClass::getAllByCondition(['sjId' => $sjId, 'ptStyle' => 1], null, 'id,shopName,merchantName,sjId,mainId,ptStyle', 'id');
  32. }
  33. $adminId = $this->adminId ?? 0;
  34. $staffList = ShopAdminClass::getAllByCondition(['adminId' => $adminId], null, '*', null, true);
  35. $mainIdList = [];
  36. if (!empty($staffList)) {
  37. foreach ($staffList as $staff) {
  38. $isPt = $staff->isPt ?? 0;
  39. $delStatus = $staff->delStatus ?? 0;
  40. //$super = $staff->super ?? 0;
  41. $mainId = $staff->mainId ?? 0;
  42. if ($isPt == 0 && $delStatus == 0) {
  43. $mainIdList[] = $mainId;
  44. }
  45. }
  46. }
  47. if (!empty($mainIdList)) {
  48. $addShopList = ShopClass::getAllByCondition(['mainId' => ['in', $mainIdList], 'ptStyle' => 1], null, 'id,shopName,merchantName,sjId,mainId,ptStyle', 'id');
  49. if (!empty($addShopList)) {
  50. foreach ($addShopList as $addShop) {
  51. $addShopId = $addShop['id'] ?? 0;
  52. if (isset($shopList[$addShopId]) == false) {
  53. $shopList[$addShopId] = $addShop;
  54. }
  55. }
  56. }
  57. }
  58. $list = array_values($shopList);
  59. util::success(['list' => $list]);
  60. }
  61. //是否开启完整功能 ssh 20230226
  62. public function actionChangeOnlyCg()
  63. {
  64. $shop = $this->shop;
  65. $get = Yii::$app->request->get();
  66. $onlyCg = $get['onlyCg'] ?? 1;
  67. $shop->onlyCg = $onlyCg;
  68. $shop->save();
  69. util::complete('操作成功');
  70. }
  71. public function actionCheckShop()
  72. {
  73. $get = Yii::$app->request->get();
  74. $mobile = $get['mobile'] ?? '';
  75. $shop = ShopClass::getByCondition(['mobile' => $mobile], true);
  76. if (!empty($shop)) {
  77. util::success(['has' => 1]);
  78. }
  79. util::success(['has' => 0]);
  80. }
  81. //获取当前门店信息 ssh 2021.3.27
  82. public function actionCurrentShop()
  83. {
  84. util::success($this->shop);
  85. }
  86. //门店列表 ssh 20202.2.29
  87. public function actionList()
  88. {
  89. $where = [];
  90. $where['sjId'] = $this->sjId;
  91. $where['delStatus'] = 0;
  92. $list = \bizHd\shop\classes\ShopClass::getShopList($where);
  93. util::success($list);
  94. }
  95. //更新门店 ssh 2020.2.29
  96. public function actionUpdate()
  97. {
  98. $data = Yii::$app->request->post();
  99. $data['sjId'] = $this->sjId;
  100. $id = isset($data['id']) ? $data['id'] : 0;
  101. unset($data['id']);
  102. $shop = ShopService::getById($id);
  103. ShopService::valid($shop, $this->sjId);
  104. $data['img'] = isset($data['img']) && is_array($data['img']) ? json_encode($data['img']) : '';
  105. $mobile = $data['mobile'];
  106. //手机号具有唯一性,暂时不允许修改 --- 同批发端 app-ghs/controllers/ShopController.php 保持一致
  107. unset($data['mobile']);
  108. ShopClass::updateShop($shop, $data);
  109. //同步更新同城配送微信门店
  110. // 校验有无经纬度数据,没有不更新(说明地图定位功能不可用了)
  111. if ($data['lat'] != '' && $data['long'] != '') {
  112. $main = MainClass::getById($this->mainId, false, false, 'id, wxStoreId');
  113. if ($main['wxStoreId'] == '') {
  114. Yii::error(__CLASS__ . __METHOD__ . ' - 未创建门店,请先创建');
  115. util::complete();
  116. }
  117. $shopName = isset($data['shopName']) ? $data['shopName'] : '';
  118. $sjName = $shop['merchantName'] ?? '';
  119. $storeName = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
  120. $storeData = [
  121. 'keys' => ['wx_store_id' => $main['wxStoreId']],
  122. 'content' => [
  123. 'store_name' => $storeName,
  124. 'address_info' => [
  125. 'province' => $data['province'],
  126. 'city' => $data['city'],
  127. 'area' => $data['dist'],
  128. 'street' => '',
  129. 'house' => $data['floor'] ? $data['address'] . $data['floor'] : $data['address'],
  130. 'lat' => $data['lat'],
  131. 'lng' => $data['long'],
  132. 'phone' => $mobile
  133. ],
  134. //"order_pattern" => 2,
  135. //"service_trans_prefer" => "SFTC" //order_pattern = 2时必填
  136. ]
  137. ];
  138. $result = IntraCityExpress::updateStore($storeData);
  139. if ($result['errcode'] == 0) {
  140. Yii::info("同步更新同城配送微信门店成功");
  141. } else {
  142. Yii::error("同步更新同城配送微信门店失败:" . $result['errmsg']);
  143. }
  144. }
  145. util::complete();
  146. }
  147. public function actionAdd()
  148. {
  149. $data = Yii::$app->request->post();
  150. $data['sjId'] = $this->sjId;
  151. $data['shopId'] = $this->shopId;
  152. if (isset($data['shopName']) == false || empty($data['shopName'])) {
  153. util::fail('请填写门店名称');
  154. }
  155. $sj = $this->sj;
  156. $sjName = $sj->name ?? '';
  157. $data['merchantName'] = $sjName;
  158. $ptStyle = $sj->style ?? dict::getDict('ptStyle', 'hd');
  159. $data['ptStyle'] = $ptStyle;
  160. $connection = Yii::$app->db;
  161. $transaction = $connection->beginTransaction();
  162. try {
  163. $mobile = $data['mobile'] ?? 0;
  164. if (stringUtil::isMobile($mobile) == false) {
  165. util::fail('请填写正确手机号');
  166. }
  167. $adminInfo = ['name' => $mobile, 'mobile' => $mobile];
  168. $fromApp = dict::getDict('userSourceGetId', 'app', 'id');
  169. $admin = AdminClass::replaceAdmin($adminInfo, $fromApp);
  170. $adminId = $admin['id'] ?? 0;
  171. $data['adminId'] = $adminId;
  172. \bizHd\shop\classes\ShopClass::addMainShop($data, $this->shop, $this->sj);
  173. $transaction->commit();
  174. util::complete();
  175. } catch (\Exception $exception) {
  176. $transaction->rollBack();
  177. util::fail();
  178. }
  179. }
  180. //切换门店 lqh 2021.1.31
  181. public function actionToggleShop()
  182. {
  183. $adminId = $this->adminId;
  184. $mainId = $this->mainId;
  185. //多处要同步修改,关键词 no_allow_toggle_shop
  186. if (getenv('YII_ENV') == 'production') {
  187. //安海恋善不允许切换门店
  188. if (in_array($mainId, [44759, 56314, 56315, 56452])) {
  189. if (in_array($adminId, [54630, 54723])) {
  190. util::fail('专用账号,不允许切换门店');
  191. }
  192. }
  193. //惠雅南城不允许切换门店
  194. if ($mainId == 52 && $adminId == 3539) {
  195. util::fail('收银专用账号,不允许切换门店');
  196. }
  197. //惠雅莞城不允许切换门店
  198. if ($mainId == 1459 && $adminId == 2812) {
  199. util::fail('收银专用账号,不允许切换门店');
  200. }
  201. //淘花里中山店
  202. if ($mainId == 16948 && $adminId == 17908) {
  203. util::fail('收银专用账号,不允许切换门店');
  204. }
  205. //淘花里小榄店
  206. if ($mainId == 72057 && $adminId == 69792) {
  207. util::fail('收银专用账号,不允许切换门店');
  208. }
  209. }
  210. $shopAdmin = $this->shopAdmin;
  211. $switchShop = \biz\shop\classes\ShopAdminClass::hasSwitchShopRight($shopAdmin);
  212. if ($switchShop == 0) {
  213. util::fail('不能切换门店');
  214. }
  215. $newShopId = Yii::$app->request->post('shopId', 0);
  216. $newShop = ShopClass::getById($newShopId, true);
  217. if (empty($newShop)) {
  218. util::fail('没有找到门店30');
  219. }
  220. $res = \bizHd\admin\classes\AdminClass::toggleShop($this->admin, $newShop, $this->sjId, $this->shopAdmin);
  221. util::success($res);
  222. }
  223. //网页端使用,下载微信和支付宝收款码 ssh 20250821
  224. public function actionGatheringCode()
  225. {
  226. $id = Yii::$app->request->get('id', 0);
  227. $shop = ShopService::getById($id, true);
  228. if (empty($shop)) {
  229. util::fail('没有找到门店31');
  230. }
  231. ShopClass::valid($shop->attributes, $this->sjId);
  232. $imgUrl = ShopClass::generateGatheringCode($this->sjId, $id);
  233. $shopName = $shop->shopName ?? '';
  234. $file = fopen($imgUrl, "rb");
  235. Header("Content-type: application/octet-stream ");
  236. Header("Accept-Ranges: bytes ");
  237. Header("Content-Disposition: attachment;filename={$shopName}收款码.jpg");
  238. $contents = "";
  239. while (!feof($file)) {
  240. $contents .= fread($file, 8192);
  241. }
  242. echo $contents;
  243. fclose($file);
  244. }
  245. //小程序端使用,下载收款码 ssh 20250821
  246. public function actionMiniGatheringCode()
  247. {
  248. $shop = $this->shop;
  249. $shopId = $shop->id;
  250. $imgUrl = ShopClass::generateGatheringCode($this->sjId, $shopId);
  251. util::success(['imgUrl' => $imgUrl]);
  252. }
  253. //获取二维码
  254. public function actionGetGatheringCodeUrl()
  255. {
  256. $id = Yii::$app->request->get('id', 0);
  257. $shop = ShopService::getById($id, true);
  258. if (empty($shop)) {
  259. util::fail('没有找到门店32');
  260. }
  261. ShopClass::valid($shop->attributes, $this->sjId);
  262. $imgUrl = Yii::$app->params['hdHost'] . '/auth/gathering-img-url?id=' . $id;
  263. util::success(['url' => $imgUrl]);
  264. }
  265. public function actionGatheringImgUrl()
  266. {
  267. $id = Yii::$app->request->get('id', 0);
  268. $shop = ShopService::getById($id, true);
  269. if (empty($shop)) {
  270. util::fail('没有找到门店33');
  271. }
  272. $sjId = $shop->sjId;
  273. $imgUrl = ShopClass::generateGatheringCode($sjId, $id);
  274. $fileResource = file_get_contents($imgUrl);
  275. header('Content-type: image/jpeg');
  276. echo $fileResource;
  277. }
  278. //获取小程序的收款码 ssh
  279. public function actionGatheringMiniCode()
  280. {
  281. $id = Yii::$app->request->get('id', 0);
  282. $extend = MerchantExtendService::getBySjId($this->sjId);
  283. if ($extend['miniAuth'] == 0) {
  284. util::fail('您的小程序还没有授权');
  285. }
  286. $shop = ShopService::getById($id);
  287. ShopService::valid($shop, $this->sjId);
  288. $applyMemberCode = wxUtil::generateGatheringMiniCode($this->sj->attributes, $id);
  289. $file = dirUtil::getImgDir() . $applyMemberCode;
  290. if (file_exists($file) == false) {
  291. util::fail('没有找到注册会员码');
  292. }
  293. $fileName = "门店({$id})小程序收款码.jpg";
  294. $contentType = 'image/jpeg';
  295. header("Cache-control: private");
  296. header("Content-type: $contentType"); //设置要下载的文件类型
  297. header("Content-Length:" . filesize($file)); //设置要下载文件的文件大小
  298. header("Content-Disposition: attachment; filename=" . urldecode($fileName)); //设置要下载文件的文件名
  299. readfile($file);
  300. }
  301. //下载会员注册码 ssh 2020.2.29
  302. public function actionApplyMemberCode()
  303. {
  304. $id = Yii::$app->request->get('id', 0);
  305. $extend = MerchantExtendService::getBySjId($this->sjId);
  306. if ($extend['miniAuth'] == 0) {
  307. util::fail('您的小程序还没有授权');
  308. }
  309. $shop = ShopService::getById($id);
  310. ShopService::valid($shop, $this->sjId);
  311. $applyMemberCode = wxUtil::generateMemberCode($this->sj->attributes, $id);
  312. $file = dirUtil::getImgDir() . $applyMemberCode;
  313. if (file_exists($file) == false) {
  314. util::fail('没有找到注册会员码');
  315. }
  316. $fileName = "注册会员码{$id}.jpg";
  317. $contentType = 'image/jpeg';
  318. header("Cache-control: private");
  319. header("Content-type: $contentType"); //设置要下载的文件类型
  320. header("Content-Length:" . filesize($file)); //设置要下载文件的文件大小
  321. header("Content-Disposition: attachment; filename=" . urldecode($fileName)); //设置要下载文件的文件名
  322. readfile($file);
  323. }
  324. //删除门店 ssh 2020.3.1
  325. public function actionDelete()
  326. {
  327. util::fail('禁止操作');
  328. $id = Yii::$app->request->get('id', 0);
  329. util::fail('功能开发中');
  330. $shop = ShopService::getById($id);
  331. ShopService::valid($shop, $this->sjId);
  332. ShopService::deleteShop($shop);
  333. util::complete();
  334. }
  335. //获取门店详情 ssh 2021.4.23
  336. public function actionDetail()
  337. {
  338. $shopId = Yii::$app->request->get('shopId');
  339. if (empty($shopId)) {
  340. $shopId = $this->shopId;
  341. }
  342. $shop = ShopClass::getShopInfo($shopId);
  343. $shop['hasMap'] = dict::getDict('hasMap'); //添加 hasMap 属性(字典中的 hasMap)
  344. util::success(['info' => $shop]);
  345. }
  346. //提现信息更新 ssh 2021.3.23
  347. public function actionCashUpdate()
  348. {
  349. $get = Yii::$app->request->get();
  350. $cashAccount = $get['cashAccount'] ?? '';
  351. $cashName = $get['cashName'] ?? '';
  352. $cashBank = $get['cashBank'] ?? '';
  353. if (empty($cashName)) {
  354. util::fail('请填写姓名');
  355. }
  356. if (empty($cashAccount)) {
  357. util::fail('请填写银行卡号');
  358. }
  359. if (empty($cashBank)) {
  360. util::fail('请输入开户行');
  361. }
  362. $shopAdmin = $this->shopAdmin;
  363. $shopId = $shopAdmin->shopId ?? 0;
  364. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  365. util::fail('超管才能操作');
  366. }
  367. ShopClass::updateById($shopId, ['cashAccount' => $cashAccount, 'cashName' => $cashName, 'cashBank' => $cashBank]);
  368. $shop = $this->shop;
  369. $sjName = $shop->merchantName ?? '';
  370. $shopName = $shop->shopName ?? '';
  371. //noticeUtil::push("{$sjName}-{$shopName} 修改提现账号,{$cashName} {$cashAccount} {$cashBank}", '15280215347');
  372. util::complete();
  373. }
  374. //获取所有门店lqh 2021.1.31
  375. public function actionAll()
  376. {
  377. $res = [];
  378. $shopAdmin = isset($this->shopAdmin) && !empty($this->shopAdmin) ? $this->shopAdmin->attributes : [];
  379. if (isset($shopAdmin['super']) && $shopAdmin['super'] == 1) {
  380. $res = ShopClass::getAllShop($this->sjId);
  381. }
  382. util::success($res);
  383. }
  384. //获取采购时的支付方式 ssh 20211004
  385. public function actionGetCgPayWay()
  386. {
  387. $get = Yii::$app->request->get();
  388. $id = $get['id'] ?? 0;
  389. $cg = PurchaseClass::getById($id, true);
  390. PurchaseClass::valid($cg, $this->shopId);
  391. $customId = $cg->customId ?? 0;
  392. $custom = CustomClass::getById($customId);
  393. $hasDebtPay = $custom['debt'] ?? 0;
  394. //预订单不能欠款
  395. if ($cg->book == 1) {
  396. $hasDebtPay = 0;
  397. }
  398. $current = time();
  399. $cgDeadTime = strtotime($cg->deadline);
  400. //让客户在失效前$aheadTime秒要支付,这样回调通知时订单才不会过期状态
  401. $aheadTime = dict::getDict('order_online_pay_has_ahead_time');
  402. $count = $cgDeadTime - $current - $aheadTime;
  403. $data = [];
  404. $data['balance'] = $this->shop->balance ?? 0;
  405. $data['count'] = $count;
  406. $data['hasDebtPay'] = $hasDebtPay;
  407. util::success($data);
  408. }
  409. //地区 ssh 20220606
  410. public function actionRegion()
  411. {
  412. $regionTree = RegionService::tree();
  413. util::success(['region' => $regionTree]);
  414. }
  415. //散客门店详情 ssh 20211007
  416. public function actionKjCustom()
  417. {
  418. $defaultCustomId = $this->shop->defaultCustomId ?? 0;
  419. $custom = [];
  420. if (!empty($defaultCustomId)) {
  421. $custom = HdCustomClass::getById($defaultCustomId);
  422. $avatar = $custom['avatar'] ?? '';
  423. $custom['avatar'] = imgUtil::groupImg($avatar) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  424. }
  425. util::success(['custom' => $custom]);
  426. }
  427. //散客门店设置
  428. public function actionKjCustomSet()
  429. {
  430. $id = Yii::$app->request->get('id', 0);
  431. $custom = HdCustomClass::getById($id, true);
  432. if (empty($custom)) {
  433. util::fail('没有找到客户');
  434. }
  435. if ($custom->shopId != $this->shopId) {
  436. util::fail('客户不属于当前门店');
  437. }
  438. $shop = $this->shop;
  439. $shop->defaultCustomId = $id;
  440. $shop->save();
  441. util::complete('修改成功');
  442. }
  443. public function actionUpdateShopAvatar()
  444. {
  445. $data = Yii::$app->request->post();
  446. $id = isset($data['id']) ? $data['id'] : 0;
  447. $shop = ShopClass::getById($id, true, 'id, sjId, avatar');
  448. ShopService::valid($shop, $this->sjId);
  449. if(isset($data['avatar']) && $data['avatar']!='') {
  450. $shop->avatar = $data['avatar'];
  451. $re = $shop->save();
  452. if ($re) {
  453. \bizGhs\custom\classes\CustomClass::updateByCondition(['shopId'=>$id], [
  454. 'avatar'=>$data['avatar']
  455. ]);
  456. util::complete('头像修改完成');
  457. }
  458. util::fail('头像修改失败');
  459. }
  460. util::fail('头像参数出错');
  461. }
  462. }