ShopController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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\merchant\services\ShopService;
  9. use bizHd\purchase\classes\PurchaseClass;
  10. use bizHd\saas\services\RegionService;
  11. use common\components\dict;
  12. use common\components\dirUtil;
  13. use common\components\noticeUtil;
  14. use common\components\stringUtil;
  15. use common\components\util;
  16. use common\components\wxUtil;
  17. use Yii;
  18. class ShopController extends BaseController
  19. {
  20. public $guestAccess = ['all', 'gathering-img-url', 'check-shop'];
  21. //我的可以进入的门店 ssh 20230424
  22. public function actionMyShop()
  23. {
  24. $staff = $this->shopAdmin;
  25. $shopList = [];
  26. if (isset($staff->super) && $staff->super == 1) {
  27. $sjId = $this->sjId ?? 0;
  28. $shopList = ShopClass::getAllByCondition(['sjId' => $sjId, 'ptStyle' => 1], null, 'id,shopName,merchantName,sjId,mainId,ptStyle', 'id');
  29. }
  30. $adminId = $this->adminId ?? 0;
  31. $staffList = ShopAdminClass::getAllByCondition(['adminId' => $adminId], null, '*', null, true);
  32. $mainIdList = [];
  33. if (!empty($staffList)) {
  34. foreach ($staffList as $staff) {
  35. $isPt = $staff->isPt ?? 0;
  36. $delStatus = $staff->delStatus ?? 0;
  37. $super = $staff->super ?? 0;
  38. $mainId = $staff->mainId ?? 0;
  39. if ($isPt == 0 && $delStatus == 0 && $super == 1) {
  40. $mainIdList[] = $mainId;
  41. }
  42. }
  43. }
  44. if (!empty($mainIdList)) {
  45. $addShopList = ShopClass::getAllByCondition(['mainId' => ['in', $mainIdList], 'ptStyle' => 1], null, 'id,shopName,merchantName,sjId,mainId,ptStyle', 'id');
  46. if (!empty($addShopList)) {
  47. foreach ($addShopList as $addShop) {
  48. $addShopId = $addShop['id'] ?? 0;
  49. if (isset($shopList[$addShopId]) == false) {
  50. $shopList[$addShopId] = $addShop;
  51. }
  52. }
  53. }
  54. }
  55. $list = array_values($shopList);
  56. util::success(['list' => $list]);
  57. }
  58. //是否开启完整功能 ssh 20230226
  59. public function actionChangeOnlyCg()
  60. {
  61. $shop = $this->shop;
  62. $get = Yii::$app->request->get();
  63. $onlyCg = $get['onlyCg'] ?? 1;
  64. $shop->onlyCg = $onlyCg;
  65. $shop->save();
  66. util::complete('操作成功');
  67. }
  68. public function actionCheckShop()
  69. {
  70. $get = Yii::$app->request->get();
  71. $mobile = $get['mobile'] ?? '';
  72. $shop = ShopClass::getByCondition(['mobile' => $mobile], true);
  73. if (!empty($shop)) {
  74. util::success(['has' => 1]);
  75. }
  76. util::success(['has' => 0]);
  77. }
  78. //获取当前门店信息 ssh 2021.3.27
  79. public function actionCurrentShop()
  80. {
  81. util::success($this->shop);
  82. }
  83. //门店列表 ssh 20202.2.29
  84. public function actionList()
  85. {
  86. $where = [];
  87. $where['sjId'] = $this->sjId;
  88. $where['delStatus'] = 0;
  89. $list = \bizHd\shop\classes\ShopClass::getShopList($where);
  90. util::success($list);
  91. }
  92. //更新门店 ssh 2020.2.29
  93. public function actionUpdate()
  94. {
  95. $data = Yii::$app->request->post();
  96. $data['sjId'] = $this->sjId;
  97. $id = isset($data['id']) ? $data['id'] : 0;
  98. unset($data['id']);
  99. $shop = ShopService::getById($id);
  100. ShopService::valid($shop, $this->sjId);
  101. $data['img'] = isset($data['img']) && is_array($data['img']) ? json_encode($data['img']) : '';
  102. ShopClass::updateShop($shop, $data);
  103. util::complete();
  104. }
  105. public function actionAdd()
  106. {
  107. $data = Yii::$app->request->post();
  108. $data['sjId'] = $this->sjId;
  109. $data['shopId'] = $this->shopId;
  110. if (isset($data['shopName']) == false || empty($data['shopName'])) {
  111. util::fail('请填写门店名称');
  112. }
  113. $sj = $this->sj;
  114. $sjName = $sj->name ?? '';
  115. $data['merchantName'] = $sjName;
  116. $ptStyle = $sj->style ?? dict::getDict('ptStyle', 'hd');
  117. $data['ptStyle'] = $ptStyle;
  118. $connection = Yii::$app->db;
  119. $transaction = $connection->beginTransaction();
  120. try {
  121. $mobile = $data['mobile'] ?? 0;
  122. if (stringUtil::isMobile($mobile) == false) {
  123. util::fail('请填写正确手机号');
  124. }
  125. $adminInfo = ['name' => $mobile, 'mobile' => $mobile];
  126. $fromApp = dict::getDict('userSourceGetId', 'app', 'id');
  127. $admin = AdminClass::replaceAdmin($adminInfo, $fromApp);
  128. $adminId = $admin['id'] ?? 0;
  129. $data['adminId'] = $adminId;
  130. \bizHd\shop\classes\ShopClass::addMainShop($data, $this->shop, $this->sj);
  131. $transaction->commit();
  132. util::complete();
  133. } catch (\Exception $exception) {
  134. $transaction->rollBack();
  135. util::fail();
  136. }
  137. }
  138. //切换门店 lqh 2021.1.31
  139. public function actionToggleShop()
  140. {
  141. //惠雅鲜花员工不能切换门店
  142. $shopAdmin = $this->shopAdmin;
  143. $adminId = $shopAdmin->adminId ?? 0;
  144. if (in_array($adminId, [2812, 3539, 3735, 3407])) {
  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('没有找到门店');
  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('没有找到门店');
  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('没有找到门店');
  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('没有找到门店');
  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. }