governmentArticlesDetail.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // pages/publics/governmentArticlesDetail/governmentArticlesDetail.js
  2. import {
  3. imgUrl
  4. } from "../../api/request"
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. // 屏幕宽度
  11. windowsWidth: '',
  12. // 图片前缀
  13. imgUrl: imgUrl,
  14. // 文章内容
  15. articles: '',
  16. // 文本字号大小
  17. fontSize: '中',
  18. // 是否播放语音
  19. isPlay: false,
  20. // 是否存在文件
  21. isFile: true,
  22. // 文件路径前缀
  23. fileUrlPrefix: '',
  24. // 文件列表
  25. fileList: []
  26. },
  27. chooseFontSize(e) {
  28. let temp = this.data.articles;
  29. switch (this.data.fontSize) {
  30. case '大':
  31. temp.DOCHTMLCON = temp.DOCHTMLCON.replace(/\<span class="fontSizeMax"/gi, '<span"');
  32. break;
  33. case '中':
  34. temp.DOCHTMLCON = temp.DOCHTMLCON.replace(/\<span class="fontSize"/gi, '<span');
  35. break;
  36. case '小':
  37. temp.DOCHTMLCON = temp.DOCHTMLCON.replace(/\<span class="fontSizeSmall"/gi, '<span');
  38. break;
  39. }
  40. switch (e.currentTarget.dataset.size) {
  41. case '大':
  42. temp.DOCHTMLCON = temp.DOCHTMLCON.replace(/\<span/gi, '<span class="fontSizeMax"');
  43. break;
  44. case '中':
  45. temp.DOCHTMLCON = temp.DOCHTMLCON.replace(/\<span/gi, '<span class="fontSize"');
  46. break;
  47. case '小':
  48. temp.DOCHTMLCON = temp.DOCHTMLCON.replace(/\<span/gi, '<span class="fontSizeSmall"');
  49. break;
  50. }
  51. this.setData({
  52. fontSize: e.currentTarget.dataset.size,
  53. articles: temp
  54. })
  55. },
  56. /**
  57. * 生命周期函数--监听页面加载
  58. */
  59. onLoad(options) {
  60. this.setData({
  61. windowsWidth: wx.getSystemInfoSync().windowWidth * 0.85 + 'px'
  62. })
  63. // 是否显示附件下载
  64. if (options.isFile == 'false') {
  65. this.setData({
  66. isFile: false
  67. })
  68. }
  69. // 图文解读
  70. if (options.isPic) {
  71. wx.request({
  72. url: 'https://data.cqna.gov.cn/mini/hlw/info?channelId=' + options.channelId + '&docId=' + options.docId,
  73. method: 'GET',
  74. success: res => {
  75. res.data.data.DOCPUBTIME = res.data.data.DOCPUBTIME.split(" ")[0];
  76. res.data.data.DOCHTMLCON = res.data.data.DOCHTMLCON.replace(/\<span/gi, '<span class="fontSize"');
  77. // 存在多个隐藏图片,取最后一张图片
  78. if (res.data.data.THUMBFILES.indexOf(",") >= 0) {
  79. let index = res.data.data.THUMBFILES.lastIndexOf(",")
  80. res.data.data.THUMBFILES = res.data.data.THUMBFILES.substring(index + 1)
  81. }
  82. // 替换图片路径
  83. res.data.data.DOCHTMLCON = res.data.data.DOCHTMLCON.replace(/src="[^"]+"/gi, 'style="width:' + this.data.windowsWidth + '" src="' + options.url + res.data.data.THUMBFILES + '"');
  84. // 替换特定图片宽度样式
  85. res.data.data.DOCHTMLCON = res.data.data.DOCHTMLCON.replace('style="width:600px;"', 'style="width:' + this.data.windowsWidth + '"')
  86. this.setData({
  87. fileUrlPrefix: options.url,
  88. articles: res.data.data,
  89. fileList: res.data.data.DOCATTACHFILEFIELD
  90. })
  91. }
  92. })
  93. return
  94. }
  95. if (options.docId == undefined) {
  96. wx.request({
  97. url: 'https://data.cqna.gov.cn/mini/hlw/send?page=1&limit=1&channelId=' + options.channelId,
  98. method: 'GET',
  99. success: res => {
  100. let url = res.data.data.DATA[0].DOCPUBURL.substring(0, res.data.data.DATA[0].DOCPUBURL.lastIndexOf("/") + 1)
  101. wx.request({
  102. url: 'https://data.cqna.gov.cn/mini/hlw/info?channelId=' + res.data.data.DATA[0].CHNLID + '&docId=' + res.data.data.DATA[0].DOCID,
  103. method: 'GET',
  104. success: res1 => {
  105. if (res1.data.data.DOCHTMLCON == undefined) {
  106. res1.data.data.DOCHTMLCON = res1.data.data.CONTENT;
  107. res1.data.data.DOCTITLE = res1.data.data.TITLE;
  108. res1.data.data.DOCSOURCENAME = res1.data.data.ORGANCAT_DISP;
  109. }
  110. res1.data.data.DOCPUBTIME = res1.data.data.DOCPUBTIME.split(" ")[0];
  111. res1.data.data.DOCHTMLCON = res1.data.data.DOCHTMLCON.replace(/\<span/gi, '<span class="fontSize"');
  112. // let text = res1.data.data.DOCHTMLCON.replace(/<[^>]+>/g, "");
  113. res1.data.data.DOCHTMLCON = res1.data.data.DOCHTMLCON.replace(/<img [^>]*src="([^'"]+)[^>]*>/gi,
  114. function (match, capture) {
  115. // 替换小图片地址
  116. if (capture.split("/").length > 1) {
  117. return match.replace(/src=['"]([^'"]+)[^>]/gi, 'src="' + 'https://www.cqna.gov.cn' +
  118. capture +
  119. '"');
  120. }
  121. return match.replace(/src=['"]([^'"]+)[^>]/gi, 'src="' + url +
  122. capture +
  123. '"');
  124. });
  125. if (res1.data.data.DOCATTACHFILEFIELD.length > 0) {
  126. this.setData({
  127. fileList: res1.data.data.DOCATTACHFILEFIELD
  128. })
  129. } else {
  130. this.getAhrefs(res1.data.data.DOCHTMLCON);
  131. }
  132. this.setData({
  133. fileUrlPrefix: url,
  134. articles: res1.data.data
  135. })
  136. }
  137. })
  138. }
  139. })
  140. } else {
  141. wx.request({
  142. url: 'https://data.cqna.gov.cn/mini/hlw/info?channelId=' + options.channelId + '&docId=' + options.docId,
  143. method: 'GET',
  144. success: res => {
  145. res.data.data.DOCPUBTIME = res.data.data.DOCPUBTIME.split(" ")[0];
  146. res.data.data.DOCHTMLCON = res.data.data.DOCHTMLCON.replace(/\<span/gi, '<span class="fontSize"');
  147. res.data.data.DOCHTMLCON = res.data.data.DOCHTMLCON.replace(/<img [^>]*src="([^'"]+)[^>]*>/gi,
  148. function (match, capture) {
  149. // 替换小图片地址
  150. if (capture.split("/").length > 1) {
  151. return match.replace(/src=['"]([^'"]+)[^>]/gi, 'src="' + 'https://www.cqna.gov.cn' +
  152. capture +
  153. '"');
  154. }
  155. return match.replace(/src=['"]([^'"]+)[^>]/gi, 'src="' + options.url +
  156. capture +
  157. '"');
  158. });
  159. if (res.data.data.DOCATTACHFILEFIELD.length > 0) {
  160. this.setData({
  161. fileList: res.data.data.DOCATTACHFILEFIELD
  162. })
  163. } else {
  164. this.getAhrefs(res.data.data.DOCHTMLCON);
  165. }
  166. this.setData({
  167. fileUrlPrefix: options.url,
  168. articles: res.data.data
  169. })
  170. }
  171. })
  172. }
  173. },
  174. audioPlay() {
  175. if (!this.data.isPlay) {
  176. let that = this;
  177. that.innerAudioContext = wx.createInnerAudioContext({
  178. useWebAudioImplement: false
  179. })
  180. that.innerAudioContext.src = 'https://www.cqna.gov.cn/voice_assistant/2022-11-28/a05-60ff109e2a32_10281519.mp3';
  181. that.innerAudioContext.play()
  182. } else {
  183. this.innerAudioContext.pause();
  184. }
  185. this.setData({
  186. isPlay: !this.data.isPlay
  187. })
  188. },
  189. downloadFile(e) {
  190. wx.downloadFile({
  191. url: this.data.fileUrlPrefix + e.currentTarget.dataset.url,
  192. success: function (res) {
  193. if (res.statusCode === 200) {
  194. let filename = res.tempFilePath;
  195. let fileType = filename.split('.').pop();
  196. wx.openDocument({
  197. filePath: res.tempFilePath,
  198. fileType: fileType,
  199. showMenu: true, // 关键,这里开启预览页面的右上角菜单,才能另存为
  200. });
  201. } else {
  202. wx.showToast({
  203. title: '失败',
  204. });
  205. }
  206. },
  207. });
  208. },
  209. getAhrefs(content) {
  210. let fileTemp = [];
  211. let temp;
  212. let regex = /<a[^>]*href=['"]([^"]*)['"][^>]*>(.*?)<\/a>/g;
  213. while (regex.exec(content) != null) {
  214. temp = {
  215. APPDESC: RegExp.$2,
  216. APPFILE: RegExp.$1.substring(RegExp.$1.lastIndexOf('/') + 1, RegExp.$1.length)
  217. }
  218. }
  219. fileTemp.push(temp);
  220. this.setData({
  221. fileList: fileTemp
  222. })
  223. },
  224. /**
  225. * 生命周期函数--监听页面初次渲染完成
  226. */
  227. onReady() {
  228. },
  229. /**
  230. * 生命周期函数--监听页面显示
  231. */
  232. onShow() {
  233. },
  234. /**
  235. * 生命周期函数--监听页面隐藏
  236. */
  237. onHide() {
  238. if (this.innerAudioContext) {
  239. this.innerAudioContext.stop(); //暂停音频
  240. }
  241. },
  242. /**
  243. * 生命周期函数--监听页面卸载
  244. */
  245. onUnload() {
  246. if (this.innerAudioContext) {
  247. this.innerAudioContext.stop(); //暂停音频
  248. }
  249. },
  250. /**
  251. * 页面相关事件处理函数--监听用户下拉动作
  252. */
  253. onPullDownRefresh() {
  254. },
  255. /**
  256. * 页面上拉触底事件的处理函数
  257. */
  258. onReachBottom() {
  259. },
  260. /**
  261. * 用户点击右上角分享
  262. */
  263. onShareAppMessage() {
  264. return {
  265. title: this.data.articles.DOCTITLE
  266. };
  267. }
  268. })