feat(projects): 1.0 beta

This commit is contained in:
Soybean
2023-11-17 08:45:00 +08:00
parent 1ea4817f6a
commit e918a2c0f5
499 changed files with 15918 additions and 24708 deletions

74
src/hooks/common/form.ts Normal file
View File

@ -0,0 +1,74 @@
import { ref } from 'vue';
import type { FormInst } from 'naive-ui';
import { REG_USER_NAME, REG_PHONE, REG_PWD, REG_CODE_SIX, REG_EMAIL } from '@/constants/reg';
import { $t } from '@/locales';
export function useFormRules() {
const constantRules = {
userName: [
createRequiredRule($t('form.userName.required')),
{
pattern: REG_USER_NAME,
message: $t('form.userName.invalid'),
trigger: 'change'
}
],
phone: [
createRequiredRule($t('form.phone.required')),
{
pattern: REG_PHONE,
message: $t('form.phone.invalid'),
trigger: 'change'
}
],
pwd: [
createRequiredRule($t('form.pwd.required')),
{
pattern: REG_PWD,
message: $t('form.pwd.invalid'),
trigger: 'change'
}
],
code: [
createRequiredRule($t('form.code.required')),
{
pattern: REG_CODE_SIX,
message: $t('form.code.invalid'),
trigger: 'change'
}
],
email: [
createRequiredRule($t('form.email.required')),
{
pattern: REG_EMAIL,
message: $t('form.email.invalid'),
trigger: 'change'
}
]
} satisfies Record<string, App.Global.FormRule[]>;
function createRequiredRule(message: string) {
return {
required: true,
message
};
}
return {
constantRules,
createRequiredRule
};
}
export function useNaiveForm() {
const formRef = ref<FormInst | null>(null);
async function validate() {
await formRef.value?.validate();
}
return {
formRef,
validate
};
}

View File

@ -1,7 +0,0 @@
import useContext from './use-context';
import useBoolean from './use-boolean';
import useLoading from './use-loading';
import useLoadingEmpty from './use-loading-empty';
import useReload from './use-reload';
export { useContext, useBoolean, useLoading, useLoadingEmpty, useReload };

101
src/hooks/common/router.ts Normal file
View File

@ -0,0 +1,101 @@
import { useRouter } from 'vue-router';
import type { RouteLocationRaw } from 'vue-router';
import type { RouteKey } from '@elegant-router/types';
import { router as globalRouter } from '@/router';
/**
* router push
* @description jump to the specified route, it can replace function router.push
* @param inSetup whether is in vue script setup
*/
export function useRouterPush(inSetup = true) {
const router = inSetup ? useRouter() : globalRouter;
const route = globalRouter.currentRoute;
const routerPush = router.push;
const routerBack = router.back;
interface RouterPushOptions {
query?: Record<string, string>;
params?: Record<string, string>;
}
async function routerPushByKey(key: RouteKey, options?: RouterPushOptions) {
const { query, params } = options || {};
const routeLocation: RouteLocationRaw = {
name: key
};
if (query) {
routeLocation.query = query;
}
if (params) {
routeLocation.params = params;
}
return routerPush(routeLocation);
}
async function toHome() {
return routerPushByKey('root');
}
/**
* navigate to login page
* @param loginModule the login module
* @param redirectUrl the redirect url, if not specified, it will be the current route fullPath
*/
async function toLogin(loginModule?: UnionKey.LoginModule, redirectUrl?: string) {
const module = loginModule || 'pwd-login';
const options: RouterPushOptions = {
params: {
module
}
};
const redirect = redirectUrl || route.value.fullPath;
options.query = {
redirect
};
return routerPushByKey('login', options);
}
/**
* toggle login module
* @param module
*/
async function toggleLoginModule(module: UnionKey.LoginModule) {
const query = route.value.query as Record<string, string>;
return routerPushByKey('login', { query, params: { module } });
}
/**
* redirect from login
*/
async function redirectFromLogin() {
const redirect = route.value.query?.redirect as string;
if (redirect) {
routerPush(redirect);
} else {
toHome();
}
}
return {
route,
routerPush,
routerBack,
routerPushByKey,
toLogin,
toggleLoginModule,
redirectFromLogin
};
}

View File

@ -1,30 +0,0 @@
import { ref } from 'vue';
/**
* boolean组合式函数
* @param initValue 初始值
*/
export default function useBoolean(initValue = false) {
const bool = ref(initValue);
function setBool(value: boolean) {
bool.value = value;
}
function setTrue() {
setBool(true);
}
function setFalse() {
setBool(false);
}
function toggle() {
setBool(!bool.value);
}
return {
bool,
setBool,
setTrue,
setFalse,
toggle
};
}

View File

@ -1,21 +0,0 @@
import { inject, provide } from 'vue';
import type { InjectionKey } from 'vue';
/** 创建共享上下文状态 */
export default function useContext<T>(contextName = 'context') {
const injectKey: InjectionKey<T> = Symbol(contextName);
function useProvide(context: T) {
provide(injectKey, context);
return context;
}
function useInject() {
return inject(injectKey) as T;
}
return {
useProvide,
useInject
};
}

View File

@ -1,14 +0,0 @@
import useBoolean from './use-boolean';
export default function useLoadingEmpty(initLoading = false, initEmpty = false) {
const { bool: loading, setTrue: startLoading, setFalse: endLoading } = useBoolean(initLoading);
const { bool: empty, setBool: setEmpty } = useBoolean(initEmpty);
return {
loading,
startLoading,
endLoading,
empty,
setEmpty
};
}

View File

@ -1,11 +0,0 @@
import useBoolean from './use-boolean';
export default function useLoading(initValue = false) {
const { bool: loading, setTrue: startLoading, setFalse: endLoading } = useBoolean(initValue);
return {
loading,
startLoading,
endLoading
};
}

View File

@ -1,28 +0,0 @@
import { nextTick } from 'vue';
import useBoolean from './use-boolean';
/** 重载 */
export default function useReload() {
// 重载的标志
const { bool: reloadFlag, setTrue, setFalse } = useBoolean(true);
/**
* 触发重载
* @param duration - 延迟时间(ms)
*/
async function handleReload(duration = 0) {
setFalse();
await nextTick();
if (duration > 0) {
setTimeout(() => {
setTrue();
}, duration);
}
}
return {
reloadFlag,
handleReload
};
}