index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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}}</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. // 富文本内容格式化
  189. return html && html.replace(/<img[^>]*>/gi, function(match, capture) {
  190. // 查找所有的 img 元素
  191. return match.replace(/style=".*"/gi, '').replace(/style='.*'/gi,
  192. '')
  193. // 删除找到的所有 img 元素中的 style 属性
  194. }).replace(/\<img/gi, '<img style="width:100%;"') // 对 img 元素增加 style 属性,并设置宽度为 100%
  195. },
  196. // 查看评论
  197. async searchComment() {
  198. const that = this;
  199. let res;
  200. let info = {
  201. skip: that.skip,
  202. limit: that.limit,
  203. is_use: '0',
  204. status: '1',
  205. source: that.info._id
  206. }
  207. res = await that.$api(`/comment`, 'GET', {
  208. ...info,
  209. });
  210. if (res.errcode == '0') {
  211. for (let val of res.data) {
  212. const user = await that.$api(`/user/${val.user}`, 'GET', {});
  213. if (user.errcode == '0') {
  214. val.user_file = user.data.logo
  215. val.user_name = user.data.name || user.data.nick_name
  216. }
  217. }
  218. let list = [...that.list, ...res.data];
  219. that.$set(that, `list`, list);
  220. that.$set(that, `total`, res.total)
  221. } else {
  222. uni.showToast({
  223. title: res.errmsg,
  224. icon: 'none'
  225. })
  226. }
  227. },
  228. // 查询字典表
  229. searchDict(e, model) {
  230. const that = this;
  231. let data
  232. if (model == 'type') {
  233. data = that.typeList.find((i) => i.value == e);
  234. if (data) return data.label
  235. else return '暂无'
  236. }
  237. },
  238. // 滑动到评论专区
  239. pageScroll() {
  240. this.$set(this, `scrollTop`, 0);
  241. this.$nextTick(() => {
  242. setTimeout(() => {
  243. uni.createSelectorQuery().in(this).select(`.thr`).boundingClientRect(res => {
  244. this.$set(this, `scrollTop`, res.top);
  245. }).exec();
  246. }, 100);
  247. });
  248. },
  249. // 点赞/取消点赞
  250. async toLike(type) {
  251. const that = this;
  252. const info = that.info
  253. let res;
  254. let like;
  255. if (type == '0') like = info.is_like
  256. else like = info.is_collect
  257. if (like == false) {
  258. const form = {
  259. user: that.user._id,
  260. source: info._id,
  261. type: type,
  262. create_time: moment().format('YYYY-MM-DD HH:mm:ss'),
  263. }
  264. res = await that.$api(`/like`, 'POST', form);
  265. if (res.errcode == '0') {
  266. that.clearPage();
  267. that.search()
  268. }
  269. } else {
  270. res = await that.$api(`/like`, 'GET', {
  271. source: info._id,
  272. user: that.user._id,
  273. type: type
  274. });
  275. if (res.errcode == '0' && res.total > 0) {
  276. await that.$api(`/like/${res.data[0]._id}`, 'DELETE', {})
  277. that.clearPage();
  278. that.search()
  279. }
  280. }
  281. },
  282. // 发送
  283. async toSend(content) {
  284. const that = this;
  285. if (that.user && that.user._id) {
  286. const form = {
  287. user: that.user._id,
  288. user_name: that.user.name || that.user.nick_name,
  289. source: that.info._id,
  290. content,
  291. create_time: moment().format('YYYY-MM-DD HH:mm:ss'),
  292. }
  293. let res = await that.$api(`/comment`, 'POST', form);
  294. if (res.errcode == '0') {
  295. uni.showToast({
  296. title: '评论成功 等待审核',
  297. icon: 'none'
  298. })
  299. that.$set(that, `content`, '')
  300. that.searchComment()
  301. } else {
  302. uni.showToast({
  303. title: res.errmsg,
  304. icon: 'none'
  305. })
  306. }
  307. } else {
  308. uni.navigateTo({
  309. url: `/pagesIndex/login/index`
  310. })
  311. }
  312. },
  313. // 分页
  314. toPage(e) {
  315. const that = this;
  316. let list = that.list;
  317. let limit = that.limit;
  318. if (that.total > list.length) {
  319. uni.showLoading({
  320. title: '加载中',
  321. mask: true
  322. })
  323. let page = that.page + 1;
  324. that.$set(that, `page`, page)
  325. let skip = page * limit;
  326. that.$set(that, `skip`, skip)
  327. that.searchComment();
  328. uni.hideLoading();
  329. } else that.$set(that, `is_bottom`, true)
  330. },
  331. // 触底
  332. toScroll(e) {
  333. const that = this;
  334. let up = that.scrollTop;
  335. that.$set(that, `scrollTop`, e.detail.scrollTop);
  336. let num = Math.sign(up - e.detail.scrollTop);
  337. if (num == 1) that.$set(that, `is_bottom`, false);
  338. },
  339. // 清空列表
  340. clearPage() {
  341. const that = this;
  342. that.$set(that, `list`, [])
  343. that.$set(that, `skip`, 0)
  344. that.$set(that, `limit`, 6)
  345. that.$set(that, `page`, 0)
  346. },
  347. // 查询其他信息
  348. async searchOther() {
  349. const that = this;
  350. let res;
  351. // 查询类型
  352. res = await that.$api(`/dictData`, 'GET', {
  353. type: 'home_tabs',
  354. is_use: '0',
  355. })
  356. if (res.errcode == '0') that.$set(that, `typeList`, res.data);
  357. },
  358. }
  359. }
  360. </script>
  361. <style lang="scss" scoped>
  362. .main {
  363. display: flex;
  364. flex-direction: column;
  365. box-sizing: border-box;
  366. width: 100vw;
  367. height: 100vh;
  368. .info {
  369. position: relative;
  370. flex-grow: 1;
  371. .two {
  372. padding: 0 2vw;
  373. .two_1 {
  374. font-weight: bold;
  375. font-size: var(--font16Size);
  376. }
  377. .two_2 {
  378. padding: 1vw;
  379. font-size: var(--font14Size);
  380. color: var(--f3CColor);
  381. }
  382. .two_4 {
  383. padding: 3vw 0;
  384. font-size: var(--font12Size);
  385. color: var(--f99Color);
  386. border-bottom: 1px solid var(--f9Color);
  387. }
  388. }
  389. .thr {
  390. .thr_1 {
  391. padding: 2vw 4vw;
  392. font-weight: 500;
  393. font-size: var(--font15Size);
  394. border-bottom: 1px solid var(--f9Color);
  395. }
  396. .list {
  397. display: flex;
  398. align-items: center;
  399. padding: 2vw;
  400. .image {
  401. width: 10vw;
  402. height: 10vw;
  403. border-radius: 10vw;
  404. }
  405. .other {
  406. display: flex;
  407. flex-direction: column;
  408. width: 80vw;
  409. padding: 2vw;
  410. border-bottom: 1px solid var(--f9Color);
  411. font-size: var(--font12Size);
  412. .name {
  413. color: var(--f85Color);
  414. padding: 2px 0;
  415. }
  416. }
  417. }
  418. }
  419. }
  420. .foot {
  421. padding: 2vw;
  422. background-color: var(--mainColor);
  423. .foot_1 {
  424. display: flex;
  425. justify-content: space-between;
  426. align-items: center;
  427. .left {
  428. .is-input-border {
  429. border-radius: 45px;
  430. }
  431. }
  432. .right {
  433. width: 45%;
  434. display: flex;
  435. justify-content: space-around;
  436. .right_1 {
  437. display: flex;
  438. align-items: center;
  439. font-size: var(--font17Szie);
  440. .iconfont {
  441. padding: 0 1vw;
  442. font-size: 18px;
  443. }
  444. }
  445. }
  446. }
  447. }
  448. }
  449. .scroll-view {
  450. position: absolute;
  451. top: 0;
  452. left: 0;
  453. right: 0;
  454. bottom: 0;
  455. .list-scroll-view {
  456. display: flex;
  457. flex-direction: column;
  458. }
  459. }
  460. .is_bottom {
  461. width: 100%;
  462. text-align: center;
  463. text {
  464. padding: 2vw 0;
  465. display: inline-block;
  466. color: var(--f85Color);
  467. font-size: var(--font14Size);
  468. }
  469. }
  470. </style>