LjhController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. $price = bcsub(20.05, 20, 2);
  64. } else {
  65. $price = bcsub(20.05, 20, 2);
  66. }
  67. $post['actPrice'] = $price;
  68. $post['realPrice'] = $price;
  69. $ljhApply = LjhApplyClass::addApply($post);
  70. $ljhApplyId = $ljhApply->id ?? 0;
  71. $transaction->commit();
  72. util::success(['id' => $ljhApplyId, 'newUser' => 0,]);
  73. } catch (\Exception $exception) {
  74. $transaction->rollBack();
  75. Yii::info("报名出错了:" . $exception->getMessage());
  76. util::fail('报名出错了');
  77. }
  78. }
  79. //微信支付 ssh 20230304
  80. public function actionWxPay()
  81. {
  82. ini_set('date.timezone', 'Asia/Shanghai');
  83. $post = Yii::$app->request->post();
  84. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  85. $order = LjhApplyClass::getByCondition(['orderSn' => $orderSn], true);
  86. if (empty($order)) {
  87. util::fail('订单号无效');
  88. }
  89. if ($order->status == 2) {
  90. util::fail('已报名成功了');
  91. }
  92. $name = '零交会报名 ' . $orderSn;
  93. $totalFee = $order->actPrice;
  94. //强制使用小程序的miniOpenId
  95. $openId = isset($post['miniOpenId']) && !empty($post['miniOpenId']) ? $post['miniOpenId'] : '';
  96. if (empty($openId)) {
  97. $admin = $this->admin;
  98. $openId = $admin->miniOpenId ?? '';
  99. }
  100. //没有openId
  101. if (empty($openId)) {
  102. util::success(['hasNoMiniOpenId' => 1]);
  103. }
  104. $purchaseCapital = dict::getDict('capitalType', 'ljhApply', 'id');
  105. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  106. $wxPayType = httpUtil::isMiniProgram() ? 1 : 0;
  107. $couponId = 0;
  108. $attach = "couponId=" . $couponId . "&capitalType=" . $purchaseCapital . '&wxPayType=' . $wxPayType;
  109. //订单20天后过期
  110. $now = time();
  111. $expireTime = $now + 1728000;
  112. $wx = Yii::getAlias("@vendor/weixin");
  113. require_once($wx . '/lib/WxPay.Api.php');
  114. require_once($wx . '/example/WxPay.JsApiPay.php');
  115. $input = new \WxPayUnifiedOrder();
  116. $input->SetBody($name);
  117. $input->SetOut_trade_no($orderSn);
  118. $input->SetTotal_fee($totalFee * 100);
  119. $input->SetTime_start(date("YmdHis", $now));
  120. //将流水类型、代金劵等传给微信再回传
  121. $input->SetAttach($attach);
  122. $input->SetTime_expire(date("YmdHis", $expireTime));
  123. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  124. $input->SetTrade_type("JSAPI");
  125. //花卉宝代为申请的微信支付
  126. $merchantExtend = WxOpenClass::getWxInfo();
  127. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  128. $input->SetSub_openid($openId);
  129. } else {
  130. $input->SetOpenid($openId);
  131. }
  132. //强制使用小程序的miniAppId
  133. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  134. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  135. $tools = new \JsApiPay();
  136. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  137. $newParams = json_decode($jsApiParameters, true);
  138. util::success($newParams);
  139. }
  140. }