import axios from "./axios";
import sign from "./sign";

axios.defaults.withCredentials = false  // 设置axios不让携带cookie

const reportUrl = "https://report.zhangxinhulian.com";
const payUrl = "https://zxpay.zhangxinhulian.com";

const request = (url, method, data_or_params = {}, form, data) => {
  if (process.env.NODE_ENV === "development") {
    data_or_params.tgyz = 1; //跳过验证
    data_or_params.tgtk = 1; //跳过token
  } else {
    data_or_params.sign = sign(data_or_params)[0];
  }

  if (form == "form") {
    return axios({
      url: url,
      method: method,
      params: data_or_params,
      data
    });
  } else {
    if (method == "get") {
      return axios({
        url: url,
        method: method,
        params: data_or_params
      });
    } else if (method == "post") {
      return axios({
        url: url,
        method: method,
        data: data_or_params
      });
    }
  }
};

const API = {
  API_test: function (data) {
    return request("/test", "get", data);
  },

  // 创建订单  将信息以请求体的形式传递给后端
  API_createorder: function (params, data) {
    return request("/h5game/locationH5/createOrder", "post", params, 'form', data);
  },

  // 获取支付价格
  API_payConfig: function (data) {
    return request("/app/v1/location/user/pay-config", "get", data);
  },

  // 查询结果
  API_orderdetail: function (data) {
    return request("/h5game/orderdetail", "get", data);
  },

  // 发起支付
  API_gatewayCreateorder: function (data) {
    return request(
      payUrl + "/unifiedpay/gateway/createOrder",
      "post",
      data
    );
  },

  // 查询支付结果
  API_gatewayQueryOrder: function (data) {
    return request(payUrl + "/unifiedpay/gateway/queryOrderByNo", "get", data);
  },

  // 收集数据
  API_h5Logs: function (data) {
    return request(reportUrl + "/v1/collect/h5Logs", "post", data);
  },

  // 用户反馈
  API_feedback: function (data) {
    return request("/app/v1/user/feedback", "post", data, "form");
  },

  // 点击上报
  API_reportClick: function (data) {
    return request(
      reportUrl +
      "/logCollection/log/v1/userLogCollection?module=web&pkg=com.h5game.taluo",
      "post",
      data
    );
  },

  // 展示上报
  API_reportView: function (data) {
    return request(
      reportUrl +
      "/logCollection/log/v1/userLogCollection?module=web&pkg=com.h5game.peidui",
      "post",
      data
    );
  },

  // 获取结果信息
  API_fetchInfo(params) {
    return request('/h5game/peidui/fetchInfo', 'GET', params, 'form')
  }
};

export default API;
