"use strict";
const common_vendor = require("../../common/vendor.js");
const modules_Config = require("../Config.js");
function objectToQueryParams(obj) {
  return Object.keys(obj).map((key) => encodeURIComponent(key) + "=" + encodeURIComponent(obj[key])).join("&");
}
var AppServer = function() {
};
var serverConfig = {};
var baseUrl = modules_Config.Config.API_BASE_URL;
serverConfig.apiUrl_Auth_WechatLogin = baseUrl + "/api/v1/auth/wechat-login";
serverConfig.apiUrl_Auth_RefreshToken = baseUrl + "/api/v1/auth/refresh-token";
serverConfig.apiUrl_Auth_Logout = baseUrl + "/api/v1/auth/logout";
serverConfig.apiUrl_User_GetProfile = baseUrl + "/api/v1/users/profile";
serverConfig.apiUrl_User_UpdateProfile = baseUrl + "/api/v1/users/profile";
serverConfig.apiUrl_User_SetLanguage = baseUrl + "/api/v1/users/language";
serverConfig.apiUrl_Service_GetCategories = baseUrl + "/api/v1/categories";
serverConfig.apiUrl_Service_GetServices = baseUrl + "/api/v1/services";
serverConfig.apiUrl_Service_GetServiceByKey = baseUrl + "/api/v1/services";
serverConfig.apiUrl_Appointment_Create = baseUrl + "/api/v1/appointments";
serverConfig.apiUrl_Appointment_GetList = baseUrl + "/api/v1/appointments";
serverConfig.apiUrl_Appointment_Stats = baseUrl + "/api/v1/appointments/stats";
serverConfig.apiUrl_Appointment_GetById = baseUrl + "/api/v1/appointments";
serverConfig.apiUrl_Appointment_Update = baseUrl + "/api/v1/appointments";
serverConfig.apiUrl_Appointment_Cancel = baseUrl + "/api/v1/appointments";
serverConfig.apiUrl_Notification_GetList = baseUrl + "/api/v1/notifications";
serverConfig.apiUrl_Notification_UnreadCount = baseUrl + "/api/v1/notifications/unread-count";
serverConfig.apiUrl_Notification_MarkAsRead = baseUrl + "/api/v1/notifications";
serverConfig.apiUrl_Notification_MarkAllAsRead = baseUrl + "/api/v1/notifications/read-all";
serverConfig.apiUrl_Notification_Delete = baseUrl + "/api/v1/notifications";
serverConfig.apiUrl_Invitation_Generate = baseUrl + "/api/v1/invitations/generate";
serverConfig.apiUrl_Invitation_GetStats = baseUrl + "/api/v1/invitations/stats";
serverConfig.apiUrl_Invitation_GetRecords = baseUrl + "/api/v1/invitations/records";
serverConfig.apiUrl_Commission_GetList = baseUrl + "/api/v1/commissions";
serverConfig.apiUrl_Commission_GetStats = baseUrl + "/api/v1/commissions/stats";
serverConfig.apiUrl_Withdrawal_Create = baseUrl + "/api/v1/withdrawals";
serverConfig.apiUrl_Withdrawal_GetList = baseUrl + "/api/v1/withdrawals";
serverConfig.apiUrl_Withdrawal_GetById = baseUrl + "/api/v1/withdrawals";
serverConfig.apiUrl_Upload_Image = baseUrl + "/api/v1/upload/image";
serverConfig.apiUrl_QRCode_GetMiniProgram = baseUrl + "/api/v1/qrcode/miniprogram";
AppServer.prototype.getPostFormBody = function(postData) {
  Object.assign({}, postData);
};
AppServer.prototype.getSign = function(postData) {
  let arr = [];
  for (let key in postData) {
    if (key == "sign")
      continue;
    arr.push(key);
  }
  arr.sort();
  let str = "";
  for (let i in arr) {
    let value = postData[arr[i]];
    if (value || value == 0) {
      str += value;
    }
  }
  let saltKey = "";
  try {
    let token = common_vendor.index.getStorageSync("token");
    if (token) {
      saltKey = token.split(".")[2];
    }
  } catch (e) {
    saltKey = "";
  }
  str += saltKey;
  let sign = md5(str);
  return sign;
};
AppServer.prototype.postData = async function(url, postData) {
  let authToken = "";
  try {
    const app = getApp();
    if (app && app.globalData && app.globalData.token) {
      authToken = app.globalData.token;
    } else {
      authToken = common_vendor.index.getStorageSync("token") || "";
    }
  } catch (e) {
    authToken = common_vendor.index.getStorageSync("token") || "";
  }
  return common_vendor.index.request({
    url,
    method: "POST",
    header: {
      "Content-Type": "application/json",
      "Authorization": authToken
    },
    data: JSON.stringify(postData)
  }).then((res) => {
    common_vendor.index.__f__("log", "at modules/api/AppServer.js:147", `post,url=${url},form=${JSON.stringify(postData)},response=${res.data}`);
    return res.data;
  }).catch((err) => {
    common_vendor.index.__f__("error", "at modules/api/AppServer.js:153", err);
    return err;
  });
};
AppServer.prototype.getData = async function(url, postData) {
  let authToken = "";
  try {
    const app = getApp();
    if (app && app.globalData && app.globalData.token) {
      authToken = app.globalData.token;
    } else {
      authToken = common_vendor.index.getStorageSync("token") || "";
    }
  } catch (e) {
    authToken = common_vendor.index.getStorageSync("token") || "";
  }
  if (postData != null) {
    var parmat = objectToQueryParams(postData);
    if (url.indexOf("?") > -1) {
      url += "&" + parmat;
    } else {
      url += "?" + parmat;
    }
  }
  return common_vendor.index.request({
    url,
    method: "GET",
    header: {
      "Authorization": authToken
    }
  }).then((res) => {
    common_vendor.index.__f__("log", "at modules/api/AppServer.js:188", `GET,url=${url},response=${JSON.stringify(res.data)}`);
    return res.data;
  }).catch((err) => {
    common_vendor.index.__f__("error", "at modules/api/AppServer.js:192", err);
    return err;
  });
};
AppServer.prototype.putData = async function(url, postData) {
  let authToken = "";
  try {
    const app = getApp();
    if (app && app.globalData && app.globalData.token) {
      authToken = app.globalData.token;
    } else {
      authToken = common_vendor.index.getStorageSync("token") || "";
    }
  } catch (e) {
    authToken = common_vendor.index.getStorageSync("token") || "";
  }
  return common_vendor.index.request({
    url,
    method: "PUT",
    header: {
      "Content-Type": "application/json",
      "Authorization": authToken
    },
    data: JSON.stringify(postData)
  }).then((res) => {
    common_vendor.index.__f__("log", "at modules/api/AppServer.js:223", `PUT,url=${url},form=${JSON.stringify(postData)},response=${JSON.stringify(res.data)}`);
    return res.data;
  }).catch((err) => {
    common_vendor.index.__f__("error", "at modules/api/AppServer.js:227", err);
    return err;
  });
};
AppServer.prototype.deleteData = async function(url) {
  let authToken = "";
  try {
    const app = getApp();
    if (app && app.globalData && app.globalData.token) {
      authToken = app.globalData.token;
    } else {
      authToken = common_vendor.index.getStorageSync("token") || "";
    }
  } catch (e) {
    authToken = common_vendor.index.getStorageSync("token") || "";
  }
  return common_vendor.index.request({
    url,
    method: "DELETE",
    header: {
      "Authorization": authToken
    }
  }).then((res) => {
    common_vendor.index.__f__("log", "at modules/api/AppServer.js:256", `DELETE,url=${url},response=${JSON.stringify(res.data)}`);
    return res.data;
  }).catch((err) => {
    common_vendor.index.__f__("error", "at modules/api/AppServer.js:260", err);
    return err;
  });
};
AppServer.prototype.WechatLogin = async function(code) {
  var url = serverConfig.apiUrl_Auth_WechatLogin;
  return this.postData(url, { code }).then((data) => {
    return data;
  });
};
AppServer.prototype.RefreshToken = async function(refreshToken) {
  var url = serverConfig.apiUrl_Auth_RefreshToken;
  return this.postData(url, { refreshToken }).then((data) => {
    if (data.success && data.data && data.data.token) {
      const token = "Bearer " + data.data.token;
      const app = getApp();
      app.globalData.token = token;
      common_vendor.index.setStorageSync("token", token);
    }
    return data;
  });
};
AppServer.prototype.Logout = async function() {
  var url = serverConfig.apiUrl_Auth_Logout;
  return this.postData(url, {}).then((data) => {
    const app = getApp();
    app.globalData.token = "";
    common_vendor.index.setStorageSync("token", "");
    return data;
  });
};
AppServer.prototype.GetUserProfile = async function() {
  var url = serverConfig.apiUrl_User_GetProfile;
  return this.getData(url).then((data) => {
    return data;
  });
};
AppServer.prototype.UpdateUserProfile = async function(profileData) {
  var url = serverConfig.apiUrl_User_UpdateProfile;
  return this.putData(url, profileData).then((data) => {
    return data;
  });
};
AppServer.prototype.SetLanguage = async function(language) {
  var url = serverConfig.apiUrl_User_SetLanguage;
  return this.putData(url, { language }).then((data) => {
    return data;
  });
};
AppServer.prototype.GetCategories = async function(params = {}) {
  var url = serverConfig.apiUrl_Service_GetCategories;
  return this.getData(url, params).then((data) => {
    return data;
  });
};
AppServer.prototype.GetServices = async function(params = {}) {
  var url = serverConfig.apiUrl_Service_GetServices;
  return this.getData(url, params).then((data) => {
    return data;
  });
};
AppServer.prototype.GetServiceByKey = async function(serviceKey, language) {
  var url = serverConfig.apiUrl_Service_GetServiceByKey + "/" + serviceKey;
  var params = language ? { language } : null;
  return this.getData(url, params).then((data) => {
    return data;
  });
};
AppServer.prototype.CreateAppointment = async function(appointmentData) {
  var url = serverConfig.apiUrl_Appointment_Create;
  return this.postData(url, appointmentData).then((data) => {
    return data;
  });
};
AppServer.prototype.GetAppointments = async function(params = {}) {
  var url = serverConfig.apiUrl_Appointment_GetList;
  return this.getData(url, params).then((data) => {
    return data;
  });
};
AppServer.prototype.GetAppointmentStats = async function() {
  var url = serverConfig.apiUrl_Appointment_Stats;
  return this.getData(url, {}).then((data) => {
    return data;
  });
};
AppServer.prototype.GetAppointmentById = async function(appointmentId) {
  var url = serverConfig.apiUrl_Appointment_GetById + "/" + appointmentId;
  return this.getData(url).then((data) => {
    return data;
  });
};
AppServer.prototype.UpdateAppointment = async function(appointmentId, updateData) {
  var url = serverConfig.apiUrl_Appointment_Update + "/" + appointmentId;
  return this.putData(url, updateData).then((data) => {
    return data;
  });
};
AppServer.prototype.CancelAppointment = async function(appointmentId) {
  var url = serverConfig.apiUrl_Appointment_Cancel + "/" + appointmentId;
  return this.deleteData(url).then((data) => {
    return data;
  });
};
AppServer.prototype.GetNotifications = async function(params = {}) {
  var url = serverConfig.apiUrl_Notification_GetList;
  return this.getData(url, params).then((data) => {
    return data;
  });
};
AppServer.prototype.GetNotificationUnreadCount = async function() {
  var url = serverConfig.apiUrl_Notification_UnreadCount;
  return this.getData(url, {}).then((data) => {
    return data;
  });
};
AppServer.prototype.MarkNotificationAsRead = async function(notificationId) {
  var url = serverConfig.apiUrl_Notification_MarkAsRead + "/" + notificationId + "/read";
  return this.putData(url, {}).then((data) => {
    return data;
  });
};
AppServer.prototype.MarkAllNotificationsAsRead = async function() {
  var url = serverConfig.apiUrl_Notification_MarkAllAsRead;
  return this.putData(url, {}).then((data) => {
    return data;
  });
};
AppServer.prototype.DeleteNotification = async function(notificationId) {
  var url = serverConfig.apiUrl_Notification_Delete + "/" + notificationId;
  return this.deleteData(url).then((data) => {
    return data;
  });
};
AppServer.prototype.GenerateInvitationCode = async function() {
  var url = serverConfig.apiUrl_Invitation_Generate;
  return this.postData(url, {}).then((data) => {
    return data;
  });
};
AppServer.prototype.GetInvitationStats = async function() {
  var url = serverConfig.apiUrl_Invitation_GetStats;
  return this.getData(url).then((data) => {
    return data;
  });
};
AppServer.prototype.GetInvitationRecords = async function() {
  var url = serverConfig.apiUrl_Invitation_GetRecords;
  return this.getData(url).then((data) => {
    return data;
  });
};
AppServer.prototype.GetCommissions = async function(params = {}) {
  var url = serverConfig.apiUrl_Commission_GetList;
  return this.getData(url, params).then((data) => {
    return data;
  });
};
AppServer.prototype.GetCommissionStats = async function() {
  var url = serverConfig.apiUrl_Commission_GetStats;
  return this.getData(url).then((data) => {
    return data;
  });
};
AppServer.prototype.CreateWithdrawal = async function(withdrawalData) {
  var url = serverConfig.apiUrl_Withdrawal_Create;
  return this.postData(url, withdrawalData).then((data) => {
    return data;
  });
};
AppServer.prototype.GetWithdrawals = async function(params = {}) {
  var url = serverConfig.apiUrl_Withdrawal_GetList;
  return this.getData(url, params).then((data) => {
    return data;
  });
};
AppServer.prototype.GetWithdrawalById = async function(withdrawalId) {
  var url = serverConfig.apiUrl_Withdrawal_GetById + "/" + withdrawalId;
  return this.getData(url).then((data) => {
    return data;
  });
};
AppServer.prototype.GetMiniProgramQRCode = async function(params = {}) {
  var url = serverConfig.apiUrl_QRCode_GetMiniProgram;
  return this.postData(url, params).then((data) => {
    return data;
  });
};
AppServer.prototype.UploadImage = async function(filePath) {
  var url = serverConfig.apiUrl_Upload_Image;
  let authToken = "";
  try {
    const app = getApp();
    if (app && app.globalData && app.globalData.token) {
      authToken = app.globalData.token;
    } else {
      authToken = common_vendor.index.getStorageSync("token") || "";
    }
  } catch (e) {
    authToken = common_vendor.index.getStorageSync("token") || "";
  }
  return new Promise((resolve, reject) => {
    common_vendor.index.uploadFile({
      url,
      filePath,
      name: "image",
      header: {
        "Authorization": authToken
      },
      success: (res) => {
        try {
          const data = JSON.parse(res.data);
          common_vendor.index.__f__("log", "at modules/api/AppServer.js:639", "Upload image success:", data);
          resolve(data);
        } catch (e) {
          common_vendor.index.__f__("error", "at modules/api/AppServer.js:642", "Parse upload response error:", e);
          reject(e);
        }
      },
      fail: (err) => {
        common_vendor.index.__f__("error", "at modules/api/AppServer.js:647", "Upload image failed:", err);
        reject(err);
      }
    });
  });
};
AppServer.prototype.GetPlatformIsAndroid = function() {
  let port = common_vendor.index.getSystemInfoSync().platform;
  switch (port) {
    case "android":
      common_vendor.index.__f__("log", "at modules/api/AppServer.js:673", "运行Android上", port);
      return true;
    case "ios":
      common_vendor.index.__f__("log", "at modules/api/AppServer.js:677", "运行iOS上", port);
      return false;
    default:
      common_vendor.index.__f__("log", "at modules/api/AppServer.js:681", "运行在开发者工具上");
      return false;
  }
};
exports.AppServer = AppServer;
//# sourceMappingURL=../../../.sourcemap/mp-weixin/modules/api/AppServer.js.map
