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.

72 lines
1.7 KiB

12 months ago
import { defineStore } from 'pinia';
import http from '../api/httpRequest.js'
import { TERMINAL_TYPE } from "../api/enums.js"
12 months ago
export default defineStore('friendStore', {
state: () => {
return {
friends: [], // 好友列表
timer: null
}
},
12 months ago
actions: {
setFriends(friends) {
this.friends = friends;
},
12 months ago
updateFriend(friend) {
this.friends.forEach((f, index) => {
if (f.id == friend.id) {
// 拷贝属性
12 months ago
let online = this.friends[index].online;
Object.assign(this.friends[index], friend);
this.friends[index].online = online;
}
})
},
12 months ago
removeFriend(id) {
this.friends.filter(f => f.id == id).forEach(f => f.deleted = true);
},
12 months ago
addFriend(friend) {
if (this.friends.some(f => f.id == friend.id)) {
this.updateFriend(friend)
} else {
12 months ago
this.friends.unshift(friend);
}
},
12 months ago
updateOnlineStatus(onlineData) {
let friend = this.findFriend(onlineData.userId);
if (onlineData.terminal == TERMINAL_TYPE.WEB) {
friend.onlineWeb = onlineData.online;
} else if (onlineData.terminal == TERMINAL_TYPE.APP) {
friend.onlineApp = onlineData.online;
}
12 months ago
friend.online = friend.onlineWeb || friend.onlineApp;
},
12 months ago
clear() {
this.timer && clearTimeout(this.timer);
this.friends = [];
this.timer = null;
},
12 months ago
loadFriend() {
return new Promise((resolve, reject) => {
http({
url: '/friend/list',
method: 'GET'
12 months ago
}).then(async (friends) => {
this.setFriends(friends);
resolve();
}).catch(e => {
reject(e);
})
});
}
},
getters: {
isFriend: (state) => (userId) => {
return state.friends.filter((f) => !f.deleted).some((f) => f.id == userId);
},
findFriend: (state) => (userId) => {
return state.friends.find((f) => f.id == userId);
}
}
12 months ago
});