Skip to content

Commit

Permalink
refactor(projects): 代码优化
Browse files Browse the repository at this point in the history
ISSUES CLOSED: \
  • Loading branch information
honghuangdc committed May 28, 2022
1 parent 8f6d6ce commit d28b903
Show file tree
Hide file tree
Showing 17 changed files with 45 additions and 38 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/composables/layout.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
1 change: 0 additions & 1 deletion src/enum/business.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ export enum EnumUserRole {
super = '超级管理员',
admin = '管理员',
user = '普通用户'
// custom = '自定义角色'
}

/** 登录模块 */
Expand Down
3 changes: 2 additions & 1 deletion src/enum/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]'
}
2 changes: 2 additions & 0 deletions src/hooks/business/useImageVerify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -81,5 +82,6 @@ function draw(dom: HTMLCanvasElement, width: number, height: number) {
ctx.fillStyle = randomColor(150, 200);
ctx.fill();
}

return imgCode;
}
2 changes: 2 additions & 0 deletions src/hooks/business/useSmsCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 6 additions & 3 deletions src/hooks/common/useReload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ export default function useReload() {
async function handleReload(duration = 0) {
setFalse();
await nextTick();
setTimeout(() => {
setTrue();
}, duration);

if (duration > 0) {
setTimeout(() => {
setTrue();
}, duration);
}
}

return {
Expand Down
3 changes: 3 additions & 0 deletions src/utils/common/typeof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
30 changes: 22 additions & 8 deletions src/utils/service/transform.ts
Original file line number Diff line number Diff line change
@@ -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';

/**
* 请求数据的转换
Expand All @@ -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(
Expand All @@ -43,5 +58,4 @@ async function transformFile(file: File[] | File, key: string) {
// 单文件
await formData.append(key, file);
}
return formData;
}
4 changes: 1 addition & 3 deletions src/views/exception/403/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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>
4 changes: 1 addition & 3 deletions src/views/exception/404/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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>
4 changes: 1 addition & 3 deletions src/views/exception/500/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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>
3 changes: 0 additions & 3 deletions src/views/system-view/components/index.ts

This file was deleted.

4 changes: 1 addition & 3 deletions src/views/system-view/no-permission/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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>
4 changes: 1 addition & 3 deletions src/views/system-view/not-found-page/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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>
4 changes: 1 addition & 3 deletions src/views/system-view/not-found/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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>
4 changes: 1 addition & 3 deletions src/views/system-view/service-error/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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>

1 comment on commit d28b903

@vercel
Copy link

@vercel vercel bot commented on d28b903 May 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.