Skip to content

Commit 4bba848

Browse files
committed
优化密码验证及代码生成loading
1 parent e916f42 commit 4bba848

File tree

11 files changed

+228
-94
lines changed

11 files changed

+228
-94
lines changed

src/components/Generator/Index.vue

Lines changed: 71 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,13 @@
4141
:ref="setTableRef"
4242
:data="item.default_fields"
4343
:option="item"
44+
:index="index"
4445
:fieldTypes="currentGenerator.table.field_types"
4546
/>
4647
</div>
4748
<template #footer>
4849
<a-button danger ghost @click="onCancelModal">{{ t("common.cancel") }}</a-button>
49-
<a-button type="primary" @click="handleOk">{{ t("common.ok") }}</a-button>
50+
<a-button :loading="loading" type="primary" @click="handleOk">{{ t("common.ok") }}</a-button>
5051
</template>
5152
</a-modal>
5253
</template>
@@ -67,6 +68,7 @@ import {
6768
Menu,
6869
Checkbox,
6970
notification,
71+
Spin,
7072
} from "ant-design-vue";
7173
import useModal from "@/hooks/useModal";
7274
import { useStore } from "vuex";
@@ -109,6 +111,7 @@ export default defineComponent({
109111
[Menu.name]: Menu,
110112
[Menu.Item.name]: Menu.Item,
111113
[Checkbox.name]: Checkbox,
114+
[Spin.name]: Spin,
112115
DataForm,
113116
DataTableEditor,
114117
FilesEditor,
@@ -120,10 +123,11 @@ export default defineComponent({
120123
const { visible, onShow, onCancel } = useModal();
121124
const formRef = ref();
122125
const filesRef = ref();
123-
let tableRefs: any = [];
126+
let tableRefs: any = {};
124127
const setTableRef = (el: any) => {
125128
if (el) {
126-
tableRefs.push(el);
129+
const index = el.$attrs.index;
130+
tableRefs[index] = el;
127131
}
128132
};
129133
@@ -154,74 +158,86 @@ export default defineComponent({
154158
formData: formData,
155159
formItems: formItems,
156160
tableItems: tableItems,
161+
loading: false,
157162
});
158163
159164
async function handleOk() {
160-
const formValues = await formRef.value.onSubmit();
165+
state.loading = true;
161166
162-
const fileDatas = filesRef.value.getData();
163-
if (!fileDatas) {
164-
return;
165-
}
167+
try {
168+
const formValues = await formRef.value.onSubmit();
166169
167-
let tables = [];
168-
if (tableRefs && tableRefs.length) {
169-
let isError = false;
170-
for (let i = 0; i < tableRefs.length; i++) {
171-
const tableItem: any = tableRefs[i];
172-
if (tableItem && tableItem.getData) {
173-
const tableData = tableItem.getData();
174-
if (tableData) {
175-
tables.push(tableData);
176-
} else {
177-
isError = true;
178-
break;
179-
}
180-
}
181-
}
182-
if (isError) {
170+
const fileDatas = filesRef.value.getData();
171+
if (!fileDatas) {
172+
state.loading = false;
183173
return;
184174
}
185-
}
186175
187-
const json = {
188-
index: state.currentIndex,
189-
form: formValues,
190-
files: fileDatas,
191-
tables: tables,
192-
};
176+
let tables = [];
193177
194-
generator(json)
195-
.then(() => {
196-
message.success(t("generator.submitSuccess"));
197-
onCancelModal();
198-
emit("submitSuccess");
199-
})
200-
.catch((err) => {
201-
if (err.response) {
202-
const message =
203-
err.response && err.response.data && err.response.data.message
204-
? err.response.data.message
205-
: err.message;
206-
notification.error({
207-
message: err.response.status,
208-
description: message,
209-
});
210-
} else {
211-
notification.error({
212-
message: err.response.status,
213-
description: err.message,
214-
});
178+
if (Object.keys(tableRefs).length) {
179+
let isError = false;
180+
for (const key in tableRefs) {
181+
const tableItem: any = tableRefs[key];
182+
if (tableItem && tableItem.getData) {
183+
const tableData = tableItem.getData();
184+
if (tableData) {
185+
tables.push(tableData);
186+
} else {
187+
isError = true;
188+
break;
189+
}
190+
}
191+
}
192+
if (isError) {
193+
state.loading = false;
194+
return;
215195
}
216-
});
196+
}
197+
198+
const json = {
199+
index: state.currentIndex,
200+
form: formValues,
201+
files: fileDatas,
202+
tables: tables,
203+
};
204+
205+
generator(json)
206+
.then(() => {
207+
message.success(t("generator.submitSuccess"));
208+
state.loading = false;
209+
onCancelModal();
210+
emit("submitSuccess");
211+
})
212+
.catch((err) => {
213+
state.loading = false;
214+
if (err.response) {
215+
const message =
216+
err.response && err.response.data && err.response.data.message
217+
? err.response.data.message
218+
: err.message;
219+
notification.error({
220+
message: err.response.status,
221+
description: message,
222+
});
223+
} else {
224+
notification.error({
225+
message: err.response.status,
226+
description: err.message,
227+
});
228+
}
229+
});
230+
} catch (error) {
231+
state.loading = false;
232+
}
217233
}
218234
219235
/**
220236
* 监听表单的App选择
221237
*/
222238
function onAppChange(appKey: string) {
223239
state.currentAppKey = appKey;
224-
tableRefs = [];
240+
tableRefs = {};
225241
const appConfig = getAppsConfigItemByKey(state.config.apps as ConfigAppItem[], appKey);
226242
if (appConfig as ConfigAppItem) {
227243
const groups = appConfig?.groups as ConfigAppGroupItem[];
@@ -248,7 +264,7 @@ export default defineComponent({
248264
249265
function handleMenuClick(e: MenuInfo) {
250266
state.currentIndex = e.key;
251-
tableRefs = [];
267+
tableRefs = {};
252268
const generatorFind = state.config.generator && state.config.generator[e.key as number];
253269
if (generatorFind) {
254270
state.modalTitle = `${t("generator.title")} - ${generatorFind.title}`;

src/components/Menu/src/BasicMenu.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ export default defineComponent({
182182
watch(
183183
() => route.fullPath,
184184
(v) => {
185-
console.log(state.selectedKeys, route.query, v);
186185
if (route.query.key) {
187186
state.selectedKeys = [route.query.key as string];
188187
} else {

src/components/VerifyAuth/Index.vue

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
>
1010
<div>
1111
<a-input-password
12+
ref="inputRef"
1213
:placeholder="t('auth.input.placeholder')"
1314
size="large"
1415
:allowClear="true"
@@ -23,48 +24,56 @@
2324
</template>
2425

2526
<script lang="ts">
26-
import { defineComponent, computed, ref, watchEffect, unref } from "vue";
27+
import { defineComponent, computed, ref, onMounted, unref } from "vue";
2728
import { Modal, Input, Button, message } from "ant-design-vue";
2829
import useModal from "@/hooks/useModal";
29-
import { useStore } from "vuex";
30-
import { GlobalState } from "@/store";
3130
import md5 from "js-md5";
3231
import { checkAuth } from "@/api";
3332
import { getTreePathByKeys } from "@/utils/helper/treeHelper";
3433
import * as ApidocTypes from "@/store/modules/Apidoc/types";
3534
import { useI18n } from "@/hooks/useI18n";
35+
import store from "@/store";
3636
3737
export default defineComponent({
3838
components: {
3939
[Modal.name]: Modal,
4040
[Button.name]: Button,
4141
[Input.Password.name]: Input.Password,
4242
},
43+
props: {
44+
onSuccess: {
45+
type: Function,
46+
default: () => {
47+
return;
48+
},
49+
},
50+
},
4351
emits: ["check"],
4452
setup(props, { emit }) {
4553
const { t } = useI18n();
46-
const store = useStore<GlobalState>();
4754
const { visible, onShow, onCancel } = useModal();
4855
const password = ref<string>("");
49-
const appKey = computed(() => store.state.app.appKey);
50-
const config = computed(() => store.state.app.config);
51-
const authData = computed(() => store.state.apidoc.authData);
56+
const appKey = store.getters["app/appKey"];
57+
const config = store.getters["app/config"];
58+
const authData = store.getters["apidoc/authData"];
59+
const inputRef = ref<HTMLElement | null>(null);
5260
5361
function handleOk() {
5462
checkAuth({
55-
appKey: appKey.value,
63+
appKey: appKey,
5664
password: md5(password.value),
5765
}).then((res) => {
5866
if (res.data.code !== 0 || !(res.data.data && res.data.data.token)) {
5967
res.data.msg && message.error(res.data.msg);
6068
return false;
6169
}
70+
6271
const token = res.data.data.token;
6372
let tokenKey = "global";
64-
const appKeys = appKey.value.split(",");
65-
if (config.value.apps) {
73+
const appKeys = appKey.split(",");
74+
if (config.apps) {
6675
const appPath = getTreePathByKeys(
67-
config.value.apps,
76+
config.apps,
6877
appKeys,
6978
appKeys[0],
7079
[],
@@ -75,28 +84,33 @@ export default defineComponent({
7584
if (appPath && appPath.length) {
7685
const appConfig = appPath[appPath.length - 1];
7786
if (appConfig.hasPassword) {
78-
tokenKey = appKey.value;
87+
tokenKey = appKey;
7988
}
8089
}
8190
}
8291
const json = {
83-
...authData.value,
92+
...authData,
8493
[tokenKey]: token,
8594
};
8695
store.dispatch(`apidoc/${ApidocTypes.SET_AUTH_DATA}`, json);
87-
emit("check");
8896
password.value = "";
89-
onCancel();
97+
props.onSuccess();
9098
});
9199
}
92-
100+
onMounted(() => {
101+
onShow();
102+
setTimeout(() => {
103+
unref(inputRef) && (unref(inputRef) as any).focus();
104+
}, 500);
105+
});
93106
return {
94107
visible,
95108
onShow,
96109
onCancel,
97110
password,
98111
handleOk,
99112
t,
113+
inputRef,
100114
};
101115
},
102116
});

src/components/VerifyAuth/index.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,33 @@
1-
import VerifyAuth from "./Index.vue";
1+
// import VerifyAuth from "./Index.vue";
22

3-
export default VerifyAuth;
3+
// export default VerifyAuth;
4+
5+
import { createApp } from "vue";
6+
import Modal from "./Index.vue";
7+
function modal(props: any): any {
8+
return new Promise((resolve, reject) => {
9+
// 实例化组件,createApp第二个参数是props
10+
const confirmInstance = createApp(Modal, {
11+
...props,
12+
onSuccess: (res: any) => {
13+
unmount();
14+
resolve(res);
15+
},
16+
onCancel: () => {
17+
unmount();
18+
reject(new Error());
19+
},
20+
});
21+
// 卸载组件
22+
const unmount = () => {
23+
confirmInstance.unmount();
24+
document.body.removeChild(parentNode);
25+
};
26+
// 创建一个挂载容器
27+
const parentNode = document.createElement("div");
28+
document.body.appendChild(parentNode);
29+
// 挂载组件
30+
confirmInstance.mount(parentNode);
31+
});
32+
}
33+
export default modal;

0 commit comments

Comments
 (0)