index.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. 1111
  11. </view>
  12. <view class="is_bottom" v-if="is_bottom">
  13. <text>{{config.bottom_title||'到底了!'}}</text>
  14. </view>
  15. </view>
  16. </scroll-view>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. export default {
  22. data() {
  23. return {
  24. config: {},
  25. user: {},
  26. list: [],
  27. total: 0,
  28. skip: 0,
  29. limit: 6,
  30. page: 0,
  31. // 数据是否触底
  32. is_bottom: false,
  33. scrollTop: 0,
  34. }
  35. },
  36. onLoad: async function(e) {
  37. const that = this;
  38. that.searchToken();
  39. that.searchConfig();
  40. },
  41. onShow: async function() {
  42. const that = this;
  43. // that.clearPage();
  44. // await that.search();
  45. },
  46. onPullDownRefresh: async function() {
  47. const that = this;
  48. // that.clearPage();
  49. // await that.search();
  50. uni.stopPullDownRefresh();
  51. },
  52. methods: {
  53. // 用户信息
  54. searchToken() {
  55. const that = this;
  56. try {
  57. const res = uni.getStorageSync('token');
  58. if (res) {
  59. const user = that.$jwt(res);
  60. that.$set(that, `user`, user);
  61. }
  62. } catch (e) {}
  63. },
  64. searchConfig() {
  65. const that = this;
  66. try {
  67. const res = uni.getStorageSync('config');
  68. if (res) that.$set(that, `config`, res);
  69. } catch (e) {}
  70. },
  71. async search() {
  72. const that = this;
  73. let info = {
  74. skip: that.skip,
  75. limit: that.limit,
  76. is_use: '0'
  77. }
  78. const res = await that.$api(`/article`, 'GET', {
  79. ...info,
  80. ...that.searchInfo
  81. })
  82. if (res.errcode == '0') {
  83. let list = [...that.list, ...res.data];
  84. that.$set(that, `list`, list)
  85. that.$set(that, `total`, res.total)
  86. } else {
  87. uni.showToast({
  88. title: res.errmsg,
  89. icon: 'none'
  90. });
  91. }
  92. },
  93. // 输入框
  94. toInput(e) {
  95. const that = this;
  96. if (that.searchInfo.title) that.$set(that.searchInfo, `title`, e.detail.value)
  97. else that.$set(that, `searchInfo`, {})
  98. that.clearPage();
  99. that.search();
  100. },
  101. // 查看详情
  102. toInfo(item) {
  103. uni.navigateTo({
  104. url: `/pagesScience/science/index?id=${item.id||item._id}`
  105. })
  106. },
  107. // 分页
  108. toPage(e) {
  109. const that = this;
  110. let list = that.list;
  111. let limit = that.limit;
  112. if (that.total > list.length) {
  113. uni.showLoading({
  114. title: '加载中',
  115. mask: true
  116. })
  117. let page = that.page + 1;
  118. that.$set(that, `page`, page)
  119. let skip = page * limit;
  120. that.$set(that, `skip`, skip)
  121. that.search();
  122. uni.hideLoading();
  123. } else that.$set(that, `is_bottom`, true)
  124. },
  125. toScroll(e) {
  126. const that = this;
  127. let up = that.scrollTop;
  128. that.$set(that, `scrollTop`, e.detail.scrollTop);
  129. let num = Math.sign(up - e.detail.scrollTop);
  130. if (num == 1) that.$set(that, `is_bottom`, false);
  131. },
  132. // 清空列表
  133. clearPage() {
  134. const that = this;
  135. that.$set(that, `list`, [])
  136. that.$set(that, `skip`, 0)
  137. that.$set(that, `limit`, 6)
  138. that.$set(that, `page`, 0)
  139. }
  140. }
  141. }
  142. </script>
  143. <style lang="scss" scoped>
  144. .main {
  145. display: flex;
  146. flex-direction: column;
  147. width: 100vw;
  148. height: 100vh;
  149. .one {
  150. display: flex;
  151. justify-content: center;
  152. align-items: center;
  153. padding: 2vw;
  154. input {
  155. width: 100%;
  156. padding: 2vw;
  157. background-color: var(--f1Color);
  158. font-size: var(--font14Size);
  159. border-radius: 5px;
  160. }
  161. }
  162. .two {
  163. position: relative;
  164. flex-grow: 1;
  165. background-color: var(--f9Color);
  166. margin: 2vw 0 0 0;
  167. .list {
  168. display: flex;
  169. background-color: var(--mainColor);
  170. border: 1px solid var(--f5Color);
  171. padding: 2vw;
  172. margin: 2vw 2vw 0 2vw;
  173. border-radius: 5px;
  174. }
  175. }
  176. }
  177. .scroll-view {
  178. position: absolute;
  179. top: 0;
  180. left: 0;
  181. right: 0;
  182. bottom: 0;
  183. .list-scroll-view {
  184. display: flex;
  185. flex-direction: column;
  186. }
  187. }
  188. .is_bottom {
  189. width: 100%;
  190. text-align: center;
  191. text {
  192. padding: 2vw 0;
  193. display: inline-block;
  194. color: var(--f85Color);
  195. font-size: var(--font14Size);
  196. }
  197. }
  198. </style>