fix(admin): 统一一诊医生看板与获客助手并修正排行口径显示

This commit is contained in:
2026-12-02 11:27:01 +08:00
parent e73e8b1b18
commit 7a0c62bfb6
8 changed files with 577 additions and 27 deletions
@@ -0,0 +1,120 @@
const assert = require('node:assert/strict')
const fs = require('node:fs')
const path = require('node:path')
const test = require('node:test')
const { parse, compileScript } = require('@vue/compiler-sfc')
const ts = require('typescript')
const vue = require('vue')
const { renderToString } = require('vue/server-renderer')
const filename = path.join(__dirname, '../src/views/first_visit/doctor_dashboard/index.vue')
const { descriptor, errors } = parse(fs.readFileSync(filename, 'utf8'), { filename })
assert.deepEqual(errors, [])
const compiled = compileScript(descriptor, { id: 'doctor-dashboard', inlineTemplate: true, templateOptions: { ssr: true } })
function compile(content) {
return ts.transpileModule(content, { compilerOptions: {
module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022
} }).outputText
}
const outputText = compile(compiled.content)
function loadPage(code, imports) {
const mod = { exports: {} }
new Function('require', 'module', 'exports', code)(id => imports[id] || require(id), mod, mod.exports)
return mod.exports.default
}
const light = { surface: '#ffffff', ink: '#323251', border: '#e2e8ee', muted: '#62718b', grid: '#e6eaeb', accentInk: '#5d87ff', accentText: '#4569bf' }
const dark = { surface: '#161618', ink: '#e3e3e8', border: '#383840', muted: '#9999a9', grid: '#323238', accentInk: '#ba9aff', accentText: '#d0c3ff' }
async function renderDashboard(values, palette = null) {
const skin = vue.shallowRef(palette)
const stub = { render: () => null }
let option
const Chart = { props: ['option'], render() { option = this.option; return null } }
const imports = {
vue: { ...vue, onMounted: vue.onServerPrefetch },
'@/api/first_visit': { firstVisitDoctorDashboardOverview: async () => ({
summary: { registration_total: 18, appointment_total: 12 },
rankings: { amounts: values.map((value, i) => ({ doctor_id: i + 1, name: '医生' + i, value })), conversion: [] },
trend: { labels: ['12-01', '12-02'], amounts: [0, 10000] }
}) },
'@/hooks/useStatsChartPalette': { useStatsChartPalette: () => skin },
'@element-plus/icons-vue': new Proxy({}, { get: () => stub }),
'element-plus': {}, 'vue-echarts': { default: Chart }
}
const app = vue.createSSRApp(loadPage(outputText, imports))
for (const [, tag] of descriptor.template.content.matchAll(/<(el-[\w-]+)/g)) {
if (!app.component(tag)) app.component(tag, stub)
}
app.directive('loading', {})
const html = await renderToString(app)
return { html, option }
}
test('挂号和预约在各自的指标下显示接口原值', async () => {
const { html } = await renderDashboard([])
const cards = [...html.matchAll(/<article class="metric-card[^"]*"[^>]*>(.*?)<\/article>/gs)]
.map(([, body]) => body.replace(/<[^>]+>/g, ' ').replace(/\s+/g, ' '))
assert.equal(cards.length, 6)
assert.match(cards[0], /总挂号 18 /)
assert.match(cards[5], /预约完成率 .*总预约 12/)
})
for (const [values, widths] of [
[[10000, 100, 0], ['100%', '1%', '0%']],
[[0, 0], ['0%', '0%']],
[['10', 0.025, -1, 'invalid'], ['100%', '0.25%', '0%', '0%']]
]) {
test('金额排行按真实比例并保留顺序:' + values.join('/'), async () => {
const { html } = await renderDashboard(values)
const rows = [...html.matchAll(/<div class="bar-row"[^>]*><b[^>]*>(\d+)<\/b>.*?style="width:([^;"]+).*?<strong[^>]*>([^<]+)<\/strong>/gs)]
assert.deepEqual(rows.map(([,rank,width]) => [Number(rank), width]), widths.map((w,i) => [i+1, w]))
if (values[0] === 10000) assert.deepEqual(rows.map(r => r[3]), ['¥10,000.00', '¥100.00', '¥0.00'])
})
}
for (const [name, skin] of [['浅色', light], ['暗色自选主色', dark]]) {
test(name + '趋势的实际序列、坐标和 tooltip 使用主题,业务数值不变', async () => {
const { option } = await renderDashboard([], skin)
const [amount] = option.series
assert.equal(amount.name, '成交金额')
assert.deepEqual([...amount.data], [0, 10000])
assert.equal(amount.lineStyle.color, skin.accentInk)
assert.equal(amount.itemStyle.color, skin.accentInk)
assert.equal(amount.areaStyle.color, skin.accentInk)
assert.equal(option.xAxis.axisLabel.color, skin.muted)
assert.equal(option.yAxis.splitLine.lineStyle.color, skin.grid)
assert.equal(option.tooltip.textStyle.color, skin.ink)
assert.equal(option.tooltip.backgroundColor, skin.surface)
})
}
test('经典版保留既有趋势和提示框外观', async () => {
const { option } = await renderDashboard([])
assert.equal(option.series[0].lineStyle.color, '#139a8c')
assert.equal(option.series[0].itemStyle.color, '#ffffff')
assert.equal(option.tooltip.backgroundColor, undefined)
})
// Exercise the compiled page's computed option on one ECharts instance, not a remount per color.
test('原地切色后 ECharts 实际绘制新色,切回经典色也能还原', () => {
const echarts = require('echarts')
const skin = vue.shallowRef(light)
const page = loadPage(compile(compileScript(descriptor, { id: 'doctor-dashboard' }).content), {
vue: { ...vue, onMounted() {} }, '@/api/first_visit': {}, 'vue-echarts': {},
'element-plus': {}, '@element-plus/icons-vue': {},
'@/hooks/useStatsChartPalette': { useStatsChartPalette: () => skin }
})
const { dashboard, trendChartOption } = page.setup({}, { expose() {} })
dashboard.trend.labels = ['12-01', '12-02']
dashboard.trend.amounts = [25, 100]
const plot = echarts.init(null, null, { ssr: true, renderer: 'svg', width: 500, height: 200 })
try {
for (const current of [light, dark, null]) {
skin.value = current
plot.setOption({ ...trendChartOption.value, animation: false }, { notMerge: true })
const color = current?.accentInk ?? '#139a8c'
assert.equal(plot.getOption().series[0].lineStyle.color, color)
assert.ok(plot.renderToSVGString().includes('stroke="' + color + '"'))
}
} finally { plot.dispose() }
})
+132
View File
@@ -0,0 +1,132 @@
const assert = require('node:assert/strict')
const test = require('node:test')
const fs = require('node:fs')
const path = require('node:path')
const postcss = require('postcss')
const sass = require('sass')
const { parseDOM } = require('htmlparser2')
const { selectAll, selectOne, compile } = require('css-select')
const { parse, compileStyle } = require('@vue/compiler-sfc')
const src = path.join(__dirname, '../src')
const base = path.join(src, 'views/first_visit/wecom_promotion')
const scope = 'data-v-c02'
// Use Vue's actual scope rewriting and template classes. No browser or live data.
function component(name) {
const filename = path.join(base, name)
const { descriptor, errors } = parse(fs.readFileSync(filename, 'utf8'), { filename })
assert.deepEqual(errors, [])
const rules = []
for (const block of descriptor.styles.slice(1)) {
const file = block.src ? path.resolve(base, block.src) : filename
const source = block.src ? fs.readFileSync(file, 'utf8') : block.content
const compiled = compileStyle({ filename: file, source, scoped: block.scoped, id: scope, preprocessLang: block.lang })
assert.deepEqual(compiled.errors, [])
postcss.parse(compiled.code).walkRules(rule => {
assert.ok(rule.selectors.every(selector => selector.startsWith('html[data-admin-ui=v2] ')), rule.selector)
rules.push({ rule, matches: compile(rule.selector.replaceAll(':focus-visible', '[data-focus-visible]')) })
})
}
const dom = parseDOM('<html data-admin-ui="v2"><body>' + descriptor.template.content + '</body></html>')
selectAll('body *', dom).forEach(el => { el.attribs[scope] = '' })
return { dom, rules }
}
const page = component('index.vue')
function properties(rules, node) {
const values = {}
for (const { rule, matches } of rules) {
if (matches(node)) {
rule.walkDecls(d => { values[d.prop] = d.value })
}
}
return values
}
const theme = postcss.parse(sass.compile(path.join(src, 'styles/admin-v2.scss')).css)
function themeColors(dark) {
const vars = {}
for (const selector of [':root[data-admin-ui=v2]', ...(dark ? [':root[data-admin-ui=v2].dark'] : [])]) {
theme.nodes.find(n => n.selector === selector).walkDecls(d => { vars[d.prop] = d.value })
}
function rgb(value) {
const ref = /^var\((--[\w-]+)\)$/.exec(value)
if (ref) return rgb(vars[ref[1]])
if (value.startsWith('rgb(')) return value.match(/[\d.]+/g).map(Number)
const mix = /^color-mix\(in srgb, (var\([^)]+\)) (\d+)%, (var\([^)]+\))\)$/.exec(value)
if (mix) {
const a = rgb(mix[1]), b = rgb(mix[3]), weight = Number(mix[2]) / 100
return a.map((v, i) => v * weight + b[i] * (1 - weight))
}
assert.match(value, /^#(?:[a-f\d]{3}|[a-f\d]{6})$/i)
const hex = value.slice(1)
return (hex.length === 3 ? [...hex].map(c => c + c) : hex.match(/../g)).map(c => parseInt(c, 16))
}
return rgb
}
function contrast(a, b) {
const luma = rgb => rgb.reduce((sum, byte, i) => {
const c = byte / 255
return sum + [0.2126, 0.7152, 0.0722][i] * (c <= 0.04045 ? c / 12.92 : ((c + .055) / 1.055) ** 2.4)
}, 0)
const [high, low] = [luma(a), luma(b)].sort((x, y) => y - x)
return (high + .05) / (low + .05)
}
test('both sets of four counts share a neutral card and a legible hint; the active tab follows the primary action', () => {
const { dom, rules } = page
const cards = selectAll('.metric-card, .customer-metric-card', dom)
assert.equal(cards.length, 8)
for (const card of cards) {
const node = properties(rules, card), hint = properties(rules, selectOne('p', card))
const icon = properties(rules, selectOne('.metric-icon', card))
assert.equal(node.background, 'var(--zyt-surface)')
assert.equal(node['border-color'], 'var(--zyt-card-border)')
assert.equal(node['box-shadow'], 'none')
assert.equal(icon.background, 'var(--zyt-accent-soft)')
assert.ok(parseFloat(hint['font-size']) >= 12)
for (const rgb of [themeColors(false), themeColors(true)]) {
assert.ok(contrast(rgb(hint.color), rgb(node.background)) >= 4.5)
}
}
const tab = selectOne('.tab-nav button', dom)
tab.attribs.class = 'active'
assert.equal(properties(rules, tab)['border-bottom-color'], 'var(--zyt-primary)')
assert.ok(selectOne('.section-heading-actions el-button[type="primary"]', dom))
})
test('running, offline, waiting and error text keep distinct, legible color pairs in both themes', () => {
const { dom, rules } = page
const states = []
for (const className of ['status-tag is-online', 'status-tag is-offline', 'availability is-waiting', 'availability is-error']) {
const el = selectOne('.pool-title-row span', dom)
el.attribs.class = className
const { color, background } = properties(rules, el)
states.push(background)
for (const rgb of [themeColors(false), themeColors(true)]) {
assert.ok(contrast(rgb(color), rgb(background)) >= 4.5, className)
}
}
assert.equal(new Set(states).size, states.length)
})
test('classic is unchanged and V2 editor styles do not reach the customer previews, template swatches or copied code', () => {
const classicPage = component('index.vue')
selectOne('html', classicPage.dom).attribs['data-admin-ui'] = 'legacy'
for (const node of selectAll('body *', classicPage.dom)) assert.deepEqual(properties(classicPage.rules, node), {})
for (const [name, preview] of [
['components/WecomFloatingWidgetBuilder.vue', '.preview-canvas, .template-swatch, .code-card pre'],
['components/WelcomeMessageEditor.vue', '.welcome-preview'],
['components/PromotionAutomationForm.vue', '.remark-preview']
]) {
const { dom, rules } = component(name)
assert.ok(rules.length, name)
const roots = selectAll(preview, dom)
assert.ok(roots.length, name)
for (const node of roots.flatMap(n => [n, ...selectAll('*', n)])) {
assert.deepEqual(properties(rules, node), {}, name + ': ' + node.attribs.class)
}
selectOne('html', dom).attribs['data-admin-ui'] = 'legacy'
for (const node of selectAll('body *', dom)) assert.deepEqual(properties(rules, node), {})
}
})