ShopController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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 common\components\dict;
  13. use common\components\dirUtil;
  14. use common\components\imgUtil;
  15. use common\components\stringUtil;
  16. use common\components\util;
  17. use common\components\wxUtil;
  18. use Yii;
  19. class ShopController extends BaseController
  20. {
  21. public $guestAccess = ['all', 'gathering-img-url', 'check-shop'];
  22. //我的可以进入的门店 ssh 20230424
  23. public function actionMyShop()
  24. {
  25. $staff = $this->shopAdmin;
  26. $shopList = [];
  27. if (isset($staff->super) && $staff->super == 1) {
  28. $sjId = $this->sjId ?? 0;
  29. $shopList = ShopClass::getAllByCondition(['sjId' => $sjId, 'ptStyle' => 1], null, 'id,shopName,merchantName,sjId,mainId,ptStyle', 'id');
  30. }
  31. $adminId = $this->adminId ?? 0;
  32. $staffList = ShopAdminClass::getAllByCondition(['adminId' => $adminId], null, '*', null, true);
  33. $mainIdList = [];
  34. if (!empty($staffList)) {
  35. foreach ($staffList as $staff) {
  36. $isPt = $staff->isPt ?? 0;
  37. $delStatus = $staff->delStatus ?? 0;
  38. //$super = $staff->super ?? 0;
  39. $mainId = $staff->mainId ?? 0;
  40. if ($isPt == 0 && $delStatus == 0) {
  41. $mainIdList[] = $mainId;
  42. }
  43. }
  44. }
  45. if (!empty($mainIdList)) {
  46. $addShopList = ShopClass::getAllByCondition(['mainId' => ['in', $mainIdList], 'ptStyle' => 1], null, 'id,shopName,merchantName,sjId,mainId,ptStyle', 'id');
  47. if (!empty($addShopList)) {
  48. foreach ($addShopList as $addShop) {
  49. $addShopId = $addShop['id'] ?? 0;
  50. if (isset($shopList[$addShopId]) == false) {
  51. $shopList[$addShopId] = $addShop;
  52. }
  53. }
  54. }
  55. }
  56. $list = array_values($shopList);
  57. util::success(['list' => $list]);
  58. }
  59. //是否开启完整功能 ssh 20230226
  60. public function actionChangeOnlyCg()
  61. {
  62. $shop = $this->shop;
  63. $get = Yii::$app->request->get();
  64. $onlyCg = $get['onlyCg'] ?? 1;
  65. $shop->onlyCg = $onlyCg;
  66. $shop->save();
  67. util::complete('操作成功');
  68. }
  69. public function actionCheckShop()
  70. {
  71. $get = Yii::$app->request->get();
  72. $mobile = $get['mobile'] ?? '';
  73. $shop = ShopClass::getByCondition(['mobile' => $mobile], true);
  74. if (!empty($shop)) {
  75. util::success(['has' => 1]);
  76. }
  77. util::success(['has' => 0]);
  78. }
  79. //获取当前门店信息 ssh 2021.3.27
  80. public function actionCurrentShop()
  81. {
  82. util::success($this->shop);
  83. }
  84. //门店列表 ssh 20202.2.29
  85. public function actionList()
  86. {
  87. $where = [];
  88. $where['sjId'] = $this->sjId;
  89. $where['delStatus'] = 0;
  90. $list = \bizHd\shop\classes\ShopClass::getShopList($where);
  91. util::success($list);
  92. }
  93. //更新门店 ssh 2020.2.29
  94. public function actionUpdate()
  95. {
  96. $data = Yii::$app->request->post();
  97. $data['sjId'] = $this->sjId;
  98. $id = isset($data['id']) ? $data['id'] : 0;
  99. unset($data['id']);
  100. $shop = ShopService::getById($id);
  101. ShopService::valid($shop, $this->sjId);
  102. $data['img'] = isset($data['img']) && is_array($data['img']) ? json_encode($data['img']) : '';
  103. ShopClass::updateShop($shop, $data);
  104. util::complete();
  105. }
  106. public function actionAdd()
  107. {
  108. $data = Yii::$app->request->post();
  109. $data['sjId'] = $this->sjId;
  110. $data['shopId'] = $this->shopId;
  111. if (isset($data['shopName']) == false || empty($data['shopName'])) {
  112. util::fail('请填写门店名称');
  113. }
  114. $sj = $this->sj;
  115. $sjName = $sj->name ?? '';
  116. $data['merchantName'] = $sjName;
  117. $ptStyle = $sj->style ?? dict::getDict('ptStyle', 'hd');
  118. $data['ptStyle'] = $ptStyle;
  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. \bizHd\shop\classes\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. //切换门店 lqh 2021.1.31
  140. public function actionToggleShop()
  141. {
  142. $adminId = $this->adminId;
  143. $mainId = $this->mainId;
  144. //多处要同步修改,关键词 no_allow_toggle_shop
  145. if (getenv('YII_ENV') == 'production') {
  146. //安海恋善不允许切换门店
  147. if (in_array($mainId, [44759, 56314, 56315, 56452])) {
  148. if (in_array($adminId, [54630, 54723])) {
  149. util::fail('专用账号,不允许切换门店');
  150. }
  151. }
  152. //惠雅南城不允许切换门店
  153. if ($mainId == 52 && $adminId == 3539) {
  154. util::fail('收银专用账号,不允许切换门店');
  155. }
  156. //惠雅莞城不允许切换门店
  157. if ($mainId == 1459 && $adminId == 2812) {
  158. util::fail('收银专用账号,不允许切换门店');
  159. }
  160. }
  161. $shopAdmin = $this->shopAdmin;
  162. $switchShop = \biz\shop\classes\ShopAdminClass::hasSwitchShopRight($shopAdmin);
  163. if ($switchShop == 0) {
  164. util::fail('不能切换门店');
  165. }
  166. $newShopId = Yii::$app->request->post('shopId', 0);
  167. $newShop = ShopClass::getById($newShopId, true);
  168. if (empty($newShop)) {
  169. util::fail('没有找到门店30');
  170. }
  171. $res = \bizHd\admin\classes\AdminClass::toggleShop($this->admin, $newShop, $this->sjId, $this->shopAdmin);
  172. util::success($res);
  173. }
  174. //网页端使用,下载微信和支付宝收款码 ssh 20250821
  175. public function actionGatheringCode()
  176. {
  177. $id = Yii::$app->request->get('id', 0);
  178. $shop = ShopService::getById($id, true);
  179. if (empty($shop)) {
  180. util::fail('没有找到门店31');
  181. }
  182. ShopClass::valid($shop->attributes, $this->sjId);
  183. $imgUrl = ShopClass::generateGatheringCode($this->sjId, $id);
  184. $shopName = $shop->shopName ?? '';
  185. $file = fopen($imgUrl, "rb");
  186. Header("Content-type: application/octet-stream ");
  187. Header("Accept-Ranges: bytes ");
  188. Header("Content-Disposition: attachment;filename={$shopName}收款码.jpg");
  189. $contents = "";
  190. while (!feof($file)) {
  191. $contents .= fread($file, 8192);
  192. }
  193. echo $contents;
  194. fclose($file);
  195. }
  196. //小程序端使用,下载收款码 ssh 20250821
  197. public function actionMiniGatheringCode()
  198. {
  199. $shop = $this->shop;
  200. $shopId = $shop->id;
  201. ShopClass::valid($shop->attributes, $this->sjId);
  202. $imgUrl = ShopClass::generateGatheringCode($this->sjId, $shopId);
  203. util::success(['imgUrl' => $imgUrl]);
  204. }
  205. //获取二维码
  206. public function actionGetGatheringCodeUrl()
  207. {
  208. $id = Yii::$app->request->get('id', 0);
  209. $shop = ShopService::getById($id, true);
  210. if (empty($shop)) {
  211. util::fail('没有找到门店32');
  212. }
  213. ShopClass::valid($shop->attributes, $this->sjId);
  214. $imgUrl = Yii::$app->params['hdHost'] . '/auth/gathering-img-url?id=' . $id;
  215. util::success(['url' => $imgUrl]);
  216. }
  217. public function actionGatheringImgUrl()
  218. {
  219. $id = Yii::$app->request->get('id', 0);
  220. $shop = ShopService::getById($id, true);
  221. if (empty($shop)) {
  222. util::fail('没有找到门店33');
  223. }
  224. $sjId = $shop->sjId;
  225. $imgUrl = ShopClass::generateGatheringCode($sjId, $id);
  226. $fileResource = file_get_contents($imgUrl);
  227. header('Content-type: image/jpeg');
  228. echo $fileResource;
  229. }
  230. //获取小程序的收款码 ssh
  231. public function actionGatheringMiniCode()
  232. {
  233. $id = Yii::$app->request->get('id', 0);
  234. $extend = MerchantExtendService::getBySjId($this->sjId);
  235. if ($extend['miniAuth'] == 0) {
  236. util::fail('您的小程序还没有授权');
  237. }
  238. $shop = ShopService::getById($id);
  239. ShopService::valid($shop, $this->sjId);
  240. $applyMemberCode = wxUtil::generateGatheringMiniCode($this->sj->attributes, $id);
  241. $file = dirUtil::getImgDir() . $applyMemberCode;
  242. if (file_exists($file) == false) {
  243. util::fail('没有找到注册会员码');
  244. }
  245. $fileName = "门店({$id})小程序收款码.jpg";
  246. $contentType = 'image/jpeg';
  247. header("Cache-control: private");
  248. header("Content-type: $contentType"); //设置要下载的文件类型
  249. header("Content-Length:" . filesize($file)); //设置要下载文件的文件大小
  250. header("Content-Disposition: attachment; filename=" . urldecode($fileName)); //设置要下载文件的文件名
  251. readfile($file);
  252. }
  253. //下载会员注册码 ssh 2020.2.29
  254. public function actionApplyMemberCode()
  255. {
  256. $id = Yii::$app->request->get('id', 0);
  257. $extend = MerchantExtendService::getBySjId($this->sjId);
  258. if ($extend['miniAuth'] == 0) {
  259. util::fail('您的小程序还没有授权');
  260. }
  261. $shop = ShopService::getById($id);
  262. ShopService::valid($shop, $this->sjId);
  263. $applyMemberCode = wxUtil::generateMemberCode($this->sj->attributes, $id);
  264. $file = dirUtil::getImgDir() . $applyMemberCode;
  265. if (file_exists($file) == false) {
  266. util::fail('没有找到注册会员码');
  267. }
  268. $fileName = "注册会员码{$id}.jpg";
  269. $contentType = 'image/jpeg';
  270. header("Cache-control: private");
  271. header("Content-type: $contentType"); //设置要下载的文件类型
  272. header("Content-Length:" . filesize($file)); //设置要下载文件的文件大小
  273. header("Content-Disposition: attachment; filename=" . urldecode($fileName)); //设置要下载文件的文件名
  274. readfile($file);
  275. }
  276. //删除门店 ssh 2020.3.1
  277. public function actionDelete()
  278. {
  279. util::fail('禁止操作');
  280. $id = Yii::$app->request->get('id', 0);
  281. util::fail('功能开发中');
  282. $shop = ShopService::getById($id);
  283. ShopService::valid($shop, $this->sjId);
  284. ShopService::deleteShop($shop);
  285. util::complete();
  286. }
  287. //获取门店详情 ssh 2021.4.23
  288. public function actionDetail()
  289. {
  290. $shopId = Yii::$app->request->get('shopId');
  291. if (empty($shopId)) {
  292. $shopId = $this->shopId;
  293. }
  294. $shop = ShopClass::getShopInfo($shopId);
  295. $shop['hasMap'] = dict::getDict('hasMap'); //添加 hasMap 属性(字典中的 hasMap)
  296. util::success(['info' => $shop]);
  297. }
  298. //提现信息更新 ssh 2021.3.23
  299. public function actionCashUpdate()
  300. {
  301. $get = Yii::$app->request->get();
  302. $cashAccount = $get['cashAccount'] ?? '';
  303. $cashName = $get['cashName'] ?? '';
  304. $cashBank = $get['cashBank'] ?? '';
  305. if (empty($cashName)) {
  306. util::fail('请填写姓名');
  307. }
  308. if (empty($cashAccount)) {
  309. util::fail('请填写银行卡号');
  310. }
  311. if (empty($cashBank)) {
  312. util::fail('请输入开户行');
  313. }
  314. $shopAdmin = $this->shopAdmin;
  315. $shopId = $shopAdmin->shopId ?? 0;
  316. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  317. util::fail('超管才能操作');
  318. }
  319. ShopClass::updateById($shopId, ['cashAccount' => $cashAccount, 'cashName' => $cashName, 'cashBank' => $cashBank]);
  320. $shop = $this->shop;
  321. $sjName = $shop->merchantName ?? '';
  322. $shopName = $shop->shopName ?? '';
  323. //noticeUtil::push("{$sjName}-{$shopName} 修改提现账号,{$cashName} {$cashAccount} {$cashBank}", '15280215347');
  324. util::complete();
  325. }
  326. //获取所有门店lqh 2021.1.31
  327. public function actionAll()
  328. {
  329. $res = [];
  330. $shopAdmin = isset($this->shopAdmin) && !empty($this->shopAdmin) ? $this->shopAdmin->attributes : [];
  331. if (isset($shopAdmin['super']) && $shopAdmin['super'] == 1) {
  332. $res = ShopClass::getAllShop($this->sjId);
  333. }
  334. util::success($res);
  335. }
  336. //获取采购时的支付方式 ssh 20211004
  337. public function actionGetCgPayWay()
  338. {
  339. $get = Yii::$app->request->get();
  340. $id = $get['id'] ?? 0;
  341. $cg = PurchaseClass::getById($id, true);
  342. PurchaseClass::valid($cg, $this->shopId);
  343. $customId = $cg->customId ?? 0;
  344. $custom = CustomClass::getById($customId);
  345. $hasDebtPay = $custom['debt'] ?? 0;
  346. //预订单不能欠款
  347. if ($cg->book == 1) {
  348. $hasDebtPay = 0;
  349. }
  350. $current = time();
  351. $cgDeadTime = strtotime($cg->deadline);
  352. //让客户在失效前$aheadTime秒要支付,这样回调通知时订单才不会过期状态
  353. $aheadTime = dict::getDict('order_online_pay_has_ahead_time');
  354. $count = $cgDeadTime - $current - $aheadTime;
  355. $data = [];
  356. $data['balance'] = $this->shop->balance ?? 0;
  357. $data['count'] = $count;
  358. $data['hasDebtPay'] = $hasDebtPay;
  359. util::success($data);
  360. }
  361. //地区 ssh 20220606
  362. public function actionRegion()
  363. {
  364. $regionTree = RegionService::tree();
  365. util::success(['region' => $regionTree]);
  366. }
  367. //散客门店详情 ssh 20211007
  368. public function actionKjCustom()
  369. {
  370. $defaultCustomId = $this->shop->defaultCustomId ?? 0;
  371. $custom = [];
  372. if (!empty($defaultCustomId)) {
  373. $custom = HdCustomClass::getById($defaultCustomId);
  374. $avatar = $custom['avatar'] ?? '';
  375. $custom['avatar'] = imgUtil::groupImg($avatar) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  376. }
  377. util::success(['custom' => $custom]);
  378. }
  379. //散客门店设置
  380. public function actionKjCustomSet()
  381. {
  382. $id = Yii::$app->request->get('id', 0);
  383. $custom = HdCustomClass::getById($id, true);
  384. if (empty($custom)) {
  385. util::fail('没有找到客户');
  386. }
  387. if ($custom->shopId != $this->shopId) {
  388. util::fail('客户不属于当前门店');
  389. }
  390. $shop = $this->shop;
  391. $shop->defaultCustomId = $id;
  392. $shop->save();
  393. util::complete('修改成功');
  394. }
  395. }