Files
cmgd-mini-app/api/request.ts
2026-01-16 03:58:57 +08:00

145 lines
4.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// import { ref } from "vue";
// import config from "../config";
// import { goToLoginPage } from "../utils/helper";
// interface Options {
// url : string,
// method ?: 'GET' | 'POST',
// data ?: any,
// params ?: any,
// dataType ?: string,
// responseType ?: string
// }
// interface Response {
// code : number;
// msg ?: string;
// data : any;
// }
// export const getUploadImageUrl = () => {
// return config.api_base_url + '/upload/image' + '&wxapp_id=' + config.wxapp_id + '&token=' + uni.getStorageSync('token');
// }
// export const request = (options : Options) => {
// return new Promise((resolve, reject) => {
// uni.request({
// url: config.api_base_url + options.url + '&wxapp_id=' + config.wxapp_id + '&token=' + uni.getStorageSync('token'),
// method: options.method || 'GET',
// data: options.data || options.params || {},
// dataType: options.dataType || "json",
// responseType: options.responseType || "text",
// success(res) {
// const ret : Response = res.data
// if (ret.code === -1) {
// goToLoginPage()
// } else if (ret.code === 1) {
// resolve(ret.data)
// } else if (ret.code === 0) {
// uni.showToast({
// title:ret.msg,
// icon:'none'
// })
// }
// },
// fail(err) {
// reject(err)
// },
// })
// })
// }
import { ref } from 'vue';
import config from '@/config';
import { goToLoginPage } from '@/utils/helper';
// 定义请求参数接口
interface Options {
url : string; // 请求路径不包含基础URL
method ?: 'GET' | 'POST'; // 请求方法默认为GET
data ?: any; // POST请求体数据
params ?: any; // GET请求参数与data二选一
dataType ?: string; // 返回数据格式默认json
responseType ?: string; // 响应数据类型默认text
}
// 定义响应数据结构接口
interface Response {
code : number;
msg ?: string;
data : any;
}
// /**
// * 获取图片上传的完整URL
// * @returns {string} 完整的图片上传URL包含基础URL、wxapp_id和token参数
// */
// export const getUploadImageUrl = () => {
// return config.api_base_url + '/upload/image' + '&wxapp_id=' + config.wxapp_id + '&token=' + uni.getStorageSync('token');
// };
/**
* 封装uni.request的通用请求方法
* @param {Options} options 请求配置项
* @returns {Promise} 返回Promise对象
*
* 功能说明:
* 1. 自动拼接基础URL和认证参数
* 2. 统一处理登录状态code=-1跳转登录页
* 3. 统一处理错误提示code=0显示Toast
* 4. 成功时返回data字段code=1
*/
export const request = (options : Options) => {
return new Promise((resolve, reject) => {
uni.request({
// 拼接完整请求URL基础URL + 路径 + 固定参数)
url: config.api_base_url + options.url + '&wxapp_id=' + config.wxapp_id + '&token=' + uni.getStorageSync('token'),
method: options.method || 'GET', // 默认GET方法
data: options.data || options.params || {}, // 兼容data/params传参
dataType: options.dataType || 'json', // 默认json格式
responseType: options.responseType || 'text', // 默认text类型
// 请求成功回调
success(res) {
const ret : Response = res.data;
// console.log(ret);
// 状态码处理
switch (ret.code) {
case -1: // 未登录状态
goToLoginPage();
break;
case 1: // 成功状态
// console.log(options.url);
// if (options.url === '/parts.order/preview') {
// console.log('ressss', ret);
// resolve(ret);
// break;
// }
resolve(ret.data); // 返回data字段
break;
case 0: // 失败状态
// console.log(options.url);
// if (options.url === '/parts.order/preview') {
// console.log('ressss', ret);
// resolve(ret);
// break;
// }
uni.showToast({
title: ret.msg || '操作失败', // 显示错误信息
icon: 'none', // 不显示图标
});
break;
}
},
// 请求失败回调
fail(err) {
uni.$emit('z-paging-error-emit');
reject(err); // 返回错误对象
uni.showToast({
title: '网络请求失败',
icon: 'none',
});
},
});
});
};