ShopController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. $shopExt = MainClass::getById($this->mainId, false, false, 'id, wxStoreId');
  113. if ($shopExt['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' => $shopExt['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. $shopAdmin = $this->shopAdmin;
  203. $switchShop = \biz\shop\classes\ShopAdminClass::hasSwitchShopRight($shopAdmin);
  204. if ($switchShop == 0) {
  205. util::fail('不能切换门店');
  206. }
  207. $newShopId = Yii::$app->request->post('shopId', 0);
  208. $newShop = ShopClass::getById($newShopId, true);
  209. if (empty($newShop)) {
  210. util::fail('没有找到门店30');
  211. }
  212. $res = \bizHd\admin\classes\AdminClass::toggleShop($this->admin, $newShop, $this->sjId, $this->shopAdmin);
  213. util::success($res);
  214. }
  215. //网页端使用,下载微信和支付宝收款码 ssh 20250821
  216. public function actionGatheringCode()
  217. {
  218. $id = Yii::$app->request->get('id', 0);
  219. $shop = ShopService::getById($id, true);
  220. if (empty($shop)) {
  221. util::fail('没有找到门店31');
  222. }
  223. ShopClass::valid($shop->attributes, $this->sjId);
  224. $imgUrl = ShopClass::generateGatheringCode($this->sjId, $id);
  225. $shopName = $shop->shopName ?? '';
  226. $file = fopen($imgUrl, "rb");
  227. Header("Content-type: application/octet-stream ");
  228. Header("Accept-Ranges: bytes ");
  229. Header("Content-Disposition: attachment;filename={$shopName}收款码.jpg");
  230. $contents = "";
  231. while (!feof($file)) {
  232. $contents .= fread($file, 8192);
  233. }
  234. echo $contents;
  235. fclose($file);
  236. }
  237. //小程序端使用,下载收款码 ssh 20250821
  238. public function actionMiniGatheringCode()
  239. {
  240. $shop = $this->shop;
  241. $shopId = $shop->id;
  242. ShopClass::valid($shop->attributes, $this->sjId);
  243. $imgUrl = ShopClass::generateGatheringCode($this->sjId, $shopId);
  244. util::success(['imgUrl' => $imgUrl]);
  245. }
  246. //获取二维码
  247. public function actionGetGatheringCodeUrl()
  248. {
  249. $id = Yii::$app->request->get('id', 0);
  250. $shop = ShopService::getById($id, true);
  251. if (empty($shop)) {
  252. util::fail('没有找到门店32');
  253. }
  254. ShopClass::valid($shop->attributes, $this->sjId);
  255. $imgUrl = Yii::$app->params['hdHost'] . '/auth/gathering-img-url?id=' . $id;
  256. util::success(['url' => $imgUrl]);
  257. }
  258. public function actionGatheringImgUrl()
  259. {
  260. $id = Yii::$app->request->get('id', 0);
  261. $shop = ShopService::getById($id, true);
  262. if (empty($shop)) {
  263. util::fail('没有找到门店33');
  264. }
  265. $sjId = $shop->sjId;
  266. $imgUrl = ShopClass::generateGatheringCode($sjId, $id);
  267. $fileResource = file_get_contents($imgUrl);
  268. header('Content-type: image/jpeg');
  269. echo $fileResource;
  270. }
  271. //获取小程序的收款码 ssh
  272. public function actionGatheringMiniCode()
  273. {
  274. $id = Yii::$app->request->get('id', 0);
  275. $extend = MerchantExtendService::getBySjId($this->sjId);
  276. if ($extend['miniAuth'] == 0) {
  277. util::fail('您的小程序还没有授权');
  278. }
  279. $shop = ShopService::getById($id);
  280. ShopService::valid($shop, $this->sjId);
  281. $applyMemberCode = wxUtil::generateGatheringMiniCode($this->sj->attributes, $id);
  282. $file = dirUtil::getImgDir() . $applyMemberCode;
  283. if (file_exists($file) == false) {
  284. util::fail('没有找到注册会员码');
  285. }
  286. $fileName = "门店({$id})小程序收款码.jpg";
  287. $contentType = 'image/jpeg';
  288. header("Cache-control: private");
  289. header("Content-type: $contentType"); //设置要下载的文件类型
  290. header("Content-Length:" . filesize($file)); //设置要下载文件的文件大小
  291. header("Content-Disposition: attachment; filename=" . urldecode($fileName)); //设置要下载文件的文件名
  292. readfile($file);
  293. }
  294. //下载会员注册码 ssh 2020.2.29
  295. public function actionApplyMemberCode()
  296. {
  297. $id = Yii::$app->request->get('id', 0);
  298. $extend = MerchantExtendService::getBySjId($this->sjId);
  299. if ($extend['miniAuth'] == 0) {
  300. util::fail('您的小程序还没有授权');
  301. }
  302. $shop = ShopService::getById($id);
  303. ShopService::valid($shop, $this->sjId);
  304. $applyMemberCode = wxUtil::generateMemberCode($this->sj->attributes, $id);
  305. $file = dirUtil::getImgDir() . $applyMemberCode;
  306. if (file_exists($file) == false) {
  307. util::fail('没有找到注册会员码');
  308. }
  309. $fileName = "注册会员码{$id}.jpg";
  310. $contentType = 'image/jpeg';
  311. header("Cache-control: private");
  312. header("Content-type: $contentType"); //设置要下载的文件类型
  313. header("Content-Length:" . filesize($file)); //设置要下载文件的文件大小
  314. header("Content-Disposition: attachment; filename=" . urldecode($fileName)); //设置要下载文件的文件名
  315. readfile($file);
  316. }
  317. //删除门店 ssh 2020.3.1
  318. public function actionDelete()
  319. {
  320. util::fail('禁止操作');
  321. $id = Yii::$app->request->get('id', 0);
  322. util::fail('功能开发中');
  323. $shop = ShopService::getById($id);
  324. ShopService::valid($shop, $this->sjId);
  325. ShopService::deleteShop($shop);
  326. util::complete();
  327. }
  328. //获取门店详情 ssh 2021.4.23
  329. public function actionDetail()
  330. {
  331. $shopId = Yii::$app->request->get('shopId');
  332. if (empty($shopId)) {
  333. $shopId = $this->shopId;
  334. }
  335. $shop = ShopClass::getShopInfo($shopId);
  336. $shop['hasMap'] = dict::getDict('hasMap'); //添加 hasMap 属性(字典中的 hasMap)
  337. util::success(['info' => $shop]);
  338. }
  339. //提现信息更新 ssh 2021.3.23
  340. public function actionCashUpdate()
  341. {
  342. $get = Yii::$app->request->get();
  343. $cashAccount = $get['cashAccount'] ?? '';
  344. $cashName = $get['cashName'] ?? '';
  345. $cashBank = $get['cashBank'] ?? '';
  346. if (empty($cashName)) {
  347. util::fail('请填写姓名');
  348. }
  349. if (empty($cashAccount)) {
  350. util::fail('请填写银行卡号');
  351. }
  352. if (empty($cashBank)) {
  353. util::fail('请输入开户行');
  354. }
  355. $shopAdmin = $this->shopAdmin;
  356. $shopId = $shopAdmin->shopId ?? 0;
  357. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  358. util::fail('超管才能操作');
  359. }
  360. ShopClass::updateById($shopId, ['cashAccount' => $cashAccount, 'cashName' => $cashName, 'cashBank' => $cashBank]);
  361. $shop = $this->shop;
  362. $sjName = $shop->merchantName ?? '';
  363. $shopName = $shop->shopName ?? '';
  364. //noticeUtil::push("{$sjName}-{$shopName} 修改提现账号,{$cashName} {$cashAccount} {$cashBank}", '15280215347');
  365. util::complete();
  366. }
  367. //获取所有门店lqh 2021.1.31
  368. public function actionAll()
  369. {
  370. $res = [];
  371. $shopAdmin = isset($this->shopAdmin) && !empty($this->shopAdmin) ? $this->shopAdmin->attributes : [];
  372. if (isset($shopAdmin['super']) && $shopAdmin['super'] == 1) {
  373. $res = ShopClass::getAllShop($this->sjId);
  374. }
  375. util::success($res);
  376. }
  377. //获取采购时的支付方式 ssh 20211004
  378. public function actionGetCgPayWay()
  379. {
  380. $get = Yii::$app->request->get();
  381. $id = $get['id'] ?? 0;
  382. $cg = PurchaseClass::getById($id, true);
  383. PurchaseClass::valid($cg, $this->shopId);
  384. $customId = $cg->customId ?? 0;
  385. $custom = CustomClass::getById($customId);
  386. $hasDebtPay = $custom['debt'] ?? 0;
  387. //预订单不能欠款
  388. if ($cg->book == 1) {
  389. $hasDebtPay = 0;
  390. }
  391. $current = time();
  392. $cgDeadTime = strtotime($cg->deadline);
  393. //让客户在失效前$aheadTime秒要支付,这样回调通知时订单才不会过期状态
  394. $aheadTime = dict::getDict('order_online_pay_has_ahead_time');
  395. $count = $cgDeadTime - $current - $aheadTime;
  396. $data = [];
  397. $data['balance'] = $this->shop->balance ?? 0;
  398. $data['count'] = $count;
  399. $data['hasDebtPay'] = $hasDebtPay;
  400. util::success($data);
  401. }
  402. //地区 ssh 20220606
  403. public function actionRegion()
  404. {
  405. $regionTree = RegionService::tree();
  406. util::success(['region' => $regionTree]);
  407. }
  408. //散客门店详情 ssh 20211007
  409. public function actionKjCustom()
  410. {
  411. $defaultCustomId = $this->shop->defaultCustomId ?? 0;
  412. $custom = [];
  413. if (!empty($defaultCustomId)) {
  414. $custom = HdCustomClass::getById($defaultCustomId);
  415. $avatar = $custom['avatar'] ?? '';
  416. $custom['avatar'] = imgUtil::groupImg($avatar) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  417. }
  418. util::success(['custom' => $custom]);
  419. }
  420. //散客门店设置
  421. public function actionKjCustomSet()
  422. {
  423. $id = Yii::$app->request->get('id', 0);
  424. $custom = HdCustomClass::getById($id, true);
  425. if (empty($custom)) {
  426. util::fail('没有找到客户');
  427. }
  428. if ($custom->shopId != $this->shopId) {
  429. util::fail('客户不属于当前门店');
  430. }
  431. $shop = $this->shop;
  432. $shop->defaultCustomId = $id;
  433. $shop->save();
  434. util::complete('修改成功');
  435. }
  436. }