From f5553435b896d6951f738205e8b2ac81509ba50c Mon Sep 17 00:00:00 2001 From: mackt <1033530438@qq.com> Date: Fri, 26 Apr 2024 16:18:07 +0800 Subject: [PATCH] =?UTF-8?q?perf(utils):=20=E7=9B=AE=E5=BD=95=E7=BB=93?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components.json | 2 +- src/components/AudioPlayer/PlayerControl.tsx | 3 +-- src/components/Header/UserCard.tsx | 3 +-- src/components/Login/LoginForm.tsx | 4 +--- src/components/ui/alert-dialog.tsx | 2 +- src/components/ui/avatar.tsx | 2 +- src/components/ui/button.tsx | 2 +- src/components/ui/dialog.tsx | 2 +- src/components/ui/hover-card.tsx | 2 +- src/components/ui/popover.tsx | 2 +- src/components/ui/toast.tsx | 2 +- src/lib/cache.ts | 8 -------- src/store/user.ts | 15 -------------- src/utils/cache.ts | 12 +++++++++++ src/utils/context.ts | 21 -------------------- src/utils/index.ts | 6 +++++- src/utils/login.ts | 7 +++++++ src/utils/logout.ts | 8 -------- src/utils/request/client/utils.ts | 5 +---- src/utils/time.ts | 5 +++-- src/{lib/utils.ts => utils/ui.ts} | 0 src/utils/wrapper.ts | 18 ----------------- uno.config.ts | 2 +- 23 files changed, 40 insertions(+), 93 deletions(-) delete mode 100644 src/lib/cache.ts create mode 100644 src/utils/cache.ts delete mode 100644 src/utils/context.ts delete mode 100644 src/utils/logout.ts rename src/{lib/utils.ts => utils/ui.ts} (100%) delete mode 100644 src/utils/wrapper.ts diff --git a/components.json b/components.json index a14986d..052fb9b 100644 --- a/components.json +++ b/components.json @@ -11,7 +11,7 @@ "rsc": false, "tsx": true, "aliases": { - "utils": "/src/lib/utils", + "utils": "/src/utils/ui", "components": "/src/components", "ui": "/src/components/ui" } diff --git a/src/components/AudioPlayer/PlayerControl.tsx b/src/components/AudioPlayer/PlayerControl.tsx index b779f94..ba27b6c 100644 --- a/src/components/AudioPlayer/PlayerControl.tsx +++ b/src/components/AudioPlayer/PlayerControl.tsx @@ -4,11 +4,10 @@ import Image from 'next/image'; -import { secondToDate } from '@/utils/time'; - import styles from './index.module.css'; import { PlayerButton, CollectButton } from '@/components'; +import { secondToDate } from '@/utils'; interface Props { audio: SongInfo | null; diff --git a/src/components/Header/UserCard.tsx b/src/components/Header/UserCard.tsx index 44f5608..4fe81ba 100644 --- a/src/components/Header/UserCard.tsx +++ b/src/components/Header/UserCard.tsx @@ -8,9 +8,8 @@ import { useShallow } from 'zustand/react/shallow'; import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar'; -import logout from '@/utils/logout'; - import useUserStore from '@/store/user'; +import { logout } from '@/utils'; interface Props { className?: string; diff --git a/src/components/Login/LoginForm.tsx b/src/components/Login/LoginForm.tsx index f91bb58..4c7d7dd 100644 --- a/src/components/Login/LoginForm.tsx +++ b/src/components/Login/LoginForm.tsx @@ -6,12 +6,10 @@ import { debounce } from 'lodash'; import { useRouter } from 'next/navigation'; import { useShallow } from 'zustand/react/shallow'; -import { useLoginRedirect } from '@/utils/login'; - import { Button } from '@/components'; -import { setAccessToken } from '@/lib/cache'; import { apiSendSMS, apiUserLogin } from '@/services'; import useUserStore from '@/store/user'; +import { useLoginRedirect, setAccessToken } from '@/utils'; export default function LoginForm({ className }: { className?: string }) { const redirect = useLoginRedirect(); diff --git a/src/components/ui/alert-dialog.tsx b/src/components/ui/alert-dialog.tsx index a0b1dec..9646fe1 100644 --- a/src/components/ui/alert-dialog.tsx +++ b/src/components/ui/alert-dialog.tsx @@ -1,7 +1,7 @@ import * as React from 'react'; import * as AlertDialogPrimitive from '@radix-ui/react-alert-dialog'; -import { cn } from '@/lib/utils'; +import { cn } from '@/utils'; import { buttonVariants } from '@/components/ui/button'; const AlertDialog = AlertDialogPrimitive.Root; diff --git a/src/components/ui/avatar.tsx b/src/components/ui/avatar.tsx index acef0d1..c202234 100644 --- a/src/components/ui/avatar.tsx +++ b/src/components/ui/avatar.tsx @@ -2,7 +2,7 @@ import * as React from 'react'; import * as AvatarPrimitive from '@radix-ui/react-avatar'; -import { cn } from '@/lib/utils'; +import { cn } from '@/utils'; const Avatar = React.forwardRef< React.ElementRef, diff --git a/src/components/ui/button.tsx b/src/components/ui/button.tsx index 7d2be38..8a28123 100644 --- a/src/components/ui/button.tsx +++ b/src/components/ui/button.tsx @@ -4,7 +4,7 @@ import * as React from 'react'; import { Slot } from '@radix-ui/react-slot'; import { cva, type VariantProps } from 'class-variance-authority'; -import { cn } from '@/lib/utils'; +import { cn } from '@/utils'; const buttonVariants = cva( 'inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50', diff --git a/src/components/ui/dialog.tsx b/src/components/ui/dialog.tsx index 9eb0e07..7409988 100644 --- a/src/components/ui/dialog.tsx +++ b/src/components/ui/dialog.tsx @@ -2,7 +2,7 @@ import * as React from 'react'; import * as DialogPrimitive from '@radix-ui/react-dialog'; import { X } from 'lucide-react'; -import { cn } from '@/lib/utils'; +import { cn } from '@/utils'; const Dialog = DialogPrimitive.Root; diff --git a/src/components/ui/hover-card.tsx b/src/components/ui/hover-card.tsx index 4c5c0b3..caa6686 100644 --- a/src/components/ui/hover-card.tsx +++ b/src/components/ui/hover-card.tsx @@ -2,7 +2,7 @@ import * as React from 'react'; import * as HoverCardPrimitive from '@radix-ui/react-hover-card'; -import { cn } from '@/lib/utils'; +import { cn } from '@/utils'; const HoverCard = HoverCardPrimitive.Root; diff --git a/src/components/ui/popover.tsx b/src/components/ui/popover.tsx index 325d7b7..8ba065b 100644 --- a/src/components/ui/popover.tsx +++ b/src/components/ui/popover.tsx @@ -1,7 +1,7 @@ import * as React from 'react'; import * as PopoverPrimitive from '@radix-ui/react-popover'; -import { cn } from '@/lib/utils'; +import { cn } from '@/utils'; const Popover = PopoverPrimitive.Root; diff --git a/src/components/ui/toast.tsx b/src/components/ui/toast.tsx index 268ea25..57bfbc6 100644 --- a/src/components/ui/toast.tsx +++ b/src/components/ui/toast.tsx @@ -3,7 +3,7 @@ import * as ToastPrimitives from '@radix-ui/react-toast'; import { cva, type VariantProps } from 'class-variance-authority'; import { X } from 'lucide-react'; import styles from './index.module.css'; -import { cn } from '@/lib/utils'; +import { cn } from '@/utils'; const ToastProvider = ToastPrimitives.Provider; diff --git a/src/lib/cache.ts b/src/lib/cache.ts deleted file mode 100644 index d999c7c..0000000 --- a/src/lib/cache.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { setCookie } from 'nookies'; - -export function setAccessToken(token: string) { - setCookie(null, 'token', token, { - maxAge: 7 * 24 * 60 * 60, - path: '/', - }); -} diff --git a/src/store/user.ts b/src/store/user.ts index 6df722f..23d467a 100644 --- a/src/store/user.ts +++ b/src/store/user.ts @@ -75,21 +75,6 @@ const useUserStore = create()( ), setUserInfo, // userLogin: async (params) => { - // const result = { code: 200, data: '1', message: '登录成功' }; - // if (result.code === 200) { - // setCookie( - // null, - // 'token', - // 'Bearer eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiIxNzc5MDMxOTU1MzUwODg0MzUyIiwic3ViIjoi6ZuA5LmQLVQ5WkhteGkxVSIsImlhdCI6MTcxMzMzNTY1MCwiYXZhdGFyIjoidXNlci9hdmF0YXIvZGVmYXVsdC5wbmciLCJyb2xlcyI6InVzZXIiLCJleHAiOjE3MTM5NDA0NTB9.YtNph3J4XPyADfFAgCpUvh5VE4RgSRC0FH1ZDcNtPhU', - // { - // maxAge: 7 * 24 * 60 * 60, - // path: '/', - // }, - // ); - // await getUserInfo(); - // } - - // return result; // // const result = await apiUserLogin(params); // // if (result.code === 200) { // // setCookie(null, 'token', result.data, { diff --git a/src/utils/cache.ts b/src/utils/cache.ts new file mode 100644 index 0000000..67c5f5c --- /dev/null +++ b/src/utils/cache.ts @@ -0,0 +1,12 @@ +import { setCookie, destroyCookie } from 'nookies'; + +export function setAccessToken(token: string) { + setCookie(null, 'token', token, { + maxAge: 7 * 24 * 60 * 60, + path: '/', + }); +} + +export function clearAccessToken() { + destroyCookie(null, 'token', { path: '/' }); +} diff --git a/src/utils/context.ts b/src/utils/context.ts deleted file mode 100644 index fea6639..0000000 --- a/src/utils/context.ts +++ /dev/null @@ -1,21 +0,0 @@ -/** - * 存储用户信息 - */ - -import React from 'react'; - -export interface IMyContextProps { - /** 是否登录标记 */ - isLoggedIn: boolean; - /** 用户信息 */ - user: UserInfo | null; // 用户信息 - /** useState 创建的函数 , 用于设置用户信息 */ - setUser?: any; -} - -const MyContext = React.createContext({ - isLoggedIn: false, - user: null, -}); - -export default MyContext; diff --git a/src/utils/index.ts b/src/utils/index.ts index f2a6b49..7bfceb0 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,2 +1,6 @@ +export * from './cache'; export * from './helpers'; -export * from './wrapper'; +export * from './login'; +export * from './time'; +export * from './ua'; +export * from './ui'; diff --git a/src/utils/login.ts b/src/utils/login.ts index fd0e82a..2113604 100644 --- a/src/utils/login.ts +++ b/src/utils/login.ts @@ -1,8 +1,15 @@ import { useSearchParams } from 'next/navigation'; +import { clearAccessToken } from '@/utils'; + export const useLoginRedirect = () => { const searchParams = useSearchParams(); const redirect = searchParams.get('redirect'); if (!redirect) return '/'; return redirect; }; + +export const logout = () => { + clearAccessToken(); + localStorage.clear(); +}; diff --git a/src/utils/logout.ts b/src/utils/logout.ts deleted file mode 100644 index 1b30b78..0000000 --- a/src/utils/logout.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { destroyCookie } from 'nookies'; - -const logout = () => { - destroyCookie(null, 'token', { path: '/' }); - localStorage.clear(); -}; - -export default logout; diff --git a/src/utils/request/client/utils.ts b/src/utils/request/client/utils.ts index d591009..2afc913 100644 --- a/src/utils/request/client/utils.ts +++ b/src/utils/request/client/utils.ts @@ -7,12 +7,9 @@ import qs from 'qs'; import { openLogin } from '@/components/Login/loginContext'; import { toast } from '@/components/ui/use-toast'; -import logout from '@/utils/logout'; import { IQueryParams, IResponse } from '@/utils/request/type'; -// import { apiAutoLogin } from '@/services'; - -// import jwtDecode from 'jwt-decode'; +import { logout } from '@/utils'; const defaultHost = process.env.NEXT_PUBLIC_HOST; const aiHost = process.env.NEXT_PUBLIC_AI_HOST; diff --git a/src/utils/time.ts b/src/utils/time.ts index deb4278..c123fe8 100644 --- a/src/utils/time.ts +++ b/src/utils/time.ts @@ -1,3 +1,4 @@ +// 格式化歌词时间 export const secondToDate: (v: number) => string = (second) => { const hourNum = Math.floor(second / 3600); const minuteNum = Math.floor((second / 60) % 60); @@ -9,7 +10,7 @@ export const secondToDate: (v: number) => string = (second) => { return `${h}${m}${s}`; }; -// 格式化个位数 -const unitAddZero: (v: number) => string = (v) => { +// 格式化个位数到两位数 +export const unitAddZero: (v: number) => string = (v) => { return v < 10 ? `0${v}` : `${v}`; }; diff --git a/src/lib/utils.ts b/src/utils/ui.ts similarity index 100% rename from src/lib/utils.ts rename to src/utils/ui.ts diff --git a/src/utils/wrapper.ts b/src/utils/wrapper.ts deleted file mode 100644 index ebf4b2c..0000000 --- a/src/utils/wrapper.ts +++ /dev/null @@ -1,18 +0,0 @@ -export const headerContentType = { - form: { - headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, - }, - json: { - headers: { 'Content-Type': 'application/json; charset=utf-8' }, - }, -}; - -export const createFormBody: (formDataObject: any) => string = (formDataObject: any) => { - const encodedForm = new URLSearchParams(); - for (const key in formDataObject) { - if (formDataObject.hasOwnProperty(key)) { - encodedForm.append(key, formDataObject[key]); - } - } - return encodedForm.toString(); -}; diff --git a/uno.config.ts b/uno.config.ts index d2dc5b1..0709b64 100644 --- a/uno.config.ts +++ b/uno.config.ts @@ -1,7 +1,7 @@ import { defineConfig, presetUno, presetAttributify, presetIcons } from 'unocss'; +// import { colors } from 'unocss/preset-mini'; import presetAnimations from 'unocss-preset-animations'; import { presetShadcn } from 'unocss-preset-shadcn'; -import { colors } from 'unocss/preset-mini'; export default defineConfig({ presets: [