const App = getApp();
export function request(route, data = null, method = "GET") {
  method = String(method).toLowerCase();
  return new Promise((resolve, reject) => {
    const url = App.getNetAddresss(route);
    const config = {
      url,
      success: ({ data: { result, msg, data: response } }) => {
        if (result == 0) {
          reject({ result, msg, data });
          return;
        }
        resolve(response);
      },
      fail(response) {
        reject(response);
      },
    };
    if (data) {
      config["data"] = data;
    }
    switch (method) {
    case "post":
      App._postNetWork(config);
      break;
    case "get":
      App._getNetWork(config);
      break;
    }
  });
}

export function get(route, query = null) {
  return request(route, query, "get");
}

export function post(route, data = null) {
  return request(route, data, "post");
}

export default {
  request,
  get,
  post,
};
