import Taro from '@tarojs/taro';
import ttappid from '../../project.tt.json'//抖音
import wxappid from '../../project.config.json'//微信
import { baseUrl } from './config';


export default (options = {
  method: 'GET',
  data: {}
}) => {

  // 必传参数
  const request_data = {
    session_key: Taro.getStorageSync('session_key') || ''//缓存取值
  };
  if (process.env.TARO_ENV === 'weapp') {
    request_data['appid'] = wxappid.appid
  } else if (process.env.TARO_ENV === 'tt') {
    request_data['appid'] = ttappid.appid
  }
  return Taro.request({
    url: baseUrl + options.url,
    data: {
      ...request_data,
      ...options.data,
    },
    header: {
      'Content-Type': 'application/json',
      'accept': 'application/json'
    },
    method: options.method.toUpperCase(),
  }).then(res => {
    const {
      code,
      data
    } = res;
    if (code == 200) {
      return data;
    } else {
      Taro.showToast({
        title: `${res.msg}~`,
        icon: 'none',
        mask: true,
      });
    }
  });
};