store/modules/user.js
import $U from '@/common/free-lib/util.js';
import $H from '@/common/free-lib/request.js';
export default {
state: {
user: false,
apply: {
rows: [],
count: 0,
},
mailList:[]
},
actions: {
login({
state,
dispatch
}, user) {
state.user = user;
$U.setStorage('token', user.token);
$U.setStorage('user', JSON.stringify(user));
$U.setStorage('user_id', user.id);
dispatch('updateMailBadge');
},
logout({
state
}) {
state.user = false;
$U.removeStorage('token');
$U.removeStorage('user');
$U.removeStorage('user_id');
uni.reLaunch({
url: '/pages/common/login/login'
})
},
initLogin({
state,
dispatch
}) {
let user = $U.getStorage('user');
if (user) {
state.user = JSON.parse(user);
let socket = uni.connectSocket({
url:'ws://xxx:7001/ws',
complete:()=>{}
})
socket.onOpen(()=>{
console.log('socket已连接')
})
socket.onMessage((res)=>{
console.log('监听接收信息',res)
})
socket.onClose(()=>{
console.log('socket已断开')
})
socket.onError(()=>{
console.log('连接错误')
})
dispatch('getApply');
}
},
getApply({
state,
dispatch
}, page = 1) {
$H.get('/apply/' + page).then(res => {
if (page === 1) {
state.apply = res
} else {
state.apply.rows = [...state.apply.rows, ...res.rows]
state.apply.count = res.count
}
dispatch('updateMailBadge');
});
},
updateMailBadge({
state
}) {
let count = state.apply.count > 99 ? '99+' : state.apply.count.toString();
console.log(state.apply.count);
if (state.apply.count > 0) {
return uni.setTabBarBadge({
index: 1,
text: count
})
}
uni.removeTabBarBadge({
index: 1
})
},
getMailList({state}){
$H.get('/friend/list').then(res=>{
state.mailList = res.rows.newList ? res.rows.newList : [];
})
}
},
}
|