uniapp开发微信公众号登录

uniapp开发微信公众号登录

app.vue

onLaunch: function(options) {
            //  获取缓存token
			let storage_token = getStorageSync('token');
			if (storage_token) {
				setStorageSync('token', storage_token);
				// 通过之后获取个人信息
				this.upDateUserInforMutaions();
				// 配置信息
				this.upDateConfigComnMutaions();
			} else {
				//没有token的话   判断能不能获取到code
				if (this.getUrlKey('code')) {
					// 能获取到code去登录
					this.loginList(this.getUrlKey('code'), options.query.referid)
				} else {
					// 不能获取到code 去重新授权
					this.login()
				}
			}
},
methods: {
            login() {
                // currentUrl重定向地址    encodeURIComponent(`${location.href}`)
                // weiXinUrl微信置换code   https://open.weixin.qq.com/connect/oauth2/authorize?appid=${自己的appid}&redirect_uri=${currentUrl}&response_type=code&scope=snsapi_userinfo&state=#wechat_redirect
				window.location.href = apiBaseConfig.weiXinUrl;
			},
			async loginList(wxcode, referid) {
				let self = this;
				const data_back = await getLoginCodeAuth({
					code: wxcode,
					refer_id: referid
				})
				const {
					code,
					data
				} = data_back;
				if (code === 0) {
					setStorageSync("token", data.token)
					self.upDateUserInforMutaions();
					self.upDateConfigComnMutaions();
					uni.reLaunch({
						url: '/pages/index/index'
					})
				} else {
					uni.showToast({
						title: '抱歉,登录失败!',
						icon: "none",
						duration: 2000
					});
				}
			},
			getUrlKey(name) {
				return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) ||
				[, ""
				])[1].replace(/\+/g, '%20')) || null
			},
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
//微信充值 //支付接口测试 function balance(url, data) { uni.request({ url: cfg.originUrl + '/wx/mp/js_sig.do', data: { route: url }, method: 'GET', success: (res) => { jweixin.config({ debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来 appId: res.data.appId, // 必填,公众号的唯一标识 timestamp: res.data.timestamp, // 必填,生成签名的时间戳 nonceStr: res.data.nonceStr, // 必填,生成签名的随机串 signature: res.data.signature, // 必填,签名 jsApiList: ['chooseWXPay'] // 必填,需要使用的JS接口列表 }); jweixin.ready(function() { uni.request({ url: cfg.originUrl + '/wx/recharge/pay.do', method: 'POST', header: { 'Content-type': "application/x-www-form-urlencoded", }, data: JSON.stringify(data), success: function(res) { alert("下单成功"); alert(JSON.stringify(res)); alert(res.data.order_id); all.globalData.orderId = res.data.order_id; uni.setStorageSync('orderId', res.data.order_id); jweixin.chooseWXPay({ timestamp: res.data.payParams.timeStamp, // 支付签名时间戳 nonceStr: res.data.payParams.nonceStr, // 支付签名随机串 package: res.data.payParams.package, // 接口返回的prepay_id参数 signType: res.data.payParams.signType, // 签名方式 paySign: res.data.payParams.paySign, // 支付签名 success: function(e) { alert("支付成功"); alert(JSON.stringify(e)); // 支付成功后的回调函数 } }); } }) }); jweixin.error(function(res) { // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。 console.log("验证失败!") }); } }) }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值