articleDetail.vue 15 KB

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