| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- /**
- * 人员模型 1970:新增户籍地址 c_household_address;
- * 将 c_address 别名统一为「联系地址」(原「家庭地址/联系地址」拆分)。
- *
- * 只改 fieldList,勿走 updatefieldList(生产 DMS 对 rename 会 500)。
- * 用法:node migrate-household-address-20260909.js
- */
- const http = require("http");
- const qs = require("querystring");
- const fs = require("fs");
- const path = require("path");
- const HOST = "121.43.55.7";
- const PORT = 2101;
- const PERSONNEL_MODEL = 1970;
- function post(urlPath, fields, token) {
- return new Promise((resolve, reject) => {
- const body = qs.stringify(fields || {});
- const headers = {
- "Content-Type": "application/x-www-form-urlencoded",
- "Content-Length": Buffer.byteLength(body),
- };
- if (token) {
- headers.Token = token;
- headers.token = token;
- }
- const req = http.request(
- { hostname: HOST, port: PORT, path: urlPath, method: "POST", headers },
- (res) => {
- let data = "";
- res.on("data", (c) => (data += c));
- res.on("end", () => {
- try {
- resolve(JSON.parse(data));
- } catch (e) {
- resolve({ raw: data, status: res.statusCode });
- }
- });
- }
- );
- req.on("error", reject);
- req.write(body);
- req.end();
- });
- }
- function text(shortName, alias, must, seq) {
- return {
- alias,
- customType: "",
- describe: "格式为富文本",
- frontType: "content",
- id: 2,
- must: !!must,
- name: "c_" + shortName,
- searchType: "2",
- sequence: seq || 1,
- showParam: "name,alias,desc,type,front_type,must,default_value",
- type: "text",
- };
- }
- function normalizeFieldList(raw) {
- let fl = raw;
- if (typeof fl === "string") fl = JSON.parse(fl);
- const out = {};
- Object.keys(fl || {}).forEach((k) => {
- let v = fl[k];
- if (typeof v === "string") v = JSON.parse(v);
- out[k] = v;
- });
- return out;
- }
- async function getModel(token, modelId) {
- const res = await post("/proxy_dms/model/getModelById", { modelId: String(modelId) }, token);
- if (res.code != 200) throw new Error("getModel " + modelId + " fail: " + JSON.stringify(res));
- return res.content;
- }
- async function applyUpdates(token, modelId, addMap, renameAliasMap) {
- const model = await getModel(token, modelId);
- const fieldList = normalizeFieldList(model.fieldList);
- if (Object.keys(fieldList).length < 2) {
- throw new Error("model " + modelId + " fieldList 异常,拒绝覆盖");
- }
- const addfieldList = {};
- Object.keys(addMap || {}).forEach((shortName) => {
- const full = "c_" + shortName;
- if (fieldList[full]) return;
- const p = addMap[shortName];
- p.name = full;
- fieldList[full] = p;
- addfieldList[shortName] = {
- alias: p.alias,
- type: p.type,
- frontType: p.frontType,
- must: p.must,
- name: shortName,
- describe: p.describe,
- searchType: p.searchType,
- sortType: p.sortType,
- sequence: p.sequence,
- showParam: p.showParam,
- customType: p.customType || "",
- id: p.id,
- };
- });
- const aliasFixed = [];
- Object.keys(renameAliasMap || {}).forEach((full) => {
- if (!fieldList[full]) return;
- const nextAlias = renameAliasMap[full];
- if (fieldList[full].alias === nextAlias) return;
- fieldList[full] = Object.assign({}, fieldList[full], { alias: nextAlias });
- aliasFixed.push(full);
- });
- if (Object.keys(addfieldList).length === 0 && aliasFixed.length === 0) {
- return {
- modelId,
- modelName: model.modelName,
- code: 200,
- message: "无需变更",
- added: [],
- updated: [],
- };
- }
- const res = await post(
- "/proxy_dms/model/updateFieldList",
- {
- modelId: String(modelId),
- fieldList: JSON.stringify(fieldList),
- addfieldList: JSON.stringify(addfieldList),
- delfieldList: JSON.stringify({}),
- updatefieldList: JSON.stringify({}),
- },
- token
- );
- const verify = await getModel(token, modelId);
- const vfl = normalizeFieldList(verify.fieldList);
- return {
- modelId,
- modelName: model.modelName,
- code: res.code,
- message: res.message || res.content,
- added: Object.keys(addfieldList),
- updated: aliasFixed,
- addressAlias: vfl.c_address ? vfl.c_address.alias : null,
- householdAddress: vfl.c_household_address || null,
- };
- }
- async function main() {
- const login = await post("/proxy_oauth/user/login", {
- userName: "user_liu",
- password: "WE176852439@lmx",
- clientId: "1",
- });
- const token = login.message;
- if (!token) throw new Error("login failed: " + JSON.stringify(login));
- const result = await applyUpdates(
- token,
- PERSONNEL_MODEL,
- {
- household_address: text("household_address", "户籍地址", false, 41),
- },
- {
- c_address: "联系地址",
- }
- );
- const outPath = path.join(
- __dirname,
- "../../docs/_migrate_household_address_20260909.json"
- );
- fs.writeFileSync(outPath, JSON.stringify(result, null, 2), "utf8");
- console.log(JSON.stringify(result, null, 2));
- }
- main().catch((e) => {
- console.error(e);
- process.exit(1);
- });
|