update state

This commit is contained in:
LittleBoy 2019-06-27 23:30:34 +08:00
parent e4f573060f
commit 8e0af0ce82
6 changed files with 84 additions and 10 deletions

5
app.js
View File

@ -13,8 +13,9 @@ App({
// wx.setStorageSync('logs', logs) // wx.setStorageSync('logs', logs)
let userInfo = wx.getStorageSync('userinfo'); let userInfo = wx.getStorageSync('userinfo');
let openId = wx.getStorageSync('openId') || ''; let openId = wx.getStorageSync('openId') || '';
let needReload = wx.getStorageSync('reload-userinfo');
this.globalData.openId = openId this.globalData.openId = openId
if (userInfo) { if (!needReload && userInfo) {
this.globalData.userInfo = userInfo; this.globalData.userInfo = userInfo;
return; return;
} }
@ -91,8 +92,10 @@ App({
try { try {
let openid = this.globalData.openId; let openid = this.globalData.openId;
let userInfo = await api.userInfo(openid, false); let userInfo = await api.userInfo(openid, false);
wx.removeStorageSync('reload-userinfo')
console.log('init user info data', userInfo); console.log('init user info data', userInfo);
this.globalData.userInfo = userInfo; this.globalData.userInfo = userInfo;
wx.setStorageSync('userinfo', userInfo)
} catch (e) { } catch (e) {
console.log('init user info error', e); console.log('init user info error', e);
} }

View File

@ -7,6 +7,12 @@ import api from './../../utils/api.js';
Page({ Page({
data: { data: {
motto: 'Hello', motto: 'Hello',
levels:[
'没有高反',
'轻度高反',
'中度高反',
'重度高反'
],
userInfo: { userInfo: {
avatarUrl: '/assets/head.svg', avatarUrl: '/assets/head.svg',
realname: '张二娃', realname: '张二娃',
@ -84,8 +90,9 @@ Page({
async processGetUserInfo(userInfo) { async processGetUserInfo(userInfo) {
// //
if (app.globalData.userInfo) { // 已经存在数据则直接跳过 if (app.globalData.userInfo) { // 已经存在数据则直接跳过
let userInfo = app.globalData.userInfo;
this.setData({ this.setData({
userInfo: app.globalData.userInfo, userInfo: userInfo,
hasUserInfo: true hasUserInfo: true
}); });
return; return;
@ -104,7 +111,16 @@ Page({
hasUserInfo: true hasUserInfo: true
}); });
return; return;
}catch(e){} }catch(e){
if (e['errMsg'] && e.errMsg.trim() == 'request:fail'){
wx.showModal({
title: '获取数据失败',
showCancel:false
})
return;
}
console.log('get user info error',e);
}
try { try {
let data = await api.createUserInfo({ let data = await api.createUserInfo({
open_id: app.globalData.openId, open_id: app.globalData.openId,
@ -112,6 +128,7 @@ Page({
username: app.globalData.openId, username: app.globalData.openId,
avatarUrl: userInfo.avatarUrl, avatarUrl: userInfo.avatarUrl,
}); });
wx.setStorageSync('userinfo', data)
// 保存数据 // 保存数据
app.globalData.userInfo = data; app.globalData.userInfo = data;
this.setData({ this.setData({
@ -124,7 +141,6 @@ Page({
} }
// 判断是否需要跳转 // 判断是否需要跳转
if (this.data.userInfo.detail) { return; } if (this.data.userInfo.detail) { return; }
this.gotoAgreementPage()
}, },
gotoAgreementPage() { gotoAgreementPage() {
@ -149,5 +165,18 @@ Page({
} else { } else {
this.gotoAgreementPage() 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
});
}
} }
}) })

View File

@ -4,17 +4,22 @@
<view class="cu-item" bindtap="gotoUserIndex"> <view class="cu-item" bindtap="gotoUserIndex">
<view class="cu-avatar round lg" style="background-image:url({{userInfo.avatarUrl}});"></view> <view class="cu-avatar round lg" style="background-image:url({{userInfo.avatarUrl}});"></view>
<view class="content"> <view class="content">
<view class="text-grey"> <view class="text-grey user-info-header">
<text class="text-cut">{{userInfo.realname}}</text> <text class="text-cut text-nickname">{{userInfo.realname}}</text>
<text wx:if="{{userInfo.lastEvaluationLevel==0}}" class="last-evaluation level-0">{{levels[userInfo.lastEvaluationLevel]}}</text>
<text wx:if="{{userInfo.lastEvaluationLevel==1}}" class="last-evaluation level-1">{{levels[userInfo.lastEvaluationLevel]}}</text>
<text wx:if="{{userInfo.lastEvaluationLevel==2}}" class="last-evaluation level-2">{{levels[userInfo.lastEvaluationLevel]}}</text>
<text wx:if="{{userInfo.lastEvaluationLevel==3}}" class="last-evaluation level-3">{{levels[userInfo.lastEvaluationLevel]}}</text>
</view> </view>
<view class="text-gray text-sm flex"> <view class="text-gray text-sm flex">
<view class="text-cut"> <view class="text-cut">
评估时间:<text>{{userInfo.lastEvaluation?userInfo.lastEvaluation:'还没有评估过'}}</text> 评估时间:
<text>{{userInfo.lastEvaluation?userInfo.lastEvaluation:'还没有评估过'}}</text>
</view> </view>
</view> </view>
</view> </view>
<view class="action"> <view class="action">
<view class="cuIcon-right text-gray"></view> <view class="cuIcon-right text-gray"></view>
<view class="text-gray text-sm flex"></view> <view class="text-gray text-sm flex"></view>
</view> </view>
</view> </view>

View File

@ -25,4 +25,31 @@
} }
.user-info{ .user-info{
border-bottom: solid 1px #efefef; border-bottom: solid 1px #efefef;
}
.user-info-header{
position: relative;
}
.text-nickname{
font-size: 40rpx;
}
.last-evaluation{
font-size: 24rpx;
background:#259B24;
color:#fff;
margin-left:20rpx;
border-radius: 8rpx;
padding:0rpx 8rpx;
}
.last-evaluation.level-0{
background:#fff;
color:#999;
}
.last-evaluation.level-1{
background-color: #259b24;
}
.last-evaluation.level-2{
background-color: #ff9800;
}
.last-evaluation.level-3{
background-color: #e51c23;
} }

View File

@ -127,6 +127,8 @@ Page({
data[key] = value data[key] = value
await api.updateUserDetail(data) await api.updateUserDetail(data)
this.setUserDetail(key,value); this.setUserDetail(key,value);
wx.setStorageSync('refresh-userinfo', true)
}catch(e){ }catch(e){
console.log('update error',e); console.log('update error',e);
} }

View File

@ -1,5 +1,6 @@
// pages/user/result.js // pages/user/result.js
import {queryEvaluationById} from './../../utils/api.js'; import api,{queryEvaluationById} from './../../utils/api.js';
const app = getApp();
Page({ Page({
/** /**
@ -18,6 +19,13 @@ Page({
this.setData({ this.setData({
item:data item:data
}) })
wx.setStorageSync('refresh-userinfo', true)
// try {
// } catch (e) {
// wx.setStorageSync('reload-userinfo', true)
// console.log('update user info error', e);
// }
}, },
/** /**
@ -31,7 +39,7 @@ Page({
* 生命周期函数--监听页面显示 * 生命周期函数--监听页面显示
*/ */
onShow: function () { onShow: function () {
}, },
/** /**