async getuserinfoh5appwx() {
var _this = this
var weixinService = null;
// 参考:http://www.html5plus.org/doc/zh_cn/oauth.html#plus.oauth.getServices
plus.oauth.getServices(
async (services) => {
if (services && services.length) {
for (var i = 0, len = services.length; i < len; i++) {
if (services[i].id === ‘weixin’) {
weixinService = services[i]
break
}
}
if (!weixinService) {
console.log(‘没有微信登录授权服务’)
return
}
//参考: http://www.html5plus.org/doc/zh_cn/oauth.html#plus.oauth.AuthService.authorize
// weixinService.authorize(
await weixinService.login(
async (event) => {
uni.showToast({
title: '微信登陆成功' + JSON.stringify(event.userInfo),
icon: 'none'
})
await weixinService.getUserInfo(function(e) {
plus.nativeUI.alert("获取用户信息成功:" + JSON.stringify(
weixinService.userInfo));
uni.request({
url: Api.BASEURI + Api.index.appLogin,
method: 'POST',
header: {
'content-type': 'application/x-www-form-urlencoded'
},
data: {
sex: weixinService.userInfo.sex,
city: weixinService.userInfo.country + '-' +
weixinService.userInfo
.province + '-' + weixinService.userInfo
.city,
source: 1,
unionid: weixinService.userInfo.unionid,
openid: weixinService.userInfo.openid,
nickname: weixinService.userInfo.nickname,
headimgurl: weixinService.userInfo.headimgurl
},
success: res => {
plus.nativeUI.alert("接口回调:" + JSON.stringify(res));
plus.nativeUI.alert("接口回调res.data:" + JSON.stringify(res.data));
// 登录成功 记录会员信息到本地
if (res) {
console.log(res);
// that.login(res.data.data);
uni.setStorageSync('userInfos',
res.data
.data.userInfo);
uni.setStorageSync('token', res
.data.data
.tokenHead + res.data
.data.token);
uni.switchTab({
url: '/pages/index/index'
});
} else {
uni.showToast({
title: JSON
.stringify(res
.data),
icon: 'none'
});
}
},
fail: e => {
console.log(JSON.stringify(e));
}
});
}, function(e) {
plus.nativeUI.alert("获取用户信息失败: " + JSON.stringify(e));
});
},
function(error) {
console.error('authorize fail:' + JSON.stringify(error))
}, {
scope: 'snsapi_userinfo',
state: 'authorize',
appid: Api.WXAPPID,
appsecret: Api.appsecret
}
)
} else {
console.log('无可用的登录授权服务')
}
},
function(error) {
console.error('getServices fail:' + JSON.stringify(error))
}
)
}