articleDetail.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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 || 133;
  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. console.log("img", this.home_image);
  230. // 更新标签列表
  231. // if (res.data.data.tags && res.data.data.tags !== "") {
  232. // this.tag_list = res.data.data.tags.split(",");
  233. // }
  234. } else {
  235. uni.showToast({
  236. title: '获取文章信息失败',
  237. icon: 'none'
  238. });
  239. }
  240. },
  241. complete: (com) => {
  242. // uni.hideLoading();
  243. },
  244. fail: (e) => {
  245. console.log("请求失败:", e);
  246. uni.showToast({
  247. title: '网络请求失败',
  248. icon: 'none'
  249. });
  250. }
  251. });
  252. },
  253. loadCommentData() {
  254. uni.request({
  255. url: this.$apiHost + '/Article/getcomments',
  256. data: {
  257. uuid: getApp().globalData.uuid,
  258. type: 'article',
  259. id: this.arcID,
  260. page: 1,
  261. limit: 10
  262. },
  263. header: {
  264. "content-type": "application/json",
  265. 'sign': getApp().globalData.headerSign
  266. },
  267. success: (res) => {
  268. console.log("评论列表:", res.data);
  269. if (res.data.success == "yes") {
  270. this.$set(this, 'tableData', res.data.list);
  271. this.tableTotal = res.data.total;
  272. console.log("tabddd", this.tableData)
  273. } else {
  274. uni.showToast({
  275. title: '获取评论列表失败',
  276. icon: 'none'
  277. });
  278. }
  279. },
  280. complete: (com) => {
  281. // uni.hideLoading();
  282. },
  283. fail: (e) => {
  284. console.log("----e:", e);
  285. }
  286. });
  287. },
  288. // 唤起新评论弹框
  289. openComment() {
  290. let ccRef = this.$refs["ccRef"];
  291. ccRef.newCommentFun();
  292. },
  293. // 点赞回调事件
  294. likeFun({
  295. params
  296. }, callback) {
  297. console.log("likeFun", params);
  298. uni.request({
  299. url: this.$apiHost + '/Article/zanComment',
  300. data: {
  301. uuid: getApp().globalData.uuid,
  302. id: params.id
  303. },
  304. header: {
  305. "content-type": "application/json",
  306. 'sign': getApp().globalData.headerSign
  307. },
  308. success: (res) => {
  309. console.log("点赞结果:", res.data);
  310. if (res.data.success !== "yes") {
  311. callback(res); // 请求失败时重置点赞效果
  312. }
  313. },
  314. fail: (e) => {
  315. console.log("点赞失败:", e);
  316. callback(e); // 请求失败时重置点赞效果
  317. }
  318. });
  319. },
  320. // 文章点赞
  321. likeArticle() {
  322. uni.request({
  323. url: this.$apiHost + '/Article/like',
  324. data: {
  325. uuid: getApp().globalData.uuid,
  326. id: this.arcID
  327. },
  328. header: {
  329. "content-type": "application/json",
  330. 'sign': getApp().globalData.headerSign
  331. },
  332. success: (res) => {
  333. console.log("点赞结果:", res.data);
  334. if (res.data.success === "yes") {
  335. // 更新点赞状态
  336. if (!this.articleInfo.is_like) {
  337. this.articleInfo.like_count = (this.articleInfo.like_count || 0) + 1;
  338. this.articleInfo.is_like = true;
  339. uni.showToast({
  340. title: '点赞成功',
  341. icon: 'none'
  342. });
  343. } else {
  344. this.articleInfo.like_count = (this.articleInfo.like_count || 0) - 1;
  345. this.articleInfo.is_like = false;
  346. uni.showToast({
  347. title: '取消点赞',
  348. icon: 'none'
  349. });
  350. }
  351. } else {
  352. uni.showToast({
  353. title: '点赞失败',
  354. icon: 'none'
  355. });
  356. }
  357. },
  358. fail: (e) => {
  359. console.log("点赞失败:", e);
  360. uni.showToast({
  361. title: '网络请求失败',
  362. icon: 'none'
  363. });
  364. }
  365. });
  366. },
  367. // 分享文章
  368. shareArticle() {
  369. // 如果在微信小程序环境
  370. if (uni.getSystemInfoSync().platform === 'mp-weixin') {
  371. uni.showShareMenu({
  372. withShareTicket: true,
  373. menus: ['shareAppMessage', 'shareTimeline']
  374. });
  375. } else {
  376. // 其他环境,如APP
  377. uni.share({
  378. provider: "weixin",
  379. scene: "WXSceneSession",
  380. type: 0,
  381. title: this.articleInfo.title,
  382. summary: this.articleInfo.content.substring(0, 40) + "...",
  383. imageUrl: this.home_image,
  384. success: function(res) {
  385. console.log("分享成功:" + JSON.stringify(res));
  386. },
  387. fail: function(err) {
  388. console.log("分享失败:" + JSON.stringify(err));
  389. }
  390. });
  391. }
  392. },
  393. // 评论回调事件
  394. replyFun({
  395. params
  396. }, callback) {
  397. // params = {
  398. // ...params,
  399. // user_id: this.myInfo.user_id, // 用户id
  400. // user_name: this.myInfo.user_name, // 用户名
  401. // user_avatar: this.myInfo.user_avatar, // 用户头像地址
  402. // user_content: this.commentValue, // 用户评论内容
  403. // is_like: false, // 是否点赞
  404. // like_count: 0, // 点赞数统计
  405. // create_time: "刚刚", // 创建时间
  406. // owner: true, // 是否为所有者 所有者可以进行删除 管理员默认true
  407. // };
  408. console.log("replyFun", {
  409. uuid: getApp().globalData.uuid,
  410. article_id: this.arcID, // 文章ID
  411. content: params.user_content, // 评论内容
  412. parent_id: params.parent_id || 0, // 父评论ID
  413. reply_id: params.reply_id || 0, // 回复的评论ID
  414. reply_name: params.reply_name || '' // 被回复人名称
  415. });
  416. uni.request({
  417. url: this.$apiHost + '/Article/newComment',
  418. data: {
  419. uuid: getApp().globalData.uuid,
  420. type: 'article',
  421. article_id: this.arcID, // 文章ID
  422. content: params.user_content, // 评论内容
  423. parent_id: params.parent_id || 0, // 父评论ID
  424. reply_id: params.reply_id || 0, // 回复的评论ID
  425. reply_name: params.reply_name || '' // 被回复人名称
  426. },
  427. header: {
  428. "content-type": "application/json",
  429. 'sign': getApp().globalData.headerSign
  430. },
  431. success: (res) => {
  432. console.log("评论结果:", res.data);
  433. if (res.data.success === "yes") {
  434. callback(res.data); // 评论成功,传入后端返回的数据
  435. }
  436. },
  437. fail: (e) => {
  438. console.log("评论失败:", e);
  439. uni.showToast({
  440. title: '评论失败,请重试',
  441. icon: 'none'
  442. });
  443. }
  444. });
  445. },
  446. /** 删除回调事件
  447. * mode 删除模式
  448. * -- bind: 当被删除的一级评论存在回复评论, 那么该评论内容变更显示为[当前评论内容已被移除]
  449. * -- only: 仅删除当前评论(后端删除相关联的回复评论, 否则总数显示不对)
  450. * -- all : 删除所有评论包括回复评论 前端遍历子评论上报
  451. */
  452. deleteFun({
  453. params,
  454. mode
  455. }, callback) {
  456. console.log("deleteFun", {
  457. params,
  458. mode
  459. });
  460. // 将params转换为逗号分隔的字符串
  461. const idsString = Array.isArray(params) ? params.join(',') : params.toString();
  462. console.log("删除评论", idsString, mode)
  463. uni.request({
  464. url: this.$apiHost + '/Article/delComment',
  465. data: {
  466. uuid: getApp().globalData.uuid,
  467. ids: idsString, // 将params转换为逗号分隔的字符串传递给ids
  468. mode: mode
  469. },
  470. header: {
  471. "content-type": "application/json",
  472. 'sign': getApp().globalData.headerSign
  473. },
  474. success: (res) => {
  475. console.log("删除结果:", res.data);
  476. if (res.data.success === "yes") {
  477. callback(res);
  478. }
  479. },
  480. fail: (e) => {}
  481. });
  482. // switch (mode) {
  483. // case "bind":
  484. // // 逻辑: 调用接口进行评论内容修改 update
  485. // setTimeout(() => callback(), 500); // 目前为了展示效果, 直接执行callback
  486. // break;
  487. // case "only":
  488. // // 逻辑: 调用接口删除一个评论 delete
  489. // setTimeout(() => callback(), 500); // 目前为了展示效果, 直接执行callback
  490. // break;
  491. // default:
  492. // // all
  493. // // 逻辑: 调用接口删除多个评论 [delete]
  494. // setTimeout(() => callback(), 500); // 目前为了展示效果, 直接执行callback
  495. // break;
  496. // }
  497. },
  498. }
  499. }
  500. </script>
  501. <style scoped lang="scss">
  502. @import 'articleDetail.scss';
  503. </style>