From 9c26e09326a5b7e108f9eeb067d0f04370f3d59a Mon Sep 17 00:00:00 2001 From: aixianling Date: Fri, 13 Jan 2023 11:42:08 +0800 Subject: [PATCH] =?UTF-8?q?=E7=99=BB=E5=BD=95=E5=AE=8C=E6=88=90,=E7=AD=89?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wxmp/package.json | 1 + wxmp/src/main.js | 11 ++++++----- wxmp/src/mods/AppAuth/AppAuth.vue | 8 +++----- wxmp/src/utils/http.js | 1 + wxmp/src/utils/pinia.js | 11 +++++++---- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/wxmp/package.json b/wxmp/package.json index c97d825..2509034 100644 --- a/wxmp/package.json +++ b/wxmp/package.json @@ -26,6 +26,7 @@ "axios-miniprogram-adapter": "^0.3.5", "dayjs": "^1.11.7", "pinia": "^2.0.28", + "pinia-plugin-persist-uni": "^1.2.0", "query-string": "^8.1.0", "vue": "^3.2.45", "vue-i18n": "^9.1.9" diff --git a/wxmp/src/main.js b/wxmp/src/main.js index 4c2dcdc..20cbff0 100644 --- a/wxmp/src/main.js +++ b/wxmp/src/main.js @@ -1,13 +1,14 @@ import {createSSRApp} from "vue"; import App from "./App.vue"; import util from "./utils/util"; -import {createPinia} from "pinia/dist/pinia"; +import * as Pinia from "pinia/dist/pinia"; +import persist from "pinia-plugin-persist-uni"; export function createApp() { const app = createSSRApp(App); Object.keys(util).map(e => app.config.globalProperties[e] = util[e]) - app.use(createPinia()) - return { - app, - }; + const store = Pinia.createPinia() + store.use(persist) + app.use(store) + return {app, Pinia}; } diff --git a/wxmp/src/mods/AppAuth/AppAuth.vue b/wxmp/src/mods/AppAuth/AppAuth.vue index 1722c2e..49823ff 100644 --- a/wxmp/src/mods/AppAuth/AppAuth.vue +++ b/wxmp/src/mods/AppAuth/AppAuth.vue @@ -16,6 +16,7 @@ import {mainStore} from "../../utils/pinia"; import AiItem from "../../components/AiItem"; import AiBottom from "../../components/AiBottom"; +import {mapActions} from "pinia/dist/pinia"; export default { name: "AppAuth", @@ -30,16 +31,13 @@ export default { } }, methods: { + ...mapActions(mainStore, ['getToken', 'getCode']), handleAvatar({detail}) { this.form.avatar = detail.avatarUrl }, handleSignIn() { - this.store.getCode().then(code => this.store.getToken({...this.form, code})).then(() => uni.navigateBack()) + this.getCode().then(code => this.getToken({...this.form, code})).then(() => uni.navigateBack()) } - }, - setup() { - const store = mainStore() - return {store} } } diff --git a/wxmp/src/utils/http.js b/wxmp/src/utils/http.js index 6e85df0..1e79ec0 100644 --- a/wxmp/src/utils/http.js +++ b/wxmp/src/utils/http.js @@ -2,6 +2,7 @@ import axios from 'axios' import adapter from 'axios-miniprogram-adapter' const instance = axios.create({ + baseURL: "http://localhost:7001", timeout: 600000, withCredentials: true, adapter diff --git a/wxmp/src/utils/pinia.js b/wxmp/src/utils/pinia.js index c76c6be..1976916 100644 --- a/wxmp/src/utils/pinia.js +++ b/wxmp/src/utils/pinia.js @@ -8,7 +8,6 @@ export const mainStore = defineStore('main', { }), actions: { getCode(count = 0) { - console.log("getCode:%s", count) if (count > 3) { return Promise.reject("无法获取code") } else return new Promise((resolve, reject) => { @@ -25,13 +24,13 @@ export const mainStore = defineStore('main', { }) }, getToken(params) { - http.post("/api/auth/token", null, { + return http.post("/api/wxmp/token", null, { withoutToken: true, headers: {passport: "c799f2d92de34b97"}, params: {...params} }).then(res => { if (res?.data) { - this.token = res.data + return this.token = res.data } }) }, @@ -42,5 +41,9 @@ export const mainStore = defineStore('main', { } }) } - } + }, + persist: { + enabled: true, + detached: true//设置订阅与组件分离,修复持久化失效的问题 + }, })