index.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. searchToken() {
  54. const that = this;
  55. try {
  56. const res = uni.getStorageSync('token');
  57. if (res) that.$set(that, `user`, res);
  58. } catch (e) {
  59. uni.showToast({
  60. title: err.errmsg,
  61. icon: 'error',
  62. duration: 2000
  63. });
  64. }
  65. },
  66. searchConfig() {
  67. const that = this;
  68. try {
  69. const res = uni.getStorageSync('config');
  70. if (res) that.$set(that, `config`, res);
  71. } catch (e) {
  72. uni.showToast({
  73. title: err.errmsg,
  74. icon: 'error',
  75. duration: 2000
  76. });
  77. }
  78. },
  79. async search() {
  80. const that = this;
  81. let info = {
  82. skip: that.skip,
  83. limit: that.limit,
  84. is_use: '0'
  85. }
  86. const res = await that.$api(`/article`, 'GET', {
  87. ...info,
  88. ...that.searchInfo
  89. })
  90. if (res.errcode == '0') {
  91. let list = [...that.list, ...res.data];
  92. that.$set(that, `list`, list)
  93. that.$set(that, `total`, res.total)
  94. } else {
  95. uni.showToast({
  96. title: res.errmsg,
  97. });
  98. }
  99. },
  100. // 输入框
  101. toInput(e) {
  102. const that = this;
  103. if (that.searchInfo.title) that.$set(that.searchInfo, `title`, e.detail.value)
  104. else that.$set(that, `searchInfo`, {})
  105. that.clearPage();
  106. that.search();
  107. },
  108. // 查看详情
  109. toInfo(item) {
  110. uni.navigateTo({
  111. url: `/pagesScience/science/index?id=${item.id||item._id}`
  112. })
  113. },
  114. // 分页
  115. toPage(e) {
  116. const that = this;
  117. let list = that.list;
  118. let limit = that.limit;
  119. if (that.total > list.length) {
  120. uni.showLoading({
  121. title: '加载中',
  122. mask: true
  123. })
  124. let page = that.page + 1;
  125. that.$set(that, `page`, page)
  126. let skip = page * limit;
  127. that.$set(that, `skip`, skip)
  128. that.search();
  129. uni.hideLoading();
  130. } else that.$set(that, `is_bottom`, true)
  131. },
  132. toScroll(e) {
  133. const that = this;
  134. let up = that.scrollTop;
  135. that.$set(that, `scrollTop`, e.detail.scrollTop);
  136. let num = Math.sign(up - e.detail.scrollTop);
  137. if (num == 1) that.$set(that, `is_bottom`, false);
  138. },
  139. // 清空列表
  140. clearPage() {
  141. const that = this;
  142. that.$set(that, `list`, [])
  143. that.$set(that, `skip`, 0)
  144. that.$set(that, `limit`, 6)
  145. that.$set(that, `page`, 0)
  146. }
  147. }
  148. }
  149. </script>
  150. <style lang="scss" scoped>
  151. .main {
  152. display: flex;
  153. flex-direction: column;
  154. width: 100vw;
  155. height: 100vh;
  156. .one {
  157. display: flex;
  158. justify-content: center;
  159. align-items: center;
  160. padding: 2vw;
  161. input {
  162. width: 100%;
  163. padding: 2vw;
  164. background-color: var(--f1Color);
  165. font-size: var(--font14Size);
  166. border-radius: 5px;
  167. }
  168. }
  169. .two {
  170. position: relative;
  171. flex-grow: 1;
  172. background-color: var(--f9Color);
  173. margin: 2vw 0 0 0;
  174. .list {
  175. display: flex;
  176. background-color: var(--mainColor);
  177. border: 1px solid var(--f5Color);
  178. padding: 2vw;
  179. margin: 2vw 2vw 0 2vw;
  180. border-radius: 5px;
  181. }
  182. }
  183. }
  184. .scroll-view {
  185. position: absolute;
  186. top: 0;
  187. left: 0;
  188. right: 0;
  189. bottom: 0;
  190. .list-scroll-view {
  191. display: flex;
  192. flex-direction: column;
  193. }
  194. }
  195. .is_bottom {
  196. width: 100%;
  197. text-align: center;
  198. text {
  199. padding: 2vw 0;
  200. display: inline-block;
  201. color: var(--f85Color);
  202. font-size: var(--font14Size);
  203. }
  204. }
  205. </style>