ShopController.php 13 KB

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