LjhController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace hd\controllers;
  3. use bizHd\ljh\classes\LjhApplyClass;
  4. use common\components\dict;
  5. use common\components\httpUtil;
  6. use common\components\orderSn;
  7. use common\components\stringUtil;
  8. use bizHd\wx\classes\WxOpenClass;
  9. use Yii;
  10. use common\components\util;
  11. class LjhController extends BaseController
  12. {
  13. public $guestAccess = ['apply', 'detail', 'wx-pay'];
  14. public function actionDetail()
  15. {
  16. $get = Yii::$app->request->get();
  17. $id = $get['id'] ?? 0;
  18. $info = LjhApplyClass::getById($id, true);
  19. if (empty($info)) {
  20. util::fail('没有信息');
  21. }
  22. util::success($info);
  23. }
  24. public function actionApply()
  25. {
  26. $connection = Yii::$app->db;
  27. $transaction = $connection->beginTransaction();
  28. try {
  29. $post = Yii::$app->request->post();
  30. $shopName = $post['name'] ?? '';
  31. if (empty($shopName)) {
  32. util::fail('名称不能为空');
  33. }
  34. $mobile = $post['mobile'] ?? '';
  35. if (stringUtil::isMobile($mobile) == false) {
  36. util::fail('请填写正确手机号');
  37. }
  38. if (stringUtil::getWordNum($shopName) > 8) {
  39. util::fail('名称不能超过8个汉字');
  40. }
  41. $authCode = $post['authCode'] ?? '';
  42. $mobile = $post['mobile'] ?? '';
  43. //避免重复提交
  44. util::checkRepeatCommit($mobile, 5);
  45. $ptStyle = Yii::$app->params['ptStyle'];
  46. $cacheKey = 'register_mobile_code_' . $ptStyle . '_' . $mobile;
  47. $saveAuthCode = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  48. if (empty($saveAuthCode)) {
  49. util::fail('请填写验证码哦');
  50. }
  51. if (empty($authCode)) {
  52. util::fail('请填写验证码');
  53. }
  54. if ($saveAuthCode != $authCode) {
  55. util::fail('验证码错误');
  56. }
  57. $post['long'] = isset($post['longitude']) ? $post['longitude'] : '';
  58. $post['lat'] = isset($post['latitude']) ? $post['latitude'] : '';
  59. $orderSn = orderSn::getLjhApplySn();
  60. $post['orderSn'] = $orderSn;
  61. if (getenv('YII_ENV') == 'production') {
  62. $price = bcsub(790, 20, 2);
  63. } else {
  64. $price = bcsub(20.05, 20, 2);
  65. }
  66. $post['actPrice'] = $price;
  67. $post['realPrice'] = $price;
  68. $ljhApply = LjhApplyClass::addApply($post);
  69. $ljhApplyId = $ljhApply->id ?? 0;
  70. $transaction->commit();
  71. util::success(['id' => $ljhApplyId, 'newUser' => 0,]);
  72. } catch (\Exception $exception) {
  73. $transaction->rollBack();
  74. Yii::info("报名出错了:" . $exception->getMessage());
  75. util::fail('报名出错了');
  76. }
  77. }
  78. //微信支付 ssh 20230304
  79. public function actionWxPay()
  80. {
  81. ini_set('date.timezone', 'Asia/Shanghai');
  82. $post = Yii::$app->request->post();
  83. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  84. $order = LjhApplyClass::getByCondition(['orderSn' => $orderSn], true);
  85. if (empty($order)) {
  86. util::fail('订单号无效');
  87. }
  88. if ($order->status == 2) {
  89. util::fail('已报名成功了');
  90. }
  91. $name = '零交会报名 ' . $orderSn;
  92. $totalFee = $order->actPrice;
  93. //强制使用小程序的miniOpenId
  94. $openId = isset($post['miniOpenId']) && !empty($post['miniOpenId']) ? $post['miniOpenId'] : '';
  95. if (empty($openId)) {
  96. $admin = $this->admin;
  97. $openId = $admin->miniOpenId ?? '';
  98. }
  99. //没有openId
  100. if (empty($openId)) {
  101. util::success(['hasNoMiniOpenId' => 1]);
  102. }
  103. $purchaseCapital = dict::getDict('capitalType', 'ljhApply', 'id');
  104. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  105. $wxPayType = httpUtil::isMiniProgram() ? 1 : 0;
  106. $couponId = 0;
  107. $attach = "couponId=" . $couponId . "&capitalType=" . $purchaseCapital . '&wxPayType=' . $wxPayType;
  108. //订单20天后过期
  109. $now = time();
  110. $expireTime = $now + 1728000;
  111. $wx = Yii::getAlias("@vendor/weixin");
  112. require_once($wx . '/lib/WxPay.Api.php');
  113. require_once($wx . '/example/WxPay.JsApiPay.php');
  114. $input = new \WxPayUnifiedOrder();
  115. $input->SetBody($name);
  116. $input->SetOut_trade_no($orderSn);
  117. $input->SetTotal_fee($totalFee * 100);
  118. $input->SetTime_start(date("YmdHis", $now));
  119. //将流水类型、代金劵等传给微信再回传
  120. $input->SetAttach($attach);
  121. $input->SetTime_expire(date("YmdHis", $expireTime));
  122. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  123. $input->SetTrade_type("JSAPI");
  124. //花卉宝代为申请的微信支付
  125. $merchantExtend = WxOpenClass::getWxInfo();
  126. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  127. $input->SetSub_openid($openId);
  128. } else {
  129. $input->SetOpenid($openId);
  130. }
  131. //强制使用小程序的miniAppId
  132. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  133. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  134. $tools = new \JsApiPay();
  135. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  136. $newParams = json_decode($jsApiParameters, true);
  137. util::success($newParams);
  138. }
  139. }