function getReqData() {
  const req = payload.get();

  const data = req.body?.toString() ? req.body?.toObject() : {};

  return { data, query: req.query };
}

function getUniqueId() {
  return ex.suid().base58();
}

function loginVerify() {
  const req = payload.get();
  const token = req?.cookie?.token;
  if (!token) {
    return;
  }
  return jwt_parse(token);
}

function main() {
  const { query } = getReqData();
  let [action] = query.action || [""];

  const userInfo = loginVerify() || {};

  if (!userInfo && !actionWhiteList.includes(action)) {
    return errMsg(413, "Invalid or missing token");
  }

  return runAction(userInfo);
}

function runAction(userInfo) {
  const { query } = getReqData();

  let [action] = query.action || [""];
  if (!action) {
    return errMsg(500, "没有action字段,请检查");
  }

  const apiMethod = payload.get().method;

  const fnData = actionControls.find((f) => {
    const { run, method } = f;
    return run.name == action && method == apiMethod;
  });

  if (fnData) {
    return fnData.run(userInfo);
  }

  return errMsg(500, "Invalid or missing action Or method");
}
