governmentArticlesDetail.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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) {
  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.DOCTITLE = res.data.data.DOCTITLE ? res.data.data.DOCTITLE : res.data.data.TITLE;
  146. res.data.data.DOCPUBTIME = res.data.data.DOCPUBTIME.split(" ")[0];
  147. res.data.data.DOCHTMLCON = res.data.data.DOCHTMLCON?res.data.data.DOCHTMLCON:res.data.data.CONTENT;
  148. res.data.data.DOCHTMLCON = res.data.data.DOCHTMLCON.replace(/\<span/gi, '<span class="fontSize"');
  149. res.data.data.DOCHTMLCON = res.data.data.DOCHTMLCON.replace(/<img [^>]*src="([^'"]+)[^>]*>/gi,
  150. function (match, capture) {
  151. // 替换小图片地址
  152. if (capture.split("/").length > 1) {
  153. return match.replace(/src=['"]([^'"]+)[^>]/gi, 'src="' + 'https://www.cqna.gov.cn' +
  154. capture +
  155. '"');
  156. }
  157. return match.replace(/src=['"]([^'"]+)[^>]/gi, 'src="' + options.url +
  158. capture +
  159. '"');
  160. });
  161. if (res.data.data.DOCATTACHFILEFIELD.length > 0) {
  162. this.setData({
  163. fileList: res.data.data.DOCATTACHFILEFIELD
  164. })
  165. } else {
  166. this.getAhrefs(res.data.data.DOCHTMLCON);
  167. }
  168. this.setData({
  169. fileUrlPrefix: options.url,
  170. articles: res.data.data
  171. })
  172. }
  173. })
  174. }
  175. },
  176. audioPlay() {
  177. if (!this.data.isPlay) {
  178. let that = this;
  179. that.innerAudioContext = wx.createInnerAudioContext({
  180. useWebAudioImplement: false
  181. })
  182. that.innerAudioContext.src = 'https://www.cqna.gov.cn/voice_assistant/2022-11-28/a05-60ff109e2a32_10281519.mp3';
  183. that.innerAudioContext.play()
  184. } else {
  185. this.innerAudioContext.pause();
  186. }
  187. this.setData({
  188. isPlay: !this.data.isPlay
  189. })
  190. },
  191. downloadFile(e) {
  192. wx.downloadFile({
  193. url: this.data.fileUrlPrefix + e.currentTarget.dataset.url,
  194. success: function (res) {
  195. if (res.statusCode === 200) {
  196. let filename = res.tempFilePath;
  197. let fileType = filename.split('.').pop();
  198. wx.openDocument({
  199. filePath: res.tempFilePath,
  200. fileType: fileType,
  201. showMenu: true, // 关键,这里开启预览页面的右上角菜单,才能另存为
  202. });
  203. } else {
  204. wx.showToast({
  205. title: '失败',
  206. });
  207. }
  208. },
  209. });
  210. },
  211. getAhrefs(content) {
  212. let fileTemp = [];
  213. let temp;
  214. let regex = /<a[^>]*href=['"]([^"]*)['"][^>]*>(.*?)<\/a>/g;
  215. while (regex.exec(content) != null) {
  216. temp = {
  217. APPDESC: RegExp.$2,
  218. APPFILE: RegExp.$1.substring(RegExp.$1.lastIndexOf('/') + 1, RegExp.$1.length)
  219. }
  220. }
  221. fileTemp.push(temp);
  222. this.setData({
  223. fileList: fileTemp
  224. })
  225. },
  226. /**
  227. * 生命周期函数--监听页面初次渲染完成
  228. */
  229. onReady() {
  230. },
  231. /**
  232. * 生命周期函数--监听页面显示
  233. */
  234. onShow() {
  235. },
  236. /**
  237. * 生命周期函数--监听页面隐藏
  238. */
  239. onHide() {
  240. if (this.innerAudioContext) {
  241. this.innerAudioContext.stop(); //暂停音频
  242. }
  243. },
  244. /**
  245. * 生命周期函数--监听页面卸载
  246. */
  247. onUnload() {
  248. if (this.innerAudioContext) {
  249. this.innerAudioContext.stop(); //暂停音频
  250. }
  251. },
  252. /**
  253. * 页面相关事件处理函数--监听用户下拉动作
  254. */
  255. onPullDownRefresh() {
  256. },
  257. /**
  258. * 页面上拉触底事件的处理函数
  259. */
  260. onReachBottom() {
  261. },
  262. /**
  263. * 用户点击右上角分享
  264. */
  265. onShareAppMessage() {
  266. return {
  267. title: this.data.articles.DOCTITLE
  268. };
  269. }
  270. })