index.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. <rich-text :content="content"></rich-text>
  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 RichText from '@/components/RichText/index.vue';
  32. import { authLogin, getUserInfo } from '@/util';
  33. export default {
  34. name: 'content',
  35. components: { RichText },
  36. props: {},
  37. data() {
  38. return {
  39. CONTENT_TYPE,
  40. content: '',
  41. queryParams: {},
  42. title: '',
  43. date: '',
  44. contentType: '',
  45. likeCnt: 0,
  46. collectCnt: 0,
  47. likeFlag: false,
  48. collectFlag: false,
  49. id: '',
  50. };
  51. },
  52. created() {
  53. },
  54. onLoad(res) {
  55. // 登录返回之后的页面
  56. if (res) {
  57. this.queryParams = res;
  58. }
  59. this.getContentInfo();
  60. this.getUsrContent();
  61. },
  62. methods: {
  63. async getContentInfo() {
  64. const {
  65. data: {
  66. content,
  67. title,
  68. createTime,
  69. contentType,
  70. id,
  71. likeCnt,
  72. collectedCnt,
  73. },
  74. } = await fetchContentDetail(this.queryParams.id);
  75. this.content = content;
  76. this.title = title;
  77. this.contentType = contentType;
  78. this.id = id;
  79. this.likeCnt = likeCnt;
  80. this.collectCnt = collectedCnt;
  81. uni.setNavigationBarTitle({
  82. title
  83. })
  84. this.date = uni.$u.timeFormat(createTime, 'yyyy-mm-dd');
  85. },
  86. async getUsrContent() {
  87. const contentId = this.queryParams.id;
  88. const usrInfo = getUserInfo();
  89. if (usrInfo && usrInfo.openId) {
  90. const { data } = await fetchUsrContent(contentId, usrInfo.openId);
  91. if (data) {
  92. this.collectFlag = data[0]?.collectFlag === '0';
  93. this.likeFlag = data[0]?.likeFlag === '0';
  94. }
  95. }
  96. },
  97. onCollectClick() {
  98. this.collectFlag = !this.collectFlag;
  99. authLogin(async (usr) => {
  100. if (this.collectFlag) {
  101. this.collectCnt += 1;
  102. await usrCollect({
  103. id: this.queryParams.id,
  104. });
  105. return;
  106. }
  107. this.collectCnt -= 1;
  108. await usrUnCollect({
  109. id: this.queryParams.id,
  110. });
  111. });
  112. },
  113. onLikeClick() {
  114. this.likeFlag = !this.likeFlag;
  115. authLogin(async (usr) => {
  116. if (this.likeFlag) {
  117. this.likeCnt += 1;
  118. await usrLike({
  119. id: this.queryParams.id,
  120. });
  121. return;
  122. }
  123. this.likeCnt -= 1;
  124. await usrUnLike({
  125. id: this.queryParams.id,
  126. });
  127. });
  128. },
  129. },
  130. };
  131. </script>
  132. <style lang="scss" src="./index.scss" />;