ShopController.php 13 KB

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