articleDetail.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. <template>
  2. <view class="page">
  3. <!-- 引入FontAwesome -->
  4. <view>
  5. <link rel="stylesheet"
  6. href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  7. </view>
  8. <!-- 文章头图区域 -->
  9. <view class="topUser" v-if="articleInfo.type == 'user'">
  10. <image :src="home_image" class="home_image" mode="aspectFill"></image>
  11. <!-- 图片指示器 -->
  12. <view class="image-indicator" v-if="image_list.length > 1">
  13. <text>{{ selImg + 1 }}/{{ image_list.length }}</text>
  14. </view>
  15. <!-- 缩略图列表 -->
  16. <view class="list">
  17. <view class="img" :class="selImg == index ? 'active' : ''" v-for="(item, index) in image_list" :key="index"
  18. @click="selPhoto(item, index)">
  19. <image :src="item" mode="aspectFill"></image>
  20. </view>
  21. </view>
  22. </view>
  23. <!-- 文章内容区域 -->
  24. <view class="body">
  25. <!-- 文章标题与元信息 -->
  26. <view class="article-header">
  27. <view class="title">
  28. {{ articleInfo.title || '暂无标题' }}
  29. </view>
  30. <view class="meta-info">
  31. <view class="meta-item">
  32. <text class="fa fa-calendar"></text>
  33. <text class="meta-text">{{ articleInfo.create_time || '暂无时间' }}</text>
  34. </view>
  35. <view class="meta-item">
  36. <text class="fa fa-eye"></text>
  37. <text class="meta-text">{{ articleInfo.num_view || 0 }}次阅读</text>
  38. </view>
  39. <view class="meta-item" v-if="articleInfo.author">
  40. <text class="fa fa-user"></text>
  41. <text class="meta-text">{{ articleInfo.author }}</text>
  42. </view>
  43. </view>
  44. </view>
  45. <!-- 内容分隔线 -->
  46. <view class="divider"></view>
  47. <!-- 文章内容 -->
  48. <!-- <view class="article-content" v-if="articleInfo.type == 'user'">
  49. <view class="content">{{ articleInfo.content || '暂无内容' }}</view>
  50. </view> -->
  51. <view class="article-content">
  52. <view class="content">
  53. <rich-text :nodes="content" style="font-size: 14px;"></rich-text>
  54. </view>
  55. </view>
  56. <!-- 文章底部区域 -->
  57. <view class="article-footer">
  58. <view class="action-bar">
  59. <view class="action-item" @tap="likeArticle">
  60. <text class="fa" :class="articleInfo.is_like ? 'fa-thumbs-up liked' : 'fa-thumbs-o-up'"></text>
  61. <text class="action-text">{{ articleInfo.like_count || 0 }}</text>
  62. </view>
  63. <view class="action-item" @tap="openComment">
  64. <text class="fa fa-comment-o"></text>
  65. <text class="action-text">{{ tableTotal || 0 }}</text>
  66. </view>
  67. <view class="action-item" @tap="shareArticle">
  68. <text class="fa fa-share-alt"></text>
  69. <text class="action-text">分享</text>
  70. </view>
  71. </view>
  72. </view>
  73. <!-- 空白占位 -->
  74. <view class="list_info">
  75. <view class="blankHeight"></view>
  76. </view>
  77. </view>
  78. <!-- <view class="btn_submit" @click="chatTA()">
  79. <image class="icon" src="../../static/icon/icon_chat_white.png" mode="widthFix"></image>
  80. 私聊
  81. </view> -->
  82. <view class="thread2"></view>
  83. <view class="thread2"></view>
  84. <previewImage ref="previewImage" :opacity="0.8" :circular="true" :imgs="imgs" :descs="descs"></previewImage>
  85. <!-- 评论区域 -->
  86. <CommentSection ref="commentSection" @totalNumberOfComments="totalNumberOfComments" :myInfo="myInfo" :userInfo="userInfo" :articleId="arcID" :type="'article'">
  87. </CommentSection>
  88. <view class="thread2"></view>
  89. </view>
  90. </template>
  91. <script>
  92. import htmlParser from '../../common/html-parser'
  93. import CommentSection from "@/components/CommentSection/CommentSection.vue";
  94. import previewImage from '@/components/kxj-previewImage/kxj-previewImage.vue'; //引用插件
  95. function parseImgs(nodes) {
  96. nodes.forEach(node => {
  97. if (
  98. node.name === 'img' &&
  99. node.attrs &&
  100. node.attrs['data-img-size-val']
  101. ) {
  102. const sizes = node.attrs['data-img-size-val'].split(',')
  103. const width = uni.upx2px(720 * 0.9)
  104. const height = parseInt(width * (sizes[1] / sizes[0]))
  105. node.attrs.style = `width:${width};height:${height};`
  106. }
  107. if (Array.isArray(node.children)) {
  108. parseImgs(node.children)
  109. }
  110. })
  111. return nodes
  112. }
  113. export default {
  114. components: {
  115. previewImage,
  116. CommentSection
  117. },
  118. data() {
  119. return {
  120. title: '',
  121. arcID: 0,
  122. selImg: 0,
  123. home_image: '',
  124. myinfo: {},
  125. tag_list: [],
  126. image_list: [],
  127. imgs: [],
  128. descs: [],
  129. list_wish: [],
  130. content: '',
  131. tableTotal: 0,
  132. // 添加文章信息字段
  133. articleInfo: {
  134. title: '',
  135. content: '',
  136. create_time: '',
  137. images: '',
  138. view_count: 0,
  139. author: '',
  140. like_count: 0,
  141. is_like: false
  142. },
  143. myInfo: {
  144. user_id: 1, // 用户id
  145. user_name: "", // 用户名
  146. user_avatar: "", // 用户头像地址
  147. },
  148. // 文章作者信息(提示: 一般来自localstorage, 如果是实时获取的话, 那么获取到数据后再v-if显示评论组件)
  149. userInfo: {
  150. user_id: 2, // 用户id
  151. user_name: "", // 用户名
  152. user_avatar: "", // 用户头像地址
  153. },
  154. }
  155. },
  156. onLoad(parms) {
  157. let self = this;
  158. this.arcID = parms.id || 133;
  159. },
  160. onShow() {
  161. uni.$emit('check_update');
  162. this.$nextTick(() => {
  163. if (this.$refs.commentSection) {
  164. this.$refs.commentSection.loadCommentData();
  165. }
  166. });
  167. this.loadData();
  168. },
  169. methods: {
  170. totalNumberOfComments(tableTotal) {
  171. this.tableTotal = tableTotal;
  172. },
  173. onLinqu(item) {
  174. uni.navigateTo({
  175. url: '/pages/my/wishHelp?id=' + item.myid,
  176. })
  177. },
  178. selPhoto(item, sel) {
  179. this.selImg = sel;
  180. this.home_image = this.image_list[sel];
  181. },
  182. toArr(imgs) {
  183. let arr = imgs.split("|");
  184. return arr;
  185. },
  186. previewOpen(imgs1, index) {
  187. this.imgs = imgs1.split("|");
  188. setTimeout(() => this.$refs.previewImage.open(index), 0)
  189. return; //如需测试和uni原生预览差别可注释这两行
  190. },
  191. loadData() {
  192. uni.request({
  193. url: this.$apiHost + '/Article/getinfo',
  194. data: {
  195. uuid: getApp().globalData.uuid,
  196. id: this.arcID
  197. },
  198. header: {
  199. "content-type": "application/json",
  200. 'sign': getApp().globalData.headerSign
  201. },
  202. success: (res) => {
  203. console.log("文章信息:", res.data);
  204. if (res.data.success === "yes") {
  205. // 更新文章信息
  206. this.articleInfo = res.data.article;
  207. const nodes = htmlParser(res.data.article.content);
  208. // #ifdef APP-PLUS-NVUE
  209. parseImgs(nodes)
  210. // #endif
  211. this.content = nodes
  212. // 更新图片列表
  213. if (res.data.article.images && res.data.article.images !== "") {
  214. this.image_list = res.data.article.images.split(",");
  215. this.home_image = this.image_list[0];
  216. } else {
  217. this.home_image = "../../static/home/avator.png";
  218. }
  219. console.log("img", this.home_image);
  220. // 更新标签列表
  221. // if (res.data.data.tags && res.data.data.tags !== "") {
  222. // this.tag_list = res.data.data.tags.split(",");
  223. // }
  224. } else {
  225. uni.showToast({
  226. title: '获取文章信息失败',
  227. icon: 'none'
  228. });
  229. }
  230. },
  231. complete: (com) => {
  232. // uni.hideLoading();
  233. },
  234. fail: (e) => {
  235. console.log("请求失败:", e);
  236. uni.showToast({
  237. title: '网络请求失败',
  238. icon: 'none'
  239. });
  240. }
  241. });
  242. },
  243. // 评论相关方法已移至CommentSection组件
  244. // 唤起新评论弹框
  245. openComment() {
  246. if (this.$refs.commentSection) {
  247. this.$refs.commentSection.openComment();
  248. }
  249. },
  250. // 文章点赞
  251. likeArticle() {
  252. uni.request({
  253. url: this.$apiHost + '/Article/like',
  254. data: {
  255. uuid: getApp().globalData.uuid,
  256. id: this.arcID
  257. },
  258. header: {
  259. "content-type": "application/json",
  260. 'sign': getApp().globalData.headerSign
  261. },
  262. success: (res) => {
  263. console.log("点赞结果:", res.data);
  264. if (res.data.success === "yes") {
  265. // 更新点赞状态
  266. if (!this.articleInfo.is_like) {
  267. this.articleInfo.like_count = (this.articleInfo.like_count || 0) + 1;
  268. this.articleInfo.is_like = true;
  269. uni.showToast({
  270. title: '点赞成功',
  271. icon: 'none'
  272. });
  273. } else {
  274. this.articleInfo.like_count = (this.articleInfo.like_count || 0) - 1;
  275. this.articleInfo.is_like = false;
  276. uni.showToast({
  277. title: '取消点赞',
  278. icon: 'none'
  279. });
  280. }
  281. } else {
  282. uni.showToast({
  283. title: '点赞失败',
  284. icon: 'none'
  285. });
  286. }
  287. },
  288. fail: (e) => {
  289. console.log("点赞失败:", e);
  290. uni.showToast({
  291. title: '网络请求失败',
  292. icon: 'none'
  293. });
  294. }
  295. });
  296. },
  297. // 分享文章
  298. shareArticle() {
  299. // 如果在微信小程序环境
  300. if (uni.getSystemInfoSync().platform === 'mp-weixin') {
  301. uni.showShareMenu({
  302. withShareTicket: true,
  303. menus: ['shareAppMessage', 'shareTimeline']
  304. });
  305. } else {
  306. // 其他环境,如APP
  307. uni.share({
  308. provider: "weixin",
  309. scene: "WXSceneSession",
  310. type: 0,
  311. title: this.articleInfo.title,
  312. summary: this.articleInfo.content.substring(0, 40) + "...",
  313. imageUrl: this.home_image,
  314. success: function (res) {
  315. console.log("分享成功:" + JSON.stringify(res));
  316. },
  317. fail: function (err) {
  318. console.log("分享失败:" + JSON.stringify(err));
  319. }
  320. });
  321. }
  322. },
  323. // 评论回调事件
  324. replyFun({
  325. params
  326. }, callback) {
  327. // params = {
  328. // ...params,
  329. // user_id: this.myInfo.user_id, // 用户id
  330. // user_name: this.myInfo.user_name, // 用户名
  331. // user_avatar: this.myInfo.user_avatar, // 用户头像地址
  332. // user_content: this.commentValue, // 用户评论内容
  333. // is_like: false, // 是否点赞
  334. // like_count: 0, // 点赞数统计
  335. // create_time: "刚刚", // 创建时间
  336. // owner: true, // 是否为所有者 所有者可以进行删除 管理员默认true
  337. // };
  338. console.log("replyFun", {
  339. uuid: getApp().globalData.uuid,
  340. article_id: this.arcID, // 文章ID
  341. content: params.user_content, // 评论内容
  342. parent_id: params.parent_id || 0, // 父评论ID
  343. reply_id: params.reply_id || 0, // 回复的评论ID
  344. reply_name: params.reply_name || '' // 被回复人名称
  345. });
  346. uni.request({
  347. url: this.$apiHost + '/Article/newComment',
  348. data: {
  349. uuid: getApp().globalData.uuid,
  350. type: 'article',
  351. article_id: this.arcID, // 文章ID
  352. content: params.user_content, // 评论内容
  353. parent_id: params.parent_id || 0, // 父评论ID
  354. reply_id: params.reply_id || 0, // 回复的评论ID
  355. reply_name: params.reply_name || '' // 被回复人名称
  356. },
  357. header: {
  358. "content-type": "application/json",
  359. 'sign': getApp().globalData.headerSign
  360. },
  361. success: (res) => {
  362. console.log("评论结果:", res.data);
  363. if (res.data.success === "yes") {
  364. callback(res.data); // 评论成功,传入后端返回的数据
  365. }
  366. },
  367. fail: (e) => {
  368. console.log("评论失败:", e);
  369. uni.showToast({
  370. title: '评论失败,请重试',
  371. icon: 'none'
  372. });
  373. }
  374. });
  375. },
  376. /** 删除回调事件
  377. * mode 删除模式
  378. * -- bind: 当被删除的一级评论存在回复评论, 那么该评论内容变更显示为[当前评论内容已被移除]
  379. * -- only: 仅删除当前评论(后端删除相关联的回复评论, 否则总数显示不对)
  380. * -- all : 删除所有评论包括回复评论 前端遍历子评论上报
  381. */
  382. deleteFun({
  383. params,
  384. mode
  385. }, callback) {
  386. console.log("deleteFun", {
  387. params,
  388. mode
  389. });
  390. // 将params转换为逗号分隔的字符串
  391. const idsString = Array.isArray(params) ? params.join(',') : params.toString();
  392. console.log("删除评论", idsString, mode)
  393. uni.request({
  394. url: this.$apiHost + '/Article/delComment',
  395. data: {
  396. uuid: getApp().globalData.uuid,
  397. ids: idsString, // 将params转换为逗号分隔的字符串传递给ids
  398. mode: mode
  399. },
  400. header: {
  401. "content-type": "application/json",
  402. 'sign': getApp().globalData.headerSign
  403. },
  404. success: (res) => {
  405. console.log("删除结果:", res.data);
  406. if (res.data.success === "yes") {
  407. callback(res);
  408. }
  409. },
  410. fail: (e) => { }
  411. });
  412. // switch (mode) {
  413. // case "bind":
  414. // // 逻辑: 调用接口进行评论内容修改 update
  415. // setTimeout(() => callback(), 500); // 目前为了展示效果, 直接执行callback
  416. // break;
  417. // case "only":
  418. // // 逻辑: 调用接口删除一个评论 delete
  419. // setTimeout(() => callback(), 500); // 目前为了展示效果, 直接执行callback
  420. // break;
  421. // default:
  422. // // all
  423. // // 逻辑: 调用接口删除多个评论 [delete]
  424. // setTimeout(() => callback(), 500); // 目前为了展示效果, 直接执行callback
  425. // break;
  426. // }
  427. },
  428. }
  429. }
  430. </script>
  431. <style scoped lang="scss">
  432. @import 'articleDetail.scss';
  433. </style>