articleDetail.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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. <view class="comment-section">
  87. <view class="section-header">
  88. <text class="fa fa-comments"></text>
  89. <text class="section-title">评论区</text>
  90. <text class="comment-count">({{ tableTotal }})</text>
  91. </view>
  92. <CComment ref="ccRef" :myInfo="myInfo" :userInfo="userInfo" :tableData="tableData"
  93. :tableTotal.sync="tableTotal" :deleteMode="deleteMode" @likeFun="likeFun" @replyFun="replyFun"
  94. @deleteFun="deleteFun"></CComment>
  95. <view class="comment-button" @tap="openComment">
  96. <text class="fa fa-pencil"></text>
  97. <text>发表新评论</text>
  98. </view>
  99. </view>
  100. <view class="thread2"></view>
  101. </view>
  102. </template>
  103. <script>
  104. import htmlParser from '../../common/html-parser'
  105. import CComment from "@/components/cc-comment/cc-comment.vue";
  106. import previewImage from '@/components/kxj-previewImage/kxj-previewImage.vue'; //引用插件
  107. function parseImgs(nodes) {
  108. nodes.forEach(node => {
  109. if (
  110. node.name === 'img' &&
  111. node.attrs &&
  112. node.attrs['data-img-size-val']
  113. ) {
  114. const sizes = node.attrs['data-img-size-val'].split(',')
  115. const width = uni.upx2px(720 * 0.9)
  116. const height = parseInt(width * (sizes[1] / sizes[0]))
  117. node.attrs.style = `width:${width};height:${height};`
  118. }
  119. if (Array.isArray(node.children)) {
  120. parseImgs(node.children)
  121. }
  122. })
  123. return nodes
  124. }
  125. export default {
  126. components: {
  127. previewImage,
  128. CComment
  129. },
  130. data() {
  131. return {
  132. title: '',
  133. arcID: 0,
  134. selImg: 0,
  135. home_image: '',
  136. myinfo: {},
  137. tag_list: [],
  138. image_list: [],
  139. imgs: [],
  140. descs: [],
  141. list_wish: [],
  142. content: '',
  143. // 添加文章信息字段
  144. articleInfo: {
  145. title: '',
  146. content: '',
  147. create_time: '',
  148. images: '',
  149. view_count: 0,
  150. author: '',
  151. like_count: 0,
  152. is_like: false
  153. },
  154. myInfo: {
  155. user_id: 1, // 用户id
  156. user_name: "cc", // 用户名
  157. user_avatar: "https://img0.baidu.com/it/u=2836960144,3650263035&fm=253&fmt=auto&app=138&f=JPEG?w=474&h=474", // 用户头像地址
  158. },
  159. // 文章作者信息(提示: 一般来自localstorage, 如果是实时获取的话, 那么获取到数据后再v-if显示评论组件)
  160. userInfo: {
  161. user_id: 2, // 用户id
  162. user_name: "ikun", // 用户名
  163. user_avatar: "https://pic1.zhimg.com/80/v2-a79071a705f55c5d88f6c74e6111fe84_720w.webp", // 用户头像地址
  164. },
  165. deleteMode: "all", //删除模式
  166. // 评论总数
  167. tableTotal: 4,
  168. // 评论表
  169. tableData: [],
  170. }
  171. },
  172. onLoad(parms) {
  173. let self = this;
  174. this.arcID = parms.id || 132;
  175. },
  176. onShow() {
  177. uni.$emit('check_update');
  178. this.loadData();
  179. this.loadCommentData();
  180. },
  181. methods: {
  182. onLinqu(item) {
  183. uni.navigateTo({
  184. url: '/pages/my/wishHelp?id=' + item.myid,
  185. })
  186. },
  187. selPhoto(item, sel) {
  188. this.selImg = sel;
  189. this.home_image = this.image_list[sel];
  190. },
  191. toArr(imgs) {
  192. let arr = imgs.split("|");
  193. return arr;
  194. },
  195. previewOpen(imgs1, index) {
  196. this.imgs = imgs1.split("|");
  197. setTimeout(() => this.$refs.previewImage.open(index), 0)
  198. // 传入当前选中的图片地址或序号
  199. return; //如需测试和uni原生预览差别可注释这两行
  200. },
  201. loadData() {
  202. uni.request({
  203. url: this.$apiHost + '/Article/getinfo',
  204. data: {
  205. uuid: getApp().globalData.uuid,
  206. id: this.arcID
  207. },
  208. header: {
  209. "content-type": "application/json",
  210. 'sign': getApp().globalData.headerSign
  211. },
  212. success: (res) => {
  213. console.log("文章信息:", res.data);
  214. if (res.data.success === "yes") {
  215. // 更新文章信息
  216. this.articleInfo = res.data.article;
  217. const nodes = htmlParser(res.data.article.content);
  218. // #ifdef APP-PLUS-NVUE
  219. parseImgs(nodes)
  220. // #endif
  221. this.content = nodes
  222. // 更新图片列表
  223. if (res.data.article.images && res.data.article.images !== "") {
  224. this.image_list = res.data.article.images.split(",");
  225. this.home_image = this.image_list[0];
  226. } else {
  227. this.home_image = "../../static/home/avator.png";
  228. }
  229. // 更新标签列表
  230. // if (res.data.data.tags && res.data.data.tags !== "") {
  231. // this.tag_list = res.data.data.tags.split(",");
  232. // }
  233. } else {
  234. uni.showToast({
  235. title: '获取文章信息失败',
  236. icon: 'none'
  237. });
  238. }
  239. },
  240. complete: (com) => {
  241. // uni.hideLoading();
  242. },
  243. fail: (e) => {
  244. console.log("请求失败:", e);
  245. uni.showToast({
  246. title: '网络请求失败',
  247. icon: 'none'
  248. });
  249. }
  250. });
  251. },
  252. loadCommentData() {
  253. uni.request({
  254. url: this.$apiHost + '/Article/getcomments',
  255. data: {
  256. uuid: getApp().globalData.uuid,
  257. type: 'article',
  258. id: this.arcID,
  259. page: 1,
  260. limit: 10
  261. },
  262. header: {
  263. "content-type": "application/json",
  264. 'sign': getApp().globalData.headerSign
  265. },
  266. success: (res) => {
  267. console.log("评论列表:", res.data);
  268. if (res.data.success == "yes") {
  269. this.$set(this, 'tableData', res.data.list);
  270. this.tableTotal = res.data.total;
  271. console.log("tabddd", this.tableData)
  272. } else {
  273. uni.showToast({
  274. title: '获取评论列表失败',
  275. icon: 'none'
  276. });
  277. }
  278. },
  279. complete: (com) => {
  280. // uni.hideLoading();
  281. },
  282. fail: (e) => {
  283. console.log("----e:", e);
  284. }
  285. });
  286. },
  287. // 唤起新评论弹框
  288. openComment() {
  289. let ccRef = this.$refs["ccRef"];
  290. ccRef.newCommentFun();
  291. },
  292. // 点赞回调事件
  293. likeFun({
  294. params
  295. }, callback) {
  296. console.log("likeFun", params);
  297. uni.request({
  298. url: this.$apiHost + '/Article/zanComment',
  299. data: {
  300. uuid: getApp().globalData.uuid,
  301. id: params.id
  302. },
  303. header: {
  304. "content-type": "application/json",
  305. 'sign': getApp().globalData.headerSign
  306. },
  307. success: (res) => {
  308. console.log("点赞结果:", res.data);
  309. if (res.data.success !== "yes") {
  310. callback(res); // 请求失败时重置点赞效果
  311. }
  312. },
  313. fail: (e) => {
  314. console.log("点赞失败:", e);
  315. callback(e); // 请求失败时重置点赞效果
  316. }
  317. });
  318. },
  319. // 文章点赞
  320. likeArticle() {
  321. uni.request({
  322. url: this.$apiHost + '/Article/like',
  323. data: {
  324. uuid: getApp().globalData.uuid,
  325. id: this.arcID
  326. },
  327. header: {
  328. "content-type": "application/json",
  329. 'sign': getApp().globalData.headerSign
  330. },
  331. success: (res) => {
  332. console.log("点赞结果:", res.data);
  333. if (res.data.success === "yes") {
  334. // 更新点赞状态
  335. if (!this.articleInfo.is_like) {
  336. this.articleInfo.like_count = (this.articleInfo.like_count || 0) + 1;
  337. this.articleInfo.is_like = true;
  338. uni.showToast({
  339. title: '点赞成功',
  340. icon: 'none'
  341. });
  342. } else {
  343. this.articleInfo.like_count = (this.articleInfo.like_count || 0) - 1;
  344. this.articleInfo.is_like = false;
  345. uni.showToast({
  346. title: '取消点赞',
  347. icon: 'none'
  348. });
  349. }
  350. } else {
  351. uni.showToast({
  352. title: '点赞失败',
  353. icon: 'none'
  354. });
  355. }
  356. },
  357. fail: (e) => {
  358. console.log("点赞失败:", e);
  359. uni.showToast({
  360. title: '网络请求失败',
  361. icon: 'none'
  362. });
  363. }
  364. });
  365. },
  366. // 分享文章
  367. shareArticle() {
  368. // 如果在微信小程序环境
  369. if (uni.getSystemInfoSync().platform === 'mp-weixin') {
  370. uni.showShareMenu({
  371. withShareTicket: true,
  372. menus: ['shareAppMessage', 'shareTimeline']
  373. });
  374. } else {
  375. // 其他环境,如APP
  376. uni.share({
  377. provider: "weixin",
  378. scene: "WXSceneSession",
  379. type: 0,
  380. title: this.articleInfo.title,
  381. summary: this.articleInfo.content.substring(0, 40) + "...",
  382. imageUrl: this.home_image,
  383. success: function(res) {
  384. console.log("分享成功:" + JSON.stringify(res));
  385. },
  386. fail: function(err) {
  387. console.log("分享失败:" + JSON.stringify(err));
  388. }
  389. });
  390. }
  391. },
  392. // 评论回调事件
  393. replyFun({
  394. params
  395. }, callback) {
  396. // params = {
  397. // ...params,
  398. // user_id: this.myInfo.user_id, // 用户id
  399. // user_name: this.myInfo.user_name, // 用户名
  400. // user_avatar: this.myInfo.user_avatar, // 用户头像地址
  401. // user_content: this.commentValue, // 用户评论内容
  402. // is_like: false, // 是否点赞
  403. // like_count: 0, // 点赞数统计
  404. // create_time: "刚刚", // 创建时间
  405. // owner: true, // 是否为所有者 所有者可以进行删除 管理员默认true
  406. // };
  407. console.log("replyFun", {
  408. uuid: getApp().globalData.uuid,
  409. article_id: this.arcID, // 文章ID
  410. content: params.user_content, // 评论内容
  411. parent_id: params.parent_id || 0, // 父评论ID
  412. reply_id: params.reply_id || 0, // 回复的评论ID
  413. reply_name: params.reply_name || '' // 被回复人名称
  414. });
  415. uni.request({
  416. url: this.$apiHost + '/Article/newComment',
  417. data: {
  418. uuid: getApp().globalData.uuid,
  419. type: 'article',
  420. article_id: this.arcID, // 文章ID
  421. content: params.user_content, // 评论内容
  422. parent_id: params.parent_id || 0, // 父评论ID
  423. reply_id: params.reply_id || 0, // 回复的评论ID
  424. reply_name: params.reply_name || '' // 被回复人名称
  425. },
  426. header: {
  427. "content-type": "application/json",
  428. 'sign': getApp().globalData.headerSign
  429. },
  430. success: (res) => {
  431. console.log("评论结果:", res.data);
  432. if (res.data.success === "yes") {
  433. callback(res.data); // 评论成功,传入后端返回的数据
  434. }
  435. },
  436. fail: (e) => {
  437. console.log("评论失败:", e);
  438. uni.showToast({
  439. title: '评论失败,请重试',
  440. icon: 'none'
  441. });
  442. }
  443. });
  444. },
  445. /** 删除回调事件
  446. * mode 删除模式
  447. * -- bind: 当被删除的一级评论存在回复评论, 那么该评论内容变更显示为[当前评论内容已被移除]
  448. * -- only: 仅删除当前评论(后端删除相关联的回复评论, 否则总数显示不对)
  449. * -- all : 删除所有评论包括回复评论 前端遍历子评论上报
  450. */
  451. deleteFun({
  452. params,
  453. mode
  454. }, callback) {
  455. console.log("deleteFun", {
  456. params,
  457. mode
  458. });
  459. // 将params转换为逗号分隔的字符串
  460. const idsString = Array.isArray(params) ? params.join(',') : params.toString();
  461. console.log("删除评论", idsString, mode)
  462. uni.request({
  463. url: this.$apiHost + '/Article/delComment',
  464. data: {
  465. uuid: getApp().globalData.uuid,
  466. ids: idsString, // 将params转换为逗号分隔的字符串传递给ids
  467. mode: mode
  468. },
  469. header: {
  470. "content-type": "application/json",
  471. 'sign': getApp().globalData.headerSign
  472. },
  473. success: (res) => {
  474. console.log("删除结果:", res.data);
  475. if (res.data.success === "yes") {
  476. callback(res);
  477. }
  478. },
  479. fail: (e) => {}
  480. });
  481. // switch (mode) {
  482. // case "bind":
  483. // // 逻辑: 调用接口进行评论内容修改 update
  484. // setTimeout(() => callback(), 500); // 目前为了展示效果, 直接执行callback
  485. // break;
  486. // case "only":
  487. // // 逻辑: 调用接口删除一个评论 delete
  488. // setTimeout(() => callback(), 500); // 目前为了展示效果, 直接执行callback
  489. // break;
  490. // default:
  491. // // all
  492. // // 逻辑: 调用接口删除多个评论 [delete]
  493. // setTimeout(() => callback(), 500); // 目前为了展示效果, 直接执行callback
  494. // break;
  495. // }
  496. },
  497. }
  498. }
  499. </script>
  500. <style scoped lang="scss">
  501. @import 'articleDetail.scss';
  502. </style>