ShopController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. ShopClass::updateShop($shop, $data);
  106. //同步更新同城配送微信门店
  107. // 校验有无经纬度数据,没有不更新(说明地图定位功能不可用了)
  108. if($data['lat'] != '' && $data['long'] != '') {
  109. $shopExt = MainClass::getById($this->mainId, false, false, 'id, wxStoreId');
  110. if ($shopExt['wxStoreId'] == '') {
  111. Yii::error(__CLASS__ . __METHOD__ .' - 未创建门店,请先创建');
  112. util::complete();
  113. }
  114. $shopName = isset($data['shopName']) ? $data['shopName'] : '';
  115. $sjName = $shop['merchantName'] ?? '';
  116. $storeName = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
  117. $storeData = [
  118. 'keys' => ['wx_store_id' => $shopExt['wxStoreId']],
  119. 'content' => [
  120. 'store_name' => $storeName,
  121. 'address_info' => [
  122. 'province' => $data['province'],
  123. 'city' => $data['city'],
  124. 'area' => $data['dist'],
  125. 'street' => '',
  126. 'house' => $data['floor'] ? $data['address'] . $data['floor'] : $data['address'],
  127. 'lat' => $data['lat'],
  128. 'lng' => $data['long'],
  129. 'phone' => $data['mobile']
  130. ],
  131. //"order_pattern" => 2,
  132. //"service_trans_prefer" => "SFTC" //order_pattern = 2时必填
  133. ]
  134. ];
  135. $result = IntraCityExpress::updateStore($storeData);
  136. if ($result['errcode'] == 0) {
  137. Yii::info("同步更新同城配送微信门店成功");
  138. } else {
  139. Yii::error("同步更新同城配送微信门店失败:" . $result['errmsg']);
  140. }
  141. }
  142. util::complete();
  143. }
  144. public function actionAdd()
  145. {
  146. $data = Yii::$app->request->post();
  147. $data['sjId'] = $this->sjId;
  148. $data['shopId'] = $this->shopId;
  149. if (isset($data['shopName']) == false || empty($data['shopName'])) {
  150. util::fail('请填写门店名称');
  151. }
  152. $sj = $this->sj;
  153. $sjName = $sj->name ?? '';
  154. $data['merchantName'] = $sjName;
  155. $ptStyle = $sj->style ?? dict::getDict('ptStyle', 'hd');
  156. $data['ptStyle'] = $ptStyle;
  157. $connection = Yii::$app->db;
  158. $transaction = $connection->beginTransaction();
  159. try {
  160. $mobile = $data['mobile'] ?? 0;
  161. if (stringUtil::isMobile($mobile) == false) {
  162. util::fail('请填写正确手机号');
  163. }
  164. $adminInfo = ['name' => $mobile, 'mobile' => $mobile];
  165. $fromApp = dict::getDict('userSourceGetId', 'app', 'id');
  166. $admin = AdminClass::replaceAdmin($adminInfo, $fromApp);
  167. $adminId = $admin['id'] ?? 0;
  168. $data['adminId'] = $adminId;
  169. \bizHd\shop\classes\ShopClass::addMainShop($data, $this->shop, $this->sj);
  170. $transaction->commit();
  171. util::complete();
  172. } catch (\Exception $exception) {
  173. $transaction->rollBack();
  174. util::fail();
  175. }
  176. }
  177. //切换门店 lqh 2021.1.31
  178. public function actionToggleShop()
  179. {
  180. $adminId = $this->adminId;
  181. $mainId = $this->mainId;
  182. //多处要同步修改,关键词 no_allow_toggle_shop
  183. if (getenv('YII_ENV') == 'production') {
  184. //安海恋善不允许切换门店
  185. if (in_array($mainId, [44759, 56314, 56315, 56452])) {
  186. if (in_array($adminId, [54630, 54723])) {
  187. util::fail('专用账号,不允许切换门店');
  188. }
  189. }
  190. //惠雅南城不允许切换门店
  191. if ($mainId == 52 && $adminId == 3539) {
  192. util::fail('收银专用账号,不允许切换门店');
  193. }
  194. //惠雅莞城不允许切换门店
  195. if ($mainId == 1459 && $adminId == 2812) {
  196. util::fail('收银专用账号,不允许切换门店');
  197. }
  198. }
  199. $shopAdmin = $this->shopAdmin;
  200. $switchShop = \biz\shop\classes\ShopAdminClass::hasSwitchShopRight($shopAdmin);
  201. if ($switchShop == 0) {
  202. util::fail('不能切换门店');
  203. }
  204. $newShopId = Yii::$app->request->post('shopId', 0);
  205. $newShop = ShopClass::getById($newShopId, true);
  206. if (empty($newShop)) {
  207. util::fail('没有找到门店30');
  208. }
  209. $res = \bizHd\admin\classes\AdminClass::toggleShop($this->admin, $newShop, $this->sjId, $this->shopAdmin);
  210. util::success($res);
  211. }
  212. //网页端使用,下载微信和支付宝收款码 ssh 20250821
  213. public function actionGatheringCode()
  214. {
  215. $id = Yii::$app->request->get('id', 0);
  216. $shop = ShopService::getById($id, true);
  217. if (empty($shop)) {
  218. util::fail('没有找到门店31');
  219. }
  220. ShopClass::valid($shop->attributes, $this->sjId);
  221. $imgUrl = ShopClass::generateGatheringCode($this->sjId, $id);
  222. $shopName = $shop->shopName ?? '';
  223. $file = fopen($imgUrl, "rb");
  224. Header("Content-type: application/octet-stream ");
  225. Header("Accept-Ranges: bytes ");
  226. Header("Content-Disposition: attachment;filename={$shopName}收款码.jpg");
  227. $contents = "";
  228. while (!feof($file)) {
  229. $contents .= fread($file, 8192);
  230. }
  231. echo $contents;
  232. fclose($file);
  233. }
  234. //小程序端使用,下载收款码 ssh 20250821
  235. public function actionMiniGatheringCode()
  236. {
  237. $shop = $this->shop;
  238. $shopId = $shop->id;
  239. ShopClass::valid($shop->attributes, $this->sjId);
  240. $imgUrl = ShopClass::generateGatheringCode($this->sjId, $shopId);
  241. util::success(['imgUrl' => $imgUrl]);
  242. }
  243. //获取二维码
  244. public function actionGetGatheringCodeUrl()
  245. {
  246. $id = Yii::$app->request->get('id', 0);
  247. $shop = ShopService::getById($id, true);
  248. if (empty($shop)) {
  249. util::fail('没有找到门店32');
  250. }
  251. ShopClass::valid($shop->attributes, $this->sjId);
  252. $imgUrl = Yii::$app->params['hdHost'] . '/auth/gathering-img-url?id=' . $id;
  253. util::success(['url' => $imgUrl]);
  254. }
  255. public function actionGatheringImgUrl()
  256. {
  257. $id = Yii::$app->request->get('id', 0);
  258. $shop = ShopService::getById($id, true);
  259. if (empty($shop)) {
  260. util::fail('没有找到门店33');
  261. }
  262. $sjId = $shop->sjId;
  263. $imgUrl = ShopClass::generateGatheringCode($sjId, $id);
  264. $fileResource = file_get_contents($imgUrl);
  265. header('Content-type: image/jpeg');
  266. echo $fileResource;
  267. }
  268. //获取小程序的收款码 ssh
  269. public function actionGatheringMiniCode()
  270. {
  271. $id = Yii::$app->request->get('id', 0);
  272. $extend = MerchantExtendService::getBySjId($this->sjId);
  273. if ($extend['miniAuth'] == 0) {
  274. util::fail('您的小程序还没有授权');
  275. }
  276. $shop = ShopService::getById($id);
  277. ShopService::valid($shop, $this->sjId);
  278. $applyMemberCode = wxUtil::generateGatheringMiniCode($this->sj->attributes, $id);
  279. $file = dirUtil::getImgDir() . $applyMemberCode;
  280. if (file_exists($file) == false) {
  281. util::fail('没有找到注册会员码');
  282. }
  283. $fileName = "门店({$id})小程序收款码.jpg";
  284. $contentType = 'image/jpeg';
  285. header("Cache-control: private");
  286. header("Content-type: $contentType"); //设置要下载的文件类型
  287. header("Content-Length:" . filesize($file)); //设置要下载文件的文件大小
  288. header("Content-Disposition: attachment; filename=" . urldecode($fileName)); //设置要下载文件的文件名
  289. readfile($file);
  290. }
  291. //下载会员注册码 ssh 2020.2.29
  292. public function actionApplyMemberCode()
  293. {
  294. $id = Yii::$app->request->get('id', 0);
  295. $extend = MerchantExtendService::getBySjId($this->sjId);
  296. if ($extend['miniAuth'] == 0) {
  297. util::fail('您的小程序还没有授权');
  298. }
  299. $shop = ShopService::getById($id);
  300. ShopService::valid($shop, $this->sjId);
  301. $applyMemberCode = wxUtil::generateMemberCode($this->sj->attributes, $id);
  302. $file = dirUtil::getImgDir() . $applyMemberCode;
  303. if (file_exists($file) == false) {
  304. util::fail('没有找到注册会员码');
  305. }
  306. $fileName = "注册会员码{$id}.jpg";
  307. $contentType = 'image/jpeg';
  308. header("Cache-control: private");
  309. header("Content-type: $contentType"); //设置要下载的文件类型
  310. header("Content-Length:" . filesize($file)); //设置要下载文件的文件大小
  311. header("Content-Disposition: attachment; filename=" . urldecode($fileName)); //设置要下载文件的文件名
  312. readfile($file);
  313. }
  314. //删除门店 ssh 2020.3.1
  315. public function actionDelete()
  316. {
  317. util::fail('禁止操作');
  318. $id = Yii::$app->request->get('id', 0);
  319. util::fail('功能开发中');
  320. $shop = ShopService::getById($id);
  321. ShopService::valid($shop, $this->sjId);
  322. ShopService::deleteShop($shop);
  323. util::complete();
  324. }
  325. //获取门店详情 ssh 2021.4.23
  326. public function actionDetail()
  327. {
  328. $shopId = Yii::$app->request->get('shopId');
  329. if (empty($shopId)) {
  330. $shopId = $this->shopId;
  331. }
  332. $shop = ShopClass::getShopInfo($shopId);
  333. $shop['hasMap'] = dict::getDict('hasMap'); //添加 hasMap 属性(字典中的 hasMap)
  334. util::success(['info' => $shop]);
  335. }
  336. //提现信息更新 ssh 2021.3.23
  337. public function actionCashUpdate()
  338. {
  339. $get = Yii::$app->request->get();
  340. $cashAccount = $get['cashAccount'] ?? '';
  341. $cashName = $get['cashName'] ?? '';
  342. $cashBank = $get['cashBank'] ?? '';
  343. if (empty($cashName)) {
  344. util::fail('请填写姓名');
  345. }
  346. if (empty($cashAccount)) {
  347. util::fail('请填写银行卡号');
  348. }
  349. if (empty($cashBank)) {
  350. util::fail('请输入开户行');
  351. }
  352. $shopAdmin = $this->shopAdmin;
  353. $shopId = $shopAdmin->shopId ?? 0;
  354. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  355. util::fail('超管才能操作');
  356. }
  357. ShopClass::updateById($shopId, ['cashAccount' => $cashAccount, 'cashName' => $cashName, 'cashBank' => $cashBank]);
  358. $shop = $this->shop;
  359. $sjName = $shop->merchantName ?? '';
  360. $shopName = $shop->shopName ?? '';
  361. //noticeUtil::push("{$sjName}-{$shopName} 修改提现账号,{$cashName} {$cashAccount} {$cashBank}", '15280215347');
  362. util::complete();
  363. }
  364. //获取所有门店lqh 2021.1.31
  365. public function actionAll()
  366. {
  367. $res = [];
  368. $shopAdmin = isset($this->shopAdmin) && !empty($this->shopAdmin) ? $this->shopAdmin->attributes : [];
  369. if (isset($shopAdmin['super']) && $shopAdmin['super'] == 1) {
  370. $res = ShopClass::getAllShop($this->sjId);
  371. }
  372. util::success($res);
  373. }
  374. //获取采购时的支付方式 ssh 20211004
  375. public function actionGetCgPayWay()
  376. {
  377. $get = Yii::$app->request->get();
  378. $id = $get['id'] ?? 0;
  379. $cg = PurchaseClass::getById($id, true);
  380. PurchaseClass::valid($cg, $this->shopId);
  381. $customId = $cg->customId ?? 0;
  382. $custom = CustomClass::getById($customId);
  383. $hasDebtPay = $custom['debt'] ?? 0;
  384. //预订单不能欠款
  385. if ($cg->book == 1) {
  386. $hasDebtPay = 0;
  387. }
  388. $current = time();
  389. $cgDeadTime = strtotime($cg->deadline);
  390. //让客户在失效前$aheadTime秒要支付,这样回调通知时订单才不会过期状态
  391. $aheadTime = dict::getDict('order_online_pay_has_ahead_time');
  392. $count = $cgDeadTime - $current - $aheadTime;
  393. $data = [];
  394. $data['balance'] = $this->shop->balance ?? 0;
  395. $data['count'] = $count;
  396. $data['hasDebtPay'] = $hasDebtPay;
  397. util::success($data);
  398. }
  399. //地区 ssh 20220606
  400. public function actionRegion()
  401. {
  402. $regionTree = RegionService::tree();
  403. util::success(['region' => $regionTree]);
  404. }
  405. //散客门店详情 ssh 20211007
  406. public function actionKjCustom()
  407. {
  408. $defaultCustomId = $this->shop->defaultCustomId ?? 0;
  409. $custom = [];
  410. if (!empty($defaultCustomId)) {
  411. $custom = HdCustomClass::getById($defaultCustomId);
  412. $avatar = $custom['avatar'] ?? '';
  413. $custom['avatar'] = imgUtil::groupImg($avatar) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  414. }
  415. util::success(['custom' => $custom]);
  416. }
  417. //散客门店设置
  418. public function actionKjCustomSet()
  419. {
  420. $id = Yii::$app->request->get('id', 0);
  421. $custom = HdCustomClass::getById($id, true);
  422. if (empty($custom)) {
  423. util::fail('没有找到客户');
  424. }
  425. if ($custom->shopId != $this->shopId) {
  426. util::fail('客户不属于当前门店');
  427. }
  428. $shop = $this->shop;
  429. $shop->defaultCustomId = $id;
  430. $shop->save();
  431. util::complete('修改成功');
  432. }
  433. }