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. 11
      utils/http.js

2
components/deepExploration_header.vue

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

25
components/login-prompt.vue

@ -15,20 +15,40 @@
</template>
<script setup>
import { ref, nextTick, onMounted } from "vue";
import { ref, nextTick, onMounted, watch } from "vue";
import { useUserStore } from "../stores/modules/userInfo";
import { useDeviceStore } from "../stores/modules/deviceInfo";
import { useLoginStore } from "../stores/modules/login";
import { LoginApi } from "../api/start/login";
const deviceId = ref("");
const userStore = useUserStore();
const deviceStore = useDeviceStore();
const loginStore = useLoginStore();
//
onMounted(() => {
if (!userStore.userInfo) {
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);
@ -84,6 +104,7 @@ const continueAsVisitor = async () => {
if (res.code === 200) {
userStore.setUserInfo(res.data);
console.log("0loginStore.loginInfo", loginStore.loginInfo);
hide();
}
};

1
stores/index.js

@ -10,5 +10,6 @@ pinia.use(persist)
export * from './modules/userInfo'
export * from './modules/deviceInfo'
export * from './modules/deepExploration'
export * from './modules/login'
// 默认导出,给 main.js 使用
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)
},
},
},
},
)

11
utils/http.js

@ -1,5 +1,6 @@
import { useUserStore } from "../stores/modules/userInfo"
import { useDeviceStore } from "../stores/modules/deviceInfo"
import { useLoginStore } from "../stores/modules/login"
const baseURL = "https://dbqb.nfdxy.net/testApi"
@ -28,7 +29,7 @@ const httpInterceptor = {
const sys = uni.getSystemInfoSync();
// 为对齐后端文档示例,client 固定为 ios(如需按平台设置再改回)
const deviceInfo =useDeviceStore()
const deviceInfo = useDeviceStore()
options.header = {
@ -64,6 +65,14 @@ export const http = (options) => {
success: (result) => {
if (result.statusCode >= 200 && result.statusCode < 300) {
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({
title: '请先登录',
icon: 'none'

Loading…
Cancel
Save