articleDetail copy.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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="custom-navbar">
  10. <view class="navbar-left" @click="goBack">
  11. <text class="fa fa-angle-left"></text>
  12. </view>
  13. <view class="navbar-center">
  14. <view class="navbar-title">
  15. <image class="navbar-avatar" :src="author.avator" mode="aspectFill"></image>
  16. <text class="navbar-text">{{ author.nickname }}</text>
  17. <!-- <text class="navbar-badge" v-if="author.is_vip > 0">VIP</text> -->
  18. </view>
  19. </view>
  20. <view class="navbar-right">
  21. <text class="followTheAuthor followTheAuthor1" v-if="author.is_attention == 0"
  22. @click="followTheAuthor(1)">+关注</text>
  23. <text class="followTheAuthor followTheAuthor0" v-if="author.is_attention == 1"
  24. @click="followTheAuthor(0)">已关注</text>
  25. <image v-if="true" @click="showActionSheet(0)" src="@/static/icon/sy_icon_fenxiang.png"
  26. mode="widthFix"></image>
  27. <view v-else class="navbar-right" @click="showActionSheet(1)">
  28. <text class="fa fa-ellipsis-h"></text>
  29. </view>
  30. </view>
  31. </view>
  32. <!-- 文章头图区域 -->
  33. <view class="topUser" v-if="articleInfo.type == 'user'">
  34. <image :src="home_image" class="home_image" mode="aspectFill"></image>
  35. <!-- 图片指示器 -->
  36. <view class="image-indicator" v-if="image_list.length > 1">
  37. <text>{{ selImg + 1 }}/{{ image_list.length }}</text>
  38. </view>
  39. <!-- 缩略图列表 -->
  40. <view class="list">
  41. <view class="img" :class="selImg == index ? 'active' : ''" v-for="(item, index) in image_list"
  42. :key="index" @click="selPhoto(item, index)">
  43. <image :src="item" mode="aspectFill"></image>
  44. </view>
  45. </view>
  46. </view>
  47. <!-- 文章内容区域 -->
  48. <view class="body">
  49. <!-- 文章标题与元信息 -->
  50. <view class="article-header">
  51. <view class="title">
  52. {{ articleInfo.title || '暂无标题' }}
  53. </view>
  54. <view class="meta-info">
  55. <view class="meta-item">
  56. <text class="fa fa-calendar"></text>
  57. <text class="meta-text">{{ articleInfo.create_time || '暂无时间' }}</text>
  58. </view>
  59. <view class="meta-item">
  60. <text class="fa fa-eye"></text>
  61. <text class="meta-text">{{ articleInfo.num_view || 0 }}次阅读</text>
  62. </view>
  63. <view class="meta-item" v-if="articleInfo.author">
  64. <text class="fa fa-user"></text>
  65. <text class="meta-text">{{ articleInfo.author }}</text>
  66. </view>
  67. </view>
  68. </view>
  69. <!-- 内容分隔线 -->
  70. <view class="divider"></view>
  71. <!-- 文章内容 -->
  72. <!-- <view class="article-content" v-if="articleInfo.type == 'user'">
  73. <view class="content">{{ articleInfo.content || '暂无内容' }}</view>
  74. </view> -->
  75. <view class="article-content">
  76. <view class="content">
  77. <rich-text :nodes="content" style="font-size: 14px;"></rich-text>
  78. </view>
  79. </view>
  80. <!-- 文章底部区域 -->
  81. <view class="article-footer">
  82. <view class="action-bar">
  83. <view class="action-item" @tap="likeArticle">
  84. <text class="fa" :class="articleInfo.is_like ? 'fa-thumbs-up liked' : 'fa-thumbs-o-up'"></text>
  85. <text class="action-text">{{ articleInfo.like_count || 0 }}</text>
  86. </view>
  87. <view class="action-item" @tap="openComment">
  88. <text class="fa fa-comment-o"></text>
  89. <text class="action-text">{{ tableTotal || 0 }}</text>
  90. </view>
  91. <view class="action-item" @tap="shareArticle">
  92. <text class="fa fa-share-alt"></text>
  93. <text class="action-text">分享</text>
  94. </view>
  95. </view>
  96. </view>
  97. <!-- 空白占位 -->
  98. <view class="list_info">
  99. <view class="blankHeight"></view>
  100. </view>
  101. </view>
  102. <!-- <view class="btn_submit" @click="chatTA()">
  103. <image class="icon" src="../../static/icon/icon_chat_white.png" mode="widthFix"></image>
  104. 私聊
  105. </view> -->
  106. <view class="thread2"></view>
  107. <view class="thread2"></view>
  108. <previewImage ref="previewImage" :opacity="0.8" :circular="true" :imgs="imgs" :descs="descs"></previewImage>
  109. <!-- 评论区域 -->
  110. <CommentSection ref="commentSection" @totalNumberOfComments="totalNumberOfComments" :myInfo="myInfo"
  111. :userInfo="userInfo" :articleId="arcID" :type="'article'">
  112. </CommentSection>
  113. <view class="thread2"></view>
  114. <!-- 自定义 ActionSheet -->
  115. <ActionSheet ref="actionSheet" :items="items" @select="handleActionSelect" @cancel="handleActionCancel" />
  116. </view>
  117. </template>
  118. <script>
  119. import htmlParser from '../../common/html-parser'
  120. import CommentSection from "@/components/CommentSection/CommentSection.vue";
  121. import previewImage from '@/components/kxj-previewImage/kxj-previewImage.vue'; //引用插件
  122. function parseImgs(nodes) {
  123. nodes.forEach(node => {
  124. if (
  125. node.name === 'img' &&
  126. node.attrs &&
  127. node.attrs['data-img-size-val']
  128. ) {
  129. const sizes = node.attrs['data-img-size-val'].split(',')
  130. const width = uni.upx2px(720 * 0.9)
  131. const height = parseInt(width * (sizes[1] / sizes[0]))
  132. node.attrs.style = `width:${width};height:${height};`
  133. }
  134. if (Array.isArray(node.children)) {
  135. parseImgs(node.children)
  136. }
  137. })
  138. return nodes
  139. }
  140. export default {
  141. components: {
  142. previewImage,
  143. CommentSection
  144. },
  145. data() {
  146. return {
  147. items: [],
  148. title: '',
  149. arcID: 0,
  150. selImg: 0,
  151. home_image: '',
  152. myinfo: {},
  153. tag_list: [],
  154. image_list: [],
  155. imgs: [],
  156. descs: [],
  157. list_wish: [],
  158. content: '',
  159. tableTotal: 0,
  160. author: {},
  161. // 添加文章信息字段
  162. articleInfo: {
  163. title: '',
  164. content: '',
  165. create_time: '',
  166. images: '',
  167. view_count: 0,
  168. author: '',
  169. like_count: 0,
  170. is_like: false
  171. },
  172. myInfo: {
  173. user_id: 1, // 用户id
  174. user_name: "", // 用户名
  175. user_avatar: "", // 用户头像地址
  176. },
  177. // 文章作者信息(提示: 一般来自localstorage, 如果是实时获取的话, 那么获取到数据后再v-if显示评论组件)
  178. userInfo: {
  179. user_id: 2, // 用户id
  180. user_name: "", // 用户名
  181. user_avatar: "", // 用户头像地址
  182. },
  183. }
  184. },
  185. onLoad(parms) {
  186. let self = this;
  187. this.arcID = parms.id || 133;
  188. },
  189. onShow() {
  190. uni.$emit('check_update');
  191. this.$nextTick(() => {
  192. if (this.$refs.commentSection) {
  193. this.$refs.commentSection.loadCommentData();
  194. }
  195. });
  196. this.loadData();
  197. },
  198. methods: {
  199. // 返回上一页
  200. goBack() {
  201. uni.navigateBack({
  202. delta: 1
  203. });
  204. },
  205. // 文章点赞
  206. likeArticle() {
  207. uni.request({
  208. url: this.$apiHost + '/Work/zanTA',
  209. data: {
  210. uuid: getApp().globalData.uuid,
  211. id: this.arcID
  212. },
  213. header: {
  214. "content-type": "application/json",
  215. 'sign': getApp().globalData.headerSign
  216. },
  217. success: (res) => {
  218. console.log("点赞结果:", res.data);
  219. if (res.data.success === "yes") {
  220. // 更新点赞状态
  221. if (!this.articleInfo.is_like) {
  222. this.articleInfo.like_count = (this.articleInfo.like_count || 0) + 1;
  223. this.articleInfo.is_like = true;
  224. uni.showToast({
  225. title: '点赞成功',
  226. icon: 'none'
  227. });
  228. } else {
  229. this.articleInfo.like_count = (this.articleInfo.like_count || 0) - 1;
  230. this.articleInfo.is_like = false;
  231. uni.showToast({
  232. title: res.data.str,
  233. icon: 'none'
  234. });
  235. }
  236. } else {
  237. uni.showToast({
  238. title: res.data.str,
  239. icon: 'none'
  240. });
  241. }
  242. },
  243. fail: (e) => {
  244. console.log("点赞失败:", e);
  245. uni.showToast({
  246. title: '网络请求失败',
  247. icon: 'none'
  248. });
  249. }
  250. });
  251. },
  252. // 关注作者
  253. followTheAuthor(n) {
  254. uni.request({
  255. url: this.$apiHost + '/Member/attention',
  256. data: {
  257. uuid: getApp().globalData.uuid,
  258. id: this.author.id
  259. },
  260. header: {
  261. "content-type": "application/json",
  262. 'sign': getApp().globalData.headerSign
  263. },
  264. success: (res) => {
  265. console.log("点赞结果:", res.data);
  266. uni.showToast({
  267. title: res.data.str,
  268. icon: 'none'
  269. });
  270. if (res.data.success === "yes") {
  271. console.log("关注结果:", res.data, n);
  272. this.author.is_attention = n
  273. }
  274. },
  275. fail: (e) => {
  276. console.log("关注失败:", e);
  277. uni.showToast({
  278. title: '网络请求失败',
  279. icon: 'none'
  280. });
  281. }
  282. });
  283. },
  284. totalNumberOfComments(tableTotal) {
  285. this.tableTotal = tableTotal;
  286. },
  287. onLinqu(item) {
  288. uni.navigateTo({
  289. url: '/pages/my/wishHelp?id=' + item.myid,
  290. })
  291. },
  292. selPhoto(item, sel) {
  293. this.selImg = sel;
  294. this.home_image = this.image_list[sel];
  295. },
  296. toArr(imgs) {
  297. let arr = imgs.split("|");
  298. return arr;
  299. },
  300. previewOpen(imgs1, index) {
  301. this.imgs = imgs1.split("|");
  302. setTimeout(() => this.$refs.previewImage.open(index), 0)
  303. return; //如需测试和uni原生预览差别可注释这两行
  304. },
  305. loadData() {
  306. uni.request({
  307. url: this.$apiHost + '/Article/getinfo',
  308. data: {
  309. uuid: getApp().globalData.uuid,
  310. id: this.arcID
  311. },
  312. header: {
  313. "content-type": "application/json",
  314. 'sign': getApp().globalData.headerSign
  315. },
  316. success: (res) => {
  317. console.log("文章信息:", res.data);
  318. if (res.data.success === "yes") {
  319. // 更新文章信息
  320. this.articleInfo = res.data.article;
  321. this.author = res.data.author;
  322. const nodes = htmlParser(res.data.article.content);
  323. // #ifdef APP-PLUS-NVUE
  324. parseImgs(nodes)
  325. // #endif
  326. this.content = nodes
  327. // 更新图片列表
  328. if (res.data.article.images && res.data.article.images !== "") {
  329. this.image_list = res.data.article.images.split(",");
  330. this.home_image = this.image_list[0];
  331. } else {
  332. this.home_image = "../../static/home/avator.png";
  333. }
  334. console.log("img", this.home_image);
  335. // 更新标签列表
  336. // if (res.data.data.tags && res.data.data.tags !== "") {
  337. // this.tag_list = res.data.data.tags.split(",");
  338. // }
  339. } else {
  340. uni.showToast({
  341. title: '获取文章信息失败',
  342. icon: 'none'
  343. });
  344. }
  345. },
  346. complete: (com) => {
  347. // uni.hideLoading();
  348. },
  349. fail: (e) => {
  350. console.log("请求失败:", e);
  351. uni.showToast({
  352. title: '网络请求失败',
  353. icon: 'none'
  354. });
  355. }
  356. });
  357. },
  358. // 评论相关方法已移至CommentSection组件
  359. // 唤起新评论弹框
  360. openComment() {
  361. if (this.$refs.commentSection) {
  362. this.$refs.commentSection.openComment();
  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. showActionSheet(n) {
  498. if (n == 0) {
  499. this.items = [
  500. { text: '分享作品', icon: '../../static/icon/cz_icon_fenxiangzuopin.png' },
  501. ]
  502. }
  503. if (n == 1) {
  504. this.items = [
  505. { text: '分享作品', icon: '../../static/icon/cz_icon_fenxiangzuopin.png' },
  506. { text: '修改封面', icon: '../../static/icon/cz_icon_xiugaifengmian.png' },
  507. { text: '删除作品', icon: '../../static/icon/sy_icon_shanchu.png', danger: true }
  508. ]
  509. }
  510. this.$refs.actionSheet.show();
  511. },
  512. handleActionCancel() {
  513. console.log('ActionSheet cancelled');
  514. },
  515. handleActionSelect(index, item) {
  516. console.log('ActionSheet selected index:', item.text,);
  517. },
  518. }
  519. }
  520. </script>
  521. <style scoped lang="scss">
  522. @import 'articleDetail.scss';
  523. </style>