WXPay.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <template>
  2. <div :html="retMsg"></div>
  3. </template>
  4. <script>
  5. import remoteApi from '@/api/remoteApi'
  6. import wx from 'weixin-js-sdk'
  7. import hex_sha1 from 'hex-sha1'
  8. import getQueryString from '@/utils/querystring'
  9. export default {
  10. data () {
  11. return {
  12. retMsg : "",
  13. code : getQueryString("code"),
  14. openId : '',
  15. recordId: getQueryString("state")
  16. }
  17. },
  18. methods: {
  19. queryPayResult(recordId) {
  20. remoteApi.queryRechargeRecord(recordId).then((resp)=>{
  21. if(resp.result && resp.data.paymentStatus=="20"){
  22. this.$toast.clear();
  23. this.$router.push({
  24. path : '/Paystate',
  25. query: {
  26. payType : resp.data.buyType
  27. }
  28. });
  29. }
  30. else{
  31. setTimeout(()=>{
  32. this.queryPayResult(recordId);
  33. },3000);
  34. }
  35. });
  36. }
  37. },
  38. mounted (){
  39. var self = this
  40. self.$toast.loading({
  41. message: '初始化...',
  42. forbidClick: true,
  43. loadingType: 'spinner',
  44. duration: 10000
  45. });
  46. remoteApi.getWXConfigParam(self.code).then(resp=>{
  47. if(resp.result){
  48. var appId = resp.data.appId
  49. var timestamp = (new Date()).getTime()
  50. self.openId = resp.data.openId
  51. var fullStr = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
  52. var nonceStr = ''
  53. for(var i=0;i<10;i++){
  54. nonceStr += fullStr.charAt(Math.floor(Math.random()*fullStr.length))
  55. }
  56. var url = window.location.href.split('#')[0]
  57. //服务器端通过access_token获取 https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi
  58. var ticket = resp.data.ticket
  59. //注意这里noncestr中的s是小写
  60. var plainText = `jsapi_ticket=${ticket}&noncestr=${nonceStr}&timestamp=${timestamp}&url=${url}`
  61. console.log(`plainText="${plainText}"`)
  62. var signature = hex_sha1(plainText)
  63. console.log(`signature=${signature}`)
  64. wx.config({
  65. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印
  66. appId: appId, // 必填,公众号的唯一标识
  67. timestamp: timestamp, // 必填,生成签名的时间戳
  68. nonceStr: nonceStr, // 必填,生成签名的随机串
  69. signature: signature,// 必填,签名
  70. jsApiList: ['checkJsApi','chooseWXPay'] // 必填,需要使用的JS接口列表
  71. });
  72. }
  73. else{
  74. self.$toast.clear();
  75. self.$toast.fail("获取参数失败!" + resp.message)
  76. }
  77. });
  78. wx.ready(function(){
  79. self.$toast.clear();
  80. self.$toast.loading({
  81. message: '支付准备中...',
  82. forbidClick: true,
  83. loadingType: 'spinner',
  84. duration: 10000
  85. });
  86. remoteApi.wxPay(self.recordId,self.openId).then(resp=>{
  87. self.$toast.clear();
  88. console.log(resp);
  89. self.retMsg = JSON.stringify(resp)
  90. if(resp.result){
  91. var data = resp.data;
  92. wx.chooseWXPay({
  93. //注意这里的返回参数是timeStamp,不是timestamp
  94. "timestamp": data.timeStamp, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
  95. "nonceStr": data.nonceStr, // 支付签名随机串,不长于 32 位
  96. "package": data.package, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=\*\*\*)
  97. "signType": data.signType, // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
  98. "paySign": data.paySign, // 支付签名
  99. success: function (res) {
  100. self.$toast.loading({
  101. message: '订单确认中...',
  102. forbidClick: true,
  103. loadingType: 'spinner',
  104. duration: 3000
  105. });
  106. // 支付成功后的回调函数,跳转到订单列表
  107. self.queryPayResult(self.recordId);
  108. }
  109. });
  110. }
  111. else{
  112. self.$toast.fail("生成预支付订单失败!");
  113. }
  114. },"json");
  115. });
  116. wx.error((error)=>{
  117. console.log(error)
  118. });
  119. }
  120. }
  121. </script>
  122. <style>
  123. </style>