diff --git a/src/api/alova-foo.ts b/src/api/alova-foo.ts index 93a9eb4..e33a17c 100644 --- a/src/api/alova-foo.ts +++ b/src/api/alova-foo.ts @@ -1,4 +1,4 @@ -import { API_DOMAINS, http } from '@/utils/request/alova' +import { API_DOMAINS, http } from '@/http/request/alova' export interface IFoo { id: number diff --git a/src/api/login.ts b/src/api/login.ts index 9732650..de4266d 100644 --- a/src/api/login.ts +++ b/src/api/login.ts @@ -1,5 +1,5 @@ import type { ICaptcha, IUpdateInfo, IUpdatePassword, IUserInfoVo, IUserLogin } from './types/login' -import { http } from '@/utils/http' +import { http } from '@/http/http' /** * 登录表单 diff --git a/src/utils/http.ts b/src/http/http.ts similarity index 97% rename from src/utils/http.ts rename to src/http/http.ts index fb4d9bd..edf510d 100644 --- a/src/utils/http.ts +++ b/src/http/http.ts @@ -1,4 +1,4 @@ -import type { CustomRequestOptions } from '@/interceptors/request' +import type { CustomRequestOptions } from '@/http/interceptor' export function http(options: CustomRequestOptions) { // 1. 返回 Promise 对象 diff --git a/src/interceptors/request.ts b/src/http/interceptor.ts similarity index 97% rename from src/interceptors/request.ts rename to src/http/interceptor.ts index 48cbf09..8c99a22 100644 --- a/src/interceptors/request.ts +++ b/src/http/interceptor.ts @@ -1,7 +1,7 @@ import { useUserStore } from '@/store' import { getEnvBaseUrl } from '@/utils' import { platform } from '@/utils/platform' -import { stringifyQuery } from '@/utils/queryString' +import { stringifyQuery } from './queryString' export type CustomRequestOptions = UniApp.RequestOptions & { query?: Record diff --git a/src/utils/queryString.ts b/src/http/queryString.ts similarity index 100% rename from src/utils/queryString.ts rename to src/http/queryString.ts diff --git a/src/utils/request/alova.ts b/src/http/request/alova.ts similarity index 100% rename from src/utils/request/alova.ts rename to src/http/request/alova.ts diff --git a/src/utils/request/enum.ts b/src/http/request/enum.ts similarity index 100% rename from src/utils/request/enum.ts rename to src/http/request/enum.ts diff --git a/src/utils/request/types.ts b/src/http/request/types.ts similarity index 100% rename from src/utils/request/types.ts rename to src/http/request/types.ts diff --git a/src/interceptors/index.ts b/src/interceptors/index.ts deleted file mode 100644 index 786c44f..0000000 --- a/src/interceptors/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export { prototypeInterceptor } from './prototype' -export { requestInterceptor } from './request' -export { routeInterceptor } from './route' diff --git a/src/interceptors/prototype.ts b/src/interceptors/prototype.ts deleted file mode 100644 index 647e6bd..0000000 --- a/src/interceptors/prototype.ts +++ /dev/null @@ -1,14 +0,0 @@ -export const prototypeInterceptor = { - install() { - // 解决低版本手机不识别 array.at() 导致运行报错的问题 - if (typeof Array.prototype.at !== 'function') { - Array.prototype.at = function (index: number) { - if (index < 0) - return this[this.length + index] - if (index >= this.length) - return undefined - return this[index] - } - } - }, -} diff --git a/src/main.ts b/src/main.ts index b86a5fd..a357275 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,7 +1,8 @@ import { VueQueryPlugin } from '@tanstack/vue-query' import { createSSRApp } from 'vue' import App from './App.vue' -import { prototypeInterceptor, requestInterceptor, routeInterceptor } from './interceptors' +import { requestInterceptor } from './http/interceptor' +import { routeInterceptor } from './router/interceptor' import store from './store' import '@/style/index.scss' @@ -12,7 +13,6 @@ export function createApp() { app.use(store) app.use(routeInterceptor) app.use(requestInterceptor) - app.use(prototypeInterceptor) app.use(VueQueryPlugin) return { diff --git a/src/interceptors/route.ts b/src/router/interceptor.ts similarity index 100% rename from src/interceptors/route.ts rename to src/router/interceptor.ts diff --git a/src/service/app/pet.ts b/src/service/app/pet.ts index 70b95ef..1960682 100644 --- a/src/service/app/pet.ts +++ b/src/service/app/pet.ts @@ -1,7 +1,7 @@ /* eslint-disable */ // @ts-ignore import request from '@/utils/request'; -import { CustomRequestOptions } from '@/interceptors/request'; +import { CustomRequestOptions } from '@/http/interceptor'; import * as API from './types'; diff --git a/src/service/app/pet.vuequery.ts b/src/service/app/pet.vuequery.ts index c6c0b7d..4029b87 100644 --- a/src/service/app/pet.vuequery.ts +++ b/src/service/app/pet.vuequery.ts @@ -3,7 +3,7 @@ import { queryOptions, useMutation } from '@tanstack/vue-query'; import type { DefaultError } from '@tanstack/vue-query'; import request from '@/utils/request'; -import { CustomRequestOptions } from '@/interceptors/request'; +import { CustomRequestOptions } from '@/http/interceptor'; import * as apis from './pet'; import * as API from './types'; diff --git a/src/service/app/store.ts b/src/service/app/store.ts index 0d87f52..710bd1c 100644 --- a/src/service/app/store.ts +++ b/src/service/app/store.ts @@ -1,7 +1,7 @@ /* eslint-disable */ // @ts-ignore import request from '@/utils/request'; -import { CustomRequestOptions } from '@/interceptors/request'; +import { CustomRequestOptions } from '@/http/interceptor'; import * as API from './types'; diff --git a/src/service/app/store.vuequery.ts b/src/service/app/store.vuequery.ts index dd6d660..d1e20bd 100644 --- a/src/service/app/store.vuequery.ts +++ b/src/service/app/store.vuequery.ts @@ -3,7 +3,7 @@ import { queryOptions, useMutation } from '@tanstack/vue-query'; import type { DefaultError } from '@tanstack/vue-query'; import request from '@/utils/request'; -import { CustomRequestOptions } from '@/interceptors/request'; +import { CustomRequestOptions } from '@/http/interceptor'; import * as apis from './store'; import * as API from './types'; diff --git a/src/service/app/user.ts b/src/service/app/user.ts index 2474272..bb39677 100644 --- a/src/service/app/user.ts +++ b/src/service/app/user.ts @@ -1,7 +1,7 @@ /* eslint-disable */ // @ts-ignore import request from '@/utils/request'; -import { CustomRequestOptions } from '@/interceptors/request'; +import { CustomRequestOptions } from '@/http/interceptor'; import * as API from './types'; diff --git a/src/service/app/user.vuequery.ts b/src/service/app/user.vuequery.ts index 0e13636..79d2c57 100644 --- a/src/service/app/user.vuequery.ts +++ b/src/service/app/user.vuequery.ts @@ -3,7 +3,7 @@ import { queryOptions, useMutation } from '@tanstack/vue-query'; import type { DefaultError } from '@tanstack/vue-query'; import request from '@/utils/request'; -import { CustomRequestOptions } from '@/interceptors/request'; +import { CustomRequestOptions } from '@/http/interceptor'; import * as apis from './user'; import * as API from './types'; diff --git a/src/service/index/foo.ts b/src/service/index/foo.ts index 88c1138..56ecac4 100644 --- a/src/service/index/foo.ts +++ b/src/service/index/foo.ts @@ -1,4 +1,4 @@ -import { http } from '@/utils/http' +import { http } from '@/http/http' export interface IFooItem { id: string diff --git a/src/utils/dateUtil.ts b/src/utils/dateUtil.ts deleted file mode 100644 index 78e3178..0000000 --- a/src/utils/dateUtil.ts +++ /dev/null @@ -1,150 +0,0 @@ -import dayjs from 'dayjs' -import calendar from 'dayjs/plugin/calendar' -import quarterOfYear from 'dayjs/plugin/quarterOfYear' -import relativeTime from 'dayjs/plugin/relativeTime' -import updateLocale from 'dayjs/plugin/updateLocale' -import utc from 'dayjs/plugin/utc' -import weekday from 'dayjs/plugin/weekday' -import 'dayjs/locale/zh-cn' - -dayjs.extend(calendar) -dayjs.extend(quarterOfYear) -dayjs.extend(relativeTime) -dayjs.extend(updateLocale) -dayjs.extend(utc) -dayjs.extend(weekday) - -dayjs.locale('zh-cn') - -dayjs.updateLocale('zh-cn', { - calendar: { - sameDay: 'HH:mm', - nextDay: '[明天]', - nextWeek: 'dddd', - lastDay: '[昨天] HH:mm', - lastWeek: 'dddd HH:mm', - sameElse: 'YYYY年M月D日 HH:mm', - }, - relativeTime: { - future: '%s后', - past: '%s前', - s: '几秒', - m: '1分钟', - mm: '%d分钟', - h: '1小时', - hh: '%d小时', - d: '1天', - dd: '%d天', - M: '1个月', - MM: '%d个月', - y: '1年', - yy: '%d年', - }, -}) - -/** 时间工具 */ -export const dateUtil = dayjs - -export const DATETIME_FORMAT = 'YYYY-MM-DD HH:mm:ss' -export const DATE_FORMAT = 'YYYY-MM-DD' -export const TIME_FORMAT = 'HH:mm' - -/** - * 格式化日期 - * @param _date 日期对象、时间戳或字符串 - * @param format 格式字符串 - * @returns 格式化后的日期字符串 - */ -function _format(_date: dayjs.ConfigType, format: string): string { - if (!_date) { - return _date as any - } - const date = dateUtil(_date) - return date.isValid() ? date.format(format) : (_date as string) -} -/** - * 格式化为日期时间字符串 - * @param date 日期对象、时间戳或字符串 - * @param format 格式字符串,默认为 DATETIME_FORMAT - * @returns 格式化后的日期时间字符串 - */ -export function formatToDatetime(date: dayjs.ConfigType = undefined, format: string = DATETIME_FORMAT): string { - return _format(date, format) -} - -/** - * 格式化为日期字符串 - * @param date 日期对象、时间戳或字符串 - * @param format 格式字符串,默认为 DATE_FORMAT - * @returns 格式化后的日期字符串 - */ -export function formatToDate(date: dayjs.ConfigType = undefined, format: string = DATE_FORMAT): string { - return _format(date, format) -} - -/** - * 格式化为日期字符串 - * @param date 日期对象、时间戳或字符串 - * @param format 格式字符串,默认为 TIME_FORMAT - * @returns 格式化后的日期字符串 - */ -export function formatToTime(date: dayjs.ConfigType = undefined, format: string = TIME_FORMAT): string { - return _format(date, format) -} - -/** - * 时间人性化显示 - * @param date 要格式化的日期 - * @param oppositeDate 参考日期,默认为当前时间 - * @returns 人性化的时间字符串 - */ -export function humanizedDate(date: dayjs.ConfigType, oppositeDate: dayjs.ConfigType = undefined): string { - if (!date || !dateUtil(date).isValid()) { - return '' - } - - const now = oppositeDate ? dateUtil(oppositeDate) : dateUtil() - const diffSeconds = now.diff(date, 'second') - const diffMinutes = now.diff(date, 'minute') - const diffHours = now.diff(date, 'hour') - const diffDays = now.diff(date, 'day') - - if (diffSeconds < 60) { - return `${diffSeconds}秒前` - } - else if (diffMinutes < 60) { - return `${diffMinutes}分钟前` - } - else if (diffHours < 24) { - return `${diffHours}小时前` - } - else if (diffDays < 7) { - return `${diffDays}天前` - } - else { - return formatToDatetime(date) - } -} - -/** - * 获取时辰问候语 - * @returns 根据当前时间返回相应的问候语 - */ -export function getGreeting(): string { - const currentHour = dateUtil().hour() - if (currentHour >= 5 && currentHour < 12) { - return '早上好' - } - else if (currentHour >= 12 && currentHour < 14) { - return '中午好' - } - else if (currentHour >= 14 && currentHour < 18) { - return '下午好' - } - else if (currentHour >= 18 && currentHour < 24) { - return '晚上好' - } - else { - return '深夜了' - } -} diff --git a/src/utils/request.ts b/src/utils/request.ts index 9879f25..8b16925 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -1,4 +1,4 @@ -import type { CustomRequestOptions } from '@/interceptors/request' +import type { CustomRequestOptions } from '@/http/interceptor' /** * 请求方法: 主要是对 uni.request 的封装,去适配 openapi-ts-request 的 request 方法