You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.9 KiB
65 lines
1.9 KiB
3 years ago
|
// index.js
|
||
|
// 获取应用实例
|
||
|
const app = getApp()
|
||
|
|
||
|
Page({
|
||
|
data: {
|
||
|
motto: 'Hello World',
|
||
|
userInfo: {},
|
||
|
hasUserInfo: false,
|
||
|
canIUse: wx.canIUse('button.open-type.getUserInfo'),
|
||
|
canIUseGetUserProfile: false,
|
||
|
// canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName') // 如需尝试获取用户信息可改为false
|
||
|
canIUseOpenData:false
|
||
|
},
|
||
|
onLoad() {
|
||
|
if (wx.getUserProfile) {
|
||
|
this.setData({
|
||
|
canIUseGetUserProfile: true
|
||
|
})
|
||
|
}
|
||
|
},
|
||
|
getUserProfile(e) {
|
||
|
// 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
|
||
|
wx.getUserProfile({
|
||
|
desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
|
||
|
success: (response) => {
|
||
|
console.log('response',response)
|
||
|
wx.login({
|
||
|
success: res => {
|
||
|
// 发送 res.code 到后台换取 openId, sessionKey, unionId
|
||
|
console.log('code:',res.code)
|
||
|
let data = {
|
||
|
code: res.code,
|
||
|
iv: response.iv,
|
||
|
encryptedData:response.encryptedData,
|
||
|
spread: 0,
|
||
|
login_type: 0
|
||
|
}
|
||
|
app.http('POST','wxapp/auth',data).then(
|
||
|
result =>{
|
||
|
if(result.data.isSuccess){
|
||
|
wx.setStorageSync('token', 'Bearer '+ result.data.data.token);
|
||
|
} else{
|
||
|
wx.showToast({
|
||
|
title: result.data.msg,
|
||
|
icon : 'none'
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
})
|
||
|
},
|
||
|
getUserInfo(e) {
|
||
|
// 不推荐使用getUserInfo获取用户信息,预计自2021年4月13日起,getUserInfo将不再弹出弹窗,并直接返回匿名的用户个人信息
|
||
|
console.log('e',e)
|
||
|
this.setData({
|
||
|
userInfo: e.detail.userInfo,
|
||
|
hasUserInfo: true
|
||
|
})
|
||
|
}
|
||
|
})
|