widget-follow.vue
1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<template>
<AuthComponent class="btn-follow hover-opacity" :class="followClass" @click="onFollow">
{{followText}}
</AuthComponent>
</template>
<script>
import {createNamespacedHelpers} from 'vuex';
const {mapActions} = createNamespacedHelpers('user');
export default {
name: 'WidgetFollow',
props: {
authorUid: [Number, String],
follow: Boolean,
authorType: {
type: [Number, String],
default: 1
}
},
data() {
return {
loading: false,
followStatus: this.follow
};
},
watch: {
follow(val) {
this.followStatus = val;
}
},
computed: {
followClass() {
return {
loading: this.loading,
follow: this.followStatus
};
},
followText() {
return this.followStatus ? '已关注' : '关注';
}
},
methods: {
...mapActions(['followUser']),
async onFollow() {
if (this.loading) {
return;
}
this.loading = true;
const result = await this.followUser({
followUid: this.authorUid,
status: this.followStatus ? 1 : 0,
authorType: this.authorType
});
this.loading = false;
if (result.code === 200) {
this.followStatus = !this.followStatus;
this.$emit('on-follow', this.followStatus);
} else {
this.$createToast && this.$createToast({
txt: result.message || '服务器开小差了',
type: 'warn',
time: 1000
}).show();
}
}
}
};
</script>
<style lang="scss" scoped>
.btn-follow {
width: 120px;
height: 50px;
padding: 0;
font-size: 26px;
border-radius: 3PX;
background-color: #222;
color: #fff;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
&.follow {
border: solid 1px #4a4a4a;
background-color: #fff;
color: #000;
}
}
</style>