优化错误处理逻辑,增加用户不存在时的自动登出功能。更新阅读页内容解析,支持TipTap HTML格式,提升用户体验。
This commit is contained in:
@@ -9,36 +9,17 @@
|
||||
* - 使用状态机(accessState)规范权限流转
|
||||
* - 异常统一保守处理,避免误解锁
|
||||
*
|
||||
* 更新: 正文 @某人({{@userId:昵称}})
|
||||
* 更新: 正文 @某人(TipTap HTML <span data-type="mention">)
|
||||
* - contentSegments 解析每行,mention 高亮可点;点击→确认→登录/资料校验→POST /api/miniprogram/ckb/lead
|
||||
* - 回归:无@ 时仍按段展示;未登录/无联系方式/重复点击均有提示或去重
|
||||
*/
|
||||
|
||||
import accessManager from '../../utils/chapterAccessManager'
|
||||
import readingTracker from '../../utils/readingTracker'
|
||||
const { parseScene } = require('../../utils/scene.js')
|
||||
const contentParser = require('../../utils/contentParser.js')
|
||||
|
||||
const app = getApp()
|
||||
|
||||
// 解析单行中的 {{@userId:昵称}} 为片段数组,用于阅读页 @ 高亮与点击
|
||||
function parseLineToSegments(line) {
|
||||
const segments = []
|
||||
const re = /\{\{@([^:]+):(.*?)\}\}/g
|
||||
let lastEnd = 0
|
||||
let m
|
||||
while ((m = re.exec(line)) !== null) {
|
||||
if (m.index > lastEnd) {
|
||||
segments.push({ type: 'text', text: line.slice(lastEnd, m.index) })
|
||||
}
|
||||
segments.push({ type: 'mention', userId: String(m[1]).trim(), nickname: String(m[2] || '').trim() })
|
||||
lastEnd = re.lastIndex
|
||||
}
|
||||
if (lastEnd < line.length) {
|
||||
segments.push({ type: 'text', text: line.slice(lastEnd) })
|
||||
}
|
||||
return segments.length ? segments : [{ type: 'text', text: line }]
|
||||
}
|
||||
|
||||
Page({
|
||||
data: {
|
||||
// 系统信息
|
||||
@@ -233,12 +214,12 @@ Page({
|
||||
this.setData({ section })
|
||||
|
||||
if (res && res.content) {
|
||||
const lines = res.content.split('\n').filter(line => line.trim())
|
||||
const { lines, segments } = contentParser.parseContent(res.content)
|
||||
const previewCount = Math.ceil(lines.length * 0.2)
|
||||
const updates = {
|
||||
content: res.content,
|
||||
contentParagraphs: lines,
|
||||
contentSegments: lines.map(parseLineToSegments),
|
||||
contentSegments: segments,
|
||||
previewParagraphs: lines.slice(0, previewCount),
|
||||
partTitle: res.partTitle || '',
|
||||
chapterTitle: res.chapterTitle || ''
|
||||
@@ -258,13 +239,13 @@ Page({
|
||||
try {
|
||||
const cached = wx.getStorageSync(cacheKey)
|
||||
if (cached && cached.content) {
|
||||
const lines = cached.content.split('\n').filter(line => line.trim())
|
||||
const { lines, segments } = contentParser.parseContent(cached.content)
|
||||
const previewCount = Math.ceil(lines.length * 0.2)
|
||||
|
||||
this.setData({
|
||||
content: cached.content,
|
||||
contentParagraphs: lines,
|
||||
contentSegments: lines.map(parseLineToSegments),
|
||||
contentSegments: segments,
|
||||
previewParagraphs: lines.slice(0, previewCount)
|
||||
})
|
||||
console.log('[Read] 从本地缓存加载成功')
|
||||
@@ -369,7 +350,7 @@ Page({
|
||||
// 3. 都失败,显示加载中并持续重试
|
||||
this.setData({
|
||||
contentParagraphs: ['章节内容加载中...', '正在尝试连接服务器,请稍候...'],
|
||||
contentSegments: [parseLineToSegments('章节内容加载中...'), parseLineToSegments('正在尝试连接服务器,请稍候...')],
|
||||
contentSegments: contentParser.parseContent('章节内容加载中...\n正在尝试连接服务器,请稍候...').segments,
|
||||
previewParagraphs: ['章节内容加载中...']
|
||||
})
|
||||
|
||||
@@ -396,9 +377,9 @@ Page({
|
||||
})
|
||||
},
|
||||
|
||||
// 设置章节内容
|
||||
// 设置章节内容(兼容纯文本/Markdown 与 TipTap HTML)
|
||||
setChapterContent(res) {
|
||||
const lines = res.content.split('\n').filter(line => line.trim())
|
||||
const { lines, segments } = contentParser.parseContent(res.content)
|
||||
const previewCount = Math.ceil(lines.length * 0.2)
|
||||
const sectionPrice = this.data.sectionPrice ?? 1
|
||||
const sectionTitle = (res.sectionTitle || res.title || '').trim()
|
||||
@@ -414,7 +395,7 @@ Page({
|
||||
content: res.content,
|
||||
previewContent: lines.slice(0, previewCount).join('\n'),
|
||||
contentParagraphs: lines,
|
||||
contentSegments: lines.map(parseLineToSegments),
|
||||
contentSegments: segments,
|
||||
previewParagraphs: lines.slice(0, previewCount),
|
||||
partTitle: res.partTitle || '',
|
||||
// 导航栏、分享等使用的文章标题,同样统一为 sectionTitle
|
||||
@@ -440,7 +421,7 @@ Page({
|
||||
if (currentRetry >= maxRetries) {
|
||||
this.setData({
|
||||
contentParagraphs: ['内容加载失败', '请检查网络连接后下拉刷新重试'],
|
||||
contentSegments: [parseLineToSegments('内容加载失败'), parseLineToSegments('请检查网络连接后下拉刷新重试')],
|
||||
contentSegments: contentParser.parseContent('内容加载失败\n请检查网络连接后下拉刷新重试').segments,
|
||||
previewParagraphs: ['内容加载失败']
|
||||
})
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user