一、企业微信绑定解绑
项目中需要对账号进行企业微信的绑定和解绑
企业微信页面显示:
<FormItem label="企业微信:">
{{account.wechat}}
<span class="link" @click="bindWechat" v-if="!account.wechat">绑定</span>
<span class="link" @click="unBindWehat" v-else>解绑</span>
</FormItem>
二维码模态框:
<Modal v-model="bindWechatModal">
<div>
<iframe height="400" width="400" frameborder="0" :src="bindWechatUrl"></iframe>
</div>
</Modal>
1.点击绑定微信,显示二维码模态框
bindWechat() {
let param = {
redirectUri: location.href.toString().split('?')[0]
};
R.Account.buildAuthorizationUrl(param).then(resp => {
if (resp.code) {
this.bindWechatModal = true;
this.bindWechatUrl = resp.body;
}
});
}
2.企业微信扫描后,会重新刷新页面
关键就在页面重新渲染,调用mounted里的方法,
扫描后此时this.$route.query.code是有的,所以会调用bindCpAccount方法
mounted() {
if (this.$route.query.code && !this.account.wechat) {
this.bindCpAccount();
}
}
绑定接口
bindCpAccount() {
let param = {
code: this.$route.query.code
};
R.Account.bindWechat(param).then(resp => {
if (resp.code) {
this.$Message.success('绑定成功');
this.account.wechat = resp.body;
this.bindWechatModal = false;
this.$router.push(this.$route.path);
}
});
}
此时才算是成功绑定了企业微信,页面也会重新获取到企业微信数据。
3.解绑企业微信:
点击解绑
unBindWehat() {
this.$Confirm('确定要解绑当前企业微信吗?', '温馨提示').then(() => {
R.Account.unbindWechat().then(resp => {
if (resp.code) {
// window.location.href = this.$route.path; // 解绑后不刷新页面,否则不能出现重新绑定的提示
this.account.wechat = '';
// 提示绑定新企业微信
this.$Message.info('该账号未绑定企业微信,绑定后进行正常操作');
this.bindWechat();
} else {
this.$Message.warn(resp.msg);
}
});
}).catch(() => {});
},
解绑调接口后分两种情况:
(1)解绑后提示重新绑定
不需要 window.location.href = this.$route.path;
需要 this.bindWechat();
以上代码就是提示重新绑定的情况。
(2)解绑后不提示重新绑定
需要 window.location.href = this.$route.path;
不需要 this.bindWechat();