定义web页面 open-type 获取用户信息 调用
<!-- 微信授权模块 --><!-- #ifdef MP-WEIXIN --><view class='mask' v-if="scopeUserInfo"><view class='accredit'><view class='title'>需要你的授权</view><view class='info'><view class='p'>为了提供更好的服务</view><view class='p'>请在稍后的提示框中点击"允许"</view></view><view class='img'><image mode='widthFix' src="https://qsh.sharexwd.top/api/files/common/accredit.png"></image></view><view class="btn_sq"><button class='btn-jujue' size="mini" @tap="goindex">拒绝</button><button class='btn-shouquan' size="mini" open-type="getUserInfo" @getuserinfo="getUserInfo" withCredentials="true">允许</button></view></view> </view><!-- #endif -->
js 部分 (wx.login_获取code凭证,wx.getUserInfo_通过code获取用户信息)
// 获取微信用户登录信息
getUserInfo: function() {var that = this;console.log(that.baseUrl,"url")wx.login({ //获取登录凭证success: function(res) {var code = res.codeconsole.log(code,"code")wx.getUserInfo({success: function(info) {console.log(info,"info")var userInfo = info.userInfovar nickName = userInfo.nickNamevar avatarUrl = userInfo.avatarUrlvar gender = userInfo.genderwx.request({url: that.baseUrl + '后端保存code和信息的接口', //1header: {'content-type': 'application/x-www-form-urlencoded'},method: "POST",data: {nickName: nickName,//名称avatarUrl: avatarUrl,//头像code: code,//codegender:gender}, // 2success: function(res) {console.log(res.data) var d = res.data;if (d.code==200) {that.scopeUserInfo = false;uni.setStorage({key: 'token',data: d.message})that.baseToast('登录成功')setTimeout(function(){uni.navigateBack({delta:1})})}else{that.baseToast('登录失败,请稍后再试')}},fail: function(res) {console.log(res);}})that.scopeUserInfo = true},fail: function() {uni.showModal({title: '提示',showCancel: false,content: '请先授权后再访问',})}});},fail: function() {console.log("登录失败")}}) //获取登录凭证结束
},