const GET = 'GET';
const POST = 'POST';
const PUT = 'PUT';
const FORM = 'FORM';
const DELETE = 'DELETE';
// wxb299e10e65157301
// wx2938132b773c7b5a
// const baseURL = 'https://wx.scxjc.club';
const baseURL = 'https://test.hunanwanzhu.com';
// const baseURL = 'https://api.hunanwanzhu.com';

function request(method, url, data) {
  var token='';
  return new Promise(function (resolve, reject) {
    wx.getStorage({
      key: 'userInfo',
      complete: res => {
        if (res.data) {
          token = res.data.token
        }

        let header = {
          'content-type': 'application/json',
          'Authorization': token
        };
        wx.request({
          url: baseURL + url,
          method: method,
          data: data,
          header: header,
          success(res) {
            //请求成功
            //判断状态码---errCode状态根据后端定义来判断
            if (res.data.code == 0||res.data.pass==1) {
              resolve(res);
            } else if (res.data.code == 403) {
              //未登录
              reject(403)
              if (url.indexOf('authinfo')>=0){
                // wx.switchTab({
                //   url: '../user/user',
                // })
                wx.navigateTo({
                  url: '../login/user?type=0',
                })
              }
            }else{
              wx.hideNavigationBarLoading()
              wx.hideLoading()
              wx.showToast({
                title: res.data.message,
                icon:'none',
                duration:3000
              })
            }
          },
          fail(err) {
            //请求失败
            wx.showToast({
              icon:'none',
              title: '服务器开小差了',
            })
            reject(err)
          }
        })
      }
    })
    
  })
}
function uploadFile(url, filePath, name, formData, isSubDomain) {
  var token = '';
  return new Promise((resolve, reject) => {
    wx.getStorage({
      key: 'userInfo',
      complete: res => {
        if (res.data) {
          token = res.data.token
        }
        let header = {
          'content-type': 'application/json',
          'Authorization': token
        };
        
        wx.uploadFile({
          url: baseURL + url,
          filePath: filePath,
          name: name,
          // formData: formData,
          header: header,
          success: res => {
            resolve(JSON.parse(res.data))
          },
          fail(err) {
            //请求失败
            reject(err)
          }
        })
      }
    })
    
  })
}

function pushToday(e){
  wx.getStorage({
    key: 'user',
    complete: res => {
      if (res.data.role == 0) {
        wx.navigateTo({
          url: '../login/user?type=3',
        })
      } else{
        wx.navigateTo({
          url: '../today/today?id=' + e.match_id + '&record_id=' + e.record_id + '&user_id=' + e.user_id,
        })
      }
    }
  })
  
}

function initUser(){
  API.getAuthinfo().then(res=>{
    console.log(res.data.data,'user')
    if(res.data.code==0){
      wx.setStorage({
        key: 'user',
        data: res.data.data,
      })
    }

    if(res.data.data.role==0){
      wx.navigateTo({
        url: '../login/user?type=3&player_type='+res.data.data.player_type,
      })
    }
  })
}

const API = {
  getOpenid: (data) => request(GET, `/api/wx/openid`, data),
  login: (data) => request(POST, `/api/wx/v3/login`, data),
  bindphone: (data) => request(POST, `/api/wx/v3/phone/bind`, data),
  sendcode: (data) => request(POST, `/api/wx/v3/phone/code`, data),
  getAuthinfo: (data) => request(GET, `/api/wx/authinfo`, data),
  getEnumList: (data) => request(GET, `/api/wx/v2/enum/list`, data),
  getIndex: (data) => request(GET, `/api/wx/index`, data),
  getBaikeCategory: (data) => request(GET, `/api/wx/v3/baike/category/list`, data),
  getBaike: (data) => request(GET, `/api/wx/v3/baike/list`, data),
  getBaikeDetail: (data) => request(GET, `/api/wx/v3/baike/detail`, data),
  getBaikeRandom: (data) => request(GET, `/api/wx/v3/baike/random`, data),
  getRank: (data) => request(GET, `/api/wx/group/rank/list`, data),
  getRankList: (data) => request(GET, `/api/wx/group/rank`, data),
  getWinList: (data) => request(GET, `/api/wx/v3/match/winlost/top5`, data),
  getPlayerMatch: (data) => request(GET, `/api/wx/player/match`, data),//参赛资料
  updateFollow: (data) => request(POST, `/api/wx/v2/user/follow`, data),
  getRinrate: (data) => request(GET, `/api/wx/v2/winrate/rank`, data),
  getDefend: (data) => request(GET, `/api/wx/v2/defend/rank`, data),
  getHotbuyList: (data) => request(GET, `/api/wx/v2/hot/stock/buy/list`, data),
  getHotsellList: (data) => request(GET, `/api/wx/v2/hot/stock/sell/list`, data),
  getSellStock: (data) => request(GET, `/api/wx/v2/hot/stock/sell/players`, data),
  getChampionlList: (data) => request(GET, `/api/wx/v2/champion/article/list`, data),
  upload: (filePath, name) => uploadFile(`/api/wx/uploadfile`, filePath, name),
  uploadAvatar: (filePath, name) => uploadFile(`/api/wx/uploadavatar`, filePath, name),
  updateStyle: (data) => request(PUT, `/api/wx/v2/mine/style`, data),
  getMyStyle: (data) => request(GET, `/api/wx/v2/mine/style`, data),
  getMyFollow: (data) => request(GET, `/api/wx/v2/user/follow/list`, data),
  getMyFollowV3: (data) => request(GET, `/api/wx/v3/user/follow/list`, data),
  getMyMatch: (data) => request(GET, `/api/wx/player/match/list`, data),
  getComment: (data) => request(GET, `/api/wx/v2/wanzhu/comment/list`, data),
  getRecordList: (data) => request(GET, `/api/wx/player/match/record/list`, data),
  follow: (data) => request(POST, `/api/wx/v2/user/follow`, data),
  getPlayerList: (data) => request(GET, `/api/wx/v2/player/list`, data),
  updateRecord: (data) => request(POST, `/api/wx/player/record`, data),
  getRecord: (data) => request(GET, `/api/wx/player/record`, data),
  getCurrecord: (data) => request(GET, `/api/wx/player/currecord`, data),
  getArticle: (data) => request(GET, `/api/wx/article`, data),
  getStock: (data) => request(GET, `/api/wx/v3/stock`, data),
  getStockPlayer: (data) => request(GET, `/api/wx/v3/stock/players/list`, data),
  searchStock: (data) => request(GET, `/api/wx/stock/search`, data),
  getNotoice: (data) => request(GET, `/api/wx/v2/notices/list`, data),
  getDate: (data) => request(GET, `/api/wx/v2/default/date`, data),
  getHotFollow: (data) => request(GET, `/api/wx/v2/hot/follow/list`, data),
  getCalendar: (data) => request(GET, `/api/wx/v2/player/match/calendar`, data),
  getMatchList: (data) => request(GET, `/api/wx/v2/match/list`, data),
  getMineLatest: (data) => request(GET, `/api/wx/v2/mine/latest`, data),
  getCommentCount: (data) => request(GET, `/api/wx/v3/user/comments/count`, data),
  getZanCount: (data) => request(GET, `/api/wx/v3/user/zancount`, data),
  postcomment: (data) => request(POST, `/api/wx/v3/user/comments`, data),
  delcomment: (data) => request(DELETE, `/api/wx/v3/user/comments`, data),
  getComment: (data) => request(GET, `/api/wx/v3/user/comments/list`, data),
  zan: (data) => request(POST, `/api/wx/v3/user/zan`, data),
  getconsult: (data) => request(GET, `/api/wx/v3/user/consult/list`, data),
  wxpay: (data) => request(POST, `/api/wx/v3/signup/wxpay`, data),
  consult: (data) => request(POST, `/api/wx/v3/user/consult`, data),
  getsignup: (data) => request(GET, `/api/wx/v3/signup/info`, data),
  update: (data) => request(PUT, `/api/wx/v3/user/fund/modify`, data),
  getChoice: (data) => request(GET, `/api/wx/v3/user/choice/list`, data),
  addchoice: (data) => request(POST, `/api/wx/v3/user/choice`, data),
  deletechoice: (data) => request(DELETE, `/api/wx/v3/user/choice`, data),
  initfund: (data) => request(PUT, `/api/wx/player/fund`, data),
  getFans: (data) => request(GET, `/api/wx/v3/user/follower/list`, data),
  getExperience: (data) => request(GET, `/api/wx/v3/experience/list`, data),
  getUserMatch: (data) => request(GET, `/api/wx/v3/user/match/list`, data),
  getUserInfo: (data) => request(GET, `/api/wx/user/info`, data),
  isUserLogin: (data) => request(GET, `/api/wx/v3/authinfo`, data),
  getTLBlist: (data) => request(GET, `/api/wx/v3/profit/rank/list`, data),
  getBlacklist: (data) => request(GET, `/api/wx/v3/user/black/list`, data),
  cancelBlack: (data) => request(DELETE, `/api/wx/v3/user/black`, data),
  blackUser: (data) => request(POST, `/api/wx/v3/user/black`, data),
  getNbcomments: (data) => request(GET, `/api/wx/v3/stock/nbcomments/list`, data),
  getStockNbcomments: (data) => request(GET, `/api/wx/v3/stock/comments/list`, data),
  getWinLose: (data) => request(GET, `/api/wx/v3/match/group/winlose/statistic`, data),
  imageOcr: (data) => request(POST, `/api/wx/v3/ai/ocr`, data),
  initUser:() => initUser(),
};

const PUSH = {
  pushToday:(data) => pushToday(data),
};
module.exports = {
  API: API,
  PUSH: PUSH,
}