request copy.js 3.66 KB
import host from '@/utils/ip-config.js';
import { t } from '@/plugins/index.js'
let requestFlag = true
// 请求封装
export default function request(options, ipType = 'ip') {
  

  // 根据不同的类型选择不同的ip
  // // #ifdef APP-PLUS
  // const ipTypeMap = {
  //   ip: uni.getStorageSync('serverIp'),
  //   assetsIp: `${uni.getStorageSync('serverIp')}/images`
  // }
  // // #endif
  // // #ifndef APP-PLUS
  // const ipTypeMap = {
  //   ip: host.ip,
  //   assetsIp: host.assetsIp
  // }
  // // #endif
  // 声明部分接口必须要用store.keliuyun.com服务
  const ipTypeMap = {
    ip: host.ip,
    assetsIp: host.assetsIp
  }
  
  // const url = (ipType === 'store.keliuyun' ? host.ip : ipTypeMap[ipType]) + options.url
  const url = ipTypeMap[ipType] + options.url
  return new Promise((resolve, reject) => {
    const systemInfo = uni.getSystemInfoSync()
    // 请求方式
    const method = options.method?.toUpperCase() || 'GET';

    // 根据method类型调整header,GET为?a=x&b=y,POST为json
    const headerContentType = method === 'GET' ? 'application/x-www-form-urlencoded' : 'application/json';


    const _data = Array.isArray(options.data) ? options.data : {
      ...(options.data || {}),
    };
    // const _data = {
    //   ...(options.data || {}),
    //   // _t:Date.now()
    // }
    const languageObj = {
      'mall_CN': 'zh-CN,zh;q=0.9',
      'zh_CN': 'zh-CN,zh;q=0.9',
      'en_US': 'en-US,en;q=0.5',
      'zh_TW': 'zh-TW,zh;q=0.3',
      'ja_JP':'ja-JP,ja;q=0.9'
    }
    const lang = uni.getStorageSync('lang') || host.defaultLang
    const setting = {
      url,
      data: _data,
      method,
      timeout: 60 * 1000,
      header: {
        'content-type': headerContentType,
        // 请求体中添加设备信息
        'Device-OS': systemInfo.platform || 'unknown', // 直接使用同步获取的信息
        'Device-Model': systemInfo.model || 'unknown',
        'Authorization': uni.getStorageSync('Authorization') || '',
        'Accept-Language': languageObj[lang] || 'en-US,en;q=0.5',
      },
      success(res) {
        // 如果是静态资源,直接返回
        if (ipType === 'assetsIp') {
          resolve(res.data);
          return;
        }
        // 否则判断res.data内部的code
        if (res.data.ecode === 401 && ipType !== 'store.keliuyun') {
          console.log(1);
          if (requestFlag) {
            requestFlag = false
            uni.showModal({
              title: t('message.prompt'),
              showCancel: false,
              content: t('Message.atokeIsDisabled'),
              confirmText: t('app.login.reLogin'),
              success: function(resModal) {
                if (resModal.confirm) {
                  requestFlag = true
                  uni.removeStorageSync('Authorization')
                  // uni.clearStorageSync()
                  uni.reLaunch({
                    url: '/pages/login/index'
                  })
                } else if (resModal.cancel) {
                  console.log('用户点击取消');
                }
              }
            })
          }

        } else if (res.data.code !== 200) {
          if(!options.hiddenMsg){
            uni.showToast({
              icon: 'none',
              title: res?.data?.msg || 'Error'
            })
          }
          reject(res?.data?.msg || 'Error')
        } else {
          resolve(res.data);
        }

      },
      fail(err) {
        uni.showToast({
          icon: 'none',
          title: t('app.login.networkError')
        })
        reject(err);
      },
    }
    if (options.header) {
      setting.header = Object.assign(setting.header, options.header);
    }
    uni.request(setting);
  })
}