usernotice.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. Page({
  2. /**
  3. * 页面的初始数据
  4. */
  5. data: {
  6. noticeDetail: {
  7. content: ''
  8. },
  9. phoneNumbers: [] // 存储提取出的电话号码
  10. },
  11. // 处理电话号码,将电话号码转换为可点击的链接
  12. processPhoneNumbers: function(content) {
  13. if (!content) return '';
  14. // 匹配带区号的固定电话号码(如:0516-87030707)和手机号码
  15. const phoneRegex = /(?:0\d{2,3}-\d{7,8}|1[3-9]\d{9})/g;
  16. const phoneNumbers = [];
  17. // 将内容中的电话号码替换为蓝色文本
  18. let processedContent = content;
  19. // 找到所有匹配的电话号码,从后向前替换,避免位置变化
  20. const matches = [];
  21. let match;
  22. while ((match = phoneRegex.exec(content)) !== null) {
  23. matches.push({
  24. text: match[0],
  25. index: match.index,
  26. length: match[0].length
  27. });
  28. }
  29. // 从后向前替换,避免索引变化
  30. for (let i = matches.length - 1; i >= 0; i--) {
  31. const matchInfo = matches[i];
  32. const phoneText = matchInfo.text;
  33. const cleanNumber = phoneText.replace(/-/g, '');
  34. // 保存电话号码信息
  35. phoneNumbers.unshift({
  36. phoneText: phoneText,
  37. cleanNumber: cleanNumber
  38. });
  39. // 替换为带颜色的HTML
  40. const before = processedContent.substring(0, matchInfo.index);
  41. const after = processedContent.substring(matchInfo.index + matchInfo.length);
  42. processedContent = before +
  43. `<span style="color:#007AFF;">${phoneText}</span>` +
  44. after;
  45. }
  46. this.setData({
  47. phoneNumbers: phoneNumbers
  48. });
  49. return processedContent;
  50. },
  51. // 处理电话号码点击事件
  52. handlePhoneTap: function(e) {
  53. // 获取点击的位置
  54. const touchX = e.detail.x;
  55. const touchY = e.detail.y;
  56. // 获取保存的电话号码
  57. const phoneNumbers = this.data.phoneNumbers;
  58. if (phoneNumbers && phoneNumbers.length > 0) {
  59. // 弹出操作菜单让用户选择
  60. wx.showActionSheet({
  61. itemList: phoneNumbers.map(item => `拨打: ${item.phoneText}`),
  62. success: (res) => {
  63. if (res.tapIndex >= 0) {
  64. const selectedPhone = phoneNumbers[res.tapIndex].cleanNumber;
  65. wx.makePhoneCall({
  66. phoneNumber: selectedPhone,
  67. fail: (err) => {
  68. }
  69. });
  70. }
  71. }
  72. });
  73. }
  74. },
  75. onLoad: function (options) {
  76. const app = getApp();
  77. // 从全局数据中获取用户须知信息
  78. const userNotices = app.globalData.currentAccountInfo?.userNotices || '';
  79. // 处理电话号码
  80. const processedContent = this.processPhoneNumbers(userNotices);
  81. this.setData({
  82. 'noticeDetail.content': processedContent
  83. });
  84. },
  85. /**
  86. * 返回上一页
  87. */
  88. goBack: function() {
  89. wx.navigateBack();
  90. },
  91. })