wx-app/pages/index/index.js
2019-06-27 23:30:34 +08:00

182 lines
6.1 KiB
JavaScript

//"navigationStyle": "custom",
//index.js
//获取应用实例
const app = getApp()
import api from './../../utils/api.js';
Page({
data: {
motto: 'Hello',
levels:[
'没有高反',
'轻度高反',
'中度高反',
'重度高反'
],
userInfo: {
avatarUrl: '/assets/head.svg',
realname: '张二娃',
lastEvaluation: '2019-06-23 09:43'
},
hasUserInfo: false,
canIUse: wx.canIUse('button.open-type.getUserInfo')
},
//事件处理函数
bindViewTap: function() {
wx.showModal({
title: '测试',
content: '',
})
},
onLoad: function() {
console.log('index load data',app.globalData)
// 加载
if (app.globalData.openId) { // 已经获取了用户数据
this.processGetUserInfo();
} else if (this.data.canIUse) { // 可以获取用户信息
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
// app.userInfoReadyCallback = res => {
// this.processGetUserInfo(res.userInfo)
// }
// 获取用户信息
wx.getSetting({
success: res => {
console.log('getSetting==>', res);
if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
wx.getUserInfo({
success: res => {
this.processGetUserInfo(res.userInfo)
// // 可以将 res 发送给后台解码出 unionId
// this.globalData.wxuserInfo = res.userInfo
// // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// // 所以此处加入 callback 以防止这种情况
// if (this.userInfoReadyCallback) {
// this.userInfoReadyCallback(res)
// }
}
})
}
}
})
} else {
// 在没有 open-type=getUserInfo 版本的兼容处理
wx.getUserInfo({
success: res => {
this.processGetUserInfo(res.userInfo)
}
})
}
},
getUserInfo: function(e) { // 获取草用户数据
this.processGetUserInfo(e.detail.userInfo)
},
// 初始化用户数据
async initUserInfo() {
try {
let openid = await app.getOpenId();
let userInfo = await api.userInfo(openid, false);
console.log('init user info data', userInfo);
this.globalData.userInfo = userInfo;
this.processGetUserInfo(userInfo);
} catch (e) {
console.log('init user info error', e);
}
},
// 处理获取到用户数据的逻辑
async processGetUserInfo(userInfo) {
//
if (app.globalData.userInfo) { // 已经存在数据则直接跳过
let userInfo = app.globalData.userInfo;
this.setData({
userInfo: userInfo,
hasUserInfo: true
});
return;
}else{ // 尝试创建该用户
let openid = app.globalData.openId;
let userInfo = null;
try{
userInfo = await api.userInfo(openid, false);
if (userInfo.detail){
userInfo['realname'] = userInfo.detail.realname;
}
app.globalData.userInfo = userInfo;
this.setData({
userInfo: userInfo,
hasUserInfo: true
});
return;
}catch(e){
if (e['errMsg'] && e.errMsg.trim() == 'request:fail'){
wx.showModal({
title: '获取数据失败',
showCancel:false
})
return;
}
console.log('get user info error',e);
}
try {
let data = await api.createUserInfo({
open_id: app.globalData.openId,
nickname: userInfo.nickName,
username: app.globalData.openId,
avatarUrl: userInfo.avatarUrl,
});
wx.setStorageSync('userinfo', data)
// 保存数据
app.globalData.userInfo = data;
this.setData({
userInfo: data,
hasUserInfo: true
});
} catch (e) {
console.log('create user error', e);
}
}
// 判断是否需要跳转
if (this.data.userInfo.detail) { return; }
},
gotoAgreementPage() {
wx.navigateTo({
url: '../init/agreement'
})
},
gotoUserIndex() {
if (this.data.userInfo.detail) { // 判断是否已经存在用户数据
wx.navigateTo({
url: '../user/index'
})
}else{
this.gotoAgreementPage()
}
},
gotoEvaluation() {
if (this.data.userInfo.detail) { // 判断是否已经存在用户数据
wx.navigateTo({
url: './../user/evaluation',
})
} else {
this.gotoAgreementPage()
}
},
async onShow(){
if (wx.getStorageSync('refresh-userinfo')) {
wx.removeStorageSync('refresh-userinfo');
let userInfo = await api.userInfo(null, false);
app.globalData.userInfo = userInfo;
wx.setStorageSync('userinfo', userInfo)
this.setData({
userInfo: userInfo,
hasUserInfo: true
});
}
}
})