import request from '../utils/request.js'

class Integralinfo extends request {
  
  // static INT_URL = 'http://118.31.106.139:8022/'
  static INT_URL = 'https://api.imedicalmall.com/'
    /**
   * POST类型的网络请求
   */
  static postRequest(url, data, notUseLoading, catchErrorFunc) {
    let  headerSign = this.getSignHead();
    return this.requestAll(url, data, headerSign, 'POST', notUseLoading, catchErrorFunc)
  }

  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 === "0000") {
                    //200: 服务端业务处理正常结束
                    resolve(res?.data)
                } else {
                  if (catchErrorFunc) {
                      if (isFunc(catchErrorFunc)) {
                          catchErrorFunc(res?.data)
                      }
                  } else {
                    // console.log(res?.data?.msg);
                    if(res?.data?.msg != undefined && typeof(res?.data?.msg)=='string') {
                      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()
            }
        })
    })
}
  /**
   * 获取轮播图
   */
  static getBannerList(params) {
    return this.postRequest(`${this.INT_URL}app/banner/list`, params);
  }

  /**
   * 获取优惠券列表
   */
  static getList(params) {
    return this.postRequest(`${this.INT_URL}app/coupon/list`, params);
  }

  /**
   * 获取移动积分
   */
  static getBalance(params) {
    return this.postRequest(`${this.INT_URL}app/cmcc/balance`, params);
  }

   /**
   * 下单
   */
  static getOrder(params) {
    return this.postRequest(`${this.INT_URL}/app/order/order`, params);
  }

  /**
   * 一次验证码的支付
   */
  static getPayOnce(params) {
    return this.postRequest(`${this.INT_URL}/app/order/payOnce`, params);
  }

   /**
   * 移动积分扣减
   */
  static getExchange(params) {
    return this.postRequest(`${this.INT_URL}/app/order/exchange`, params);
  }

   /**
   * 支付短信验证码
   */
  static getPaySend(params) {
    return this.postRequest(`${this.INT_URL}/app/order/paySend`, params);
  }

   /**
   * 重新下发移动短信
   */
  static getSendCmccSms(params) {
    return this.postRequest(`${this.INT_URL}/app/order/sendCmccSms`, params);
  }

    /**
   * 我的兑换
   */
  static getOrderList(params) {
    return this.postRequest(`${this.INT_URL}/app/order/list`, params);
  }

       /**
   * 创建订单
   */
  static createOrder(mobile,activityId,num,shopId="", refStatisticsPostParam) {
    refStatisticsPostParam = refStatisticsPostParam || {}
    let params = {
      activityId:activityId,
      memberMobile:mobile,
      num:num,
      shopId:shopId,
      scene: refStatisticsPostParam.scene || "",
      channel: refStatisticsPostParam.channel || "0",
      department: refStatisticsPostParam.department || "0"
    };
    return this.postRequest(`${this.BASE_URL}open/activity/create-order`, params);
  }

  //获取支付参数
  static getOrderParams(orderSn,openId,isOrderPayment=false) {
    let params = {
      orderSn:orderSn,
      source: "FREE_MINI_APP",
      thirdPartyName: 'coupon-activity',
      payCode: "HSAY-SHARE",
      openId: openId
    };
    if(isOrderPayment){
      params.isOrderPayment =true;
      params.orderInfoPath = "pages/welfareMall/order/orderCompletion?ordersn="
      params.productDetailPath = "pages/welfareMall/activityInfo/activityInfo?activityId="
    }
    return this.postRequest(`${this.BASE_URL}open/activity/get-order-params`, params);
  }

  
}


export default Integralinfo