ShopController.php 9.5 KB

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