feat: 调整优化登录的逻辑
This commit is contained in:
@@ -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");
|
||||
export const onLogin = async (e) => {
|
||||
return new Promise(async (resolve) => {
|
||||
await loginAuth().then(async () => {
|
||||
const { code } = e.detail;
|
||||
console.info("onLogin code: ", code);
|
||||
|
||||
if (token) return;
|
||||
|
||||
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();
|
||||
});
|
||||
};
|
||||
|
||||
@@ -20,9 +20,7 @@ const loginAuth = () => {
|
||||
|
||||
if (response.access_token) {
|
||||
uni.setStorageSync("token", response.access_token);
|
||||
|
||||
const appStore = useAppStore();
|
||||
|
||||
appStore.setHasToken(true);
|
||||
resolve();
|
||||
} else {
|
||||
|
||||
@@ -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,8 +113,7 @@ const toggleVoiceMode = () => {
|
||||
|
||||
// 处理语音按钮长按开始
|
||||
const handleVoiceTouchStart = () => {
|
||||
checkToken().then(() => {
|
||||
manager.start({ lang: "zh_CN" });
|
||||
manager.start({ lang: "zh_CN" });
|
||||
|
||||
visibleWaveBtn.value = true;
|
||||
|
||||
@@ -128,7 +123,6 @@ const handleVoiceTouchStart = () => {
|
||||
recordingWaveBtnRef.value.startAnimation();
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
// 处理语音按钮长按结束
|
||||
|
||||
@@ -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();
|
||||
|
||||
// 有token时,加载最近会话、最近消息、初始化socket
|
||||
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: "请等待当前回复完成",
|
||||
|
||||
@@ -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>
|
||||
|
||||
|
||||
@@ -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(() => {
|
||||
|
||||
@@ -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>
|
||||
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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(() => {
|
||||
getLoginUserPhoneInfo();
|
||||
});
|
||||
// 页面生命周期钩子 - 页面显示时调用
|
||||
onShow(() => {
|
||||
getLoginUserPhoneInfo();
|
||||
});
|
||||
|
||||
const getLoginUserPhoneInfo = async () => {
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -1,63 +1,75 @@
|
||||
.login-wrapper {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
box-sizing: border-box;
|
||||
font-family: PingFang SC, PingFang SC;
|
||||
height: 100vh;
|
||||
padding-top: 168px;
|
||||
position: relative;
|
||||
background-position: 0 0;
|
||||
background-size: 100% 100%;
|
||||
background-repeat: no-repeat;
|
||||
|
||||
.back-btn {
|
||||
position: absolute;
|
||||
top: 44px;
|
||||
left: 4px;
|
||||
width: 44px;
|
||||
height: 44px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
box-sizing: border-box;
|
||||
font-family: PingFang SC, PingFang SC;
|
||||
height: 100vh;
|
||||
padding-top: 168px;
|
||||
position: relative;
|
||||
background-position: 0 0;
|
||||
background-size: 100% 100%;
|
||||
background-repeat: no-repeat;
|
||||
justify-content: center;
|
||||
z-index: 10;
|
||||
}
|
||||
|
||||
.login-header {
|
||||
text-align: center;
|
||||
max-height: 223px;
|
||||
.login-header {
|
||||
text-align: center;
|
||||
max-height: 223px;
|
||||
|
||||
.login-avatar {
|
||||
width: 150px;
|
||||
display: block;
|
||||
}
|
||||
|
||||
.login-title {
|
||||
width: 137px;
|
||||
height: 32px;
|
||||
margin: 6px auto;
|
||||
}
|
||||
|
||||
.login-desc {
|
||||
font-size: 12px;
|
||||
color: #1e4c69;
|
||||
line-height: 24px;
|
||||
}
|
||||
.login-avatar {
|
||||
width: 150px;
|
||||
display: block;
|
||||
}
|
||||
|
||||
.login-btn-area {
|
||||
margin-top: 46px;
|
||||
width: 297px;
|
||||
|
||||
.login-btn {
|
||||
background: linear-gradient(246deg, #22a7ff 0%, #2567ff 100%);
|
||||
width: 100%;
|
||||
border-radius: 50px;
|
||||
}
|
||||
.login-title {
|
||||
width: 137px;
|
||||
height: 32px;
|
||||
margin: 6px auto;
|
||||
}
|
||||
|
||||
.login-agreement {
|
||||
margin-top: 20px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
|
||||
.login-agreement-text {
|
||||
font-size: 14px;
|
||||
color: #666;
|
||||
}
|
||||
|
||||
.login-agreement-link {
|
||||
font-size: 14px;
|
||||
color: #007aff;
|
||||
margin: 0 5px;
|
||||
}
|
||||
.login-desc {
|
||||
font-size: 12px;
|
||||
color: #1e4c69;
|
||||
line-height: 24px;
|
||||
}
|
||||
}
|
||||
|
||||
.login-btn-area {
|
||||
margin-top: 46px;
|
||||
width: 297px;
|
||||
|
||||
.login-btn {
|
||||
background: linear-gradient(246deg, #22a7ff 0%, #2567ff 100%);
|
||||
width: 100%;
|
||||
border-radius: 50px;
|
||||
}
|
||||
}
|
||||
|
||||
.login-agreement {
|
||||
margin-top: 20px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
|
||||
.login-agreement-text {
|
||||
font-size: 14px;
|
||||
color: #666;
|
||||
}
|
||||
|
||||
.login-agreement-link {
|
||||
font-size: 14px;
|
||||
color: #007aff;
|
||||
margin: 0 5px;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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: {
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
const topic = item.userInputContent || item.topic.replace(/^#/, "");
|
||||
uni.$emit(RECOMMEND_POSTS_TITLE, topic);
|
||||
};
|
||||
</script>
|
||||
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
const topic = item.userInputContent || item.topic.replace(/^#/, "");
|
||||
uni.$emit(RECOMMEND_POSTS_TITLE, topic);
|
||||
};
|
||||
</script>
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user