feat: MBTI头像与用户规则链路升级,三端页面与接口同步
Made-with: Cursor
This commit is contained in:
39
miniprogram/utils/mbtiAvatar.js
Normal file
39
miniprogram/utils/mbtiAvatar.js
Normal file
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* MBTI 默认头像:与后台 system_config.mbti_avatars + GET /api/miniprogram/config/mbti-avatars 一致
|
||||
*/
|
||||
|
||||
const MBTI_RE = /^[EI][NS][FT][JP]$/
|
||||
|
||||
function normalizeMbti(m) {
|
||||
const s = (m && String(m).trim().toUpperCase()) || ''
|
||||
return MBTI_RE.test(s) ? s : ''
|
||||
}
|
||||
|
||||
/**
|
||||
* 展示用头像:优先用户已设头像(补全相对路径),否则合法 MBTI + 映射表中有 URL 则用映射
|
||||
* @param {string} avatar
|
||||
* @param {string} mbti
|
||||
* @param {Record<string,string>} map
|
||||
* @param {string} baseUrl
|
||||
*/
|
||||
function resolveAvatarWithMbti(avatar, mbti, map, baseUrl) {
|
||||
let a = (avatar && String(avatar).trim()) || ''
|
||||
if (a) {
|
||||
if (!/^https?:\/\//i.test(a) && baseUrl) {
|
||||
if (a.startsWith('/')) a = baseUrl + a
|
||||
}
|
||||
return a
|
||||
}
|
||||
const key = normalizeMbti(mbti)
|
||||
if (!key || !map || typeof map !== 'object') return ''
|
||||
let u = (map[key] && String(map[key]).trim()) || ''
|
||||
if (!u) return ''
|
||||
if (!/^https?:\/\//i.test(u) && baseUrl && u.startsWith('/')) u = baseUrl + u
|
||||
return u
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
MBTI_RE,
|
||||
normalizeMbti,
|
||||
resolveAvatarWithMbti,
|
||||
}
|
||||
13
miniprogram/utils/partIcons.js
Normal file
13
miniprogram/utils/partIcons.js
Normal file
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* 与管理端 content/ChapterTree.tsx 的 PART_ICONS、正文篇序规则一致
|
||||
* 后台篇头用 emoji 轮询;小程序目录页与之对齐(无自定义图时)
|
||||
*/
|
||||
const PART_ICONS = ['📖', '📕', '📗', '📘', '📙', '📓', '📔', '📒', '📚', '📖']
|
||||
|
||||
/** 正文篇在列表中的从 0 开始的序号 → emoji(与 ChapterTree bodyPartOrdinal 一致) */
|
||||
function partEmojiForBodyIndex(bodyIndex) {
|
||||
const i = Math.max(0, Number(bodyIndex) || 0)
|
||||
return PART_ICONS[i % PART_ICONS.length]
|
||||
}
|
||||
|
||||
module.exports = { PART_ICONS, partEmojiForBodyIndex }
|
||||
Reference in New Issue
Block a user