ShopController.php 14 KB

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