LjhController.php 5.3 KB

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