articleDetail copy.vue 15 KB

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