ShopController.php 15 KB

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