Browse Source

当返回401的时候,弹出提示

lihuilin/feature-20251024095243-我的
wangyi 4 weeks ago
parent
commit
2dabe35ca4
  1. 2
      components/deepExploration_header.vue
  2. 25
      components/login-prompt.vue
  3. 1
      stores/index.js
  4. 44
      stores/modules/login.js
  5. 13
      utils/http.js

2
components/deepExploration_header.vue

@ -72,6 +72,7 @@
<view class="history-main" @click="itemClick(item)"> <view class="history-main" @click="itemClick(item)">
<text class="history-query">{{ item.stockName }}</text> <text class="history-query">{{ item.stockName }}</text>
<text class="history-query">{{ item.stockCode }}</text> <text class="history-query">{{ item.stockCode }}</text>
<text class="history-query">{{ item.stockCode }}</text>
</view> </view>
<text class="history-time">{{ <text class="history-time">{{
formatTimeForHistory(item.createdTime) formatTimeForHistory(item.createdTime)
@ -98,6 +99,7 @@ const props = defineProps({
}, },
}); });
const showHistoryDrawer = ref(false); const showHistoryDrawer = ref(false);
const modelType = ref('');
const drawerOffsetY = ref(0); const drawerOffsetY = ref(0);
// const handleHistory = () => { // const handleHistory = () => {
// showHistoryDrawer.value = true; // showHistoryDrawer.value = true;

25
components/login-prompt.vue

@ -15,20 +15,40 @@
</template> </template>
<script setup> <script setup>
import { ref, nextTick, onMounted } from "vue";
import { ref, nextTick, onMounted, watch } from "vue";
import { useUserStore } from "../stores/modules/userInfo"; import { useUserStore } from "../stores/modules/userInfo";
import { useDeviceStore } from "../stores/modules/deviceInfo"; import { useDeviceStore } from "../stores/modules/deviceInfo";
import { useLoginStore } from "../stores/modules/login";
import { LoginApi } from "../api/start/login"; import { LoginApi } from "../api/start/login";
const deviceId = ref(""); const deviceId = ref("");
const userStore = useUserStore(); const userStore = useUserStore();
const deviceStore = useDeviceStore(); const deviceStore = useDeviceStore();
const loginStore = useLoginStore();
// //
onMounted(() => { onMounted(() => {
if (!userStore.userInfo) { if (!userStore.userInfo) {
show(); show();
} }
});
}),
// watch(
// () => loginStore.loginInfo,
// (newVal, oldVal) => {
// console.log("");
// if (newVal === "false") {
// console.log("");
// show();
// loginStore.setLoginInfo("true");
// }
// }
// );
loginStore.$subscribe(() => {
if (loginStore.loginInfo === "false") {
console.log("登录失败");
show();
}
});
// //
const showPrompt = ref(false); const showPrompt = ref(false);
@ -84,6 +104,7 @@ const continueAsVisitor = async () => {
if (res.code === 200) { if (res.code === 200) {
userStore.setUserInfo(res.data); userStore.setUserInfo(res.data);
console.log("0loginStore.loginInfo", loginStore.loginInfo);
hide(); hide();
} }
}; };

1
stores/index.js

@ -10,5 +10,6 @@ pinia.use(persist)
export * from './modules/userInfo' export * from './modules/userInfo'
export * from './modules/deviceInfo' export * from './modules/deviceInfo'
export * from './modules/deepExploration' export * from './modules/deepExploration'
export * from './modules/login'
// 默认导出,给 main.js 使用 // 默认导出,给 main.js 使用
export default pinia export default pinia

44
stores/modules/login.js

@ -0,0 +1,44 @@
import { defineStore } from 'pinia'
import { ref } from 'vue'
// 定义 Store
export const useLoginStore = defineStore(
'login',
() => {
// 会员信息
const loginInfo = ref("true")
// 保存会员信息,登录时使用
const setLoginInfo = (val) => {
loginInfo.value = val
}
// 清理会员信息,退出时使用
const clearLoginInfo = () => {
loginInfo.value = undefined
}
// 记得 return
return {
loginInfo,
setLoginInfo,
clearLoginInfo,
}
},
// TODO: 持久化
{
// 网页端持久化
// persist: true,
// 小程序端持久化
persist: {
storage: {
getItem(key) {
return uni.getStorageSync(key)
},
setItem(key, value) {
uni.setStorageSync(key, value)
},
},
},
},
)

13
utils/http.js

@ -1,5 +1,6 @@
import { useUserStore } from "../stores/modules/userInfo" import { useUserStore } from "../stores/modules/userInfo"
import { useDeviceStore } from "../stores/modules/deviceInfo" import { useDeviceStore } from "../stores/modules/deviceInfo"
import { useLoginStore } from "../stores/modules/login"
const baseURL = "https://dbqb.nfdxy.net/testApi" const baseURL = "https://dbqb.nfdxy.net/testApi"
@ -28,7 +29,7 @@ const httpInterceptor = {
const sys = uni.getSystemInfoSync(); const sys = uni.getSystemInfoSync();
// 为对齐后端文档示例,client 固定为 ios(如需按平台设置再改回) // 为对齐后端文档示例,client 固定为 ios(如需按平台设置再改回)
const deviceInfo =useDeviceStore()
const deviceInfo = useDeviceStore()
options.header = { options.header = {
@ -37,7 +38,7 @@ const httpInterceptor = {
'content-type': 'application/json', 'content-type': 'application/json',
'contentType': 'application/json', 'contentType': 'application/json',
'version': uni.getSystemInfoSync().appVersion, 'version': uni.getSystemInfoSync().appVersion,
'client': uni.getSystemInfoSync().platform == 'ios' ? 'ios' : 'android',
'client': uni.getSystemInfoSync().platform == 'ios' ? 'ios' : 'android',
'deviceId': deviceInfo.deviceInfo.deviceId 'deviceId': deviceInfo.deviceInfo.deviceId
} }
//4 添加token,优先用store,没有则回退到body中的token,保持与Apifox一致 //4 添加token,优先用store,没有则回退到body中的token,保持与Apifox一致
@ -64,6 +65,14 @@ export const http = (options) => {
success: (result) => { success: (result) => {
if (result.statusCode >= 200 && result.statusCode < 300) { if (result.statusCode >= 200 && result.statusCode < 300) {
if (result.data.code === 401) { if (result.data.code === 401) {
const loginStore = useLoginStore()
loginStore.setLoginInfo("true")
console.log("1loginStore.loginInfo", loginStore.loginInfo);
loginStore.setLoginInfo("false")
console.log("2loginStore.loginInfo", loginStore.loginInfo);
uni.showToast({ uni.showToast({
title: '请先登录', title: '请先登录',
icon: 'none' icon: 'none'

Loading…
Cancel
Save