// 后台请求地址
import store from "../store/index";
import { decryptAes } from "@/utils/aesUtil";
const requestPath = "http://192.168.20.240:8591/dy-regulatory/";
// const requestPath = "http://39.103.206.194:9002/qhdkfq-regulatory/";

function post(url, data) {
  const getUserInfo = store.getters.getUserInfo;
  const loginUser = {};
  loginUser.REGULATORY_DEPARTMENT_TREE = getUserInfo.DEPARTMENT_ID || "";
  loginUser.USER_ID = getUserInfo.USER_ID || "";
  loginUser.LOGIN_DEPARTMENT = getUserInfo.DEPARTMENT_FULL_ID
    ? getUserInfo.DEPARTMENT_FULL_ID.split(",")[0]
    : "";
  loginUser.PERMISSION_TYPE = getUserInfo.TYPE || "";
  if (loginUser.PERMISSION_TYPE === "2") {
    loginUser.PERMISSION_REGULATORY_DEPARTMENT =
      getUserInfo.PERMISSION_REGULATORY_DEPARTMENT || ""; // 后台需转换成数组
  } else if (loginUser.PERMISSION_TYPE === "3") {
    loginUser.DEPT_PROVINCE = getUserInfo.PROVINCE;
    loginUser.DEPT_CITY = getUserInfo.CITY;
    loginUser.DEPT_COUNTY = getUserInfo.COUNTY;
    loginUser.DEPT_VILLAGE = getUserInfo.VILLAGE;
    if (getUserInfo.GRID === "0") {
      loginUser.GRID_MEMBERS = getUserInfo.GRIDS || ""; // 后台需转换成数组
    } else if (getUserInfo.GRID === "1") {
      loginUser.GRID_MEMBER = getUserInfo.USER_ID || "";
    } else {
      loginUser.DEPT_STREET = getUserInfo.STREET;
    }
  }
  return new Promise((resolve, reject) => {
    if (data && data.loading !== false) {
      uni.showLoading({
        title: "加载中",
      });
    }
    uni.request({
      url: requestPath + url,
      data: {
        ...loginUser,
        ...data,
      },
      method: "POST",
      header: {
        "Content-type": "application/x-www-form-urlencoded",
      },
      success: (res) => {
        res.data = decryptAes(res.data);
        if (data && data.loading !== false) {
          uni.hideLoading();
        }
        if (res.data.result === "success") {
          resolve(res.data);
        } else {
          uni.showToast({
            title: res.data.msg || "系统开小差了",
            icon: "none",
            duration: 2000,
          });
          reject(res.data);
        }
      },
      fail: (err) => {
        uni.hideLoading();
        uni.showToast({
          title: "网络错误请重试",
          icon: "none",
          duration: 2000,
        });
        reject(err);
      },
    });
  });
}

function upload(url, data) {
  return new Promise((resolve, reject) => {
    if (data && data.loading !== false) {
      uni.showLoading({
        title: "加载中",
      });
    }
    uni.uploadFile({
      url: requestPath + url,
      filePath: data.filePath,
      name: data.name || "file",
      formData: data.formData || {},
      success: (res) => {
        if (data && data.loading !== false) {
          uni.hideLoading();
        }
        res.data = decryptAes(res.data);
        if (JSON.parse(res.data).result === "success") {
          resolve(JSON.parse(res.data));
        } else {
          uni.showToast({
            title: JSON.parse(res.data).msg || "系统开小差了",
            icon: "none",
            duration: 2000,
          });
          reject(JSON.parse(res.data));
        }
      },
      fail: (err) => {
        uni.hideLoading();
        uni.showToast({
          title: "网络错误请重试",
          icon: "none",
          duration: 2000,
        });
        reject(err);
      },
    });
  });
}

function uploads(url, data) {
  return new Promise((resolve, reject) => {
    if (data && data.loading !== false) {
      uni.showLoading({
        title: "加载中",
      });
    }
    uni.uploadFile({
      url: requestPath + url,
      files: data.files,
      formData: data.formData || {},
      success: (res) => {
        if (data && data.loading !== false) {
          uni.hideLoading();
        }
        res.data = decryptAes(JSON.parse(res.data));
        if (res.data.result === "success") {
          resolve(res.data);
        } else {
          uni.showToast({
            title: res.data.msg || "系统开小差了",
            icon: "none",
            duration: 2000,
          });
          reject(JSON.parse(res.data));
        }
      },
      fail: (err) => {
        uni.hideLoading();
        uni.showToast({
          title: "网络错误请重试",
          icon: "none",
          duration: 2000,
        });
        reject(err);
      },
    });
  });
}

export { post, upload, uploads };
