️ 扫码逻辑调整,自动获取结果/刷新

This commit is contained in:
BTMuli
2023-12-23 21:20:21 +08:00
parent 18fd7fe59f
commit 5435e75ca2
2 changed files with 48 additions and 39 deletions

View File

@@ -1,5 +1,5 @@
<template> <template>
<TOverlay v-model="visible" hide blur-val="20px"> <TOverlay v-model="visible" hide blur-val="20px" :to-click="onCancel">
<div class="tog-box"> <div class="tog-box">
<div class="tog-top"> <div class="tog-top">
<div class="tog-title">请使用米游社APP进行扫码操作</div> <div class="tog-title">请使用米游社APP进行扫码操作</div>
@@ -8,17 +8,13 @@
<div class="tog-mid"> <div class="tog-mid">
<qrcode-vue class="tog-qr" :value="qrCode" render-as="svg" /> <qrcode-vue class="tog-qr" :value="qrCode" render-as="svg" />
</div> </div>
<div class="tog-bottom">
<v-btn class="tog-btn" @click="onCancel">取消</v-btn>
<v-btn class="tog-btn" @click="freshQr">刷新</v-btn>
<v-btn class="tog-btn" :loading="loading" @click="getData">已扫码</v-btn>
</div>
</div> </div>
</TOverlay> </TOverlay>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { storeToRefs } from "pinia";
import QrcodeVue from "qrcode.vue"; import QrcodeVue from "qrcode.vue";
import { computed, reactive, ref, watch } from "vue"; import { computed, onUnmounted, reactive, ref, watch } from "vue";
import Mys from "../../plugins/Mys"; import Mys from "../../plugins/Mys";
import { useUserStore } from "../../store/modules/user"; import { useUserStore } from "../../store/modules/user";
@@ -44,7 +40,9 @@ const visible = computed({
emits("update:modelValue", value); emits("update:modelValue", value);
}, },
}); });
const loading = ref<boolean>(false); const isCycle = ref<boolean>(false);
let cycleTimer: NodeJS.Timeout | null = null;
const qrCode = ref<string>(""); const qrCode = ref<string>("");
const ticket = ref<string>(""); const ticket = ref<string>("");
const cookie = reactive<TGApp.User.Account.Cookie>({ const cookie = reactive<TGApp.User.Account.Cookie>({
@@ -58,14 +56,24 @@ const cookie = reactive<TGApp.User.Account.Cookie>({
ltoken: "", ltoken: "",
}); });
const userStore = useUserStore(); const userStore = storeToRefs(useUserStore());
watch(visible, async (value) => { watch(visible, async (value) => {
if (value) { if (value) {
await freshQr(); await freshQr();
isCycle.value = true;
cycleTimer = setInterval(cycleGetData, 1000);
} }
}); });
function onCancel(): void {
visible.value = false;
showSnackbar({
text: "已取消登录",
color: "cancel",
});
}
async function freshQr(): Promise<void> { async function freshQr(): Promise<void> {
const res = await Mys.User.getQr(); const res = await Mys.User.getQr();
if ("retcode" in res) { if ("retcode" in res) {
@@ -88,25 +96,36 @@ async function freshQr(): Promise<void> {
} }
} }
async function getData(): Promise<void> { async function cycleGetData() {
loading.value = true; if (!isCycle.value) {
if (cycleTimer) clearInterval(cycleTimer);
return;
}
const res = await Mys.User.getData(ticket.value); const res = await Mys.User.getData(ticket.value);
console.log(res);
if ("retcode" in res) { if ("retcode" in res) {
showSnackbar({ showSnackbar({
text: `[${res.retcode}] ${res.message}`, text: `[${res.retcode}] ${res.message}`,
color: "error", color: "error",
}); });
} else if (res.stat === "Init") { if (res.retcode === -106) {
showSnackbar({ // 二维码过期
text: "请先扫码", await freshQr();
color: "error", } else {
}); // 取消轮询
} else if (res.stat === "Scanned") { isCycle.value = false;
showSnackbar({ if (cycleTimer) clearInterval(cycleTimer);
text: "请在米游社APP上确认登录", visible.value = false;
color: "error", }
}); return;
} else { }
if (res.stat === "Init" || res.stat === "Scanned") {
return;
}
if (res.stat === "Confirmed") {
// 取消轮询
isCycle.value = false;
if (cycleTimer) clearInterval(cycleTimer);
const data: TGApp.Plugins.Mys.GameLogin.StatusPayloadRaw = JSON.parse(res.payload.raw); const data: TGApp.Plugins.Mys.GameLogin.StatusPayloadRaw = JSON.parse(res.payload.raw);
cookie.account_id = data.uid; cookie.account_id = data.uid;
cookie.ltuid = data.uid; cookie.ltuid = data.uid;
@@ -121,10 +140,6 @@ async function getData(): Promise<void> {
} }
} }
function onCancel(): void {
visible.value = false;
}
async function getTokens(): Promise<void> { async function getTokens(): Promise<void> {
const stokenRes = await TGRequest.User.bgGameToken.getStoken( const stokenRes = await TGRequest.User.bgGameToken.getStoken(
cookie.account_id, cookie.account_id,
@@ -141,8 +156,13 @@ async function getTokens(): Promise<void> {
if (typeof cookieTokenRes === "string") cookie.cookie_token = cookieTokenRes; if (typeof cookieTokenRes === "string") cookie.cookie_token = cookieTokenRes;
const ltokenRes = await TGRequest.User.bySToken.getLToken(cookie.mid, cookie.stoken); const ltokenRes = await TGRequest.User.bySToken.getLToken(cookie.mid, cookie.stoken);
if (typeof ltokenRes === "string") cookie.ltoken = ltokenRes; if (typeof ltokenRes === "string") cookie.ltoken = ltokenRes;
await userStore.saveCookie(cookie); userStore.cookie.value = cookie;
} }
onUnmounted(() => {
isCycle.value = false;
if (cycleTimer) clearInterval(cycleTimer);
});
</script> </script>
<style lang="css" scoped> <style lang="css" scoped>
.tog-box { .tog-box {

View File

@@ -253,18 +253,7 @@ function toOuter(url: string): void {
// 扫码登录 // 扫码登录
async function confirmScanLogin(): Promise<void> { async function confirmScanLogin(): Promise<void> {
const confirmRes = await showConfirm({ scan.value = true;
title: "请使用米游社 APP 执行操作",
text: "请在成功后刷新数据",
});
if (confirmRes) {
scan.value = true;
} else {
showSnackbar({
color: "grey",
text: "已取消扫码登录",
});
}
} }
// 刷新用户信息 // 刷新用户信息