refactor(projects): 代码优化

ISSUES CLOSED: \
This commit is contained in:
Soybean 2022-05-28 13:31:33 +08:00
parent 8f6d6ce3cb
commit d28b9039bb
17 changed files with 45 additions and 38 deletions

View File

@ -1,13 +1,13 @@
import { computed } from 'vue';
import { useAppStore, useThemeStore } from '@/store';
type LayoutMode = 'vertical' | 'horizontal';
type LayoutHeaderProps = Record<EnumType.ThemeLayoutMode, GlobalHeaderProps>;
export function useBasicLayout() {
const app = useAppStore();
const theme = useThemeStore();
type LayoutMode = 'vertical' | 'horizontal';
const mode = computed(() => {
const vertical: LayoutMode = 'vertical';
const horizontal: LayoutMode = 'horizontal';

View File

@ -3,7 +3,6 @@ export enum EnumUserRole {
super = '超级管理员',
admin = '管理员',
user = '普通用户'
// custom = '自定义角色'
}
/** 登录模块 */

View File

@ -31,5 +31,6 @@ export enum EnumDataType {
date = '[object Date]',
regexp = '[object RegExp]',
set = '[object Set]',
map = '[object Map]'
map = '[object Map]',
file = '[object File]'
}

View File

@ -52,6 +52,7 @@ function draw(dom: HTMLCanvasElement, width: number, height: number) {
ctx.fillStyle = randomColor(180, 230);
ctx.fillRect(0, 0, width, height);
for (let i = 0; i < 4; i += 1) {
const text = NUMBER_STRING[randomNum(0, NUMBER_STRING.length)];
imgCode += text;
@ -81,5 +82,6 @@ function draw(dom: HTMLCanvasElement, width: number, height: number) {
ctx.fillStyle = randomColor(150, 200);
ctx.fill();
}
return imgCode;
}

View File

@ -7,6 +7,7 @@ import useCountDown from './useCountDown';
export default function useSmsCode() {
const { loading, startLoading, endLoading } = useLoading();
const { counts, start, isCounting } = useCountDown(60);
const initLabel = '获取验证码';
const countingLabel = (second: number) => `${second}秒后重新获取`;
const label = computed(() => {
@ -40,6 +41,7 @@ export default function useSmsCode() {
async function getSmsCode(phone: string) {
const valid = isPhoneValid(phone);
if (!valid || loading.value) return;
startLoading();
const { data } = await fetchSmsCode(phone);
if (data) {

View File

@ -13,10 +13,13 @@ export default function useReload() {
async function handleReload(duration = 0) {
setFalse();
await nextTick();
if (duration > 0) {
setTimeout(() => {
setTrue();
}, duration);
}
}
return {
reloadFlag,

View File

@ -33,3 +33,6 @@ export function isSet(data: unknown) {
export function isMap(data: unknown) {
return Object.prototype.toString.call(data) === EnumDataType.map;
}
export function isFile(data: unknown) {
return Object.prototype.toString.call(data) === EnumDataType.file;
}

View File

@ -1,7 +1,7 @@
import qs from 'qs';
import FormData from 'form-data';
import { EnumContentType } from '@/enum';
import { isArray } from '../common';
import { isArray, isFile } from '../common';
/**
*
@ -17,20 +17,35 @@ export async function transformRequestData(requestData: any, contentType?: strin
}
// form-data类型转换
if (contentType === EnumContentType.formData) {
const key = Object.keys(requestData)[0];
const file = requestData.data[key];
data = await transformFile(file, key);
data = await handleFormData(requestData);
}
return data;
}
async function handleFormData(data: Record<string, any>) {
const formData = new FormData();
const entries = Object.entries(data);
entries.forEach(async ([key, value]) => {
const isFileType = isFile(value) || (isArray(value) && value.length && isFile(value[0]));
if (isFileType) {
await transformFile(formData, key, value);
} else {
formData.append(key, value);
}
});
return formData;
}
/**
*
* @param file -
* @param key -
* @param file -
*/
async function transformFile(file: File[] | File, key: string) {
const formData = new FormData();
async function transformFile(formData: FormData, key: string, file: File[] | File) {
if (isArray(file)) {
// 多文件
await Promise.all(
@ -43,5 +58,4 @@ async function transformFile(file: File[] | File, key: string) {
// 单文件
await formData.append(key, file);
}
return formData;
}

View File

@ -2,8 +2,6 @@
<exception-base type="403" />
</template>
<script lang="ts" setup>
import { ExceptionBase } from '../../system-view/components';
</script>
<script lang="ts" setup></script>
<style scoped></style>

View File

@ -2,8 +2,6 @@
<exception-base type="404" />
</template>
<script lang="ts" setup>
import { ExceptionBase } from '../../system-view/components';
</script>
<script lang="ts" setup></script>
<style scoped></style>

View File

@ -2,8 +2,6 @@
<exception-base type="500" />
</template>
<script lang="ts" setup>
import { ExceptionBase } from '../../system-view/components';
</script>
<script lang="ts" setup></script>
<style scoped></style>

View File

@ -1,3 +0,0 @@
import ExceptionBase from './ExceptionBase.vue';
export { ExceptionBase };

View File

@ -2,8 +2,6 @@
<exception-base type="403" />
</template>
<script lang="ts" setup>
import { ExceptionBase } from '../components';
</script>
<script lang="ts" setup></script>
<style scoped></style>

View File

@ -2,8 +2,6 @@
<exception-base type="404" />
</template>
<script lang="ts" setup>
import { ExceptionBase } from '../components';
</script>
<script lang="ts" setup></script>
<style scoped></style>

View File

@ -2,8 +2,6 @@
<exception-base type="404" />
</template>
<script lang="ts" setup>
import { ExceptionBase } from '../components';
</script>
<script lang="ts" setup></script>
<style scoped></style>

View File

@ -2,8 +2,6 @@
<exception-base type="500" />
</template>
<script lang="ts" setup>
import { ExceptionBase } from '../components';
</script>
<script lang="ts" setup></script>
<style scoped></style>