| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- /**
- * 网络请求工具类
- */
- import { getToken, logout } from './auth.js';
- import config from '@/config.js';
- // 基础URL配置 - 从配置文件读取
- const BASE_URL = config.BASE_URL;
- // 请求超时时间 - 从配置文件读取
- const TIMEOUT = config.TIMEOUT;
- /**
- * 网络请求封装
- */
- export function request(options) {
- return new Promise((resolve, reject) => {
- const token = getToken();
- // 默认配置
- const defaultOptions = {
- url: BASE_URL + options.url,
- method: options.method || 'GET',
- timeout: TIMEOUT,
- header: {
- 'Content-Type': 'application/json',
- ...options.header,
- },
- data: options.data || {},
- };
- // 添加Authorization header
- if (token) {
- defaultOptions.header['Authorization'] = `Bearer ${token}`;
- }
- // 显示加载提示
- if (options.loading !== false) {
- uni.showLoading({
- title: options.loadingText || '加载中...',
- mask: true,
- });
- }
- uni.request({
- ...defaultOptions,
- success: (res) => {
- // 隐藏加载提示
- if (options.loading !== false) {
- uni.hideLoading();
- }
- const { statusCode, data } = res;
- // 请求成功
- if (statusCode === 200) {
- // 业务成功
- if (data.code === 200 || data.success === true) {
- resolve(data);
- } else {
- // 业务失败
- const message = data.message || data.msg || '请求失败';
- // Token过期,跳转登录
- if (data.code === 401) {
- uni.showToast({
- title: '登录已过期,请重新登录',
- icon: 'none',
- });
- logout();
- return;
- }
- // 显示错误信息
- if (options.showError !== false) {
- uni.showToast({
- title: message,
- icon: 'none',
- });
- }
- reject(new Error(message));
- }
- } else {
- // HTTP状态码错误
- const message = `请求失败 (${statusCode})`;
- if (options.showError !== false) {
- uni.showToast({
- title: message,
- icon: 'none',
- });
- }
- reject(new Error(message));
- }
- },
- fail: (err) => {
- // 隐藏加载提示
- if (options.loading !== false) {
- uni.hideLoading();
- }
- const message = err.errMsg || '网络请求失败';
- if (options.showError !== false) {
- uni.showToast({
- title: message,
- icon: 'none',
- });
- }
- reject(new Error(message));
- },
- });
- });
- }
- /**
- * GET请求
- */
- export function get(url, params = {}, options = {}) {
- return request({
- url,
- method: 'GET',
- data: params,
- ...options,
- });
- }
- /**
- * POST请求
- */
- export function post(url, data = {}, options = {}) {
- return request({
- url,
- method: 'POST',
- data,
- ...options,
- });
- }
- /**
- * PUT请求
- */
- export function put(url, data = {}, options = {}) {
- return request({
- url,
- method: 'PUT',
- data,
- ...options,
- });
- }
- /**
- * DELETE请求
- */
- export function del(url, params = {}, options = {}) {
- return request({
- url,
- method: 'DELETE',
- data: params,
- ...options,
- });
- }
- /**
- * 上传文件
- */
- export function uploadFile(url, filePath, options = {}) {
- return new Promise((resolve, reject) => {
- const token = getToken();
- // 显示上传进度
- if (options.loading !== false) {
- uni.showLoading({
- title: options.loadingText || '上传中...',
- mask: true,
- });
- }
- uni.uploadFile({
- url: BASE_URL + url,
- filePath,
- name: options.name || 'file',
- header: {
- Authorization: token ? `Bearer ${token}` : '',
- ...options.header,
- },
- formData: options.formData || {},
- success: (res) => {
- if (options.loading !== false) {
- uni.hideLoading();
- }
- try {
- const data = JSON.parse(res.data);
- if (data.code === 200 || data.success === true) {
- resolve(data);
- } else {
- const message = data.message || data.msg || '上传失败';
- uni.showToast({
- title: message,
- icon: 'none',
- });
- reject(new Error(message));
- }
- } catch (e) {
- reject(new Error('响应数据解析失败'));
- }
- },
- fail: (err) => {
- if (options.loading !== false) {
- uni.hideLoading();
- }
- const message = err.errMsg || '上传失败';
- uni.showToast({
- title: message,
- icon: 'none',
- });
- reject(new Error(message));
- },
- });
- });
- }
|