feat: 调整优化登录的逻辑

This commit is contained in:
zoujing 2025-09-15 22:52:44 +08:00
parent 66c256cefd
commit fc63bbc994
15 changed files with 190 additions and 159 deletions

View File

@ -1,27 +1,25 @@
import { loginAuth, checkPhone, bindPhone } from "@/manager/LoginManager";
import { loginAuth, bindPhone } from "@/manager/LoginManager";
// 跳转登录
export const goLogin = () => uni.reLaunch({ url: "/pages/login/index" });
export const goLogin = () => uni.navigateTo({ url: "/pages/login/index" });
// 登录成功后,返回上一页
export const goBack = () => uni.navigateBack({ delta: 1 });
// 登录逻辑
export const onLogin = (e) => {
const token = uni.getStorageSync("token");
if (token) return;
export const onLogin = async (e) => {
return new Promise(async (resolve) => {
await loginAuth().then(async () => {
const { code } = e.detail;
console.info("onLogin code: ", code);
loginAuth().then(async () => {
// 检测是否绑定手机号
const res = await checkPhone();
if (res.data) return;
const params = { wechatPhoneCode: code, clientId: "2" };
// 绑定手机号
bindPhone(params);
const params = { wechatPhoneCode: code, clientId: "2" };
const res = await bindPhone(params);
if (res.data) {
resolve();
}
});
});
};
@ -29,9 +27,13 @@ export const onLogin = (e) => {
export const checkToken = async () => {
return new Promise((resolve) => {
const token = uni.getStorageSync("token");
if (!token) return;
resolve(token);
if (!token) {
console.log("token不存在重新登录");
loginAuth().then(() => {
resolve();
});
return;
}
resolve();
});
};

View File

@ -20,9 +20,7 @@ const loginAuth = () => {
if (response.access_token) {
uni.setStorageSync("token", response.access_token);
const appStore = useAppStore();
appStore.setHasToken(true);
resolve();
} else {

View File

@ -9,8 +9,6 @@
<!-- 输入框/语音按钮容器 -->
<view class="input-button-container">
<Interceptor />
<textarea
ref="textareaRef"
v-if="!isVoiceMode"
@ -63,9 +61,7 @@
<script setup>
import { ref, watch, nextTick, onMounted, defineExpose } from "vue";
import { checkToken } from "@/hooks/useGoLogin";
import RecordingWaveBtn from "@/components/Speech/RecordingWaveBtn.vue";
import Interceptor from "@/components/Interceptor/index.vue";
const plugin = requirePlugin("WechatSI");
const manager = plugin.getRecordRecognitionManager();
@ -117,7 +113,6 @@ const toggleVoiceMode = () => {
//
const handleVoiceTouchStart = () => {
checkToken().then(() => {
manager.start({ lang: "zh_CN" });
visibleWaveBtn.value = true;
@ -128,7 +123,6 @@ const handleVoiceTouchStart = () => {
recordingWaveBtnRef.value.startAnimation();
}
});
});
};
//

View File

@ -170,9 +170,9 @@ import {
import WebSocketManager from "@/utils/WebSocketManager";
import TypewriterManager from "@/utils/TypewriterManager";
import { IdUtils } from "@/utils";
import { useAppStore } from "@/store";
import { checkToken } from "@/hooks/useGoLogin";
import { goLogin } from "@/hooks/useGoLogin";
import { useAppStore } from "@/store";
const appStore = useAppStore();
///
const statusBarHeight = ref(20);
@ -356,7 +356,7 @@ onLoad(() => {
// token
const initHandler = () => {
console.log("initHandler");
if (!appStore.hasToken) return;
loadRecentConversation();
///loadConversationMsgList();
initWebSocket();
@ -368,6 +368,7 @@ watch(
() => appStore.hasToken,
(newValue) => {
if (newValue) {
console.log("token存在初始化数据");
initHandler();
}
}
@ -380,7 +381,7 @@ onMounted(() => {
initTypewriterManager();
// tokensocket
checkToken().then(() => initHandler());
initHandler();
} catch (error) {
console.error("页面初始化错误:", error);
}
@ -599,6 +600,13 @@ const initData = () => {
//
const sendMessage = (message, isInstruct = false) => {
console.log("发送的消息:", message);
if (!appStore.hasToken) {
console.log("没有token跳转到登录页");
goLogin();
return;
}
if (isSessionActive.value) {
uni.showToast({
title: "请等待当前回复完成",

View File

@ -7,8 +7,6 @@
:key="index"
>
<view class="more-tips-item-title" @click="sendReply(item)">
<Interceptor />
{{ item }}
</view>
</view>
@ -18,9 +16,6 @@
<script setup>
import { defineProps } from "vue";
import { checkToken } from "@/hooks/useGoLogin";
import Interceptor from "@/components/Interceptor/index.vue";
const emits = defineEmits(["replySent"]);
defineProps({
@ -39,8 +34,7 @@ defineProps({
});
const sendReply = (text) => {
//
checkToken().then(() => emits("replySent", text));
emits("replySent", text);
};
</script>

View File

@ -7,8 +7,6 @@
:key="index"
@click="sendReply(item)"
>
<Interceptor />
<image
class="quick-access-item-bg"
src="/static/quick/quick_icon_bg.png"
@ -26,15 +24,12 @@
<script setup>
import { onMounted, ref } from "vue";
import { checkToken } from "@/hooks/useGoLogin";
import Interceptor from "@/components/Interceptor/index.vue";
const itemList = ref([]);
const emits = defineEmits(["replySent"]);
const sendReply = (item) => {
checkToken().then(() => emits("replySent", item)); //
emits("replySent", item); //
};
onMounted(() => {

View File

@ -11,18 +11,30 @@
</template>
<script setup>
import { defineEmits, ref } from "vue";
import { ref } from "vue";
import DrawerHome from "@/pages/drawer/DrawerHome.vue";
import { goLogin } from "@/hooks/useGoLogin";
import { useAppStore } from "@/store";
const appStore = useAppStore();
const showLeft = ref(false);
//
const showDrawer = (e) => {
if (!appStore.hasToken) {
console.log("没有token跳转到登录页");
goLogin();
return;
}
showLeft.value.open();
//
uni.$emit("drawerShow");
};
//
const closeDrawer = (e) => {
showLeft.value.close();
//
uni.$emit("drawerHide");
};
</script>

View File

@ -11,18 +11,41 @@
<text class="title">我的</text>
</view>
<MineSetting />
<MineSetting v-if="isDrawerVisible" />
</view>
</template>
<script setup>
import MineSetting from "./MineSetting.vue";
import { defineEmits } from "vue";
import { defineEmits, ref, onMounted, onUnmounted } from "vue";
const emits = defineEmits(["closeDrawer"]);
const isDrawerVisible = ref(false);
const closeDrawer = () => {
isDrawerVisible.value = false;
emits("closeDrawer");
};
//
const handleDrawerShow = () => {
isDrawerVisible.value = true;
};
//
const handleDrawerHide = () => {
isDrawerVisible.value = false;
};
onMounted(() => {
uni.$on("drawerShow", handleDrawerShow);
uni.$on("drawerHide", handleDrawerHide);
});
onUnmounted(() => {
uni.$off("drawerShow", handleDrawerShow);
uni.$off("drawerHide", handleDrawerHide);
});
</script>
<style lang="scss" scoped>

View File

@ -36,9 +36,9 @@
</template>
<script setup>
import { ref, onMounted } from "vue";
import { ref } from "vue";
import { onShow } from "@dcloudio/uni-app";
import { getLoginUserPhone } from "@/request/api/LoginApi";
import { checkToken } from "@/hooks/useGoLogin";
//
const userInfo = ref({
@ -60,11 +60,9 @@ const menuList = ref([
{ label: "订阅消息", type: "action", action: "subscribeMessage" },
]);
//
onMounted(() => {
checkToken().then(() => {
// -
onShow(() => {
getLoginUserPhoneInfo();
});
});
const getLoginUserPhoneInfo = async () => {

View File

@ -1,5 +1,12 @@
<template>
<view class="login-wrapper" :style="{ backgroundImage: `url(${loginBg})` }">
<!-- 返回按钮 -->
<view class="back-btn" @click="goBack">
<uni-icons fontFamily="znicons" size="24" color="#333">{{
zniconsMap["zn-nav-arrow-left"]
}}</uni-icons>
</view>
<!-- 头部内容 -->
<view class="login-header">
<!-- 卡通形象 -->
@ -31,7 +38,7 @@
class="login-btn"
open-type="getPhoneNumber"
type="primary"
@getphonenumber="onLogin"
@getphonenumber="getPhoneNumber"
>
微信一键登录
</button>
@ -66,15 +73,15 @@
<script setup>
import { ref, computed } from "vue";
import { loginAuth, bindPhone, checkPhone } from "@/manager/LoginManager";
import {
getServiceAgreement,
getPrivacyAgreement,
} from "@/request/api/LoginApi";
import { goHome } from "@/hooks/useGoHome";
import { onLogin, goBack } from "@/hooks/useGoLogin";
import loginBg from "./images/bg.png";
import CheckBox from "@/components/CheckBox/index.vue";
import AgreePopup from "./components/AgreePopup/index.vue";
import { zniconsMap } from "@/static/fonts/znicons.js";
const isAgree = ref(false);
const visible = ref(false);
@ -94,30 +101,20 @@ const handleAgreeAndGetPhone = () => {
}
};
const onLogin = (e) => {
const { code } = e.detail;
console.info("onLogin code: ", code);
loginAuth()
.then(async () => {
//
const res = await checkPhone();
if (res.data) {
return goHome();
}
const { data } = await bindPhone({
wechatPhoneCode: code,
clientId: "2",
const getPhoneNumber = (e) => {
onLogin(e)
.then(() => {
uni.showToast({
title: "登录成功",
icon: "success",
});
if (data) {
goHome();
}
goBack();
})
.catch((err) => {
console.error("登录失败", err);
.catch(() => {
uni.showToast({
title: "登录失败",
icon: "none",
});
});
};
@ -155,4 +152,8 @@ getPrivacyAgreementData();
<style lang="scss" scoped>
@import "./styles/index.scss";
@font-face {
font-family: znicons;
src: url("@/static/fonts/znicons.ttf");
}
</style>

View File

@ -11,6 +11,18 @@
background-size: 100% 100%;
background-repeat: no-repeat;
.back-btn {
position: absolute;
top: 44px;
left: 4px;
width: 44px;
height: 44px;
display: flex;
align-items: center;
justify-content: center;
z-index: 10;
}
.login-header {
text-align: center;
max-height: 223px;

View File

@ -7,7 +7,6 @@
:key="`${item.commodityId}-${index}`"
>
<view class="mk-card-item" @click="placeOrderHandle(item)">
<Interceptor />
<image
class="card-img"
:src="item.commodityPhoto"
@ -51,7 +50,6 @@
import { defineProps } from "vue";
import { checkToken } from "@/hooks/useGoLogin";
import ModuleTitle from "@/components/ModuleTitle/index.vue";
import Interceptor from "@/components/Interceptor/index.vue";
const props = defineProps({
commodityList: {

View File

@ -7,8 +7,6 @@
:key="index"
>
<view class="mk-card-item" @click="sendReply(item)">
<Interceptor />
<image
class="card-img"
:src="item.coverPhoto"
@ -24,8 +22,6 @@
<script setup>
import { RECOMMEND_POSTS_TITLE } from "@/constant/constant";
import { defineProps } from "vue";
import { checkToken } from "@/hooks/useGoLogin";
import Interceptor from "@/components/Interceptor/index.vue";
import ModuleTitle from "@/components/ModuleTitle/index.vue";
const props = defineProps({
@ -36,10 +32,8 @@ const props = defineProps({
});
const sendReply = (item) => {
checkToken().then(() => {
const topic = item.userInputContent || item.topic.replace(/^#/, "");
uni.$emit(RECOMMEND_POSTS_TITLE, topic);
});
};
</script>

View File

@ -7,7 +7,6 @@
:key="index"
>
<view class="mk-card-item" @click="sendReply(item)">
<Interceptor />
<image class="card-img" :src="item.coverPhoto" mode="aspectFill" />
<view class="overlay-gradient">
@ -22,8 +21,6 @@
<script setup>
import { defineProps } from "vue";
import { RECOMMEND_POSTS_TITLE } from "@/constant/constant";
import { checkToken } from "@/hooks/useGoLogin";
import Interceptor from "@/components/Interceptor/index.vue";
import ModuleTitle from "@/components/ModuleTitle/index.vue";
const props = defineProps({
@ -34,10 +31,8 @@ const props = defineProps({
});
const sendReply = (item) => {
checkToken().then(() => {
const topic = item.userInputContent || item.topic.replace(/^#/, "");
uni.$emit(RECOMMEND_POSTS_TITLE, topic);
});
};
</script>

View File

@ -1,5 +1,6 @@
import { goLogin } from "../../hooks/useGoLogin";
import { BASE_URL } from "./baseUrl";
import { goLogin } from "@/hooks/useGoLogin";
import { loginAuth, checkPhone } from "@/manager/LoginManager";
const defaultConfig = {
header: {
@ -50,10 +51,16 @@ function request(url, args = {}, method = "POST", customConfig = {}) {
success: (res) => {
console.log("请求响应:" + JSON.stringify(res));
resolve(res.data);
// if (res.statusCode && res.statusCode === 424) {
// uni.setStorageSync("token", "");
// goLogin();
// }
if (res.statusCode && res.statusCode === 424) {
console.log("424错误重新登录");
loginAuth().then(async () => {
// 检测是否绑定手机号
const res = await checkPhone();
if (!res.data) {
goLogin();
}
});
}
},
fail: (err) => {
console.error("请求失败:", err);