index.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <template>
  2. <view class="main">
  3. <view class="one">
  4. <input type="text" v-model="searchInfo.title" @input="toInput" placeholder="搜索标题">
  5. </view>
  6. <view class="two">
  7. <scroll-view scroll-y="true" class="scroll-view" @scrolltolower="toPage" @scroll="toScroll">
  8. <view class="list-scroll-view">
  9. <view class="list" v-for="(item, index) in list" :key="index" @tap="toInfo(item)">
  10. <view class="list_1">
  11. <view class="left">
  12. <image class="image"
  13. :src="item.doctor.icon&&item.doctor.icon.length>0?item.doctor.icon[0].url:config.user_url[0].url"
  14. mode="">
  15. </image>
  16. <text>{{item.doctor.name||'暂无'}}</text>
  17. </view>
  18. <view class="right">{{formatDate(item.create_time||'暂无')}}</view>
  19. </view>
  20. <view class="list_2">
  21. <image class="image" :src="item.img_url&&item.img_url.length>0?item.img_url[0].url:''"
  22. mode="">
  23. </image>
  24. </view>
  25. <view class="list_3">
  26. <text>{{item.title||'暂无标题'}}</text>
  27. </view>
  28. </view>
  29. <view class="is_bottom" v-if="is_bottom">
  30. <text>{{config.bottom_title||'到底了!'}}</text>
  31. </view>
  32. </view>
  33. </scroll-view>
  34. </view>
  35. </view>
  36. </template>
  37. <script>
  38. export default {
  39. data() {
  40. return {
  41. config: {},
  42. user: {},
  43. searchInfo: {},
  44. list: [],
  45. total: 0,
  46. skip: 0,
  47. limit: 5,
  48. page: 0,
  49. // 数据是否触底
  50. is_bottom: false,
  51. scrollTop: 0,
  52. }
  53. },
  54. onLoad: async function(e) {
  55. const that = this;
  56. that.searchToken();
  57. that.searchConfig();
  58. },
  59. onShow: async function() {
  60. const that = this;
  61. that.clearPage();
  62. await that.search();
  63. },
  64. onPullDownRefresh: async function() {
  65. const that = this;
  66. that.clearPage();
  67. await that.search();
  68. uni.stopPullDownRefresh();
  69. },
  70. methods: {
  71. // 用户信息
  72. searchToken() {
  73. const that = this;
  74. try {
  75. const res = uni.getStorageSync('token');
  76. if (res) {
  77. const user = that.$jwt(res);
  78. that.$set(that, `user`, user);
  79. }
  80. } catch (e) {}
  81. },
  82. searchConfig() {
  83. const that = this;
  84. try {
  85. const res = uni.getStorageSync('config');
  86. if (res) that.$set(that, `config`, res);
  87. } catch (e) {}
  88. },
  89. async search() {
  90. const that = this;
  91. let info = {
  92. skip: that.skip,
  93. limit: that.limit,
  94. is_use: '0'
  95. }
  96. const res = await that.$api(`/article`, 'GET', {
  97. ...info,
  98. ...that.searchInfo
  99. })
  100. if (res.errcode == '0') {
  101. let list = [...that.list, ...res.data];
  102. that.$set(that, `list`, list)
  103. that.$set(that, `total`, res.total)
  104. } else {
  105. uni.showToast({
  106. title: res.errmsg,
  107. icon: 'none'
  108. });
  109. }
  110. },
  111. // 输入框
  112. toInput(e) {
  113. const that = this;
  114. if (that.searchInfo.title) that.$set(that.searchInfo, `title`, e.detail.value)
  115. else that.$set(that, `searchInfo`, {})
  116. that.clearPage();
  117. that.search();
  118. },
  119. // 查看详情
  120. toInfo(item) {
  121. uni.navigateTo({
  122. url: `/pagesScience/science/index?id=${item.id||item._id}&title=${item.title}`
  123. })
  124. },
  125. // 处理时间
  126. formatDate(value) {
  127. if (typeof(value) == 'undefined') {
  128. return ''
  129. } else {
  130. let date = new Date(value)
  131. let now = new Date()
  132. let y = date.getFullYear()
  133. let MM = date.getMonth() + 1
  134. MM = MM < 10 ? ('0' + MM) : MM
  135. let d = date.getDate()
  136. d = d < 10 ? ('0' + d) : d
  137. let h = date.getHours()
  138. h = h < 10 ? ('0' + h) : h
  139. let m = date.getMinutes()
  140. m = m < 10 ? ('0' + m) : m
  141. let s = date.getSeconds()
  142. s = s < 10 ? ('0' + s) : s
  143. if (now.getDate() - d == 1 && now - date < 172800000) {
  144. return '昨天' + h + ':' + m
  145. } else if (now - date < 86400000) {
  146. return h + ':' + m
  147. } else if (now - date >= 86400000 && now - date < 31536000000) {
  148. return MM + '-' + d + ' ' + h + ':' + m
  149. } else if (now - date >= 31536000000) {
  150. return y + '-' + MM + '-' + d + ' ' + h + ':' + m
  151. }
  152. }
  153. },
  154. // 分页
  155. toPage(e) {
  156. const that = this;
  157. let list = that.list;
  158. let limit = that.limit;
  159. if (that.total > list.length) {
  160. uni.showLoading({
  161. title: '加载中',
  162. mask: true
  163. })
  164. let page = that.page + 1;
  165. that.$set(that, `page`, page)
  166. let skip = page * limit;
  167. that.$set(that, `skip`, skip)
  168. that.search();
  169. uni.hideLoading();
  170. } else that.$set(that, `is_bottom`, true)
  171. },
  172. toScroll(e) {
  173. const that = this;
  174. let up = that.scrollTop;
  175. that.$set(that, `scrollTop`, e.detail.scrollTop);
  176. let num = Math.sign(up - e.detail.scrollTop);
  177. if (num == 1) that.$set(that, `is_bottom`, false);
  178. },
  179. // 清空列表
  180. clearPage() {
  181. const that = this;
  182. that.$set(that, `list`, [])
  183. that.$set(that, `skip`, 0)
  184. that.$set(that, `limit`, 5)
  185. that.$set(that, `page`, 0)
  186. }
  187. }
  188. }
  189. </script>
  190. <style lang="scss" scoped>
  191. .main {
  192. display: flex;
  193. flex-direction: column;
  194. width: 100vw;
  195. height: 100vh;
  196. .one {
  197. display: flex;
  198. justify-content: center;
  199. align-items: center;
  200. padding: 2vw;
  201. input {
  202. width: 100%;
  203. padding: 2vw;
  204. background-color: var(--f1Color);
  205. font-size: var(--font14Size);
  206. border-radius: 5px;
  207. }
  208. }
  209. .two {
  210. position: relative;
  211. flex-grow: 1;
  212. background-color: var(--f9Color);
  213. margin: 2vw 0 0 0;
  214. .list {
  215. background-color: var(--mainColor);
  216. border: 1px solid var(--f5Color);
  217. padding: 2vw 0;
  218. margin: 2vw 2vw 0 2vw;
  219. border-radius: 10px;
  220. .list_1 {
  221. display: flex;
  222. justify-content: space-between;
  223. align-items: center;
  224. padding: 2vw;
  225. .left {
  226. display: flex;
  227. align-items: center;
  228. font-size: var(--font16Size);
  229. color: var(--f191Color);
  230. .image {
  231. width: 10vw;
  232. height: 10vw;
  233. border-radius: 10vw;
  234. margin: 0 2vw 0 0;
  235. }
  236. }
  237. .right {
  238. font-size: var(--font12Size);
  239. color: var(--f99Color);
  240. }
  241. }
  242. .list_2 {
  243. .image {
  244. width: 100%;
  245. height: 30vh;
  246. }
  247. }
  248. .list_3 {
  249. padding: 2vw;
  250. font-size: var(--font16Size);
  251. }
  252. }
  253. }
  254. }
  255. .scroll-view {
  256. position: absolute;
  257. top: 0;
  258. left: 0;
  259. right: 0;
  260. bottom: 0;
  261. .list-scroll-view {
  262. display: flex;
  263. flex-direction: column;
  264. }
  265. }
  266. .is_bottom {
  267. width: 100%;
  268. text-align: center;
  269. text {
  270. padding: 2vw 0;
  271. display: inline-block;
  272. color: var(--f85Color);
  273. font-size: var(--font14Size);
  274. }
  275. }
  276. </style>