ShopController.php 10 KB

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