ensure-shba-user.mjs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /**
  2. * 确保生产 DMS 有 shba_user 栏目(系统账号:职务 + 部门)。
  3. * 凭据默认与 src/api/dmsAuth.ts 相同,不要打印密码。栏目已存在则只打印状态。
  4. */
  5. import { DMS_CLIENT_ID, DMS_GATEWAY, DMS_PASSWORD, DMS_USER } from './dms-cred.mjs'
  6. const gateway = DMS_GATEWAY
  7. const user = DMS_USER
  8. const password = DMS_PASSWORD
  9. const clientId = DMS_CLIENT_ID
  10. async function call(urlPath, fields = {}, method = 'POST', token = '') {
  11. const body = new URLSearchParams()
  12. for (const [k, v] of Object.entries(fields)) body.set(k, String(v))
  13. const headers = { 'Content-Type': 'application/x-www-form-urlencoded' }
  14. if (token) headers.Token = token
  15. const res = await fetch(urlPath.startsWith('http') ? urlPath : `${gateway}${urlPath}`, { method, headers, body })
  16. return res.json()
  17. }
  18. function walk(nodes, map = {}) {
  19. for (const n of nodes || []) {
  20. if (n.tag) map[n.tag] = n
  21. walk(n.columnList, map)
  22. }
  23. return map
  24. }
  25. const login = await call(`${gateway}/proxy_oauth/user/login`, { userName: user, password, clientId })
  26. if (login.code !== 200 || !login.message) {
  27. console.error('登录失败', login.message || login)
  28. process.exit(1)
  29. }
  30. const token = login.message
  31. const cols = walk((await call('/proxy_dms/column/getColumnList', {}, 'POST', token)).content)
  32. const col = cols.shba_user
  33. if (!col?.id) {
  34. console.error('shba_user 栏目不存在。请从 shba_org_model 克隆字段后 addColumn,不要自造 addModel 字段结构。')
  35. process.exit(1)
  36. }
  37. const listed = await call('/proxy_dms/content/selectContentList', {
  38. columnId: col.id,
  39. search: '[]',
  40. orderBy: '[{"field":"update_time","orderByType":2}]',
  41. page: 0,
  42. pageSize: 50,
  43. }, 'POST', token)
  44. const rows = listed.content?.data || []
  45. console.log('shba_user', col.id, col.modelName, 'accounts', rows.length, rows.map((r) => `${r.content}:${r.c_account}/${r.c_role}/${r.c_branch_name}`).join(' | '))