feat: 接口对接完成

This commit is contained in:
anonymous
2025-01-03 16:44:05 +08:00
parent 0889d068a8
commit 2d19bbce4c
37 changed files with 1220 additions and 398 deletions
+51 -10
View File
@@ -1,4 +1,6 @@
import { CustomRequestOptions } from '@/interceptors/request'
import { loginRoute } from '@/interceptors/route'
import { useUserStore } from '@/store'
export const http = <T>(options: CustomRequestOptions) => {
// 1. 返回 Promise 对象
@@ -10,23 +12,62 @@ export const http = <T>(options: CustomRequestOptions) => {
responseType: 'json',
// #endif
// 响应成功
success(res) {
async success(res) {
// 状态码 2xx,参考 axios 的设计
if (res.statusCode >= 200 && res.statusCode < 300) {
// 2.1 提取核心数据 res.data
resolve(res.data as IResData<T>)
if (res.header['content-type'].includes('application/json')) {
// 2.1 提取核心数据 res.data
const data = res.data as IResData<T>
if (data.code === 1) {
resolve(data as IResData<T>)
} else if (data.code === 401) {
const userStore = useUserStore()
userStore.logout()
!options.hideErrorToast &&
(await uni.showModal({
title: '提示',
content: data.msg || '请求错误',
showCancel: false,
}))
uni.reLaunch({
url: loginRoute,
})
reject(res)
} else {
!options.hideErrorToast &&
(await uni.showModal({
title: '提示',
content: data.msg || '请求错误',
showCancel: false,
}))
reject(res)
}
} else {
resolve(res.data as any)
}
} else if (res.statusCode === 401) {
// 401错误 -> 清理用户信息,跳转到登录页
// userStore.clearUserInfo()
// uni.navigateTo({ url: '/pages/login/login' })
const userStore = useUserStore()
userStore.logout()
!options.hideErrorToast &&
(await uni.showModal({
title: '提示',
content: res.data.msg || '请求错误',
showCancel: false,
}))
uni.reLaunch({
url: loginRoute,
})
reject(res)
} else {
// 其他错误 -> 根据后端错误信息轻提示
!options.hideErrorToast &&
uni.showToast({
icon: 'none',
title: (res.data as IResData<T>).msg || '请求错误',
})
(await uni.showModal({
title: '提示',
content: (res.data as IResData<T>).msg || '请求错误',
showCancel: false,
}))
reject(res)
}
},
+63 -16
View File
@@ -1,5 +1,6 @@
import { pages, subPackages, tabBar } from '@/pages.json'
import { isMp } from './platform'
import dayjs from 'dayjs'
export const getLastPage = () => {
// getCurrentPages() 至少有1个元素,所以不再额外判断
@@ -9,15 +10,8 @@ export const getLastPage = () => {
return pages[pages.length - 1]
}
export const getPath = (url: string) => {
if (typeof url === 'string') {
return url.split('?')[0].replace(/^pages/, '/pages')
}
throw new Error('please input string type')
}
/** 判断当前页面是否是tabbar页 */
export const getIsTabbar = (url?: string) => {
export const getIsTabbar = () => {
if (!tabBar) {
return false
}
@@ -25,14 +19,29 @@ export const getIsTabbar = (url?: string) => {
// 通常有tabBar的话,list不能有空,且至少有2个元素,这里其实不用处理
return false
}
if (url) {
console.log(tabBar.list, getPath(url))
return !!tabBar.list.some((e) => getPath(e.pagePath) === getPath(url))
} else {
const lastPage = getLastPage()
const currPath = lastPage.route
return !!tabBar.list.some((e) => e.pagePath === currPath)
}
const lastPage = getLastPage()
const currPath = lastPage.route
return !!tabBar.list.find((e) => e.pagePath === currPath)
}
/**
* 获取当前页面路由的 path 路径和 redirectPath 路径
* path 如 /pages/login/index
* redirectPath 如 /pages/demo/base/route-interceptor
*/
export const currRoute = () => {
const lastPage = getLastPage()
const currRoute = (lastPage as any).$page
// console.log('lastPage.$page:', currRoute)
// console.log('lastPage.$page.fullpath:', currRoute.fullPath)
// console.log('lastPage.$page.options:', currRoute.options)
// console.log('lastPage.options:', (lastPage as any).options)
// 经过多端测试,只有 fullPath 靠谱,其他都不靠谱
const { fullPath } = currRoute as { fullPath: string }
// console.log(fullPath)
// eg: /pages/login/index?redirect=%2Fpages%2Fdemo%2Fbase%2Froute-interceptor (小程序)
// eg: /pages/login/index?redirect=%2Fpages%2Froute-interceptor%2Findex%3Fname%3Dfeige%26age%3D30(h5)
return getUrlObj(fullPath)
}
const ensureDecodeURIComponent = (url: string) => {
@@ -131,6 +140,9 @@ export const getEnvBaseUrl = () => {
export const getEnvBaseUploadUrl = () => {
// 请求基准地址
let baseUploadUrl = import.meta.env.VITE_UPLOAD_BASEURL
if (JSON.parse(__VITE_APP_PROXY__)) {
baseUploadUrl = `${import.meta.env.VITE_APP_PROXY_PREFIX}/${baseUploadUrl.split(import.meta.env.VITE_APP_PROXY_PREFIX + '/')[1]}`
}
// 小程序端环境区分
if (isMp) {
@@ -180,3 +192,38 @@ export function mergeStep(wrapped: (...rest: any[]) => Promise<any>) {
return _mergeStepRequestInstance
}
}
// Promise 版modal
export const showModal = (
content: string,
{ showCancel, title, ...options }: UniApp.ShowModalOptions = {},
) => {
return new Promise((resolve, reject) =>
uni.showModal({
showCancel: showCancel === true,
title: title || '提示',
...options,
content,
success(e) {
if (e.confirm) {
return resolve(e)
}
reject(e)
},
fail: reject,
}),
)
}
/** 格式化时间 */
export const formatTime = (time: string | number) => {
return dayjs(time).format('YYYY-MM-DD HH:mm:ss')
}
/** 获取图片url */
export const getImageUrl = (image: any) => {
if (typeof image === 'string') {
return /^http|data:/.test(image) ? image : `${import.meta.env.VITE_SERVER_BASEURL}${image}`
}
return image
}