hasToken 优化

This commit is contained in:
icssoa 2022-08-09 14:55:59 +08:00
parent ae0be71e9c
commit 62020ec8a5
7 changed files with 44 additions and 18 deletions

View File

@ -291,7 +291,7 @@ declare namespace Eps {
*/ */
price?: number; price?: number;
/** /**
* * 0- 1- 2-
*/ */
type?: number; type?: number;
/** /**

View File

@ -1,6 +1,6 @@
{ {
"name": "front-next", "name": "front-next",
"version": "5.7.8", "version": "5.7.9",
"scripts": { "scripts": {
"dev": "vite --host", "dev": "vite --host",
"build": "vite build", "build": "vite build",

View File

@ -61,21 +61,28 @@ export default (): ModuleConfig => {
document.title = config.app.name; document.title = config.app.name;
}, },
async onLoad() { async onLoad() {
const { user, menu } = useStore(); const { user, menu, app } = useStore();
if (user.token) { // token 事件
async function hasToken(cb: () => Promise<any> | void) {
if (cb) {
app.addEvent("hasToken", cb);
if (user.token) {
await cb();
}
}
}
await hasToken(async () => {
// 获取用户信息 // 获取用户信息
user.get(); user.get();
// 获取菜单权限 // 获取菜单权限
await menu.get(); await menu.get();
} });
return { return {
async hasToken(cb: () => Promise<any> | void) { hasToken
if (user.token) {
if (cb) await cb();
}
}
}; };
} }
}; };

View File

@ -99,11 +99,8 @@ async function toLogin() {
user.setToken(res); user.setToken(res);
}); });
// // token
user.get(); await Promise.all(app.events.hasToken.map((e) => e()));
//
await menu.get();
// //
router.push("/"); router.push("/");

View File

@ -1,5 +1,5 @@
import { defineStore } from "pinia"; import { defineStore } from "pinia";
import { ref } from "vue"; import { reactive, ref } from "vue";
import { config } from "/@/cool"; import { config } from "/@/cool";
import { deepMerge, getBrowser, storage } from "/@/cool/utils"; import { deepMerge, getBrowser, storage } from "/@/cool/utils";
@ -15,6 +15,11 @@ export const useAppStore = defineStore("app", function () {
// 是否折叠 // 是否折叠
const isFold = ref(browser.value.isMini || false); const isFold = ref(browser.value.isMini || false);
// 事件
const events = reactive<{ [key: string]: any[] }>({
hasToken: []
});
// 折叠 // 折叠
function fold(v?: boolean) { function fold(v?: boolean) {
if (v === undefined) { if (v === undefined) {
@ -35,12 +40,21 @@ export const useAppStore = defineStore("app", function () {
browser.value = getBrowser(); browser.value = getBrowser();
} }
// 添加事件
function addEvent(name: string, func: any) {
if (func) {
events[name].push(func);
}
}
return { return {
info, info,
browser, browser,
isFold, isFold,
fold, fold,
events,
set, set,
setBrowser setBrowser,
addEvent
}; };
}); });

View File

@ -41,11 +41,14 @@
</cl-upsert> </cl-upsert>
</cl-crud> </cl-crud>
<cl-dialog title="xxx" v-model="dialog.visible"> xxxx </cl-dialog>
<cl-form ref="Form"></cl-form> <cl-form ref="Form"></cl-form>
</template> </template>
<script lang="tsx" setup name="crud"> <script lang="tsx" setup name="crud">
import { useCrud, useUpsert, useTable, useForm } from "@cool-vue/crud"; import { useCrud, useUpsert, useTable, useForm } from "@cool-vue/crud";
import { reactive } from "vue";
import { useDict } from "/$/dict"; import { useDict } from "/$/dict";
const { dict } = useDict(); const { dict } = useDict();
@ -164,6 +167,10 @@ const Table = useTable({
] ]
}); });
const dialog = reactive({
visible: false
});
const Form = useForm(); const Form = useForm();
// //
@ -190,6 +197,8 @@ const Table2 = useTable({
}); });
function openForm() { function openForm() {
return (dialog.visible = true);
Form.value?.open({ Form.value?.open({
title: "自定义表单", title: "自定义表单",
items: [ items: [

View File

@ -5,7 +5,6 @@ export default (): ModuleConfig => {
return { return {
onLoad({ hasToken }) { onLoad({ hasToken }) {
const { dict } = useDict(); const { dict } = useDict();
hasToken(() => { hasToken(() => {
dict.refresh(); dict.refresh();
}); });