index.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <template>
  2. <view class="content-info-container">
  3. <view class="title-container">
  4. <view class="title-content-text">{{ title }}</view>
  5. <view class="title-content-date">{{ date }}</view>
  6. </view>
  7. <mp-html :content="content|formatRichText" selectable="true" show-img-menu="true" />
  8. <view class="content-like-col" v-if="CONTENT_TYPE.news === contentType">
  9. <view class="icon-wrap">
  10. <u-icon
  11. :name="`${likeFlag ? 'thumb-up-fill':'thumb-up'}`"
  12. class="icon-base"
  13. @click="$u.throttle(onLikeClick,500)"
  14. ></u-icon>
  15. {{ likeCnt && likeCnt }}
  16. </view>
  17. <view class="icon-wrap">
  18. <u-icon
  19. :name="`${collectFlag ? 'star-fill':'star' }`"
  20. class="icon-base"
  21. @click="$u.throttle(onCollectClick,500)"
  22. ></u-icon>
  23. {{ collectCnt && collectCnt }}
  24. </view>
  25. </view>
  26. </view>
  27. </template>
  28. <script>
  29. import { fetchContentDetail, fetchUsrContent, usrCollect, usrLike, usrUnCollect, usrUnLike } from '@/common/api';
  30. import { CONTENT_TYPE } from '@/common/EnumConst';
  31. import { authLogin, getUserInfo } from '@/util';
  32. export default {
  33. name: 'content',
  34. props: {},
  35. data() {
  36. return {
  37. CONTENT_TYPE,
  38. content: '',
  39. queryParams: {},
  40. title: '',
  41. date: '',
  42. contentType: '',
  43. likeCnt: 0,
  44. collectCnt: 0,
  45. likeFlag: false,
  46. collectFlag: false,
  47. id: '',
  48. };
  49. },
  50. created() {
  51. },
  52. onLoad(res) {
  53. // 登录返回之后的页面
  54. if (res) {
  55. this.queryParams = res;
  56. }
  57. this.getContentInfo();
  58. this.getUsrContent();
  59. },
  60. filters: {
  61. /**
  62. * 处理富文本里的图片宽度自适应
  63. * 1.去掉img标签里的style、width、height属性
  64. * 2.img标签添加style属性:max-width:100%;height:auto
  65. * 3.修改所有style里的width属性为max-width:100%
  66. * 4.去掉<br/>标签
  67. * @param html
  68. * @returns {void|string|*}
  69. *
  70. */
  71. formatRichText(html) { //控制小程序中图片大小
  72. let newContent = html.replace(/<img[^>]*>/gi, function (match, capture) {
  73. match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
  74. match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
  75. match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
  76. return match;
  77. });
  78. newContent = newContent.replace(/style="[^"]+"/gi, function (match, capture) {
  79. match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi,
  80. 'max-width:100%;');
  81. return match;
  82. });
  83. newContent = newContent.replace(/<br[^>]*\/>/gi, '');
  84. newContent = newContent.replace(/<img src="\/dev-api/gi,
  85. `<img style="max-width:100%;height:auto;display:inline-block;margin:10rpx auto;" src="${process.env.UNI_FILE_PREFIX}`);
  86. newContent = newContent.replace(/<iframe(([\s\S])*?)<\/iframe>/ig, function (match, capture) {
  87. match = match.replace('iframe', 'video');
  88. match = match.replace(/<video/gi,
  89. '<video style="width:100%;height:auto;display:inline-block;margin:10rpx auto;"')
  90. .replace(/style='[^']+'/gi, 'style=\'max-width:100%;height:auto;display:inline-block;margin:10rpx auto;\'');
  91. match = match.replace(/src="/gi, `src="${process.env.UNI_FILE_PREFIX}`)
  92. .replace(/src='/gi, `src='${process.env.UNI_FILE_PREFIX}`);
  93. return match;
  94. });
  95. return newContent;
  96. },
  97. },
  98. methods: {
  99. async getContentInfo() {
  100. const {
  101. data: {
  102. content,
  103. title,
  104. createTime,
  105. contentType,
  106. id,
  107. likeCnt,
  108. collectedCnt,
  109. },
  110. } = await fetchContentDetail(this.queryParams.id);
  111. this.content = content;
  112. this.title = title;
  113. this.contentType = contentType;
  114. this.id = id;
  115. this.likeCnt = likeCnt;
  116. this.collectCnt = collectedCnt;
  117. uni.setNavigationBarTitle({
  118. title
  119. })
  120. this.date = uni.$u.timeFormat(createTime, 'yyyy-mm-dd');
  121. },
  122. async getUsrContent() {
  123. const contentId = this.queryParams.id;
  124. const usrInfo = getUserInfo();
  125. if (usrInfo && usrInfo.openId) {
  126. const { data } = await fetchUsrContent(contentId, usrInfo.openId);
  127. if (data) {
  128. this.collectFlag = data[0]?.collectFlag === '0';
  129. this.likeFlag = data[0]?.likeFlag === '0';
  130. }
  131. }
  132. },
  133. onCollectClick() {
  134. this.collectFlag = !this.collectFlag;
  135. authLogin(async (usr) => {
  136. if (this.collectFlag) {
  137. this.collectCnt += 1;
  138. await usrCollect({
  139. id: this.queryParams.id,
  140. });
  141. return;
  142. }
  143. this.collectCnt -= 1;
  144. await usrUnCollect({
  145. id: this.queryParams.id,
  146. });
  147. });
  148. },
  149. onLikeClick() {
  150. this.likeFlag = !this.likeFlag;
  151. authLogin(async (usr) => {
  152. if (this.likeFlag) {
  153. this.likeCnt += 1;
  154. await usrLike({
  155. id: this.queryParams.id,
  156. });
  157. return;
  158. }
  159. this.likeCnt -= 1;
  160. await usrUnLike({
  161. id: this.queryParams.id,
  162. });
  163. });
  164. },
  165. },
  166. };
  167. </script>
  168. <style lang="scss" src="./index.scss" />;