145 lines
4.1 KiB
TypeScript
145 lines
4.1 KiB
TypeScript
// 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',
|
||
});
|
||
},
|
||
});
|
||
});
|
||
}; |