fn.js 3.59 KB
/*
 * @Date: 2022-05-18 22:56:08
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2026-01-24 12:52:06
 * @FilePath: /xyxBooking-weapp/src/api/fn.js
 * @Description: 统一后端返回格式(强制 { code, data, msg })
 */
import axios from '@/utils/request';
import Taro from '@tarojs/taro'
import qs from 'qs'

/**
 * @description 统一后端返回格式(强制 { code, data, msg })
 * - code === 1 表示成功
 * - 失败时统一 toast 提示(可通过 res.data.show=false 禁用提示)
 * @param {Promise<any>} api axios 请求 Promise
 * @returns {Promise<{code:number,data:any,msg:string,show?:boolean}>} 标准化后的返回对象
 */
export const fn = (api) => {
  return api
    .then(res => {
      // 约定:后端 code === 1 为成功
      const res_data = res && res.data ? res.data : null
      if (res_data && String(res_data.code) === '1') {
        return res_data
      }
      // 失败兜底:优先返回后端响应,同时做 toast 提示
      console.warn('接口请求失败:', res)
      if (res_data && res_data.show === false) return res_data
      Taro.showToast({
        title: (res_data && res_data.msg) ? res_data.msg : '请求失败',
        icon: 'none',
        duration: 2000
      })
      return res_data || { code: 0, data: null, msg: '请求失败' }
    })
    .catch(err => {
      console.error('接口请求异常:', err);
      return { code: 0, data: null, msg: (err && (err.msg || err.message || err.errMsg)) ? (err.msg || err.message || err.errMsg) : '网络异常' }
    })
    .finally(() => { // 最终执行
    })
}

/**
 * @description 七牛上传返回格式适配
 * @param {Promise<any>} api axios 请求 Promise
 * @returns {Promise<any|false>} 成功返回七牛响应数据,失败返回 false
 */
export const uploadFn = (api) => {
  return api
    .then(res => {
      if (res.statusText === 'OK') {
        return res.data || true;
      } else {
        console.warn('七牛上传失败:', res);
        if (!res.data.show) return false;
        Taro.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000
        });
        return false;
      }
    })
    .catch(err => {
      console.error('七牛上传异常:', err);
      return false;
    })
}

/**
 * @description 统一 GET/POST 传参形式
 * - get/post:直接透传 axios
 * - stringifyPost:表单提交(x-www-form-urlencoded)
 * - basePost:保留自定义 config 的扩展位
 */
export const fetch = {
  /**
   * @description GET 请求
   * @param {string} api 接口地址
   * @param {Object} params 查询参数
   * @returns {Promise<any>} axios Promise
   */
  get: function (api, params) {
    return axios.get(api, params)
  },
  /**
   * @description POST 请求(JSON)
   * @param {string} api 接口地址
   * @param {Object} params 请求体
   * @returns {Promise<any>} axios Promise
   */
  post: function (api, params) {
    return axios.post(api, params)
  },
  /**
   * @description POST 请求(表单序列化)
   * @param {string} api 接口地址
   * @param {Object} params 请求体
   * @returns {Promise<any>} axios Promise
   */
  stringifyPost: function (api, params) {
    return axios.post(api, qs.stringify(params), {
      headers: {
        'content-type': 'application/x-www-form-urlencoded'
      }
    })
  },
  /**
   * @description POST 请求(自定义 config)
   * @param {string} url 接口地址
   * @param {any} data 请求体
   * @param {Object} config axios 配置
   * @returns {Promise<any>} axios Promise
   */
  basePost: function (url, data, config) {
    return axios.post(url, data, config)
  }
}