ShopController.php 10 KB

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