import BASEURL from './config'
import Tip from './tip'
// import { removeLocalStorage, getLocalStorage } from '@/plug/wxPlug.js'
let timer = null
const request = async(params = {}) => {
  
    let data = params.query || {};
    let url = BASEURL + params.url;
    let method = params.method
    let token = uni.getStorageSync('token');
    
    // if (params.method === 'GET') {}
    const res = await uni.request({
        url: url,
        method: method,
        data: data,
        header: {
            'Content-Type': 'application/json',
            token
        }
    })

    const response = res[1]

    const result = response.data
    
    if (result.status === 401) {
      clearTimeout(timer)
      timer = setTimeout(() => {
        Tip.toast('登录失效，请重新登录', '', 'none')
        uni.redirectTo ({
          url: '/pages/index/index'
        })
      }, 1000);
     
    }
    if (result.status === 500) {
      Tip.error(result.msg)
    }
  return result;


}

module.exports = {
    request
}
