366 lines
10 KiB
Vue
366 lines
10 KiB
Vue
<template>
|
||
<div class="bind-container">
|
||
<div class="bind-item">
|
||
<div class="bind-info">
|
||
<div class="label">微信绑定</div>
|
||
<div class="desc">绑定微信后,即可快速登录,且能接收系统重要通知</div>
|
||
</div>
|
||
<div class="bind-action">
|
||
<template v-if="isBound">
|
||
<span class="bound-status">
|
||
<el-icon><CircleCheckFilled /></el-icon>
|
||
已绑定:{{ binderInfo.nickname || '微信用户' }}
|
||
</span>
|
||
<el-button type="danger" plain size="small" :loading="unbinding" @click="handleUnbind">解绑</el-button>
|
||
</template>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 未绑定时内联显示二维码区域 -->
|
||
<div v-if="!isBound" class="qrcode-section">
|
||
<div class="qrcode-wrapper">
|
||
<!-- 加载中 -->
|
||
<div v-if="loading" class="status-overlay loading-overlay">
|
||
<el-icon class="is-loading"><Loading /></el-icon>
|
||
<span>加载中...</span>
|
||
</div>
|
||
<!-- 二维码过期 -->
|
||
<div v-else-if="qrStatus === 'expired'" class="status-overlay expired-overlay" @click="refreshQrCode">
|
||
<el-icon><RefreshRight /></el-icon>
|
||
<span>二维码已过期,点击刷新</span>
|
||
</div>
|
||
<!-- 扫码成功等待确认 -->
|
||
<div v-else-if="qrStatus === 'scanned'" class="status-overlay scanned-overlay">
|
||
<el-icon><CircleCheckFilled /></el-icon>
|
||
<span>扫码成功,正在确认...</span>
|
||
</div>
|
||
<!-- 二维码图片 -->
|
||
<img v-else-if="qrCodeUrl" :src="qrCodeUrl" alt="微信二维码" class="qrcode-img">
|
||
<!-- 初始占位 -->
|
||
<div v-else class="status-overlay loading-overlay">
|
||
<el-icon class="is-loading"><Loading /></el-icon>
|
||
<span>正在获取二维码...</span>
|
||
</div>
|
||
|
||
<p class="qrcode-tip">
|
||
<template v-if="qrStatus === 'scanned'">请在微信中确认绑定</template>
|
||
<template v-else-if="qrStatus === 'expired'">二维码已失效,点击上方刷新</template>
|
||
<template v-else>请使用微信扫码关注公众号完成绑定</template>
|
||
</p>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { ref, reactive, onMounted, onUnmounted } from 'vue'
|
||
import { ElMessage, ElMessageBox } from 'element-plus'
|
||
import { CircleCheckFilled, Loading, RefreshRight } from '@element-plus/icons-vue'
|
||
import { getBindWechatCode, unbindWechat, getBindInfo } from '@/api/profile'
|
||
|
||
type QrStatus = 'waiting' | 'pending' | 'scanned' | 'bound' | 'expired'
|
||
|
||
const isBound = ref(false)
|
||
const unbinding = ref(false)
|
||
const binderInfo = reactive({ nickname: '', openid: '' })
|
||
const loading = ref(false)
|
||
const qrCodeUrl = ref('')
|
||
const qrStatus = ref<QrStatus>('waiting')
|
||
|
||
const qrBaseUrl = 'https://ye4s.wms.gztbyc.com/qrcode/build?text='
|
||
const bindLinkTemplate = 'https://ye4s.wms.gztbyc.com/mini/?spm=0.3.${code}.1.4'
|
||
|
||
/** 是否已获取到首次二维码(决定后续请求是否携带 longPoll) */
|
||
let hasInitQr = false
|
||
/** 停止长轮询的标志位 */
|
||
let stopPollFlag = false
|
||
/** 轮询版本号,每次停止时递增,用于丢弃过期回调 */
|
||
let pollVersion = 0
|
||
|
||
/** 构造完整二维码 URL */
|
||
const getFullQrUrl = (code: string) => {
|
||
const targetUrl = bindLinkTemplate.replace('${code}', code)
|
||
return qrBaseUrl + encodeURIComponent(targetUrl)
|
||
}
|
||
|
||
/**
|
||
* 处理后端返回数据,更新组件状态。
|
||
* @returns true 表示已绑定,轮询应终止
|
||
*/
|
||
const processBindData = (data: any, showSuccessMsg = false): boolean => {
|
||
const boundNow = data.status === 'bound' || data.is_bound === true
|
||
if (boundNow) {
|
||
isBound.value = true
|
||
binderInfo.nickname = data.nickname || ''
|
||
binderInfo.openid = data.openid || ''
|
||
if (showSuccessMsg) {
|
||
ElMessage.success(`微信绑定成功:${data.nickname || '微信用户'}`)
|
||
}
|
||
return true
|
||
}
|
||
const mapped: QrStatus = data.status === 'pending' ? 'waiting' : (data.status as QrStatus)
|
||
qrStatus.value = mapped
|
||
if (data.code) {
|
||
qrCodeUrl.value = getFullQrUrl(data.code)
|
||
}
|
||
return false
|
||
}
|
||
|
||
/**
|
||
* 启动长轮询循环。
|
||
* 每次请求携带 longPoll + waitForChange,服务端最长等待 60 秒后返回;
|
||
* 收到响应后立刻发起下一次,直至绑定成功或 stopPollFlag 为 true。
|
||
*/
|
||
const startLongPoll = () => {
|
||
stopPollFlag = false
|
||
const version = pollVersion
|
||
const poll = async () => {
|
||
if (stopPollFlag || pollVersion !== version || isBound.value) return
|
||
try {
|
||
const res = await getBindWechatCode({
|
||
longPoll: true,
|
||
waitForChange: true,
|
||
waitTimeoutMs: 60000
|
||
})
|
||
if (stopPollFlag || pollVersion !== version || isBound.value) return
|
||
const data = res?.data
|
||
if (data) {
|
||
const bound = processBindData(data, true)
|
||
if (bound) return // 已绑定,终止轮询
|
||
}
|
||
// 继续下一轮(不等待,立即发起)
|
||
poll()
|
||
} catch {
|
||
// 网络异常时静默处理,稍后重试
|
||
if (!stopPollFlag && pollVersion === version && !isBound.value) {
|
||
setTimeout(poll, 3000)
|
||
}
|
||
}
|
||
}
|
||
poll()
|
||
}
|
||
|
||
/** 停止长轮询 */
|
||
const stopPolling = () => {
|
||
stopPollFlag = true
|
||
pollVersion++ // 递增版本号,使所有进行中的回调失效
|
||
}
|
||
|
||
/**
|
||
* 首次初始化二维码:携带 init:true,服务端立即返回二维码数据,不进入长等待。
|
||
* 成功拿到数据后启动长轮询。
|
||
*/
|
||
const initQrCode = async () => {
|
||
loading.value = true
|
||
qrStatus.value = 'waiting'
|
||
qrCodeUrl.value = ''
|
||
hasInitQr = false
|
||
try {
|
||
const res = await getBindWechatCode({ init: true })
|
||
const data = res?.data
|
||
if (!data) throw new Error('无返回数据')
|
||
const bound = processBindData(data)
|
||
if (!bound) {
|
||
hasInitQr = true
|
||
startLongPoll()
|
||
}
|
||
} catch {
|
||
ElMessage.error('获取二维码失败,请重试')
|
||
} finally {
|
||
loading.value = false
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 手动刷新二维码(过期后点击触发):
|
||
* 携带 forceRefresh:true + init:true,强制跳过缓存并立即返回。
|
||
*/
|
||
const refreshQrCode = async () => {
|
||
stopPolling()
|
||
loading.value = true
|
||
qrStatus.value = 'waiting'
|
||
qrCodeUrl.value = ''
|
||
hasInitQr = false
|
||
try {
|
||
const res = await getBindWechatCode({ forceRefresh: true, init: true })
|
||
const data = res?.data
|
||
if (!data) throw new Error('无返回数据')
|
||
const bound = processBindData(data)
|
||
if (!bound) {
|
||
hasInitQr = true
|
||
startLongPoll()
|
||
}
|
||
} catch {
|
||
ElMessage.error('获取二维码失败,请重试')
|
||
} finally {
|
||
loading.value = false
|
||
}
|
||
}
|
||
|
||
/** 初始化:先查询绑定状态,未绑定则自动加载二维码 */
|
||
const fetchBindInfo = async () => {
|
||
try {
|
||
const res = await getBindInfo()
|
||
const data = res?.data
|
||
if (data?.status === 'bound' || data?.isBound) {
|
||
isBound.value = true
|
||
binderInfo.nickname = data.nickname || ''
|
||
binderInfo.openid = data.openid || ''
|
||
} else {
|
||
isBound.value = false
|
||
await initQrCode()
|
||
}
|
||
} catch {
|
||
// 查询失败时仍尝试显示二维码
|
||
isBound.value = false
|
||
await initQrCode()
|
||
}
|
||
}
|
||
|
||
/** 解绑微信 */
|
||
const handleUnbind = () => {
|
||
ElMessageBox.confirm('确定要解除微信绑定吗?', '提示', {
|
||
confirmButtonText: '确定',
|
||
cancelButtonText: '取消',
|
||
type: 'warning'
|
||
}).then(async () => {
|
||
unbinding.value = true
|
||
try {
|
||
await unbindWechat()
|
||
isBound.value = false
|
||
binderInfo.nickname = ''
|
||
binderInfo.openid = ''
|
||
ElMessage.success('微信绑定已解除')
|
||
// 解绑后重新加载二维码
|
||
await initQrCode()
|
||
} catch {
|
||
ElMessage.error('解绑失败,请重试')
|
||
} finally {
|
||
unbinding.value = false
|
||
}
|
||
}).catch(() => {})
|
||
}
|
||
|
||
onMounted(() => {
|
||
fetchBindInfo()
|
||
})
|
||
|
||
onUnmounted(() => {
|
||
stopPolling()
|
||
})
|
||
</script>
|
||
|
||
<style lang="scss" scoped>
|
||
.bind-container {
|
||
padding: 10px 0;
|
||
|
||
.bind-item {
|
||
display: flex;
|
||
justify-content: space-between;
|
||
align-items: center;
|
||
padding: 20px;
|
||
border: 1px solid var(--el-border-color-lighter);
|
||
border-radius: 8px;
|
||
background-color: var(--el-fill-color-blank);
|
||
|
||
.bind-info {
|
||
.label {
|
||
font-size: 16px;
|
||
font-weight: bold;
|
||
color: var(--el-text-color-primary);
|
||
margin-bottom: 8px;
|
||
}
|
||
.desc {
|
||
font-size: 14px;
|
||
color: var(--el-text-color-secondary);
|
||
}
|
||
}
|
||
|
||
.bind-action {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 15px;
|
||
|
||
.bound-status {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 5px;
|
||
color: var(--el-color-success);
|
||
font-size: 14px;
|
||
|
||
.el-icon {
|
||
font-size: 18px;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
.qrcode-section {
|
||
margin-top: 16px;
|
||
display: flex;
|
||
justify-content: center;
|
||
}
|
||
|
||
.qrcode-wrapper {
|
||
display: flex;
|
||
flex-direction: column;
|
||
align-items: center;
|
||
padding: 20px;
|
||
border: 1px solid var(--el-border-color-lighter);
|
||
border-radius: 8px;
|
||
background-color: var(--el-fill-color-blank);
|
||
|
||
.qrcode-img {
|
||
width: 200px;
|
||
height: 200px;
|
||
border: 1px solid var(--el-border-color);
|
||
border-radius: 4px;
|
||
}
|
||
|
||
.qrcode-tip {
|
||
margin-top: 15px;
|
||
font-size: 13px;
|
||
color: var(--el-text-color-secondary);
|
||
text-align: center;
|
||
}
|
||
|
||
.status-overlay {
|
||
width: 200px;
|
||
height: 200px;
|
||
display: flex;
|
||
flex-direction: column;
|
||
align-items: center;
|
||
justify-content: center;
|
||
gap: 10px;
|
||
font-size: 13px;
|
||
border: 1px solid var(--el-border-color);
|
||
border-radius: 4px;
|
||
|
||
.el-icon {
|
||
font-size: 36px;
|
||
}
|
||
}
|
||
|
||
.loading-overlay {
|
||
background: var(--el-fill-color-light);
|
||
color: var(--el-text-color-secondary);
|
||
.el-icon { color: var(--el-color-primary); }
|
||
}
|
||
|
||
.expired-overlay {
|
||
background: var(--el-fill-color);
|
||
color: var(--el-text-color-secondary);
|
||
cursor: pointer;
|
||
transition: background 0.2s;
|
||
&:hover { background: var(--el-fill-color-dark); }
|
||
.el-icon { color: var(--el-color-warning); }
|
||
}
|
||
|
||
.scanned-overlay {
|
||
background: var(--el-color-success-light-9);
|
||
color: var(--el-color-success);
|
||
.el-icon { color: var(--el-color-success); }
|
||
}
|
||
}
|
||
}
|
||
</style> |