最近在开发公众号的时候碰上了这个问题,记录一下,以后有用到再来看
uniApp 公众号登录公用封装方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| var wxUtil = { getJscode: function (url, success) { var option = { appid: 'wxf3d0029f0ef462b1', scope: 'snsapi_userinfo', success: success, url: url, } this.start_get(option) }, start_get: function (option) { let jscode = this.p('code') if (jscode) { return option.success(jscode, option) } if (this.is_weixn_qq() != '微信') { alert('当前环境非微信内置浏览器') return } let appid = option.appid let uri = encodeURIComponent(option.url) let scope = option.scope let url = 'https://open.weixin.qq.com/connect/oauth2/authorize' + '?appid=' + appid + '&redirect_uri=' + uri + '&response_type=code' + '&scope=' + scope + '&state=123' + '#wechat_redirect' window.location.href = url }, p: function (name, defaultValue) { var query = window.location.search.substring(1) var vars = query.split('&') for (var i = 0; i < vars.length; i++) { var pair = vars[i].split('=') if (pair[0] == name) { return pair[1] } } return defaultValue == undefined ? null : defaultValue }, is_weixn_qq: function () { var ua = navigator.userAgent.toLowerCase() if (ua.match(/MicroMessenger/i) == 'micromessenger') { return '微信' } else if (ua.match(/QQ/i) == 'qq') { return 'QQ' } return false }, } module.exports = wxUtil
|
调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| <script> const wxPayUtil = require('./common/uni-wx-util.js') export default { globalData: { baseUrl: 'http://xy.myym.top/xy-server' }, onLaunch: function() { console.log('App Launch') this.$u.post('/SysUser/getByCurr', {}).then(res => { }).catch(res => { wxPayUtil.getJscode('http://xy.myym.top/shop/#/', (code, option) => { console.log(code, option) this.$u.post('/SysUserAcc/doWeChat',{ code }).then(res=>{ uni.setStorageSync('satoken',res.data.tokenInfo.tokenValue) }).catch(res=>{ console.log(res) }) }) }) }, onShow: function() { console.log('App Show') }, onHide: function() { console.log('App Hide') } } </script>
|
uniApp 公众号支付公用封装方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| let wxPayUtil = { jsapi_pay: function (info, successFn, failFn) { WeixinJSBridge.invoke( 'getBrandWCPayRequest', { appId: info.appId, timeStamp: info.timeStamp, nonceStr: info.nonceStr, package: info['package'], signType: info.signType, paySign: info.paySign, }, function (res) { if (res.err_msg == 'get_brand_wcpay_request:ok') { if (successFn) { successFn() } } else { if (failFn) { failFn() } } } ) }, } module.exports = wxPayUtil
|
调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| const wxPayUtil = require('../../common/uni-wx-pay-util.js')
pay() { this.payShow = !this.payShow this.$u.post('/SmOrderPay/getPayInfoByWxAppPay',{ pay_id:this.pay_id }).then(res=>{ console.log(res) wxPayUtil.jsapi_pay(res.data,()=>{ this.$all.toast('','支付成功') this.getOrder() },()=>{ this.$u.toast('支付失败') },) }).catch(res=>{ console.log(res) }) },
|
注意:以上方法需要在公众号使用才会有效果