cmvr-wms/ruoyi-project/scripts/demo.mjs

68 lines
5.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { pathToFileURL } from 'node:url'
import { resolve } from 'node:path'
import { randomUUID } from 'node:crypto'
/** 使用真实登录和业务接口创建可重复运行的示例;不会直接写状态或修改既有业务项目。 */
export async function seedDemo(base = process.env.PM_BASE_URL || 'http://127.0.0.1:8080') {
if (!process.env.PM_PASSWORD) throw new Error('请设置 PM_PASSWORD;凭据不会写入文件或输出')
const login = await (await fetch(`${base}/login`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username: process.env.PM_USER || 'admin', password: process.env.PM_PASSWORD, code: '', uuid: '' }) })).json()
if (login.code !== 200) throw new Error(login.msg || '登录失败;若启用了验证码,请用已登录界面手工创建示例')
const token = login.token
/** 模块请求使用当前会话,HTTP 和业务冲突都失败退出,不伪造成功结果。 */
async function call(path, method = 'GET', input) {
const response = await fetch(`${base}/project/manage${path}`, { method, headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' }, body: input === undefined ? undefined : JSON.stringify(input) })
const body = await response.json()
if (!response.ok || body.code !== 200) throw new Error(`${body.message || body.msg}:${JSON.stringify(body.blockedReasons || body.conflicts || [])}`)
return body.data
}
const name = '任务依赖与排期示例'
let project = (await call('/projects')).find(p => p.name === name)
if (!project) {
const spaces = await call('/spaces')
const space = spaces.find(s => s.name === '项目协作演示')?.id || await call('/spaces', 'POST', { name: '项目协作演示' })
const id = await call('/projects', 'POST', { name, spaceId: space })
project = (await call('/projects')).find(p => p.id === id)
}
const iterations = await call(`/projects/${project.id}/iterations?includeArchived=true`)
let iteration = iterations.find(candidate => candidate.code === 'DEPENDENCY-DEMO') || iterations.find(candidate => candidate.code === 'LEGACY-DEFAULT')
if (iteration) return { base, token, project, iteration, items: await listItems(iteration.id), call }
if (iterations.length) throw new Error('同名项目已有其他迭代;请使用独立示例项目,脚本不会改写既有业务迭代')
iteration = await call(`/projects/${project.id}/iterations`, 'POST', { code: 'DEPENDENCY-DEMO', name: '任务依赖与排期示例迭代', goal: '比较实际前置条件与预计排期。', sortOrder: 0, idempotencyKey: randomUUID() })
const scope = `/projects/${project.id}/iterations/${iteration.id}`
/** flat 分页读取当前迭代全部示例,避免把新接口响应包误当作数组或漏掉后续页。 */
async function listItems(iterationId) {
const result = []
for (let page = 1; ; page++) {
const data = await call(`/projects/${project.id}/iterations/${iterationId}/items?view=flat&page=${page}&pageSize=200`)
result.push(...data.rows)
if (!data.rows.length || result.length >= data.total) return result
}
}
/** 所有示例在 PLANNED 阶段通过真实创建入口筹备,普通请求没有 status 字段。 */
async function create(type, title, start, end) {
return call(`${scope}/items`, 'POST', { type, title, description: '用于验证实际前置条件与预计排期分离。可在开发库中手工调整。', assigneeId: null, priority: 'MEDIUM', plannedStartAt: start, plannedEndAt: end })
}
const a = await create('TASK', '任务 A:接口设计', '2026-10-19T09:00:00+08:00', '2026-10-20T18:00:00+08:00')
const b = await create('TASK', '任务 B:接口开发', '2026-10-20T18:00:00+08:00', '2026-10-22T18:00:00+08:00')
const c = await create('TASK', '任务 C:联调测试', '2026-10-22T18:00:00+08:00', '2026-10-24T18:00:00+08:00')
await create('REQUIREMENT', '需求 X:技术方案', null, null)
const d = await create('TASK', '任务 D:方案实施', '2026-10-20T18:00:00+08:00', '2026-10-25T18:00:00+08:00')
/** 修改依赖前取得当前后续版本,示例也不能绕过乐观锁。 */
async function dependency(successor, condition) { const item = await call(`/items/${successor}`); await call(`/items/${successor}/dependencies`, 'POST', { ...condition, taskVersion: item.version }) }
await dependency(c, { conditionType: 'TASK_COMPLETED', predecessorTaskId: a })
await dependency(c, { conditionType: 'TASK_COMPLETED', predecessorTaskId: b })
await dependency(d, { conditionType: 'TASK_COMPLETED', predecessorTaskId: a })
iteration = await call(scope)
iteration = await call(`${scope}/activate`, 'POST', { version: iteration.version, graphVersion: iteration.graphVersion, idempotencyKey: randomUUID() })
await call(`/items/${a}/start`, 'POST', { version: (await call(`/items/${a}`)).version })
await call(`/items/${a}/transition`, 'POST', { status: 'COMPLETED', version: (await call(`/items/${a}`)).version })
await call(`/items/${c}/comments`, 'POST', { body: '示例预期:A 已完成、B 未完成,C 显示已满足 1/2 条件;C 最早预计开始为北京时间 2026-10-22 18:00。' })
const items = await listItems(iteration.id)
return { base, token, project, iteration, items, call }
}
if (process.argv[1] && import.meta.url === pathToFileURL(resolve(process.argv[1])).href) {
const demo = await seedDemo()
console.log(JSON.stringify({ projectId: demo.project.id, projectName: demo.project.name, iterationId: demo.iteration.id, workItems: demo.items.map(i => ({ id: i.id, title: i.title, canStart: i.canStart })), frontendPath: `/project/collaboration?projectId=${demo.project.id}&iterationId=${demo.iteration.id}` }, null, 2))
}