微信小程序-下拉滚动加载数据

1.实现流程图

所谓工欲善其事,必先利其器,做程序之前咱们把思路整理一下 ,画流程图是比较好的方式

2.前端JS 和 WXML

视图绑定 scrollToLower 方法 ,当离底部还有100时候触发;

WXML:

复制代码
<scroll-view scroll-y lower-threshold="100" bindscrolltolower="scrollToLower" style="height: 100vh;">
    <view class="items" wx:for="{{list}}" wx:key="{{item.id}}">
        <image src="{{item.img}}"/>
        <view class="datas">
            <view class="beauty">订单金额:{{item.price}}元</view>
            <view class="times">2022-09-30</view>
        </view>
    </view>
</scroll-view>
<view class='text-style'>{{text}}</view>

datalock 等于1 时, 不发出网络请求, 已免不必要的网络开销;

数据返回时 使用 concat 拼接

JS:

html 复制代码
    data: {
        list:[],
        page:1,
        text:'',
        datalock:0
    },
    onLoad: function (options) {
        this.getDatas();
    },
    scrollToLower:function(){
        if(this.data.datalock==1){
            return false;
        }else{
            wx.showToast({
                title: '加载中...',
                icon:"loading",
                duration:600
            });
            this.getDatas();
        }  
    },getDatas:function(){
        var that = this;
        wx.request({
            url:'https://www.test.com/loaddata',
            method:"GET",
            data:{
                token:wx.getStorageSync('userid'),
                page:that.data.page,
            },
            success:function(res){
                //console.log(res.data);
                if(res.data.data.length > 0 ){
                    let newlist = that.data.list.concat(res.data.data); //拼接数据
                    for(let i=0;i<newlist.length;i++){
                        newlist[i].jsonformat =  JSON.parse(newlist[i].json);
                        newlist[i].timeformat =  tools.formatTime(newlist[i].addtime,'Y-M-D h:m');
                    }
                    that.setData({
                        list : newlist,
                        page : that.data.page +1,
                        text: '加载更多'
                    });
                    //console.log(that.data.list);
                }else{
                    that.setData({
                        text: '到底啦!',
                        datalock:1
                    });
                }
                
            },fail:function(res){
                console.log(res);
                
            }
        });
    }
复制代码
    

3.后端代码

这里使用tp6.1 来做接口通讯

php 复制代码
use think\Request;
use think\Db;
use think\Config;
...
public function loaddata(Request $request){
	$token = trim($request->param('token'));
	$page = intval($request->param('page'));
	$pageNum = 5;
	if(!$page){
		$page=1;
	}
	$res_data = Db::name('usercolor')->where("uidtoken='".$token."'")->order('id desc')->limit(($page-1)*$pageNum,$pageNum)->select();
	if($res_data){
		make_jsons($res_data);
		exit(); 
	}else{
		make_jsons([],400,'error');
		exit();
	}
}
...

顺带附上make_jsons方法:

php 复制代码
function make_jsons($dataArr=[],$code=200,$message='suc'){

	$codedata = [  
		'status' => $code,  
		'message' => $message,  
		'data' =>$dataArr,  
	];  
	echo  json_encode($codedata);
	exit();
	
}
相关推荐
游戏开发爱好者81 小时前
日常开发与测试的 App 测试方法、查看设备状态、实时日志、应用数据
android·ios·小程序·https·uni-app·iphone·webview
2501_915106323 小时前
app 上架过程,安装包准备、证书与描述文件管理、安装测试、上传
android·ios·小程序·https·uni-app·iphone·webview
2501_915106324 小时前
使用 Sniffmaster TCP 抓包和 Wireshark 网络分析
网络协议·tcp/ip·ios·小程序·uni-app·wireshark·iphone
宠友信息5 小时前
2025社交+IM及时通讯社区APP仿小红书小程序
java·spring boot·小程序·uni-app·web app
“负拾捌”5 小时前
python + uniapp 结合腾讯云实现实时语音识别功能(WebSocket)
python·websocket·微信小程序·uni-app·大模型·腾讯云·语音识别
换日线°1 天前
NFC标签打开微信小程序
前端·微信小程序
光影少年1 天前
AIGC + Taro / 小程序
小程序·aigc·taro
2501_915918411 天前
在 iOS 环境下查看 App 详细信息与文件目录
android·ios·小程序·https·uni-app·iphone·webview
2501_916007471 天前
没有 Mac 用户如何上架 App Store,IPA生成、证书与描述文件管理、跨平台上传
android·macos·ios·小程序·uni-app·iphone·webview
天空属于哈夫克31 天前
Go 语言实战:构建一个企微外部群“技术贴收藏夹”小程序后端
小程序·golang·企业微信