index.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. // pages/user/map/index.js
  2. const { museumApi } = require('../../../utils/api.js');
  3. const { processHtmlContent } = require('../../../utils/htmlProcessor.js');
  4. const WxParse = require('../../../utils/wxParse/wxParse.js');
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. loading: false,
  11. detailData: null,
  12. processedContent: '',
  13. shouldShowBackButton: true
  14. },
  15. /**
  16. * 生命周期函数--监听页面加载
  17. */
  18. onLoad(options) {
  19. // 检查是否来自微信,如果是则隐藏返回按钮
  20. if (options.isfrom === 'weixin') {
  21. this.setData({
  22. shouldShowBackButton: false
  23. });
  24. }
  25. // 加载详情数据
  26. this.loadDetailData();
  27. },
  28. /**
  29. * 导航前往
  30. */
  31. openNavitor() {
  32. wx.openLocation({
  33. latitude: 45.605550,
  34. longitude: 84.902629,
  35. name: '克拉玛依博物馆',
  36. address: '克拉玛依博物馆',
  37. scale: 18,
  38. success: function(res) {
  39. console.log('打开地图成功', res);
  40. },
  41. fail: function(err) {
  42. console.error('打开地图失败', err);
  43. wx.showToast({
  44. title: '打开地图失败',
  45. icon: 'none'
  46. });
  47. }
  48. });
  49. },
  50. /**
  51. * 返回上一页
  52. */
  53. goBack() {
  54. wx.navigateBack({
  55. delta: 1
  56. });
  57. },
  58. /**
  59. * 加载详情数据
  60. */
  61. loadDetailData() {
  62. let that = this;
  63. this.setData({
  64. loading: true
  65. });
  66. museumApi.getMuseumDetail(2)
  67. .then(response => {
  68. if (response) {
  69. // const parsedContent = this.parseContent(response.content || response.context);
  70. let article = response.context;
  71. // console.log('解析后的内容:', parsedContent);
  72. WxParse.wxParse('article', 'html', article, that, 5);
  73. this.setData({
  74. detailData: response,
  75. // contentItems: parsedContent
  76. });
  77. }
  78. })
  79. .catch(error => {
  80. console.error('获取地图详情数据失败:', error);
  81. this.setData({
  82. detailData: null
  83. });
  84. wx.showToast({
  85. title: '加载失败',
  86. icon: 'none'
  87. });
  88. })
  89. .finally(() => {
  90. this.setData({
  91. loading: false
  92. });
  93. });
  94. },
  95. /**
  96. * 处理HTML实体编码
  97. */
  98. processHtmlEntities(text) {
  99. if (!text) return '';
  100. return text
  101. .replace(/ /g, ' ')
  102. .replace(/ /g, '  ')
  103. .replace(/ /g, '    ')
  104. .replace(/ /g, ' ')
  105. .replace(/ /g, ' ');
  106. },
  107. /**
  108. * 解析style属性
  109. */
  110. parseStyleAttribute(styleStr) {
  111. const styles = {};
  112. if (styleStr) {
  113. styleStr.split(';').forEach(rule => {
  114. const [property, value] = rule.split(':').map(s => s.trim());
  115. if (property && value) {
  116. styles[property] = value;
  117. }
  118. });
  119. }
  120. return styles;
  121. },
  122. /**
  123. * 检查是否有删除线样式
  124. */
  125. hasStrikethrough(attributes) {
  126. // 检查style属性中的text-decoration
  127. const styleMatch = attributes.match(/style="([^"]*)"/i);
  128. if (styleMatch) {
  129. const styles = this.parseStyleAttribute(styleMatch[1]);
  130. return styles['text-decoration']?.includes('line-through') ||
  131. styles['text-decoration-line']?.includes('line-through');
  132. }
  133. // 检查class属性中是否包含删除线相关类名
  134. const classMatch = attributes.match(/class="([^"]*)"/i);
  135. if (classMatch) {
  136. const classes = classMatch[1].toLowerCase();
  137. return classes.includes('strikethrough') ||
  138. classes.includes('line-through') ||
  139. classes.includes('deleted');
  140. }
  141. return false;
  142. },
  143. /**
  144. * 解析HTML内容为不同类型的内容项
  145. */
  146. parseContent(content) {
  147. if (!content) return [];
  148. const contentItems = [];
  149. const matches = [];
  150. // 定义所有匹配规则
  151. const patterns = [
  152. { regex: /<h[1-6]([^>]*?)>(.*?)<\/h[1-6]>/gi, type: 'heading', handler: (match) => { let text = match[2].replace(/<[^>]*>/g, ''); let attributes = match[1]; let isCenter = /text-align\s*:\s*center/i.test(attributes) || /style="[^"]*text-align\s*:\s*center[^"]*"/i.test(attributes); return { type: 'heading', content: this.processHtmlEntities(text), center: isCenter }; } },
  153. {
  154. regex: /<span[^>]*>(.*?)<\/span>/gi,
  155. type: 'span',
  156. handler: (match) => {
  157. let text = match[1].replace(/<[^>]*>/g, '');
  158. return {
  159. type: 'span',
  160. content: this.processHtmlEntities(text)
  161. };
  162. }
  163. },
  164. // 检测删除线标签(del, s, strike)
  165. {
  166. regex: /<(del|s|strike)[^>]*>(.*?)<\/(del|s|strike)>/gi,
  167. type: 'strikethrough',
  168. handler: (match) => {
  169. let text = match[2].replace(/<[^>]*>/g, '');
  170. return {
  171. type: 'strikethrough',
  172. content: this.processHtmlEntities(text)
  173. };
  174. }
  175. },
  176. // 检测带有删除线样式的任意标签
  177. {
  178. regex: /<(\w+)([^>]*style="[^"]*text-decoration[^"]*line-through[^"]*"[^>]*)>(.*?)<\/\1>/gi,
  179. type: 'strikethrough',
  180. handler: (match) => {
  181. let text = match[3].replace(/<[^>]*>/g, '');
  182. return {
  183. type: 'strikethrough',
  184. content: this.processHtmlEntities(text)
  185. };
  186. }
  187. },
  188. // 检测带有删除线类名的任意标签
  189. {
  190. regex: /<(\w+)([^>]*class="[^"]*(?:strikethrough|line-through|deleted)[^"]*"[^>]*)>(.*?)<\/\1>/gi,
  191. type: 'strikethrough',
  192. handler: (match) => {
  193. let text = match[3].replace(/<[^>]*>/g, '');
  194. return {
  195. type: 'strikethrough',
  196. content: this.processHtmlEntities(text)
  197. };
  198. }
  199. },
  200. {
  201. regex: /<p[^>]*>(.*?)<\/p>/gi,
  202. type: 'text',
  203. handler: (match) => {
  204. let text = match[1].replace(/<[^>]*>/g, '');
  205. return {
  206. type: 'text',
  207. content: this.processHtmlEntities(text)
  208. };
  209. }
  210. },
  211. {
  212. regex: /<div[^>]*class="[^"]*media-wrap[^"]*image-wrap[^"]*"[^>]*>.*?<img[^>]*src="([^"]+)"[^>]*(?:alt="([^"]*)")?\/?>.* ?<\/div>/gi,
  213. type: 'image',
  214. handler: (match) => ({
  215. type: 'image',
  216. src: match[1],
  217. alt: match[2] || '图片'
  218. })
  219. },
  220. {
  221. regex: /<div[^>]*class="[^"]*media-wrap[^"]*video-wrap[^"]*"[^>]*>.*?<video[^>]*src="([^"]+)"[^>]*(?:poster="([^"]*)")?\/?>.* ?<\/div>/gi,
  222. type: 'video',
  223. handler: (match) => ({
  224. type: 'video',
  225. src: match[1],
  226. poster: match[2] || ''
  227. })
  228. },
  229. {
  230. regex: /<div[^>]*class="[^"]*media-wrap[^"]*audio-wrap[^"]*"[^>]*>.*?<audio[^>]*src="([^"]+)"[^>]*(?:title="([^"]*)")?\/?>.* ?<\/div>/gi,
  231. type: 'audio',
  232. handler: (match) => ({
  233. type: 'audio',
  234. src: match[1],
  235. title: match[2] || '音频'
  236. })
  237. }
  238. ];
  239. // 收集所有匹配项及其位置
  240. patterns.forEach(pattern => {
  241. let match;
  242. pattern.regex.lastIndex = 0; // 重置正则表达式的lastIndex
  243. while ((match = pattern.regex.exec(content)) !== null) {
  244. const item = pattern.handler(match);
  245. if ((item.type === 'text' && item.content.trim()) || item.type !== 'text') {
  246. matches.push({
  247. index: match.index,
  248. length: match[0].length,
  249. item: item
  250. });
  251. }
  252. }
  253. });
  254. // 按照在原HTML中的位置排序
  255. matches.sort((a, b) => a.index - b.index);
  256. // 移除重叠的匹配项(保持原始顺序,只过滤真正重复的内容)
  257. const filteredMatches = [];
  258. const typesPriority = ['heading', 'strikethrough', 'span', 'text']; // 优先级从高到低
  259. for (let i = 0; i < matches.length; i++) {
  260. const current = matches[i];
  261. let shouldSkip = false;
  262. // 检查是否与已添加的项有重叠
  263. for (let j = 0; j < filteredMatches.length; j++) {
  264. const existing = filteredMatches[j];
  265. // 检查是否有重叠
  266. if (current.index < existing.index + existing.length &&
  267. current.index + current.length > existing.index) {
  268. // 如果有重叠,比较优先级
  269. const currentPriority = typesPriority.indexOf(current.item.type);
  270. const existingPriority = typesPriority.indexOf(existing.item.type);
  271. // 只有当前项优先级明显低于已存在项时才跳过
  272. if (currentPriority > existingPriority && existingPriority !== -1) {
  273. shouldSkip = true;
  274. break;
  275. }
  276. }
  277. }
  278. if (!shouldSkip) {
  279. filteredMatches.push(current);
  280. }
  281. }
  282. // 提取排序后的内容项
  283. return filteredMatches.map(match => match.item);
  284. },
  285. /**
  286. * 生命周期函数--监听页面初次渲染完成
  287. */
  288. onReady() {
  289. },
  290. /**
  291. * 生命周期函数--监听页面显示
  292. */
  293. onShow() {
  294. },
  295. /**
  296. * 生命周期函数--监听页面隐藏
  297. */
  298. onHide() {
  299. },
  300. /**
  301. * 生命周期函数--监听页面卸载
  302. */
  303. onUnload() {
  304. },
  305. /**
  306. * 页面相关事件处理函数--监听用户下拉动作
  307. */
  308. onPullDownRefresh() {
  309. },
  310. /**
  311. * 页面上拉触底事件的处理函数
  312. */
  313. onReachBottom() {
  314. },
  315. /**
  316. * 用户点击右上角分享
  317. */
  318. onShareAppMessage() {
  319. }
  320. });