Skip to content

Commit

Permalink
bugfix: follow btn
Browse files Browse the repository at this point in the history
  • Loading branch information
bbbbbbbbbbbbba committed Nov 9, 2024
1 parent 7a53863 commit 3af0314
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
17 changes: 10 additions & 7 deletions site/src/components/FollowBtn.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,37 @@ const props = defineProps({
type: Number,
required: true,
},
followed: {
type: Boolean,
default: false,
},
});
const emits = defineEmits(["onFollowed"]);
const { data: followed } = await useAsyncData(`followed:${props.userId}`, () =>
useMyFetch(`/api/fans/isfollowed?userId=${props.userId}`)
);
async function follow() {
if (!userStore.isLogin) {
useMsgSignIn();
return;
}
try {
if (props.followed) {
if (followed.value) {
await useHttpPostForm("/api/fans/unfollow", {
body: {
userId: props.userId,
},
});
followed.value = false;
emits("onFollowed", props.userId, false);
useMsgSuccess("取消关注成功");
// useMsgSuccess("取消关注成功");
} else {
await useHttpPostForm("/api/fans/follow", {
body: {
userId: props.userId,
},
});
followed.value = true;
emits("onFollowed", props.userId, true);
useMsgSuccess("关注成功");
// useMsgSuccess("关注成功");
}
} catch (e) {
useMsgError(e.message || e);
Expand Down
10 changes: 0 additions & 10 deletions site/src/components/UserProfile.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
<follow-btn
v-if="!currentUser || currentUser.id !== localUser.id"
:user-id="localUser.id"
:followed="followed"
@onFollowed="onFollowed"
/>
</div>
</div>
Expand Down Expand Up @@ -70,10 +68,6 @@ const backgroundImage = computed(() => {
return defaultUserBg;
});
const { data: followed } = await useAsyncData("followed", () =>
useMyFetch(`/api/fans/isfollowed?userId=${localUser.value.id}`)
);
async function uploadBackground(e) {
const files = e.target.files;
if (files.length <= 0) {
Expand Down Expand Up @@ -102,10 +96,6 @@ async function uploadBackground(e) {
console.error(e);
}
}
function onFollowed(userId, _followed) {
followed.value = _followed;
}
</script>

<style lang="scss" scoped>
Expand Down
7 changes: 6 additions & 1 deletion site/src/composables/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ export function useMyFetch(url, options = {}) {
return;
}
if (data.value.success) {
resolve(data.value.data);
// TODO 这里如果数据是空,那么返回一个空对象
if (data.value.data === null || data.value.data === undefined) {
resolve(data.value.data || {});
} else {
resolve(data.value.data);
}
} else {
reject(data.value);
}
Expand Down

0 comments on commit 3af0314

Please sign in to comment.