uniapp 微信小程序 微信登录 请求微信用户基本信息

小程序登录流程

html部分

<button class="login-btn" type="primary" @click="getUserInfo">
  微信用户一键登录
</button>
<view class="user">
  用户名+头像
  <view>
    {{userName}} // 昵称
  </view>
  <img :src="pic" alt="" style="width: 100px; height:100px;"> // 头像
</view>

登录请求


	export default {
		name: 'Index',
		data() {
			return {
				userName: '',
				pic: ''
			}
		},
		onLoad() {
			// this.getUserInfo();
		},
		methods: {
      // 获取微信用户信息,一键登录
			getUserInfo() {
				var that = this
				uni.showModal({
					title: '温馨提示',
					content: '亲,授权微信登录后才能正常使用小程序功能',
					success(res) {
            if (res.confirm) {
							uni.getUserProfile({ // 获取微信用户的基本信息
								desc: "注册用户信息使用",
								lang: "zh_CN",
								success: (res) => {
									console.log('res', res)
									that.userName = res.userInfo.nickName, // 拿到用户昵称
									that.pic = res.userInfo.avatarUrl, // 用户头像
									uni.login({ // 登录,拿code // 微信接口服务器需要通过code换取 openid、unionid、session_key 等信息
										provider: 'weixin',
										success: function(loginRes) {
											console.log('loginRes.authResult', loginRes);
											// 在这个地方普通开发中就应该去调用后端给的api进行登录操作了
											// 现在这个地方我们需要换成云函数进行相关操作
											uni.request({  // 获取openid
                        url: 'https://api.weixin.qq.com/sns/jscode2session',  
                        method:'GET',
                        data: {  
                          appid: "wxxxxxxxxxxxx", // 你的小程序的APPID 
                          secret: "xxxxxxxxxxxx",       //你的小程序的secret,
                          js_code: loginRes.code       //wx.login 登录成功后的code  
                        },  
                        success: (cts) => {  
                          console.log(cts); // 获得openid
                        }  
                      });  
										}
									});
								}
							})
						} else {
							uni.showToast({
								title: '您取消了授权',
								duration: 2000
							});
						}
					}
				})
			},
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值