index.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. require('./index.less');
  2. import 'jquery.cookie'
  3. import './helper/response';
  4. import Api from './helper/const';
  5. import Utils from './helper/utils';
  6. let wxPay = false;
  7. let aliPay = false;
  8. // 发送验证码
  9. const getSignAjax = (mobile) => {
  10. $.ajax({
  11. url: Api.composeApiPath(`msg?mobileNo=${mobile}`),
  12. dataType: "json",
  13. async: true,
  14. type: "GET",
  15. success: function (res) {
  16. console.log(res);
  17. alert('验证码已发送!')
  18. }
  19. });
  20. }
  21. const regTest = (n) => {
  22. const zhengze = /^[1]([3-9])[0-9]{9}$/;
  23. if (zhengze.test(n)) {
  24. return true;
  25. } else {
  26. return false
  27. }
  28. }
  29. let count = 60;
  30. const renderTimer = () => {
  31. let timer = setTimeout(() => {
  32. count--;
  33. $('#sign_btn').html(`${count}秒后重新发送`)
  34. if (count <= 0) {
  35. $('#sign_btn').html(`发送验证码`)
  36. count = 60;
  37. clearTimeout(timer)
  38. } else {
  39. renderTimer()
  40. }
  41. }, 1000)
  42. }
  43. const getSign = () => {
  44. if (count < 60 && count > 0) {
  45. alert(`请在${count}秒后重新获取`)
  46. return;
  47. }
  48. renderTimer()
  49. console.log('获取验证码')
  50. console.log(document.getElementById('mobile_number').value)
  51. let mobileNumber = document.getElementById('mobile_number').value;
  52. if (regTest(mobileNumber)) {
  53. getSignAjax(mobileNumber)
  54. } else {
  55. alert('手机号不规则')
  56. }
  57. }
  58. const showDialog = (type, text1, text2) => {
  59. if (type === 'bad') {
  60. $('.success').css('display', 'none');
  61. }
  62. $('.pay_type').html(text1);
  63. $('.pay_type_s').html(text2);
  64. $('.buy-dialog').css('display', 'block')
  65. }
  66. const onBridgeReady = (data) => {
  67. WeixinJSBridge.invoke(
  68. 'getBrandWCPayRequest',
  69. data,
  70. function (res) {
  71. if (res.err_msg == "get_brand_wcpay_request:ok") {
  72. // 使用以上方式判断前端返回,微信团队郑重提示:
  73. //res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
  74. showDialog('success', '支付成功', '您已完成支付');
  75. setTimeout(() => {
  76. window.location.href = 'http://ott80-web.ai160.com/otttest/efunbox_xyyf/res/h5pay/about.html'
  77. }, 1000)
  78. return;
  79. }
  80. if (res.err_msg == "get_brand_wcpay_request:cancel" || res.err_msg == "get_brand_wcpay_request: fail") {
  81. showDialog('bad', '支付失败', '请重新支付');
  82. setTimeout(() => {
  83. window.location.href = 'http://ott80-web.ai160.com/otttest/efunbox_xyyf/res/h5pay/about.html'
  84. }, 2000)
  85. return;
  86. }
  87. });
  88. }
  89. const postBuyInfo = (mobile, sign, payType) => {
  90. $.ajax({
  91. url: Api.composeApiPath('order'),
  92. dataType: "json",
  93. async: true,
  94. data: JSON.stringify({
  95. "mobileNo": mobile,
  96. "authorizeCode": Utils.GetQueryString('code'),
  97. "identifyCode": sign,
  98. "productId": 101001,
  99. "payType": payType
  100. }),
  101. type: "POST",
  102. contentType: "application/json",
  103. success: function (res) {
  104. console.log('创建订单', res)
  105. const resData = res.data;
  106. if (payType === 'ALIPAY') {
  107. document.body.innerHTML = resData;
  108. console.log(resData);
  109. // $("form[0]").submit();
  110. document.forms[0].submit();
  111. } else if (payType === 'WXPAY') {
  112. // 拉起支付
  113. if (typeof WeixinJSBridge == "undefined") {
  114. if (document.addEventListener) {
  115. document.addEventListener('WeixinJSBridgeReady', onBridgeReady.bind(this, {
  116. "appId": resData.appId, //公众号名称,由商户传入
  117. "timeStamp": resData.timeStamp, //时间戳,自1970年以来的秒数
  118. "nonceStr": resData.nonceStr, //随机串
  119. "package": resData.package,
  120. "signType": "MD5", //微信签名方式:
  121. "paySign": resData.sign //微信签名
  122. }), false);
  123. } else if (document.attachEvent) {
  124. document.attachEvent('WeixinJSBridgeReady', onBridgeReady.bind(this, {
  125. "appId": resData.appId, //公众号名称,由商户传入
  126. "timeStamp": resData.timeStamp, //时间戳,自1970年以来的秒数
  127. "nonceStr": resData.nonceStr, //随机串
  128. "package": resData.package,
  129. "signType": "MD5", //微信签名方式:
  130. "paySign": resData.sign //微信签名
  131. }));
  132. document.attachEvent('onWeixinJSBridgeReady', onBridgeRead.bind(this, {
  133. "appId": resData.appId, //公众号名称,由商户传入
  134. "timeStamp": resData.timeStamp, //时间戳,自1970年以来的秒数
  135. "nonceStr": resData.nonceStr, //随机串
  136. "package": resData.package,
  137. "signType": "MD5", //微信签名方式:
  138. "paySign": resData.sign //微信签名
  139. }));
  140. }
  141. } else {
  142. onBridgeReady({
  143. "appId": resData.appId, //公众号名称,由商户传入
  144. "timeStamp": resData.timeStamp, //时间戳,自1970年以来的秒数
  145. "nonceStr": resData.nonceStr, //随机串
  146. "package": resData.package,
  147. "signType": "MD5", //微信签名方式:
  148. "paySign": resData.sign //微信签名
  149. });
  150. }
  151. }
  152. },
  153. fail: function (res) {
  154. showDialog('bad', '支付失败', '')
  155. }
  156. });
  157. };
  158. const buyTouchHandler = () => {
  159. let mobileNumber = document.getElementById('mobile_number').value;
  160. let signNumber = document.getElementById('sign_number').value;
  161. if (!regTest(mobileNumber)) {
  162. alert('手机号不规则');
  163. return;
  164. }
  165. const signTest = /[0-9]{4}$/;
  166. if (!signTest.test(signNumber)) {
  167. alert('验证码格式不正确');
  168. return;
  169. }
  170. let payType = aliPay ? 'ALIPAY' : 'WXPAY'
  171. postBuyInfo(mobileNumber, signNumber, payType);
  172. }
  173. // 绑定事件
  174. $(document).ready(() => {
  175. const ua = navigator.userAgent.toLowerCase();
  176. aliPay = ua.indexOf('alipayclient') !== -1;
  177. wxPay = ua.indexOf('micromessenger') !== -1;
  178. if (wxPay && !aliPay) {
  179. if (!Utils.GetQueryString('code')) {
  180. const url = encodeURIComponent(window.location.href)
  181. window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf05800eea975a68b&redirect_uri=${url}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect`
  182. return;
  183. }
  184. }
  185. // 获取验证码
  186. $('#sign_btn').on('click', () => {
  187. getSign();
  188. });
  189. // 立即支付
  190. $('#buy_btn').on('click', () => {
  191. buyTouchHandler();
  192. });
  193. $('.close').on('click', () => {
  194. $('.buy-dialog').css('display', 'none')
  195. });
  196. let price = parseInt(Utils.GetQueryString('p')) / 100;
  197. let originPrice = parseInt(Utils.GetQueryString('op')) / 100;
  198. if (price) {
  199. $('#op').html(`¥${originPrice.toFixed(2)}`);
  200. $('#minus').html(`¥${(originPrice - price).toFixed(2)}`);
  201. $('#price').html(`¥${price.toFixed(2)}`);
  202. console.log(decodeURIComponent(Utils.GetQueryString('name')))
  203. // $('.productName').html(decodeURIComponent(Utils.GetQueryString('name')));
  204. }
  205. });