ShopController.php 13 KB

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