From b3bd7a75d54038765915584ca76b66a4586c43da Mon Sep 17 00:00:00 2001 From: ronger Date: Tue, 24 Aug 2021 22:32:08 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E8=8E=B7=E5=8F=96=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E5=85=B3=E6=B3=A8=E7=94=A8=E6=88=B7/=E7=B2=89=E4=B8=9D?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/layouts/pc/header.vue | 7 ++- store/follow.js | 95 ++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 store/follow.js diff --git a/components/layouts/pc/header.vue b/components/layouts/pc/header.vue index ff62cbb..5b784ab 100644 --- a/components/layouts/pc/header.vue +++ b/components/layouts/pc/header.vue @@ -300,9 +300,12 @@ export default { } }, mounted() { - let user = this.user; + let _ts = this; + let user = _ts.user; if (user) { - this.getUnreadNotifications(); + _ts.getUnreadNotifications(); + _ts.$store.dispatch('follow/fetchUserFollowerList'); + _ts.$store.dispatch('follow/fetchUserFollowingList'); // sockClient.initSocket(this.$store.state.userInfo); } } diff --git a/store/follow.js b/store/follow.js new file mode 100644 index 0000000..ccefd4a --- /dev/null +++ b/store/follow.js @@ -0,0 +1,95 @@ +/** + * @file 用户关注用户、粉丝数据 / ES module + * @module store/category + * @author Ronger + */ + +export const USER_API_PATH = '/api/user' + +export const state = () => { + return { + fetching: false, + followers: { + users: [], + pagination: {} + }, + followings: { + users: [], + pagination: {} + } + } +} + +export const mutations = { + updateFetching(state, action) { + state.fetching = action + }, + updateUserFollowerList(state, action) { + state.followers = action + }, + updateUserFollowingList(state, action) { + state.followings = action + } +} + +export const actions = { + fetchUserFollowerList(store) { + const {commit, rootState} = store + commit('updateFetching', true); + commit('updateUserFollowerList', { + users: [], + pagination: {} + }) + const userInfo = rootState.userInfo + if (userInfo == null) { + return + } + let account = userInfo.account + return this.$axios + .$get(`${USER_API_PATH}/${account}/followers`) + .then(response => { + commit('updateUserFollowerList', response) + commit('updateFetching', false) + }) + .catch(error => { + commit('updateFetching', false) + }) + }, + fetchUserFollowingList(store) { + const {commit, rootState} = store + commit('updateFetching', true); + commit('updateUserFollowingList', { + users: [], + pagination: {} + }) + const userInfo = rootState.userInfo + if (userInfo == null) { + return + } + let account = userInfo.account + return this.$axios + .$get(`${USER_API_PATH}/${account}/followings`) + .then(response => { + commit('updateUserFollowingList', response) + commit('updateFetching', false) + }) + .catch(error => { + commit('updateFetching', false) + }) + } +} + +export const getters = { + isFollower: (state) => (scenes) => { + let isFollower = false + let users = state.followings.users + for (let i in users) { + let user = users[i] + if (user.idUser === scenes) { + isFollower = true; + break; + } + } + return isFollower + } +}