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 }\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 * 上传多个文件\r\n * @param url 文件上传的后台地址\r\n * @param filePaths 本地文件路径数组\r\n * @param name 文件表单字段名\r\n * @param formData 额外的表单数据\r\n * @returns Promise\r\n */\r\nexport const httpUploadMultipleFiles = async (\r\n url: string,\r\n filePaths: string[],\r\n name = 'file',\r\n formData: Record<string, any> = {},\r\n) => {\r\n const uploadResults = []\r\n for (const filePath of filePaths) {\r\n try {\r\n const result = await httpUploadFile(url, filePath, name, formData)\r\n if (result.code == 200) {\r\n uploadResults.push(result.data.url)\r\n }\r\n } catch (err) {\r\n // 可以根据需求处理上传失败的情况\r\n console.error('上传文件失败:', err)\r\n }\r\n }\r\n return uploadResults\r\n}\r\n\r\nhttp.uploadFile = httpUploadFile\r\nhttp.uploadMultipleFiles = httpUploadMultipleFiles\r\nhttp.get = httpGet\r\nhttp.post = httpPost\r\n"],"names":["useUserStore","uni"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGa,MAAA,OAAO,CAAI,YAAkC;AACxD,QAAM,YAAYA,WAAAA;AAElB,SAAO,IAAI,QAAqB,CAAC,SAAS,WAAW;AACnDC,kBAAAA,MAAI,QAAQ,iCACP,UADO;AAAA,MAEV,KAAK,QAAQ;AAAA,MACb,UAAU;AAAA;AAAA,MAKV,QAAQ,KAAK;AAEX,YAAI,IAAI,cAAc,OAAO,IAAI,aAAa,KAAK;AAEjD,kBAAQ,IAAI,IAAmB;AAAA,QAAA,WACtB,IAAI,eAAe,KAAK;AAEjC,oBAAU,cAAc;AACxBA,wBAAAA,MAAI,WAAW,EAAE,KAAK,qBAAsB,CAAA;AAC5C,iBAAO,GAAG;AAAA,QAAA,OACL;AAEJ,WAAA,QAAQ,kBACPA,cAAA,MAAI,UAAU;AAAA,YACZ,MAAM;AAAA,YACN,OAAO,IAAI,KAAK,WAAW;AAAA,UAAA,CAC5B;AACH,iBAAO,GAAG;AAAA,QACZ;AAAA,MACF;AAAA;AAAA,MAEA,KAAK,KAAK;AACRA,sBAAAA,MAAI,UAAU;AAAA,UACZ,MAAM;AAAA,UACN,OAAO;AAAA,QAAA,CACR;AACD,eAAO,GAAG;AAAA,MACZ;AAAA,IAAA,EACD;AAAA,EAAA,CACF;AACH;AAQa,MAAA,UAAU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