index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. <template>
  2. <view class="main">
  3. <view class="info">
  4. <scroll-view scroll-y="true" :scroll-top="scrollTop" class="scroll-view" @scrolltolower="toPage"
  5. @scroll="toScroll">
  6. <view class="list-scroll-view">
  7. <view class="one">
  8. <swiperImg :imgsList='info.file'></swiperImg>
  9. </view>
  10. <view class="two">
  11. <view class="two_1">{{info.title||'暂无标题'}}</view>
  12. <view class="two_2">#{{info.zhType||'暂无类型'}}</view>
  13. <view class="two_3">
  14. <rich-text :nodes="formatRichText(info.content)"></rich-text>
  15. </view>
  16. <view class="two_4">发布时间:{{info.create_time||'暂无'}}</view>
  17. </view>
  18. <view class="thr">
  19. <view class="thr_1">评论 {{total||0}}</view>
  20. <scroll-view scroll-y="true" class="scroll">
  21. <view class="list-scroll-view">
  22. <view class="list" v-for="(item,index) in list" :key="index">
  23. <image class="image"
  24. :src="item.user_file&&item.user_file.length>0?item.user_file[0].url:'../../static/my.png'"
  25. mode="aspectFill">
  26. </image>
  27. <view class="other">
  28. <view class="name textOver">{{item.user_name||'暂无'}}</view>
  29. <view class="content">{{item.content||'暂无'}}</view>
  30. <view class="name">{{item.create_time||'暂无'}}</view>
  31. </view>
  32. </view>
  33. <view class="is_bottom" v-if="is_bottom">
  34. <text>{{config.bottom_title||'到底了!'}}</text>
  35. </view>
  36. </view>
  37. </scroll-view>
  38. </view>
  39. </view>
  40. </scroll-view>
  41. </view>
  42. <view class="foot">
  43. <view class="foot_1">
  44. <view class="left">
  45. <uni-easyinput confirmType="send" prefixIcon="chatbubble" v-model="content" placeholder="说点什么..."
  46. @confirm="toSend">
  47. </uni-easyinput>
  48. </view>
  49. <view class="right">
  50. <view class="right_1" @tap.stop="toLike('0')">
  51. <text v-if="info.is_like" class="iconfont icon-aixin1"></text>
  52. <text v-else class="iconfont icon-aixin"></text>
  53. <text>{{like||0}}</text>
  54. </view>
  55. <view class="right_1" @tap.stop="toLike('1')">
  56. <text v-if="info.is_collect" class="iconfont icon-shoucang"></text>
  57. <text v-else class="iconfont icon-shoucang1"></text>
  58. <text>{{collect||0}}</text>
  59. </view>
  60. <view class="right_1" @click="pageScroll">
  61. <text class="iconfont icon-pinglun"></text>
  62. <text>{{total||0}}</text>
  63. </view>
  64. </view>
  65. </view>
  66. </view>
  67. </view>
  68. </view>
  69. </template>
  70. <script>
  71. import moment from 'moment';
  72. import swiperImg from '../../components/swiper/index.vue';
  73. export default {
  74. components: {
  75. swiperImg
  76. },
  77. data() {
  78. return {
  79. id: '',
  80. config: {},
  81. user: {},
  82. info: {},
  83. // 评论数量
  84. list: [],
  85. total: 0,
  86. skip: 0,
  87. limit: 6,
  88. page: 0,
  89. // 数据是否触底
  90. is_bottom: false,
  91. scrollTop: 0,
  92. // 评论
  93. content: '',
  94. // 点赞
  95. like: 0,
  96. // 收藏
  97. collect: 0,
  98. // 字典表
  99. typeList: []
  100. }
  101. },
  102. onLoad: async function(e) {
  103. const that = this;
  104. that.$set(that, `id`, e && e.id || '');
  105. that.searchConfig();
  106. that.searchToken();
  107. await that.searchOther();
  108. await that.search();
  109. },
  110. methods: {
  111. searchConfig() {
  112. const that = this;
  113. try {
  114. const res = uni.getStorageSync('config');
  115. if (res) that.$set(that, `config`, res);
  116. } catch (e) {
  117. uni.showToast({
  118. title: err.errmsg,
  119. icon: 'error',
  120. duration: 2000
  121. });
  122. }
  123. },
  124. searchToken() {
  125. const that = this;
  126. try {
  127. const res = uni.getStorageSync('token');
  128. if (res) that.$set(that, `user`, res);
  129. } catch (e) {
  130. uni.showToast({
  131. title: err.errmsg,
  132. icon: 'error',
  133. duration: 2000
  134. });
  135. }
  136. },
  137. // 查询
  138. async search() {
  139. const that = this;
  140. if (that.id) {
  141. let res;
  142. res = await that.$api(`/article/${that.id}`, 'GET', {})
  143. if (res.errcode == '0') {
  144. res.data.zhType = that.searchDict(res.data.type, 'type')
  145. if (that.user._id) {
  146. const like = await that.$api(`/like`, 'GET', {
  147. source: res.data._id,
  148. user: that.user._id,
  149. type: '0'
  150. });
  151. if (like.errcode == '0' && like.total > 0) res.data.is_like = true
  152. else res.data.is_like = false
  153. const collect = await that.$api(`/like`, 'GET', {
  154. source: res.data._id,
  155. user: that.user._id,
  156. type: '1'
  157. });
  158. if (collect.errcode == '0' && collect.total > 0) res.data.is_collect = true
  159. else res.data.is_collect = false
  160. const likenum = await that.$api(`/like`, 'GET', {
  161. source: res.data._id,
  162. type: '0',
  163. limit: 1
  164. });
  165. if (likenum.errcode == '0') that.$set(that, `like`, likenum.total)
  166. const collectnum = await that.$api(`/like`, 'GET', {
  167. source: res.data._id,
  168. type: '1',
  169. limit: 1
  170. });
  171. if (collectnum.errcode == '0') that.$set(that, `collect`, collectnum.total)
  172. }
  173. that.$set(that, `info`, res.data)
  174. uni.setNavigationBarTitle({
  175. title: res.data.contact_name
  176. });
  177. } else {
  178. uni.showToast({
  179. title: res.errmsg,
  180. });
  181. }
  182. // 查看评论
  183. that.searchComment();
  184. }
  185. },
  186. // 处理富文本
  187. formatRichText(html) {
  188. if (html) {
  189. // 富文本内容格式化
  190. return html && html.replace(/<img[^>]*>/gi, function(match, capture) {
  191. // 查找所有的 img 元素
  192. return match.replace(/style=".*"/gi, '').replace(/style='.*'/gi,
  193. '')
  194. // 删除找到的所有 img 元素中的 style 属性
  195. }).replace(/\<img/gi, '<img style="width:100%;"') // 对 img 元素增加 style 属性,并设置宽度为 100%
  196. }
  197. },
  198. // 查看评论
  199. async searchComment() {
  200. const that = this;
  201. let res;
  202. let info = {
  203. skip: that.skip,
  204. limit: that.limit,
  205. is_use: '0',
  206. status: '1',
  207. source: that.info._id
  208. }
  209. res = await that.$api(`/comment`, 'GET', {
  210. ...info,
  211. });
  212. if (res.errcode == '0') {
  213. for (let val of res.data) {
  214. const user = await that.$api(`/user/${val.user}`, 'GET', {});
  215. if (user.errcode == '0') {
  216. val.user_file = user.data.logo
  217. val.user_name = user.data.name || user.data.nick_name
  218. }
  219. }
  220. let list = [...that.list, ...res.data];
  221. that.$set(that, `list`, list);
  222. that.$set(that, `total`, res.total)
  223. } else {
  224. uni.showToast({
  225. title: res.errmsg,
  226. icon: 'none'
  227. })
  228. }
  229. },
  230. // 查询字典表
  231. searchDict(e, model) {
  232. const that = this;
  233. let data
  234. if (model == 'type') {
  235. data = that.typeList.find((i) => i.value == e);
  236. if (data) return data.label
  237. else return '暂无'
  238. }
  239. },
  240. // 滑动到评论专区
  241. pageScroll() {
  242. this.$set(this, `scrollTop`, 0);
  243. this.$nextTick(() => {
  244. setTimeout(() => {
  245. uni.createSelectorQuery().in(this).select(`.thr`).boundingClientRect(res => {
  246. this.$set(this, `scrollTop`, res.top);
  247. }).exec();
  248. }, 100);
  249. });
  250. },
  251. // 点赞/取消点赞
  252. async toLike(type) {
  253. const that = this;
  254. const info = that.info
  255. let res;
  256. let like;
  257. if (type == '0') like = info.is_like
  258. else like = info.is_collect
  259. if (like == false) {
  260. const form = {
  261. user: that.user._id,
  262. source: info._id,
  263. type: type,
  264. create_time: moment().format('YYYY-MM-DD HH:mm:ss'),
  265. }
  266. res = await that.$api(`/like`, 'POST', form);
  267. if (res.errcode == '0') {
  268. that.clearPage();
  269. that.search()
  270. }
  271. } else {
  272. res = await that.$api(`/like`, 'GET', {
  273. source: info._id,
  274. user: that.user._id,
  275. type: type
  276. });
  277. if (res.errcode == '0' && res.total > 0) {
  278. await that.$api(`/like/${res.data[0]._id}`, 'DELETE', {})
  279. that.clearPage();
  280. that.search()
  281. }
  282. }
  283. },
  284. // 发送
  285. async toSend(content) {
  286. const that = this;
  287. if (that.user && that.user._id) {
  288. const form = {
  289. user: that.user._id,
  290. user_name: that.user.name || that.user.nick_name,
  291. source: that.info._id,
  292. content,
  293. create_time: moment().format('YYYY-MM-DD HH:mm:ss'),
  294. }
  295. let res = await that.$api(`/comment`, 'POST', form);
  296. if (res.errcode == '0') {
  297. uni.showToast({
  298. title: '评论成功 等待审核',
  299. icon: 'none'
  300. })
  301. that.$set(that, `content`, '')
  302. that.searchComment()
  303. } else {
  304. uni.showToast({
  305. title: res.errmsg,
  306. icon: 'none'
  307. })
  308. }
  309. } else {
  310. uni.navigateTo({
  311. url: `/pagesIndex/login/index`
  312. })
  313. }
  314. },
  315. // 分页
  316. toPage(e) {
  317. const that = this;
  318. let list = that.list;
  319. let limit = that.limit;
  320. if (that.total > list.length) {
  321. uni.showLoading({
  322. title: '加载中',
  323. mask: true
  324. })
  325. let page = that.page + 1;
  326. that.$set(that, `page`, page)
  327. let skip = page * limit;
  328. that.$set(that, `skip`, skip)
  329. that.searchComment();
  330. uni.hideLoading();
  331. } else that.$set(that, `is_bottom`, true)
  332. },
  333. // 触底
  334. toScroll(e) {
  335. const that = this;
  336. let up = that.scrollTop;
  337. that.$set(that, `scrollTop`, e.detail.scrollTop);
  338. let num = Math.sign(up - e.detail.scrollTop);
  339. if (num == 1) that.$set(that, `is_bottom`, false);
  340. },
  341. // 清空列表
  342. clearPage() {
  343. const that = this;
  344. that.$set(that, `list`, [])
  345. that.$set(that, `skip`, 0)
  346. that.$set(that, `limit`, 6)
  347. that.$set(that, `page`, 0)
  348. },
  349. // 查询其他信息
  350. async searchOther() {
  351. const that = this;
  352. let res;
  353. // 查询类型
  354. res = await that.$api(`/dictData`, 'GET', {
  355. type: 'home_tabs',
  356. is_use: '0',
  357. })
  358. if (res.errcode == '0') that.$set(that, `typeList`, res.data);
  359. },
  360. }
  361. }
  362. </script>
  363. <style lang="scss" scoped>
  364. .main {
  365. display: flex;
  366. flex-direction: column;
  367. box-sizing: border-box;
  368. width: 100vw;
  369. height: 100vh;
  370. .info {
  371. position: relative;
  372. flex-grow: 1;
  373. .two {
  374. padding: 0 2vw;
  375. .two_1 {
  376. font-weight: bold;
  377. font-size: var(--font16Size);
  378. }
  379. .two_2 {
  380. padding: 1vw;
  381. font-size: var(--font14Size);
  382. color: var(--f3CColor);
  383. }
  384. .two_4 {
  385. padding: 3vw 0;
  386. font-size: var(--font12Size);
  387. color: var(--f99Color);
  388. border-bottom: 1px solid var(--f9Color);
  389. }
  390. }
  391. .thr {
  392. .thr_1 {
  393. padding: 2vw 4vw;
  394. font-weight: 500;
  395. font-size: var(--font15Size);
  396. border-bottom: 1px solid var(--f9Color);
  397. }
  398. .list {
  399. display: flex;
  400. align-items: center;
  401. padding: 2vw;
  402. .image {
  403. width: 10vw;
  404. height: 10vw;
  405. border-radius: 10vw;
  406. }
  407. .other {
  408. display: flex;
  409. flex-direction: column;
  410. width: 80vw;
  411. padding: 2vw;
  412. border-bottom: 1px solid var(--f9Color);
  413. font-size: var(--font12Size);
  414. .name {
  415. color: var(--f85Color);
  416. padding: 2px 0;
  417. }
  418. }
  419. }
  420. }
  421. }
  422. .foot {
  423. padding: 2vw;
  424. background-color: var(--mainColor);
  425. .foot_1 {
  426. display: flex;
  427. justify-content: space-between;
  428. align-items: center;
  429. .left {
  430. .is-input-border {
  431. border-radius: 45px;
  432. }
  433. }
  434. .right {
  435. width: 45%;
  436. display: flex;
  437. justify-content: space-around;
  438. .right_1 {
  439. display: flex;
  440. align-items: center;
  441. font-size: var(--font17Size);
  442. .iconfont {
  443. padding: 0 1vw;
  444. font-size: 18px;
  445. }
  446. }
  447. }
  448. }
  449. }
  450. }
  451. .scroll-view {
  452. position: absolute;
  453. top: 0;
  454. left: 0;
  455. right: 0;
  456. bottom: 0;
  457. .list-scroll-view {
  458. display: flex;
  459. flex-direction: column;
  460. }
  461. }
  462. .is_bottom {
  463. width: 100%;
  464. text-align: center;
  465. text {
  466. padding: 2vw 0;
  467. display: inline-block;
  468. color: var(--f85Color);
  469. font-size: var(--font14Size);
  470. }
  471. }
  472. </style>