Files
AnHanNET-UniAPP/store/modules/user.js
2023-02-16 18:00:57 +08:00

66 lines
1.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 子模块moduleB路径store/modules/moduleB.js
export default {
state: {
timestamp: 1608820295, //初始时间戳
token: uni.getStorageSync('token') || '',
userinfo: JSON.parse(uni.getStorageSync('userinfo') || '{}'),
openId: null,
sessionKey:null,
},
getters: {
timeString(state) { //时间戳转换后的时间
var date = new Date(state.timestamp);
var year = date.getFullYear();
var mon = date.getMonth() + 1;
var day = date.getDate();
var hours = date.getHours();
var minu = date.getMinutes();
var sec = date.getSeconds();
var trMon = mon < 10 ? '0' + mon : mon
var trDay = day < 10 ? '0' + day : day
return year + '-' + trMon + '-' + trDay + " " + hours + ":" + minu + ":" + sec;
},
getuserIsLogin(state){
return Object.keys(state.userinfo).length > 0 ;
},
getSessionKey(state){
return state.sessionKey;
},
getOpenId(state){
return state.openId;
},
getUserInfo(state){
let userinfo = state.userinfo;
if ( userinfo.avatar=== "undefined" || userinfo.avatar=== "null" || userinfo.avatar=== "")
userinfo.avatar = "/static/avatar1.png"
return state.userinfo;
}
},
mutations: {
updateTime(state) { //更新当前时间戳
state.timestamp = Date.now()
},
// 更新用户信息
updateUserInfo(state, userinfo) {
state.userinfo = userinfo ;
uni.setStorageSync('userinfo', JSON.stringify(state.userinfo)) ;
},
// 将 token 字符串持久化存储到本地
saveOpenIdSession(state, res) {
console.log("saveOpenIdSession",res)
state.openId = res.openid
state.sessionKey = res.session_key
//this.unionid = res.unionid
}
},
actions: {
actionLogout(context ){
uni.$ajax("/User/logout").then(res => {
context.commit('updateUserInfo', {});
uni.setStorageSync('token', "");
});
}
}
}