ShopController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. $shopAdmin = $this->shopAdmin;
  145. $switchShop = \biz\shop\classes\ShopAdminClass::hasSwitchShopRight($shopAdmin);
  146. if ($switchShop == 0) {
  147. util::fail('不能切换门店');
  148. }
  149. $newShopId = Yii::$app->request->post('shopId', 0);
  150. $newShop = ShopClass::getById($newShopId, true);
  151. if (empty($newShop)) {
  152. util::fail('没有找到门店30');
  153. }
  154. $res = \bizHd\admin\classes\AdminClass::toggleShop($this->admin, $newShop, $this->sjId, $this->shopAdmin);
  155. util::success($res);
  156. }
  157. //下载微信和支付宝收款码 ssh 2020.2.29
  158. public function actionGatheringCode()
  159. {
  160. $id = Yii::$app->request->get('id', 0);
  161. $shop = ShopService::getById($id, true);
  162. if (empty($shop)) {
  163. util::fail('没有找到门店31');
  164. }
  165. ShopClass::valid($shop->attributes, $this->sjId);
  166. $imgUrl = ShopClass::generateGatheringCode($this->sjId, $id);
  167. $shopName = $shop->shopName ?? '';
  168. $file = fopen($imgUrl, "rb");
  169. Header("Content-type: application/octet-stream ");
  170. Header("Accept-Ranges: bytes ");
  171. Header("Content-Disposition: attachment;filename={$shopName}收款码.jpg");
  172. $contents = "";
  173. while (!feof($file)) {
  174. $contents .= fread($file, 8192);
  175. }
  176. echo $contents;
  177. fclose($file);
  178. }
  179. //获取二维码
  180. public function actionGetGatheringCodeUrl()
  181. {
  182. $id = Yii::$app->request->get('id', 0);
  183. $shop = ShopService::getById($id, true);
  184. if (empty($shop)) {
  185. util::fail('没有找到门店32');
  186. }
  187. ShopClass::valid($shop->attributes, $this->sjId);
  188. $imgUrl = Yii::$app->params['hdHost'] . '/auth/gathering-img-url?id=' . $id;
  189. util::success(['url' => $imgUrl]);
  190. }
  191. public function actionGatheringImgUrl()
  192. {
  193. $id = Yii::$app->request->get('id', 0);
  194. $shop = ShopService::getById($id, true);
  195. if (empty($shop)) {
  196. util::fail('没有找到门店33');
  197. }
  198. $sjId = $shop->sjId;
  199. $imgUrl = ShopClass::generateGatheringCode($sjId, $id);
  200. $fileResource = file_get_contents($imgUrl);
  201. header('Content-type: image/jpeg');
  202. echo $fileResource;
  203. }
  204. //获取小程序的收款码 ssh
  205. public function actionGatheringMiniCode()
  206. {
  207. $id = Yii::$app->request->get('id', 0);
  208. $extend = MerchantExtendService::getBySjId($this->sjId);
  209. if ($extend['miniAuth'] == 0) {
  210. util::fail('您的小程序还没有授权');
  211. }
  212. $shop = ShopService::getById($id);
  213. ShopService::valid($shop, $this->sjId);
  214. $applyMemberCode = wxUtil::generateGatheringMiniCode($this->sj->attributes, $id);
  215. $file = dirUtil::getImgDir() . $applyMemberCode;
  216. if (file_exists($file) == false) {
  217. util::fail('没有找到注册会员码');
  218. }
  219. $fileName = "门店({$id})小程序收款码.jpg";
  220. $contentType = 'image/jpeg';
  221. header("Cache-control: private");
  222. header("Content-type: $contentType"); //设置要下载的文件类型
  223. header("Content-Length:" . filesize($file)); //设置要下载文件的文件大小
  224. header("Content-Disposition: attachment; filename=" . urldecode($fileName)); //设置要下载文件的文件名
  225. readfile($file);
  226. }
  227. //下载会员注册码 ssh 2020.2.29
  228. public function actionApplyMemberCode()
  229. {
  230. $id = Yii::$app->request->get('id', 0);
  231. $extend = MerchantExtendService::getBySjId($this->sjId);
  232. if ($extend['miniAuth'] == 0) {
  233. util::fail('您的小程序还没有授权');
  234. }
  235. $shop = ShopService::getById($id);
  236. ShopService::valid($shop, $this->sjId);
  237. $applyMemberCode = wxUtil::generateMemberCode($this->sj->attributes, $id);
  238. $file = dirUtil::getImgDir() . $applyMemberCode;
  239. if (file_exists($file) == false) {
  240. util::fail('没有找到注册会员码');
  241. }
  242. $fileName = "注册会员码{$id}.jpg";
  243. $contentType = 'image/jpeg';
  244. header("Cache-control: private");
  245. header("Content-type: $contentType"); //设置要下载的文件类型
  246. header("Content-Length:" . filesize($file)); //设置要下载文件的文件大小
  247. header("Content-Disposition: attachment; filename=" . urldecode($fileName)); //设置要下载文件的文件名
  248. readfile($file);
  249. }
  250. //删除门店 ssh 2020.3.1
  251. public function actionDelete()
  252. {
  253. util::fail('禁止操作');
  254. $id = Yii::$app->request->get('id', 0);
  255. util::fail('功能开发中');
  256. $shop = ShopService::getById($id);
  257. ShopService::valid($shop, $this->sjId);
  258. ShopService::deleteShop($shop);
  259. util::complete();
  260. }
  261. //获取门店详情 ssh 2021.4.23
  262. public function actionDetail()
  263. {
  264. $shopId = Yii::$app->request->get('shopId');
  265. if (empty($shopId)) {
  266. $shopId = $this->shopId;
  267. }
  268. $shop = ShopClass::getShopInfo($shopId);
  269. util::success(['info' => $shop]);
  270. }
  271. //提现信息更新 ssh 2021.3.23
  272. public function actionCashUpdate()
  273. {
  274. $get = Yii::$app->request->get();
  275. $cashAccount = $get['cashAccount'] ?? '';
  276. $cashName = $get['cashName'] ?? '';
  277. $cashBank = $get['cashBank'] ?? '';
  278. if (empty($cashName)) {
  279. util::fail('请填写姓名');
  280. }
  281. if (empty($cashAccount)) {
  282. util::fail('请填写银行卡号');
  283. }
  284. if (empty($cashBank)) {
  285. util::fail('请输入开户行');
  286. }
  287. $shopAdmin = $this->shopAdmin;
  288. $shopId = $shopAdmin->shopId ?? 0;
  289. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  290. util::fail('超管才能操作');
  291. }
  292. ShopClass::updateById($shopId, ['cashAccount' => $cashAccount, 'cashName' => $cashName, 'cashBank' => $cashBank]);
  293. $shop = $this->shop;
  294. $sjName = $shop->merchantName ?? '';
  295. $shopName = $shop->shopName ?? '';
  296. //noticeUtil::push("{$sjName}-{$shopName} 修改提现账号,{$cashName} {$cashAccount} {$cashBank}", '15280215347');
  297. util::complete();
  298. }
  299. //获取所有门店lqh 2021.1.31
  300. public function actionAll()
  301. {
  302. $res = [];
  303. $shopAdmin = isset($this->shopAdmin) && !empty($this->shopAdmin) ? $this->shopAdmin->attributes : [];
  304. if (isset($shopAdmin['super']) && $shopAdmin['super'] == 1) {
  305. $res = ShopClass::getAllShop($this->sjId);
  306. }
  307. util::success($res);
  308. }
  309. //获取采购时的支付方式 ssh 20211004
  310. public function actionGetCgPayWay()
  311. {
  312. $get = Yii::$app->request->get();
  313. $id = $get['id'] ?? 0;
  314. $cg = PurchaseClass::getById($id, true);
  315. PurchaseClass::valid($cg, $this->shopId);
  316. $customId = $cg->customId ?? 0;
  317. $custom = CustomClass::getById($customId);
  318. $hasDebtPay = $custom['debt'] ?? 0;
  319. //预订单不能欠款
  320. if ($cg->book == 1) {
  321. $hasDebtPay = 0;
  322. }
  323. $current = time();
  324. $cgDeadTime = strtotime($cg->deadline);
  325. //让客户在失效前$aheadTime秒要支付,这样回调通知时订单才不会过期状态
  326. $aheadTime = dict::getDict('order_online_pay_has_ahead_time');
  327. $count = $cgDeadTime - $current - $aheadTime;
  328. $data = [];
  329. $data['balance'] = $this->shop->balance ?? 0;
  330. $data['count'] = $count;
  331. $data['hasDebtPay'] = $hasDebtPay;
  332. util::success($data);
  333. }
  334. //地区 ssh 20220606
  335. public function actionRegion()
  336. {
  337. $regionTree = RegionService::tree();
  338. util::success(['region' => $regionTree]);
  339. }
  340. //散客门店详情 ssh 20211007
  341. public function actionKjCustom()
  342. {
  343. $defaultCustomId = $this->shop->defaultCustomId ?? 0;
  344. $custom = [];
  345. if (!empty($defaultCustomId)) {
  346. $custom = HdCustomClass::getById($defaultCustomId);
  347. $avatar = $custom['avatar'] ?? '';
  348. $custom['avatar'] = imgUtil::groupImg($avatar) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  349. }
  350. util::success(['custom' => $custom]);
  351. }
  352. //散客门店设置
  353. public function actionKjCustomSet()
  354. {
  355. $id = Yii::$app->request->get('id', 0);
  356. $custom = HdCustomClass::getById($id, true);
  357. if (empty($custom)) {
  358. util::fail('没有找到客户');
  359. }
  360. if ($custom->shopId != $this->shopId) {
  361. util::fail('客户不属于当前门店');
  362. }
  363. $shop = $this->shop;
  364. $shop->defaultCustomId = $id;
  365. $shop->save();
  366. util::complete('修改成功');
  367. }
  368. }