index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. source_type: "article",
  264. type: type,
  265. create_time: moment().format('YYYY-MM-DD HH:mm:ss'),
  266. }
  267. res = await that.$api(`/like`, 'POST', form);
  268. if (res.errcode == '0') {
  269. that.clearPage();
  270. that.search()
  271. }
  272. } else {
  273. res = await that.$api(`/like`, 'GET', {
  274. source: info._id,
  275. user: that.user._id,
  276. type: type
  277. });
  278. if (res.errcode == '0' && res.total > 0) {
  279. await that.$api(`/like/${res.data[0]._id}`, 'DELETE', {})
  280. that.clearPage();
  281. that.search()
  282. }
  283. }
  284. },
  285. // 发送
  286. async toSend(content) {
  287. const that = this;
  288. if (that.user && that.user._id) {
  289. const form = {
  290. user: that.user._id,
  291. user_name: that.user.name || that.user.nick_name,
  292. source: that.info._id,
  293. content,
  294. create_time: moment().format('YYYY-MM-DD HH:mm:ss'),
  295. }
  296. let res = await that.$api(`/comment`, 'POST', form);
  297. if (res.errcode == '0') {
  298. uni.showToast({
  299. title: '评论成功 等待审核',
  300. icon: 'none'
  301. })
  302. that.$set(that, `content`, '')
  303. that.searchComment()
  304. } else {
  305. uni.showToast({
  306. title: res.errmsg,
  307. icon: 'none'
  308. })
  309. }
  310. } else {
  311. uni.navigateTo({
  312. url: `/pagesIndex/login/index`
  313. })
  314. }
  315. },
  316. // 分页
  317. toPage(e) {
  318. const that = this;
  319. let list = that.list;
  320. let limit = that.limit;
  321. if (that.total > list.length) {
  322. uni.showLoading({
  323. title: '加载中',
  324. mask: true
  325. })
  326. let page = that.page + 1;
  327. that.$set(that, `page`, page)
  328. let skip = page * limit;
  329. that.$set(that, `skip`, skip)
  330. that.searchComment();
  331. uni.hideLoading();
  332. } else that.$set(that, `is_bottom`, true)
  333. },
  334. // 触底
  335. toScroll(e) {
  336. const that = this;
  337. // let up = that.scrollTop;
  338. // that.$set(that, `scrollTop`, e.detail.scrollTop);
  339. // let num = Math.sign(up - e.detail.scrollTop);
  340. // if (num == 1) that.$set(that, `is_bottom`, false);
  341. },
  342. // 清空列表
  343. clearPage() {
  344. const that = this;
  345. that.$set(that, `list`, [])
  346. that.$set(that, `skip`, 0)
  347. that.$set(that, `limit`, 6)
  348. that.$set(that, `page`, 0)
  349. },
  350. // 查询其他信息
  351. async searchOther() {
  352. const that = this;
  353. let res;
  354. // 查询类型
  355. res = await that.$api(`/dictData`, 'GET', {
  356. type: 'home_tabs',
  357. is_use: '0',
  358. })
  359. if (res.errcode == '0') that.$set(that, `typeList`, res.data);
  360. },
  361. }
  362. }
  363. </script>
  364. <style lang="scss" scoped>
  365. .main {
  366. display: flex;
  367. flex-direction: column;
  368. box-sizing: border-box;
  369. width: 100vw;
  370. height: 100vh;
  371. .info {
  372. position: relative;
  373. flex-grow: 1;
  374. .two {
  375. padding: 0 2vw;
  376. .two_1 {
  377. font-weight: bold;
  378. font-size: var(--font16Size);
  379. }
  380. .two_2 {
  381. padding: 1vw;
  382. font-size: var(--font14Size);
  383. color: var(--f3CColor);
  384. }
  385. .two_4 {
  386. padding: 3vw 0;
  387. font-size: var(--font12Size);
  388. color: var(--f99Color);
  389. border-bottom: 1px solid var(--f9Color);
  390. }
  391. }
  392. .thr {
  393. .thr_1 {
  394. padding: 2vw 4vw;
  395. font-weight: 500;
  396. font-size: var(--font15Size);
  397. border-bottom: 1px solid var(--f9Color);
  398. }
  399. .list {
  400. display: flex;
  401. align-items: center;
  402. padding: 2vw;
  403. .image {
  404. width: 10vw;
  405. height: 10vw;
  406. border-radius: 10vw;
  407. }
  408. .other {
  409. display: flex;
  410. flex-direction: column;
  411. width: 80vw;
  412. padding: 2vw;
  413. border-bottom: 1px solid var(--f9Color);
  414. font-size: var(--font12Size);
  415. .name {
  416. color: var(--f85Color);
  417. padding: 2px 0;
  418. }
  419. }
  420. }
  421. }
  422. }
  423. .foot {
  424. padding: 2vw;
  425. background-color: var(--mainColor);
  426. .foot_1 {
  427. display: flex;
  428. justify-content: space-between;
  429. align-items: center;
  430. .left {
  431. .is-input-border {
  432. border-radius: 45px;
  433. }
  434. }
  435. .right {
  436. width: 45%;
  437. display: flex;
  438. justify-content: space-around;
  439. .right_1 {
  440. display: flex;
  441. align-items: center;
  442. font-size: var(--font17Size);
  443. .iconfont {
  444. padding: 0 1vw;
  445. font-size: 18px;
  446. }
  447. }
  448. }
  449. }
  450. }
  451. }
  452. .scroll-view {
  453. position: absolute;
  454. top: 0;
  455. left: 0;
  456. right: 0;
  457. bottom: 0;
  458. .list-scroll-view {
  459. display: flex;
  460. flex-direction: column;
  461. }
  462. }
  463. .is_bottom {
  464. width: 100%;
  465. text-align: center;
  466. text {
  467. padding: 2vw 0;
  468. display: inline-block;
  469. color: var(--f85Color);
  470. font-size: var(--font14Size);
  471. }
  472. }
  473. </style>