ShopController.php 14 KB

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