import util, { isFunc } from './util.js'
class request {

    //本地的
    static BASE_GROUP_URL = 'http://hsay.clp.com:8024/'
   // static BASE_GROUP_URL = 'https://menu-api.overseasdragon.com.cn/'
  
//   pre环境的
//   static BASE_URL = 'https://oapi.shpr.top/'
//   正式的
   static BASE_URL = 'https://vapi.hsayi.com/'
//   @todo需要修改正式的域名

  static HEAD = {
      "Content-Type": "application/json",
      "SIGN":'',
      "NONCE":'',
      "TIMESTAMP":'',
  }
  static constructor() {

  }

  /**
   * 设置统一的异常处理
   */
  static setErrorHandler(handler) {
      this._errorHandler = handler
  }

  /**
   * GET类型的网络请求
   */
  static getRequest(url, data ) {
     let  headerSign = this.getSignHead();
      return this.requestAll(url, data, headerSign, 'GET')
  }

  /**
   * POST类型的网络请求
   */
  static postRequest(url, data, notUseLoading, catchErrorFunc) {
      let  headerSign = this.getSignHead();
      return this.requestAll(url, data, headerSign, 'POST', notUseLoading, catchErrorFunc)
  }

  static upload(url, name, path, ortherData, header = this.HEAD){
      return new Promise((resolve, reject) => {
          wx.uploadFile({
              url: url,
              header: header,
              filePath: path,
              name: name,
              formData: ortherData,
              success: (res => {
                  res.data = JSON.parse(res.data);
                  if (res.data.code === 200) {
                      //200: 服务端业务处理正常结束
                      resolve(res.data)
                  } else {
                        wx.showToast({
                            title: res.data.msg,
                        })
                      //其它错误，提示用户错误信息
                      if (this._errorHandler != null) {
                          //如果有统一的异常处理，就先调用统一异常处理函数对异常进行处理
                          this._errorHandler(res)
                      }
                      reject(res)
                  }
              }),
              fail: (res => {
                  if (this._errorHandler != null) {
                      this._errorHandler(res)
                  }
                  wx.showToast({
                    title: '网络异常请,稍后再试~',
                  })
                  reject(res)
              })
          })
      })
  }
  static getSignHead(){
    let timestamp = parseInt(Date.now()/1000);
    let nonce = Math.floor(Math.random() * 1000).toString()
    let signStr = timestamp+nonce+""+"U6Watb875eCiX4Lq";
    let sign =  util.sha1(signStr).toString();

    let signHeadInfo = {
        "SIGN":sign,
        "NONCE":nonce,
        "TIMESTAMP":timestamp,
        "Content-Type": "application/json"
    }
    return signHeadInfo;
  }


  /**
   * 网络请求
   */
  static requestAll(url, data, header, method, notUseLoading, catchErrorFunc) {
      if (!notUseLoading) {
        getApp().showLoading()
      }
      return new Promise((resolve, reject) => {
          wx.request({
              url: url,
              data: data,
              header: header,
              method: method,
              success: (res => {
                  if (res?.data.code === 200) {
                      //200: 服务端业务处理正常结束
                      let jsondata = JSON.stringify(res.data.data)
                      let signStr = jsondata+"menu20220422";
                      let sign =  util.sha1(signStr).toString();
                     
                      if(sign != res.data.sign){
                        wx.showModal({
                            title: "提示",
                            content: "数据走丢了"
                          })
                         return
                      }

                      resolve(res?.data)
                  } else {
                    if (catchErrorFunc) {
                        if (isFunc(catchErrorFunc)) {
                            catchErrorFunc(res?.data)
                        }
                    } else {
                        wx.showToast({
                            title: res?.data?.msg,
                            icon: "none"
                        })
                    }
                    //其它错误，提示用户错误信息
                    if (this._errorHandler != null) {
                        //如果有统一的异常处理，就先调用统一异常处理函数对异常进行处理
                        this._errorHandler(res)
                    }
                    reject(res)
                  }
              }),
              fail: (res => {
                  if (this._errorHandler != null) {
                      this._errorHandler(res)
                  }
                  wx.showToast({
                    title: '网络异常请稍后',
                  })
                  reject(res)
              }),
              complete: _ => {
                getApp().hideLoading()
              }
          })
      })
  }
}

export default request
