增加微信通知功能
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
import request from "@/utils/request";
|
||||
|
||||
/*************************************************
|
||||
* 微信公众号绑定(租户端)
|
||||
* 流程:生成二维码 → 微信扫码关注 → 公众号回复验证码 → 输入验证码完成绑定
|
||||
*************************************************/
|
||||
|
||||
/** 发起绑定:生成带参数二维码 */
|
||||
export function startWechatMpBind() {
|
||||
return request({
|
||||
url: "/backend/wechatMp/bind/start",
|
||||
method: "post",
|
||||
});
|
||||
}
|
||||
|
||||
/** 查询绑定会话状态:0待扫码 1已扫码待核销 2已核销 3已过期 */
|
||||
export function getWechatMpBindStatus(scene) {
|
||||
return request({
|
||||
url: "/backend/wechatMp/bind/status",
|
||||
method: "get",
|
||||
params: { scene },
|
||||
});
|
||||
}
|
||||
|
||||
/** 核销验证码完成绑定 */
|
||||
export function confirmWechatMpBind(data) {
|
||||
return request({
|
||||
url: "/backend/wechatMp/bind/confirm",
|
||||
method: "post",
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
||||
/** 当前租户用户的绑定信息 */
|
||||
export function getMyWechatMpBinding() {
|
||||
return request({
|
||||
url: "/backend/wechatMp/my",
|
||||
method: "get",
|
||||
});
|
||||
}
|
||||
|
||||
/** 解除当前用户的微信绑定 */
|
||||
export function unbindWechatMp() {
|
||||
return request({
|
||||
url: "/backend/wechatMp/unbind",
|
||||
method: "post",
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,273 @@
|
||||
<template>
|
||||
<el-card shadow="never" class="wechat-bind-card">
|
||||
<template #header>
|
||||
<div class="card-head">
|
||||
<span class="card-title">微信绑定</span>
|
||||
<el-tag v-if="binding.bound" type="success" size="small">已绑定</el-tag>
|
||||
<el-tag v-else type="info" size="small">未绑定</el-tag>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- 已绑定 -->
|
||||
<div v-if="binding.bound" class="bound-box">
|
||||
<div class="bound-info">
|
||||
<p>微信昵称:{{ binding.nickname || "-" }}</p>
|
||||
<p>
|
||||
关注状态:
|
||||
<el-tag size="small" :type="Number(binding.subscribe) === 1 ? 'success' : 'danger'" effect="plain">
|
||||
{{ Number(binding.subscribe) === 1 ? "已关注" : "已取关" }}
|
||||
</el-tag>
|
||||
</p>
|
||||
<p>绑定时间:{{ binding.bind_time ? formatDateTime(binding.bind_time) : "-" }}</p>
|
||||
</div>
|
||||
<el-button type="danger" plain size="small" :loading="unbinding" @click="handleUnbind">
|
||||
解除绑定
|
||||
</el-button>
|
||||
</div>
|
||||
|
||||
<!-- 未绑定:扫码关注 → 公众号回复验证码 → 输入完成绑定 -->
|
||||
<div v-else class="bind-box">
|
||||
<p class="tip">绑定微信后,可通过公众号接收系统提醒与公告推送。</p>
|
||||
<el-steps :active="bindStep" simple class="bind-steps">
|
||||
<el-step title="扫码关注公众号" />
|
||||
<el-step title="公众号回复验证码" />
|
||||
<el-step title="输入验证码完成绑定" />
|
||||
</el-steps>
|
||||
<div class="bind-area">
|
||||
<div class="qr-box">
|
||||
<img v-if="qrUrl" :src="qrUrl" class="qr-img" alt="公众号二维码" />
|
||||
<el-button v-else type="primary" size="small" :loading="starting" @click="handleStart">
|
||||
获取绑定二维码
|
||||
</el-button>
|
||||
</div>
|
||||
<div class="code-box">
|
||||
<el-input v-model="code" placeholder="输入公众号回复的验证码" maxlength="8" class="code-input" />
|
||||
<el-button type="primary" size="small" :loading="confirming" @click="handleConfirm">
|
||||
完成绑定
|
||||
</el-button>
|
||||
<p class="tip">扫码关注后公众号会自动回复 6 位验证码({{ ttlText }})。</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</el-card>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { ref, onMounted, onUnmounted } from "vue";
|
||||
import { ElMessage, ElMessageBox } from "element-plus";
|
||||
// 该卡片展示到秒的时间:YYYY-MM-DD HH:mm:ss
|
||||
import { formatDateTime } from "@/utils/datetime";
|
||||
import {
|
||||
startWechatMpBind,
|
||||
getWechatMpBindStatus,
|
||||
confirmWechatMpBind,
|
||||
getMyWechatMpBinding,
|
||||
unbindWechatMp,
|
||||
} from "@/api/wechatMp";
|
||||
|
||||
const binding = ref({ bound: false });
|
||||
const starting = ref(false);
|
||||
const confirming = ref(false);
|
||||
const unbinding = ref(false);
|
||||
const qrUrl = ref("");
|
||||
const scene = ref("");
|
||||
const code = ref("");
|
||||
const bindStep = ref(0);
|
||||
const ttlText = ref("5 分钟内有效");
|
||||
let pollTimer = null;
|
||||
|
||||
const stopPoll = () => {
|
||||
if (pollTimer) {
|
||||
clearInterval(pollTimer);
|
||||
pollTimer = null;
|
||||
}
|
||||
};
|
||||
|
||||
const loadBinding = async () => {
|
||||
try {
|
||||
const res = await getMyWechatMpBinding();
|
||||
if (res.code === 200) {
|
||||
binding.value = res.data || { bound: false };
|
||||
}
|
||||
} catch {
|
||||
binding.value = { bound: false };
|
||||
}
|
||||
};
|
||||
|
||||
const handleStart = async () => {
|
||||
starting.value = true;
|
||||
try {
|
||||
const res = await startWechatMpBind();
|
||||
if (res.code !== 200) {
|
||||
ElMessage.error(res.msg || "获取二维码失败,请联系管理员检查公众号配置");
|
||||
return;
|
||||
}
|
||||
scene.value = res.data?.scene || "";
|
||||
qrUrl.value = res.data?.qrcode_url || "";
|
||||
bindStep.value = 0;
|
||||
const ttl = Number(res.data?.expires_in || 300);
|
||||
ttlText.value = ttl >= 60 ? `${Math.floor(ttl / 60)} 分钟内有效` : `${ttl} 秒内有效`;
|
||||
stopPoll();
|
||||
pollTimer = setInterval(async () => {
|
||||
if (!scene.value) return;
|
||||
try {
|
||||
const st = await getWechatMpBindStatus(scene.value);
|
||||
const status = Number(st?.data?.status ?? 0);
|
||||
if (status === 1) {
|
||||
bindStep.value = 2; // 已扫码,公众号已回复验证码
|
||||
}
|
||||
if (status === 3) {
|
||||
stopPoll();
|
||||
qrUrl.value = "";
|
||||
ElMessage.warning("二维码已过期,请重新获取");
|
||||
}
|
||||
} catch {
|
||||
// 轮询失败忽略
|
||||
}
|
||||
}, 3000);
|
||||
} catch (e) {
|
||||
ElMessage.error(e?.message || "获取二维码失败");
|
||||
} finally {
|
||||
starting.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
const handleConfirm = async () => {
|
||||
if (!scene.value) {
|
||||
ElMessage.warning("请先获取绑定二维码并扫码关注");
|
||||
return;
|
||||
}
|
||||
if (!code.value.trim()) {
|
||||
ElMessage.warning("请输入公众号回复的验证码");
|
||||
return;
|
||||
}
|
||||
confirming.value = true;
|
||||
try {
|
||||
const res = await confirmWechatMpBind({ scene: scene.value, code: code.value.trim() });
|
||||
if (res.code === 200) {
|
||||
ElMessage.success("微信绑定成功");
|
||||
stopPoll();
|
||||
qrUrl.value = "";
|
||||
code.value = "";
|
||||
scene.value = "";
|
||||
bindStep.value = 0;
|
||||
await loadBinding();
|
||||
} else {
|
||||
ElMessage.error(res.msg || "绑定失败");
|
||||
}
|
||||
} catch (e) {
|
||||
ElMessage.error(e?.message || "绑定失败");
|
||||
} finally {
|
||||
confirming.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
const handleUnbind = async () => {
|
||||
try {
|
||||
await ElMessageBox.confirm("确定解除微信绑定吗?解除后将无法接收微信推送。", "提示", {
|
||||
type: "warning",
|
||||
});
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
unbinding.value = true;
|
||||
try {
|
||||
const res = await unbindWechatMp();
|
||||
if (res.code === 200) {
|
||||
ElMessage.success("已解除绑定");
|
||||
await loadBinding();
|
||||
} else {
|
||||
ElMessage.error(res.msg || "解绑失败");
|
||||
}
|
||||
} catch (e) {
|
||||
ElMessage.error(e?.message || "解绑失败");
|
||||
} finally {
|
||||
unbinding.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
onMounted(loadBinding);
|
||||
onUnmounted(stopPoll);
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.wechat-bind-card {
|
||||
margin-top: 20px;
|
||||
}
|
||||
|
||||
.card-head {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.card-title {
|
||||
font-size: 16px;
|
||||
font-weight: 600;
|
||||
color: #303133;
|
||||
}
|
||||
|
||||
.bound-box {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: flex-start;
|
||||
gap: 16px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.bound-info p {
|
||||
margin: 4px 0;
|
||||
font-size: 14px;
|
||||
color: #606266;
|
||||
}
|
||||
|
||||
.tip {
|
||||
font-size: 12px;
|
||||
color: #909399;
|
||||
margin: 0 0 12px;
|
||||
}
|
||||
|
||||
.bind-steps {
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.bind-area {
|
||||
display: flex;
|
||||
gap: 24px;
|
||||
align-items: flex-start;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.qr-box {
|
||||
width: 160px;
|
||||
height: 160px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
border: 1px dashed #dcdfe6;
|
||||
border-radius: 8px;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.qr-img {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: contain;
|
||||
}
|
||||
|
||||
.code-box {
|
||||
flex: 1;
|
||||
min-width: 220px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.code-input {
|
||||
width: 220px;
|
||||
}
|
||||
|
||||
.code-box .tip {
|
||||
margin: 0;
|
||||
}
|
||||
</style>
|
||||
@@ -24,6 +24,9 @@
|
||||
@bind-phone="bindPhoneDialogVisible = true"
|
||||
@bind-email="bindEmailDialogVisible = true"
|
||||
/>
|
||||
|
||||
<!-- 微信绑定:扫码关注公众号获取验证码,绑定后可接收提醒/公告 -->
|
||||
<WechatBindCard />
|
||||
</el-col>
|
||||
</el-row>
|
||||
|
||||
@@ -71,6 +74,7 @@ import EditProfileDialog from "./components/EditProfileDialog.vue";
|
||||
import ChangePasswordDialog from "./components/ChangePasswordDialog.vue";
|
||||
import BindPhoneDialog from "./components/BindPhoneDialog.vue";
|
||||
import BindEmailDialog from "./components/BindEmailDialog.vue";
|
||||
import WechatBindCard from "./components/WechatBindCard.vue";
|
||||
|
||||
const router = useRouter();
|
||||
const authStore = useAuthStore();
|
||||
|
||||
Reference in New Issue
Block a user