当前位置:   article > 正文

微信小程序---上拉/下拉获取信息组件_微信小程序下拉列表组件

微信小程序下拉列表组件

1. 前端

1.1 app.json配置(默认不支持下拉)

局部配置,如果想全局都可以下拉,那配置在全局的app.json即可

  1. {
  2. "enablePullDownRefresh": true
  3. }

1.1 html

  1. <!-- 表面信息 -->
  2. <view class="index_item" wx:for="{{ index_data_list }}" wx:key="index">
  3. <navigator url="/pages/inside/inside?id={{ item.id }}"> <text>用户:{{ item.nickName }}</text> </navigator>
  4. <text>内容:{{ item.content }}</text>
  5. <text>话题:{{ item.topic }} </text>
  6. <text>发布时间:{{ item.update_time }}</text>
  7. </view>

1.2 js

  1. // pages/index/index.js
  2. var api = require('../../config/app')
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. index_data_list: [],
  9. MinId: 0,
  10. MaxId: 0
  11. },
  12. /**
  13. * 生命周期函数--监听页面加载
  14. */
  15. initMessage: function () {
  16. wx.request({
  17. url: api.IndexAPI,
  18. dataType: "json",
  19. method: "GET",
  20. success: (res) => {
  21. this.setData({
  22. index_data_list: res.data,
  23. MinId: res.data[res.data.length - 1].id,
  24. MaxId: res.data[0].id
  25. })
  26. },
  27. })
  28. },
  29. onLoad: function (options) {
  30. this.initMessage();
  31. },
  32. * 页面相关事件处理函数--监听用户下拉动作
  33. */
  34. onPullDownRefresh: function () {
  35. this.pullData(true);
  36. },
  37. pullData: function (status) {
  38. if (!status) {
  39. wx.request({
  40. url: api.IndexAPI,
  41. data: {
  42. min_id: this.data.MinId
  43. },
  44. dataType: "json",
  45. method: "GET",
  46. success: (result) => {
  47. // console.log(result.data)
  48. if (result.data.length > 0) {
  49. this.setData({
  50. index_data_list: this.data.index_data_list.concat(result.data),
  51. MinId: result.data[result.data.length - 1].id,
  52. //MaxId: result.data[0].id
  53. })
  54. } else {
  55. wx.showToast({
  56. title: '到底部啦',
  57. icon: "none"
  58. })
  59. return
  60. }
  61. },
  62. })
  63. } else {
  64. wx.request({
  65. url: api.IndexAPI,
  66. data: {
  67. max_id: this.data.MaxId
  68. },
  69. dataType: "json",
  70. method: "GET",
  71. success: (result) => {
  72. // console.log(result.data)
  73. var new_list = result.data.reverse() // 数据是正向的
  74. if (new_list.length > 0) {
  75. this.setData({
  76. index_data_list: new_list.concat(this.data.index_data_list),
  77. MinId: new_list[new_list.length - 1].id,
  78. MaxId: new_list[0].id
  79. })
  80. } else {
  81. wx.showToast({
  82. title: '无最新动态',
  83. icon: "none"
  84. })
  85. return
  86. }
  87. },
  88. })
  89. }
  90. },
  91. /**
  92. * 页面上拉触底事件的处理函数
  93. */
  94. onReachBottom: function () {
  95. //
  96. this.pullData(false);
  97. },
  98. })

2. 后端

2.1 url

  1. # 首页内容
  2. path('index/data/', views.IndexDataListAPIView.as_view()),

2.2 view

  1. from rest_framework.generics import ListAPIView, CreateAPIView, RetrieveAPIView
  2. from utils.private import filters, pagination
  3. from .serislizer import IndexDataModelSerializer
  4. class IndexDataListAPIView(ListAPIView):
  5. """首页展示信息"""
  6. queryset = models.Release.objects.filter(is_show=True, is_delete=False).order_by('-id')
  7. serializer_class = IndexDataModelSerializer
  8. filter_backends = [filters.MaxFilterBackend, filters.MinFilterBackend]
  9. pagination_class = pagination.DataLimitPagination

2.3 filter

  1. from rest_framework.filters import BaseFilterBackend
  2. class MinFilterBackend(BaseFilterBackend):
  3. """
  4. 前端传:min_id/max_id
  5. """
  6. def filter_queryset(self, request, queryset, view):
  7. # 给视图类的queryset,添加新的条件
  8. # 就是我们在类里面配置的
  9. # queryset=models.Release.objects.filter(is_show=True, is_delete=False).order_by('-id')
  10. min_id = request.query_params.get('min_id')
  11. print(min_id)
  12. if min_id:
  13. return queryset.filter(id__lt=min_id).order_by('-id')
  14. return queryset
  15. class MaxFilterBackend(BaseFilterBackend):
  16. def filter_queryset(self, request, queryset, view):
  17. max_id = request.query_params.get('max_id')
  18. if max_id:
  19. return queryset.filter(id__gt=max_id).order_by('id')
  20. return queryset

2.4 page

  1. from rest_framework.pagination import LimitOffsetPagination
  2. from rest_framework.response import Response
  3. class DataLimitPagination(LimitOffsetPagination):
  4. """
  5. 本质上帮助我们进行切片的处理:[0:N]
  6. """
  7. default_limit = 10 # 默认返回数据
  8. max_limit = 50 # 最大返回数据
  9. limit_query_param = 'limit' # 前端查询的参数
  10. offset_query_param = 'offset'
  11. # offset 偏移数 GET http://127.0.0.1/four/students/?limit=100&offset=400 从下标为400的记录开始,取100条记录
  12. def get_offset(self, request):
  13. # 重写偏移数,修改成0
  14. return 0
  15. def get_paginated_response(self, data):
  16. # 修改返回的界面,把多余的去掉,比如总数据,上一页/下一页
  17. return Response(data)

2.5 serializer

  1. class IndexDetailDataModelSerializer(serializers.ModelSerializer):
  2. """首页的详情页返回的数据"""
  3. topic = serializers.CharField(source='topic.name')
  4. nickName = serializers.CharField(source='userinfo.nickName')
  5. class Meta:
  6. model = models.Release
  7. fields = [
  8. 'id', 'position', 'content', 'topic', 'nickName', 'update_time', 'msg_url'
  9. ]

2.6 models

  1. class Release(BaseModel):
  2. """发布"""
  3. position = models.CharField(verbose_name='位置', max_length=225, blank=True, null=True)
  4. content = models.CharField(verbose_name='内容', max_length=225)
  5. fucking_great = models.CharField(verbose_name='点赞', default=0, max_length=666)
  6. watch_num = models.CharField(verbose_name='观看人数', default=0, max_length=666)
  7. comment = models.ForeignKey(verbose_name='评论', to='self', on_delete=models.DO_NOTHING, db_constraint=False,
  8. null=True, blank=True, related_name='release')
  9. # db_constraint=False db_constraint=False 逻辑上的关联,实质上没有外键联系,增删不会受外键影响,orm查询不影响
  10. userinfo = models.ForeignKey(verbose_name='用户', to='UserInfo', on_delete=models.DO_NOTHING, db_constraint=False)
  11. topic = models.ForeignKey(verbose_name='话题', to='Topic', on_delete=models.DO_NOTHING, db_constraint=False)
  12. # 图片链接拼接
  13. @property
  14. def cover_msg_url(self):
  15. # 每一个对象走一遍
  16. msg_url = self.media.all().filter(is_show=True, is_delete=False).first().cosUrl
  17. # print(msg_url)
  18. return msg_url
  19. # 图片链接拼接
  20. @property
  21. def msg_url(self):
  22. msg_url = self.media.all().filter(is_show=True, is_delete=False).values('cosUrl')
  23. return msg_url
  24. class Media(BaseModel):
  25. name = models.CharField(verbose_name='名称', max_length=225, blank=True, null=True)
  26. choice_type = (
  27. (1, "无"),
  28. (2, "图片"),
  29. (3, "视频"),
  30. )
  31. type = models.SmallIntegerField(verbose_name='类型', default=1)
  32. cosUrl = models.CharField(verbose_name='路径', max_length=225, blank=True, null=True)
  33. release = models.ForeignKey(verbose_name='发布', to='Release', related_name='media', on_delete=models.DO_NOTHING,
  34. db_constraint=False,
  35. null=True, blank=True)
  36. cosETag = models.CharField(verbose_name='校验信息', max_length=225, blank=True, null=True)
  37. cos_media_name = models.CharField(verbose_name='cos图片名称,删除使用', max_length=225, default='1.png')
  38. class Topic(BaseModel):
  39. """话题"""
  40. name = models.CharField(verbose_name='话题名称', max_length=225)
  41. people_num = models.BigIntegerField(verbose_name='关注人数')
  42. class Meta:
  43. verbose_name = "话题"
  44. verbose_name_plural = "话题"

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/725963
推荐阅读
相关标签