fn.js 2.13 KB
/*
 * @Date: 2022-05-18 22:56:08
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2026-01-06 20:56:55
 * @FilePath: /git/xyxBooking-weapp/src/api/fn.js
 * @Description: 文件描述
 */
import axios from '@/utils/request';
import Taro from '@tarojs/taro'
// import qs from 'qs'

/**
 * 网络请求功能函数
 * @param {*} api 请求axios接口
 * @returns 请求成功后,获取数据
 */
export const fn = (api) => {
  return api
    .then(res => {
      // 适配 H5 逻辑,code === 1 为成功
      if (res.data.code === 1) {
        return res.data || true;
      } else {
        // tslint:disable-next-line: no-console
        console.warn(res);
        if (res.data.show === false) return false;
        Taro.showToast({
          title: res.data.msg || '请求失败',
          icon: 'none',
          duration: 2000
        });
        return false;
      }
    })
    .catch(err => {
      // tslint:disable-next-line: no-console
      console.error(err);
      return false;
    })
    .finally(() => { // 最终执行
    })
}

/**
 * 七牛返回格式
 * @param {*} api
 * @returns
 */
export const uploadFn = (api) => {
  return api
    .then(res => {
      if (res.statusText === 'OK') {
        return res.data || true;
      } else {
        // tslint:disable-next-line: no-console
        console.warn(res);
        if (!res.data.show) return false;
        Taro.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000
        });
        return false;
      }
    })
    .catch(err => {
      // tslint:disable-next-line: no-console
      console.error(err);
      return false;
    })
}

/**
 * 统一 GET/POST 不同传参形式
 */
export const fetch = {
  get: function (api, params) {
    return axios.get(api, { params }) // 注意 axios-miniprogram 的 get 参数通常也是放在 config 对象中, key 为 params
  },
  post: function (api, params) {
    return axios.post(api, params)
  },
  // stringifyPost: function (api, params) {
  //   return axios.post(api, qs.stringify(params))
  // },
  basePost: function (url, data, config) {
    return axios.post(url, data, config)
  }
}