1 line
7.3 KiB
Plaintext
1 line
7.3 KiB
Plaintext
|
{"version":3,"file":"http.js","sources":["../../../../src/utils/http.ts"],"sourcesContent":["import { CustomRequestOptions } from '@/interceptors/request'\r\nimport { useUserStore } from '@/store/user'\r\n\r\nexport const http = <T>(options: CustomRequestOptions) => {\r\n const userStore = useUserStore()\r\n // 1. 返回 Promise 对象\r\n return new Promise<IResData<T>>((resolve, reject) => {\r\n uni.request({\r\n ...options,\r\n url: options.url,\r\n dataType: 'json',\r\n // #ifndef MP-WEIXIN\r\n responseType: 'json',\r\n // #endif\r\n // 响应成功\r\n success(res) {\r\n // 状态码 2xx,参考 axios 的设计\r\n if (res.statusCode >= 200 && res.statusCode < 300) {\r\n // 2.1 提取核心数据 res.data\r\n resolve(res.data as IResData<T>)\r\n } else if (res.statusCode === 401) {\r\n // 401错误 -> 清理用户信息,跳转到登录页\r\n userStore.clearUserInfo()\r\n uni.navigateTo({ url: '/pages/login/index' })\r\n reject(res)\r\n } else {\r\n // 其他错误 -> 根据后端错误信息轻提示\r\n !options.hideErrorToast &&\r\n uni.showToast({\r\n icon: 'none',\r\n title: res.data.message || '请求错误',\r\n })\r\n reject(res)\r\n }\r\n },\r\n // 响应失败\r\n fail(err) {\r\n uni.showToast({\r\n icon: 'none',\r\n title: '网络错误,换个网络试试',\r\n })\r\n reject(err)\r\n },\r\n })\r\n })\r\n}\r\n\r\n/**\r\n * GET 请求\r\n * @param url 后台地址\r\n * @param query 请求query参数\r\n * @returns\r\n */\r\nexport const httpGet = <T>(url: string, query?: Record<string, any>) => {\r\n return http<T>({\r\n url,\r\n query,\r\n method: 'GET',\r\n })\r\n}\r\n\r\n/**\r\n * POST 请求\r\n * @param url 后台地址\r\n * @param formData 表单数据\r\n * @param query 请求query参数,post请求也支持query,很多微信接口都需要\r\n * @returns\r\n */\r\nexport const httpPostMultipart = <T>(\r\n url: string,\r\n formData: FormData,\r\n query?: Record<string, any>,\r\n) => {\r\n return http<T>({\r\n url,\r\n query,\r\n data: formData,\r\n method: 'POST',\r\n headers: {\r\n 'Content-Type': 'multipart/form-data',\r\n },\r\n })\r\n}\r\n\r\n/**\r\n * POST 请求\r\n * @param url 后台地址\r\n * @param data 请求body参数\r\n * @param query 请求query参数,post请求也支持query,很多微信接口都需要\r\n * @returns\r\n */\r\nexport const httpPost = <T>(\r\n url: string,\r\n data?: Record<string, any>,\r\n query?: Record<string, any>,\r\n) => {\r\n return http<T>({\r\n url,\r\n query,\r\n data,\r\n method: 'POST',\r\n })\r\n}\r\n\r\n/**\r\n * 文件上传请求\r\n * @param url 文件上传的后台地址\r\n * @param filePath 本地文件路径\r\n * @param name 文件表单字段名\r\n * @param formData 额外的表单数据\r\n * @returns Promise\r\n */\r\nexport const httpUploadFile = (\r\n url: string,\r\n filePath: string,\r\n name = 'file',\r\n formData: Record<string, any> = {},\r\n) => {\r\n const userStore = useUserStore()\r\n return new Promise<IResData<any>>((resolve, reject) => {\r\n uni.uploadFile({\r\n url,\r\n filePath,\r\n name,\r\n formData,\r\n header: {\r\n satoken: `Bearer ${userStore.token}`, // 添加认证头部\r\n },\r\n timeout: 10000,\r\n success(res) {\r\n if (res.statusCode >= 200 && res.statusCode < 300) {\r\n resolve(JSON.parse(res.data) as IResData<any>)\r\n } else if (res.statusCode === 401) {\r\n userStore.clearUserInfo()\r\n uni.navigateTo({ url: '/pages/login/index' })\r\n reject(res)\r\n } else {\r\n uni.showToast({\r\n icon: 'none',\r\n title: '上传失败,' + (JSON.parse(res.data).message || '请稍后重试'),\r\n })\r\n reject(res)\r\n
|