LjhController.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. namespace hd\controllers;
  3. use biz\shop\services\ShopAdminService;
  4. use biz\sj\classes\SjClass;
  5. use bizHd\saas\classes\ApplyClass;
  6. use bizHd\ljh\classes\LjhApplyClass;
  7. use bizHd\shop\classes\ShopClass;
  8. use common\components\dict;
  9. use common\components\httpUtil;
  10. use common\components\jwt;
  11. use common\components\noticeUtil;
  12. use common\components\orderSn;
  13. use common\components\stringUtil;
  14. use bizHd\wx\classes\WxOpenClass;
  15. use bizGhs\admin\classes\AdminClass;
  16. use bizHd\saas\services\ApplyService;
  17. use Yii;
  18. use common\components\util;
  19. class LjhController extends BaseController
  20. {
  21. public $guestAccess = ['apply', 'detail', 'wx-pay'];
  22. public function actionDetail()
  23. {
  24. $get = Yii::$app->request->get();
  25. $id = $get['id'] ?? 0;
  26. $info = LjhApplyClass::getById($id, true);
  27. if (empty($info)) {
  28. util::fail('没有信息');
  29. }
  30. util::success($info);
  31. }
  32. public function actionApply()
  33. {
  34. $connection = Yii::$app->db;
  35. $transaction = $connection->beginTransaction();
  36. try {
  37. $post = Yii::$app->request->post();
  38. $shopName = $post['name'] ?? '';
  39. if (empty($shopName)) {
  40. util::fail('名称不能为空');
  41. }
  42. $mobile = $post['mobile'] ?? '';
  43. if (stringUtil::isMobile($mobile) == false) {
  44. util::fail('请填写正确手机号');
  45. }
  46. if (stringUtil::getWordNum($shopName) > 8) {
  47. util::fail('名称不能超过8个汉字');
  48. }
  49. $authCode = $post['authCode'] ?? '';
  50. $mobile = $post['mobile'] ?? '';
  51. //避免重复提交
  52. util::checkRepeatCommit($mobile, 5);
  53. $ptStyle = Yii::$app->params['ptStyle'];
  54. $cacheKey = 'register_mobile_code_' . $ptStyle . '_' . $mobile;
  55. $saveAuthCode = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  56. if (empty($saveAuthCode)) {
  57. util::fail('请填写验证码哦');
  58. }
  59. if (empty($authCode)) {
  60. util::fail('请填写验证码');
  61. }
  62. if ($saveAuthCode != $authCode) {
  63. util::fail('验证码错误');
  64. }
  65. $post['long'] = isset($post['longitude']) ? $post['longitude'] : '';
  66. $post['lat'] = isset($post['latitude']) ? $post['latitude'] : '';
  67. $orderSn = orderSn::getLjhApplySn();
  68. $post['orderSn'] = $orderSn;
  69. if (getenv('YII_ENV') == 'production') {
  70. $price = bcsub(790, 20, 2);
  71. } else {
  72. $price = bcsub(20.05, 20, 2);
  73. }
  74. $post['actPrice'] = $price;
  75. $post['realPrice'] = $price;
  76. $ljhApply = LjhApplyClass::addApply($post);
  77. $ljhApplyId = $ljhApply->id ?? 0;
  78. $transaction->commit();
  79. util::success(['id' => $ljhApplyId, 'newUser' => 0,]);
  80. } catch (\Exception $exception) {
  81. $transaction->rollBack();
  82. Yii::info("报名出错了:" . $exception->getMessage());
  83. util::fail('报名出错了');
  84. }
  85. }
  86. //微信支付 ssh 20230304
  87. public function actionWxPay()
  88. {
  89. ini_set('date.timezone', 'Asia/Shanghai');
  90. $post = Yii::$app->request->post();
  91. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  92. $order = LjhApplyClass::getByCondition(['orderSn' => $orderSn], true);
  93. if (empty($order)) {
  94. util::fail('订单号无效');
  95. }
  96. if ($order->status == 2) {
  97. util::fail('已报名成功了');
  98. }
  99. $name = '零交会报名 ' . $orderSn;
  100. $totalFee = $order->actPrice;
  101. //强制使用小程序的miniOpenId
  102. $openId = isset($post['miniOpenId']) && !empty($post['miniOpenId']) ? $post['miniOpenId'] : '';
  103. if (empty($openId)) {
  104. $admin = $this->admin;
  105. $openId = $admin->miniOpenId ?? '';
  106. }
  107. //没有openId
  108. if (empty($openId)) {
  109. util::success(['hasNoMiniOpenId' => 1]);
  110. }
  111. $purchaseCapital = dict::getDict('capitalType', 'ljhApply', 'id');
  112. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  113. $wxPayType = httpUtil::isMiniProgram() ? 1 : 0;
  114. $couponId = 0;
  115. $attach = "couponId=" . $couponId . "&capitalType=" . $purchaseCapital . '&wxPayType=' . $wxPayType;
  116. //订单20天后过期
  117. $now = time();
  118. $expireTime = $now + 1728000;
  119. $wx = Yii::getAlias("@vendor/weixin");
  120. require_once($wx . '/lib/WxPay.Api.php');
  121. require_once($wx . '/example/WxPay.JsApiPay.php');
  122. $input = new \WxPayUnifiedOrder();
  123. $input->SetBody($name);
  124. $input->SetOut_trade_no($orderSn);
  125. $input->SetTotal_fee($totalFee * 100);
  126. $input->SetTime_start(date("YmdHis", $now));
  127. //将流水类型、代金劵等传给微信再回传
  128. $input->SetAttach($attach);
  129. $input->SetTime_expire(date("YmdHis", $expireTime));
  130. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  131. $input->SetTrade_type("JSAPI");
  132. //花卉宝代为申请的微信支付
  133. $merchantExtend = WxOpenClass::getWxInfo();
  134. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  135. $input->SetSub_openid($openId);
  136. } else {
  137. $input->SetOpenid($openId);
  138. }
  139. //强制使用小程序的miniAppId
  140. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  141. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  142. $tools = new \JsApiPay();
  143. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  144. $newParams = json_decode($jsApiParameters, true);
  145. util::success($newParams);
  146. }
  147. //免费领取商城 ssh 20230304
  148. public function actionGetMall()
  149. {
  150. $shop = $this->shop;
  151. if (!empty($shop)) {
  152. util::complete('已经有店铺了');
  153. }
  154. $get = Yii::$app->request->get();
  155. $orderSn = isset($get['orderSn']) ? $get['orderSn'] : 0;
  156. $ljh = LjhApplyClass::getByCondition(['orderSn' => $orderSn], true);
  157. if (empty($ljh)) {
  158. util::complete('订单号无效');
  159. }
  160. $mobile = $ljh->mobile ?? 0;
  161. if (empty($mobile)) {
  162. util::complete('手机号无效');
  163. }
  164. $hasApply = ApplyClass::getByCondition(['mobile' => $mobile]);
  165. if (!empty($hasApply)) {
  166. util::complete('已有申请');
  167. }
  168. $hasShop = ShopClass::getByCondition(['mobile' => $mobile]);
  169. if (!empty($hasShop)) {
  170. util::complete('已经开店');
  171. }
  172. $ptStyle = Yii::$app->params['ptStyle'];
  173. $adminName = $ljh->adminName ?? '';
  174. $shopName = $ljh->name ?? '';
  175. $miniOpenId = $post['miniOpenId'] ?? '';
  176. $adminInfo = ['name' => $adminName, 'mobile' => $mobile, 'style' => $ptStyle, 'miniOpenId' => $miniOpenId];
  177. $fromApp = dict::getDict('userSourceGetId', 'app', 'id');
  178. $admin = AdminClass::replaceAdmin($adminInfo, $fromApp);
  179. $adminId = $admin['id'] ?? 0;
  180. $currentShopId = $admin['currentShopId'] ?? 0;
  181. if (!empty($currentShopId)) {
  182. util::complete('已经有开店了');
  183. }
  184. if (isset($admin['openShop']) && $admin['openShop'] == 2) {
  185. util::complete('已经有申请了');
  186. }
  187. $post['adminId'] = $adminId;
  188. $post['adminName'] = $adminName;
  189. $post['long'] = $ljh->long ?? '';
  190. $post['lat'] = $ljh->lat ?? '';
  191. $post['from'] = ApplyClass::FROM_RETAIL;
  192. $post['style'] = SjClass::STYLE_RETAIL;
  193. //增加或更新申请
  194. $respond = ApplyService::replaceRetail($post);
  195. $id = $respond['id'] ?? 0;
  196. //提交申请资料失败
  197. if (empty($id)) {
  198. util::complete('申请失败了');
  199. }
  200. $passInfo = ApplyService::pass($id);
  201. $mobile = $post['mobile'] ?? '';
  202. //输出登录信息
  203. $newShop = $passInfo['shop'] ?? [];
  204. $newMainId = $newShop->mainId ?? 0;
  205. $newShopId = $newShop->id ?? 0;
  206. $pfShopId = $newShop->pfShopId ?? 0;
  207. $onlyCg = 1;
  208. //前端用于判断是否注册
  209. $admin['currentShopId'] = $newShopId;
  210. $adminId = $admin['id'];
  211. $shopAdmin = ShopAdminService::getByCondition(['adminId' => $adminId, 'mainId' => $newMainId], true);
  212. if (empty($shopAdmin)) {
  213. util::fail('没有找到管理员');
  214. }
  215. $shopAdmin->lastLogin = date("Y-m-d H:i:s");
  216. $shopAdmin->save();
  217. $shopAdminId = $shopAdmin->id ?? 0;
  218. //是否有切换门店的权限
  219. $switchShop = \biz\shop\classes\ShopAdminClass::hasSwitchShopRight($shopAdmin);
  220. $showDemo = 1;
  221. $token = jwt::getNewToken($adminId);
  222. noticeUtil::push("花店 {$shopName} 手机号 {$mobile} 注册", '15280215347');
  223. $apiHost = Yii::$app->params['hdHost'];
  224. $imgUploadApi = $apiHost . '/upload/save-file';
  225. //涉及几个登录的地方,需要同步修改,请搜索关键词uni_login_info
  226. util::success([
  227. 'token' => $token,
  228. 'admin' => $admin,
  229. 'shopId' => $currentShopId,
  230. 'shopAdminId' => $shopAdminId,
  231. 'switchShop' => $switchShop,
  232. 'pfShopId' => $pfShopId,
  233. 'onlyCg' => $onlyCg,
  234. 'showDemo' => $showDemo,
  235. 'imgUploadApi' => $imgUploadApi,
  236. 'staff' => $shopAdmin,
  237. 'newUser' => 1,
  238. ]);
  239. }
  240. }