修改关注的button,统一处理

This commit is contained in:
daihh
2022-11-02 17:21:51 +08:00
parent e20edb8542
commit 354d610c7a
3 changed files with 152 additions and 44 deletions

View File

@@ -0,0 +1,127 @@
<template>
<!--关注的button-->
<span>
<el-button v-loading="loading" v-if="hasFollow" :size="size" class="follow-btn" icon="el-icon-check" title="取消关注" @click="cancelFollow()" >已关注</el-button>
<el-button v-loading="loading" v-else :size="size" class="follow-btn" icon="el-icon-plus" title="关注TA" @click="addFollow()" type="primary">关注</el-button>
</span>
</template>
<script>
import { mapGetters} from 'vuex';
import apiFollow from "@/api/phase2/userfollow.js"
export default{
name:"followButton",
props:{
size:{
type:String,
default:'medium'
},
data:null,
auto:{ //是否自动检查
type:Boolean,
default:false
},
has:{
type:Boolean,
default:false
},
aid:{
type:String,
default:''
}
},
mounted(){
if(this.auto && this.aid){
this.autoCheck();
}else{
this.hasFollow=this.has;
}
},
data(){
return{
loading:false,
hasFollow:false
}
},
computed: {
...mapGetters(['userInfo']),
},
watch:{
has(newVal,oldVal){
this.has=newVal;
this.hasFollow=newVal;
if(newVal!=oldVal && this.auto){
this.autoCheck();
}
},
aid(newVal){
this.aid=newVal;
}
},
methods: {
autoCheck(){
if(this.aid){
apiFollow.checkFllow(this.aid).then(res => {
if (res.status == 200) {
this.hasFollow = res.result ? true : false;
}
})
}
},
cancelFollow(){
if(this.aid){
if(this.userInfo.aid==this.aid){
this.$message.error("不能对自己操作");
return;
}
let $this=this;
this.loading=true;
apiFollow.remove(this.aid).then(res=>{
if(res.status == 200) {
$this.hasFollow=false;
$this.$message.success("已取消关注");
$this.$emit('cancel',$this.aid,$this.data);
}else {
$this.$message.error("取消关注失败:"+res.message);
$this.$emit('error',$this.aid);
}
this.loading=false;
})
}
},
addFollow(){ //添加关注
if(this.aid){
if(this.userInfo.aid==this.aid){
this.$message.error("不能对自己操作");
return;
}
let $this=this;
this.loading=true;
apiFollow.save(this.aid).then(res=>{
if(res.status == 200) {
$this.hasFollow=true;
$this.$message.success("关注成功");
$this.$emit('add',$this.aid,$this.data);
} else {
$this.$message.error("关注失败:"+res.message);
$this.$emit('error',$this.aid);
}
this.loading=false;
})
}
}
}
}
</script>
<style scoped>
.follow-btn{
/* margin-top: 18px;
height: 40px;
width: 140px; */
}
</style>