export const isPromise = (obj) => {
  return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function';
};

export const getQueryString = (name, url) => {
  var r = new RegExp("(\\?|#|&)" + name + "=(.*?)(#|&|$)"); var m = (url || window.location.href).match(r); return decodeURIComponent(m ? m[2] : '');
};

export const fetchData = (url, params='', data={}, method='GET' ,callback, errback, timeout = 60000) => {
  const CONTENT_TYPE = 'application/x-www-form-urlencoded';
  const HOST = 'https://open.ys7.com'; // http://10.10.34.51:8081  https://open.ys7.com
  const OPEN_DOMAIN = ''; // /api/v3/console
  const ACCESSTOKEN = data ? data.accessToken : '';

  wx.request({
    url: `${HOST}${OPEN_DOMAIN}${url}${params}`, 
    method: method,
    data: data,
    header: {
      'content-type': CONTENT_TYPE,
      'accessToken': ACCESSTOKEN
    },
    timeout: timeout,
    success: (res) => {
      console.log(res.data);
      
      if (res.data) {
        callback && callback(res.data)
      } else {
        wx.showToast({
          title: '网络异常请稍后重试',
          icon: 'error'
        })
      }
      
    },
    error:(err)=>{
      console.log(err);
      wx.showToast({
        title: '网络异常请稍后重试',
        icon: 'error'
      });
      errback && errback()
    },
  })         
      
};


export const stsFailContent = (code) => {
  let content = '';
  switch (code) {
    case 0:
      break;
    case 10001:
      content = 'websocket请求参数错误';
      break;
    case 10002:
      content = 'flv转封装失败';
      break;
    case 10003:
      content = 'rtp转封装失败';
      break;
    case 10004:
      content = 'rtmp读写错误	';
      break;
    case 10005:
      content = 'rtmp接收缓存溢出';
      break;
    case 6:
      content = '连接sts服务失败';
      break;
    case 11:
      content = '无效的房间号';
      break;
    case 14:
      content = 'sts连接vtm失败';
      break;
    case 17:
      content = '房间已满';
      break;
    case 18:
      content = 'auth认证失败';
      break;
    case 35:
      content = '房间号不存在';
      break;
  }
  return content
}