import * as crypto from "crypto";

/**
 * 统一接口签名工具
 */
export class SignUtil {
  static sign(data: object, appSecret: string) {
    let signStr =
      Object.keys(data)
        .sort()
        .filter((s) => {
          if (
            data[s] === undefined ||
            data[s] === null ||
            data[s] === "" ||
            typeof data[s] === "object" ||
            Array.isArray(data[s]) ||
            ["appId", "appSecret", "key"].includes(s)
          ) {
            return false;
          }

          return true;
        })
        .map((k) => `${k}=${data[k]}`)
        .join("&") +
      "&" +
      appSecret;

    return crypto.createHash("md5").update(signStr).digest("hex").toUpperCase();
  }
}
