"use strict";
const common_vendor = require("../common/vendor.js");
const config = require("../config.js");
require("../uni_modules/uview-plus/index.js");
const uni_modules_uviewPlus_libs_function_index = require("../uni_modules/uview-plus/libs/function/index.js");
const http = (url, params, method) => {
  return new Promise((resolve, reject) => {
    let Authorization = common_vendor.index.getStorageSync("token") || "";
    let version = common_vendor.index.getStorageSync("version") || common_vendor.index.getSystemInfoSync();
    if (!common_vendor.index.getStorageSync("version"))
      common_vendor.index.setStorageSync("version", version);
    let {
      deviceId,
      deviceBrand,
      deviceModel,
      osVersion
    } = version;
    let header = {
      deviceId,
      deviceBrand,
      deviceModel,
      osVersion,
      platform: 1,
      Authorization
    };
    common_vendor.index.request({
      url: config.config.baseUrl + url,
      data: params || {},
      method: method || "POST",
      header,
      success: (res) => {
        if (res.statusCode == 401) {
          common_vendor.index.reLaunch({
            url: "/pages/login/login"
          });
          return;
        }
        const data = res.data;
        if (data.code == 10 && !data.success) {
          reject(data);
          return;
        }
        if (data.code !== 1 && !data.success) {
          uni_modules_uviewPlus_libs_function_index.toast(data.msg);
          reject(data);
          return;
        }
        resolve(data.data || null);
      },
      fail: (err) => {
        if (err.statusCode == 401) {
          common_vendor.index.reLaunch({
            url: "/pages/login/login"
          });
          return;
        }
        const data = err.data;
        if (data.code !== 1 && !data.success) {
          uni_modules_uviewPlus_libs_function_index.toast(data.msg);
          reject(data);
          return;
        }
        common_vendor.index.hideLoading();
        reject(err);
      }
    });
  });
};
exports.http = http;
