add-personnel-fields-20261012.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * 人员模型 1970 只补用户清单里缺少的字段,不删不改已有列。
  3. * 凭据:环境变量 SJ_OAUTH_PASSWORD,不要把密码写进本文件。
  4. */
  5. const http = require('http')
  6. const qs = require('querystring')
  7. const HOST = '121.43.55.7'
  8. const PORT = 2101
  9. const USER = process.env.SJ_OAUTH_USER || 'user_liu'
  10. const PASS = process.env.SJ_OAUTH_PASSWORD
  11. const MODEL_ID = 1970
  12. if (!PASS) {
  13. console.error('缺少 SJ_OAUTH_PASSWORD')
  14. process.exit(1)
  15. }
  16. function post(urlPath, fields, token) {
  17. return new Promise((resolve, reject) => {
  18. const body = qs.stringify(fields || {})
  19. const headers = {
  20. 'Content-Type': 'application/x-www-form-urlencoded',
  21. 'Content-Length': Buffer.byteLength(body),
  22. }
  23. if (token) {
  24. headers.Token = token
  25. headers.token = token
  26. }
  27. const req = http.request(
  28. { hostname: HOST, port: PORT, path: urlPath, method: 'POST', headers },
  29. (res) => {
  30. let data = ''
  31. res.on('data', (c) => (data += c))
  32. res.on('end', () => {
  33. try { resolve(JSON.parse(data)) } catch (e) { resolve({ raw: data, status: res.statusCode }) }
  34. })
  35. },
  36. )
  37. req.on('error', reject)
  38. req.write(body)
  39. req.end()
  40. })
  41. }
  42. function text(shortName, alias, seq) {
  43. return {
  44. alias,
  45. customType: '',
  46. describe: '格式为富文本',
  47. frontType: 'content',
  48. id: 2,
  49. must: false,
  50. name: 'c_' + shortName,
  51. searchType: '2',
  52. sequence: seq || 1,
  53. showParam: 'name,alias,desc,type,front_type,must,default_value',
  54. type: 'text',
  55. }
  56. }
  57. function normalizeFieldList(raw) {
  58. let fl = raw
  59. if (typeof fl === 'string') fl = JSON.parse(fl)
  60. const out = {}
  61. Object.keys(fl || {}).forEach((k) => {
  62. let v = fl[k]
  63. if (typeof v === 'string') v = JSON.parse(v)
  64. out[k] = v
  65. })
  66. return out
  67. }
  68. const ADD = [
  69. ['person_type', '人员类型'],
  70. ['urban_medicare_confirm', '转至城镇医保确认状态'],
  71. ['household_district', '户籍区县'],
  72. ['household_town', '户籍街镇'],
  73. ['household_village', '户籍村居委'],
  74. ['contact_name', '联系人'],
  75. ['contact_phone', '联系电话'],
  76. ['insurance_join_date', '参保日期'],
  77. ['person_mark', '人员标示'],
  78. ['change_reason', '变更原因'],
  79. ['benefit_apply_date', '待遇申领日期'],
  80. ['sentence_start', '刑期开始时间'],
  81. ['sentence_end', '刑期结束时间'],
  82. ['cancel_apply_date', '注销申办日期'],
  83. ['cancel_reason', '注销原因'],
  84. ['cancel_death_month', '注销(死亡)年月'],
  85. ['has_heir', '是否有继承人'],
  86. ['heir_id_type', '继承人证件类型'],
  87. ['heir_bank_name', '继承人银行名称'],
  88. ['heir_household_address', '继承人户籍地址'],
  89. ['heir_address', '继承人联系地址'],
  90. ['local_or_remote', '同城或异地'],
  91. ]
  92. async function main() {
  93. const login = await post('/proxy_oauth/user/login', {
  94. userName: USER,
  95. password: PASS,
  96. clientId: '1',
  97. })
  98. const token = login.message
  99. if (!token || String(token).indexOf('.') < 0) {
  100. console.error('login failed', login.code, String(login.message || login.msg || '').slice(0, 160))
  101. process.exit(1)
  102. }
  103. const modelRes = await post('/proxy_dms/model/getModelById', { modelId: String(MODEL_ID) }, token)
  104. if (modelRes.code != 200) throw new Error('getModel fail ' + JSON.stringify(modelRes).slice(0, 300))
  105. const model = modelRes.content
  106. const fieldList = normalizeFieldList(model.fieldList)
  107. const addfieldList = {}
  108. let seq = 80
  109. Object.keys(fieldList).forEach((k) => {
  110. const n = Number(fieldList[k] && fieldList[k].sequence) || 0
  111. if (n > seq) seq = n
  112. })
  113. ADD.forEach(([shortName, alias]) => {
  114. const full = 'c_' + shortName
  115. if (fieldList[full]) return
  116. seq += 1
  117. const p = text(shortName, alias, seq)
  118. fieldList[full] = p
  119. addfieldList[shortName] = {
  120. alias: p.alias,
  121. type: p.type,
  122. frontType: p.frontType,
  123. must: p.must,
  124. name: shortName,
  125. describe: p.describe,
  126. searchType: p.searchType,
  127. sortType: p.sortType,
  128. sequence: p.sequence,
  129. showParam: p.showParam,
  130. customType: '',
  131. id: p.id,
  132. }
  133. })
  134. const added = Object.keys(addfieldList)
  135. console.log('existing', Object.keys(fieldList).length - added.length, 'toAdd', added.join(',') || 'none')
  136. if (!added.length) return
  137. const res = await post('/proxy_dms/model/updateFieldList', {
  138. modelId: String(MODEL_ID),
  139. fieldList: JSON.stringify(fieldList),
  140. addfieldList: JSON.stringify(addfieldList),
  141. delfieldList: JSON.stringify({}),
  142. updatefieldList: JSON.stringify({}),
  143. }, token)
  144. const verify = await post('/proxy_dms/model/getModelById', { modelId: String(MODEL_ID) }, token)
  145. const after = normalizeFieldList(verify.content && verify.content.fieldList)
  146. const still = added.filter((n) => !after['c_' + n])
  147. console.log('update', res.code, String(res.message || res.content || '').slice(0, 200))
  148. console.log('after', Object.keys(after).length, 'stillMissing', still.join(',') || 'none')
  149. if (still.length) process.exit(2)
  150. }
  151. main().catch((e) => {
  152. console.error(e)
  153. process.exit(1)
  154. })