<template>
<gracePage headerBG="#FFFFFF" :rbWidth="80">
<view slot="gHeader" style="padding:0 25rpx;">
<graceNavBar :items="tabs" :currentIndex="currentIndex" @change="navChange"></graceNavBar>
</view>
<view slot="gBody" class="grace-flex-v1" id="gBody">
<swiper :style="{height:mainHeight+'px'}" :current="currentIndex" @change="swiperChange">
<swiper-item v-for="(news, newsIndex) in newsAll" :key="newsIndex">
<scroll-view scroll-y="true" :style="{height:mainHeight+'px'}" :scroll-top="scrollTops[currentIndex]"
@scrolltolower="scrollend" @scroll="scroll"
@touchstart="touchstart" @touchmove="touchmove" @touchend="touchend">
<graceReload ref="graceReload" width="700rpx" marginLeft="25rpx" @reload="reload"></graceReload>
<graceEmpty v-if="news == 'empty'">
<view slot="img" class="empty-view">
<!-- 请根据您的项目要求制作并更换为空图片 -->
<image class="empty-img" mode="widthFix" src="https://staticimgs.oss-cn-beijing.aliyuncs.com/empty.png"></image>
</view>
<text slot="text" class="grace-text-small grace-gray">本栏目暂无新闻</text>
</graceEmpty>
<view style="padding:0 25rpx;">
<block v-for="(item, index) in news" :key="index" v-if="news != 'empty'">
<view class="grace-news-list grace-border-b" v-if="item.imgs.length < 3" @tap="newsinfo">
<view class="grace-news-item">
<view class="grace-news-img grace-news-img-l">
<image :src="item.imgs[0]" mode="widthFix" class="grace-news-img-in"></image>
</view>
<view class="grace-news-body">
<text class="grace-news-title">{{item.title}}</text>
<text class="grace-news-desc">{{item.desc}}</text>
</view>
</view>
<view class="grace-news-info">
<view style="width:500rpx;" class="grace-nowrap">
<text class="grace-icons grace-news-info-text"> {{item.viewnum}}</text>
<text class="grace-icons grace-news-info-text" style="margin-left:30rpx;"> {{item.author}} · {{item.catename}}</text>
</view>
<text class="grace-icons grace-news-info-text"> 208</text>
</view>
</view>
<view class="grace-news-list grace-border-b" v-if="item.imgs.length >= 3" @tap="newsinfo">
<text class="grace-news-title">{{item.title}}</text>
<view class="grace-news-img-list">
<view class="grace-news-imgs" v-for="(img, imgIndex) in item.imgs" :key="imgIndex">
<image :src="img" mode="widthFix" class="grace-news-imgs-img"></image>
</view>
</view>
<view class="grace-news-info">
<view style="width:500rpx;" class="grace-nowrap">
<text class="grace-icons grace-news-info-text"> {{item.viewnum}}</text>
<text class="grace-icons grace-news-info-text" style="margin-left:30rpx;"> {{item.author}} · {{item.catename}}</text>
</view>
<text class="grace-icons grace-news-info-text"> 208</text>
</view>
</view>
</block>
</view>
<graceLoading :loadingType="loadingTypes[newsIndex]"></graceLoading>
</scroll-view>
</swiper-item>
</swiper>
</view>
<view slot="gRTArea">
<text class="to-top grace-icons" :class="[scrollTops[currentIndex] >= 100 ? 'to-top-show' : '']" @tap="totop"></text>
</view>
</gracePage>
</template>
<script>
import gracePage from "../../graceUI/components/gracePage.vue";
import graceNavBar from "../../graceUI/components/graceNavBar.vue";
import graceLoading from "../../graceUI/components/graceLoading.vue";
import graceEmpty from '../../graceUI/components/graceEmptyNew.vue';
import graceReload from '../../graceUI/components/graceReload.vue';
export default {
data() {
return {
currentIndex: 0,
mainHeight : 600,
//分类数据
tabsAll : [],
tabs : [],
// 新闻信息保存数组
newsAll : [],
// 每个选项卡对应的分页
pages : [],
// 加载状态
loadingTypes : [],
// 每个滚动区域的滚动值
scrollTops : [],
scrollTimer : null
}
},
onReady:function(){
setTimeout(()=>{
uni.createSelectorQuery().select('#gBody').fields(
{size: true}, (res) => {
this.mainHeight = res.height;
}
).exec();
},1000);
//加载分类信息 初始化数据
this.getCate();
},
watch:{
currentIndex : function (nVal, oldVal) {
if((this.loadingTypes[this.currentIndex] != 2 || this.loadingTypes[this.currentIndex] != 4) && this.pages[this.currentIndex] < 2){ this.getNews(); }
}
},
methods:{
navChange: function (e) {
this.currentIndex = e;
},
swiperChange: function (e) {
var index = e.detail.current;
this.currentIndex = index;
},
//加载分类信息 初始化数据
getCate : function () {
uni.request({
// 此处可以获取分类 根据分类自己拼接请求地址
// 格式请参考 接口 [ 浏览器运行直接查看 ]
url : 'http://grace.hcoder.net/api/tabs',
method : 'GET',
data : {},
success : res => {
// 初始化新闻列表数组 元素数量与分类匹配
this.tabsAll = res.data.data;
for(var i = 0; i < this.tabsAll.length; i++){
this.newsAll.push([]);
this.tabs.push(this.tabsAll[i].txt);
this.pages.push(1);
this.loadingTypes.push(3);
this.scrollTops.push(0);
}
this.getNews();
}
});
},
// 加载新闻
getNews : function(isReload){
// 当前正在展示的 选项index 为 this.currentIndex
// 那么分类 id 应该为 this.tabsAll[this.currentIndex].id
//console.log('类型 : ' + this.tabs[this.currentIndex] + ' 第'+ this.pages[this.currentIndex] +'页');
// 使用内部变量记录 当前索引,避免加载时用户切换引起的变量变化
var currentIndex = this.currentIndex;
if(!isReload){this.loadingTypes.splice(currentIndex, 1, 1);}
//console.log('http://grace.hcoder.net/api/news/index/'+this.tabsAll[this.currentIndex].id+'/'+this.pages[this.currentIndex]);
uni.request({
// 此处可以获取分类 根据分类自己拼接请求地址
// 分类 id 、页码 都已经获取到了
url : 'http://grace.hcoder.net/api/news/index/'+this.tabsAll[currentIndex].id+'/'+this.pages[currentIndex],
method : 'GET',
data : {},
success : res => {
if(res.data.status == 'ok'){
// 第一页
if(this.pages[currentIndex] == 1){
this.newsAll.splice(currentIndex, 1 , res.data.data);
}
// 之后的加载页
else{
this.newsAll[currentIndex] = this.newsAll[currentIndex].concat(res.data.data);
}
// 页码增加
this.pages[currentIndex]++;
setTimeout(()=>{
this.loadingTypes.splice(currentIndex, 1, 3);
},300)
}else if(res.data.status == 'empty'){
console.log('empty');
this.newsAll[currentIndex] = 'empty';
this.loadingTypes.splice(currentIndex, 1, 4);
}else if(res.data.status == 'nomore'){
console.log('nomore');
this.loadingTypes.splice(currentIndex, 1, 2);
}
},
complete: () => {
if(isReload){
setTimeout(()=>{
this.$refs.graceReload[currentIndex].endReload();
},300)
}
}
});
},
// 点击新闻列表
newsinfo : function () {
uni.showToast({
title:"请在api接口返回新闻id,利用此函数打开新闻详情页面",
icon:"none"
})
},
// 加载更多
scrollend:function(e){
// 判断加载状态避免多次滚动时有加载尚未完成
if( this.loadingTypes[this.currentIndex] == 2 || this.loadingTypes[this.currentIndex] == 4){ return false; }
console.log('loadmore.....');
this.getNews();
},
scroll:function(e){
if(this.scrollTimer != null){clearTimeout(this.scrollTimer);}
this.scrollTimer = setTimeout(()=>{this.scrollTops.splice(this.currentIndex, 1, e.detail.scrollTop);}, 100);
},
touchstart : function (e){
var touchObj = {scrollTop : this.scrollTops[this.currentIndex], moveY : e.changedTouches[0].pageY};
this.$refs.graceReload[this.currentIndex].touchstart(touchObj);
},
touchmove : function (e) {
var touchObj = {scrollTop : this.scrollTops[this.currentIndex], moveY : e.changedTouches[0].pageY};
this.$refs.graceReload[this.currentIndex].touchmove(touchObj);
},
touchend : function (e) {
var touchObj = {scrollTop : this.scrollTops[this.currentIndex], moveY : e.changedTouches[0].pageY};
this.$refs.graceReload[this.currentIndex].touchend(touchObj);
},
// 下拉刷新
reload:function(){
this.pages[this.currentIndex] = 1;
this.loadingTypes.splice(this.currentIndex, 1, 3);
this.getNews(1);
},
totop : function () {
this.scrollTops.splice(this.currentIndex, 1, 0);
}
},
components:{gracePage, graceNavBar, graceEmpty, graceLoading, graceReload}
}
</script>
<style>
.grace-news-img{width:220rpx; height:150rpx; overflow:hidden; font-size:0;}
.grace-news-img-in{width:100%;}
.grace-news-imgs-img{width:225rpx; height:380rpx;}
.empty-view{width:280rpx; height:280rpx; border-radius:280rpx; background-color:#F6F7F8; margin-top:300rpx;}
.empty-img{width:220rpx; height:200rpx; margin:40rpx; border-radius:200rpx;}
.grace-text-small{margin-top:10px;}
.to-top{width:80rpx; line-height:80rpx; height:80rpx; font-size:44rpx; text-align:center; display:block; background:#F5F6F8; border-radius:10rpx; color:#3688FF; opacity:0;}
.to-top-show{opacity:1;}
</style>
<template>
<gracePage headerBG="#FFFFFF" flexVal="1" :rbWidth="80">
<view slot="gHeader" class="grace-body" style="padding-top:6px;">
<graceNavBar :items="tabs" :currentIndex="currentIndex" @change="navChange"></graceNavBar>
</view>
<view slot="gBody" class="grace-flex-v1" ref="gBody">
<swiper :style="{height:mainHeight+'px'}" :current="currentIndex" @change="swiperChange">
<swiper-item v-for="(news, newsIndex) in newsAll" :key="newsIndex">
<scroll-view scroll-y="true" :style="{height:mainHeight+'px'}" :scroll-top="scrollTops[currentIndex]"
@scrolltolower="scrollend" @scroll="scroll"
@touchstart="touchstart" @touchmove="touchmove" @touchend="touchend">
<!-- 刷新组件 -->
<view><graceReload ref="graceReload" @reload="reload" width="700rpx" marginLeft="25rpx"></graceReload></view>
<!-- 新闻列表 -->
<view>
<graceEmpty v-if="news == 'empty'">
<view slot="img" class="empty-view">
<!-- 请根据您的项目要求制作并更换为空图片 -->
<image class="empty-img" mode="widthFix" src="https://staticimgs.oss-cn-beijing.aliyuncs.com/empty.png"></image>
</view>
<text slot="text" class="grace-text-small grace-gray">本栏目暂无新闻</text>
</graceEmpty>
</view>
<view v-for="(item, index) in news" :key="index" v-if="news != 'empty'" style="margin-left:25rpx; margin-right:25rpx;">
<view class="grace-news-list grace-border-b" v-if="item.imgs.length < 3" @tap="newsinfo">
<view class="grace-news-item">
<view class="grace-news-img grace-news-img-l">
<image :src="item.imgs[0]" mode="widthFix" class="grace-news-img-in"></image>
</view>
<view class="grace-news-body">
<text class="grace-news-title">{{item.title}}</text>
<text class="grace-news-desc">{{item.desc}}</text>
</view>
</view>
<view class="grace-news-info">
<view style="width:500rpx;" class="grace-nowrap">
<text class="grace-icons grace-news-info-text"> {{item.viewnum}}</text>
<text class="grace-icons grace-news-info-text" style="margin-left:30rpx;"> {{item.author}} · {{item.catename}}</text>
</view>
<text class="grace-icons grace-news-info-text"> 208</text>
</view>
</view>
<view class="grace-news-list grace-border-b" v-if="item.imgs.length >= 3" @tap="newsinfo">
<text class="grace-news-title">{{item.title}}</text>
<view class="grace-news-img-list">
<view class="grace-news-imgs" v-for="(img, imgIndex) in item.imgs" :key="imgIndex">
<image :src="img" mode="widthFix" class="grace-news-imgs-img"></image>
</view>
</view>
<view class="grace-news-info">
<view style="width:500rpx;" class="grace-nowrap">
<text class="grace-icons grace-news-info-text"> {{item.viewnum}}</text>
<text class="grace-icons grace-news-info-text" style="margin-left:30rpx;"> {{item.author}} · {{item.catename}}</text>
</view>
<text class="grace-icons grace-news-info-text"> 208</text>
</view>
</view>
</view>
<!-- 加载更多 -->
<view>
<graceLoading :loadingType="loadingTypes[newsIndex]"></graceLoading>
</view>
</scroll-view>
</swiper-item>
</swiper>
</view>
<view slot="gRTArea">
<text class="to-top grace-icons" :class="[scrollTops[currentIndex] >= 100 ? 'to-top-show' : '']" @tap="totop"></text>
</view>
</gracePage>
</template>
<script>
import gracePage from "../../graceUI/weexComponents/gracePage.nvue";
import graceNavBar from "../../graceUI/weexComponents/graceNavBar.nvue";
import graceLoading from "../../graceUI/weexComponents/graceLoading.nvue";
import graceEmpty from '../../graceUI/weexComponents/graceEmptyNew.nvue';
import graceReload from '../../graceUI/weexComponents/graceReload.nvue';
const dom = weex.requireModule('dom');
export default {
data() {
return {
currentIndex: 0,
mainHeight : 500,
//分类数据
tabsAll : [],
tabs : [],
// 新闻信息保存数组
newsAll : [],
// 每个选项卡对应的分页
pages : [],
// 加载状态
loadingTypes : [],
// 每个滚动区域的滚动值
scrollTops : [],
scrollTimer : null
}
},
onReady:function(){
setTimeout(()=>{
// 获取顶部内容高度
var el = this.$refs.gBody;
dom.getComponentRect(el, (res) => {
this.mainHeight = res.size.height;
});
},1000);
//加载分类信息 初始化数据
this.getCate();
},
watch:{
currentIndex : function (nVal, oldVal) {
if((this.loadingTypes[this.currentIndex] != 2 || this.loadingTypes[this.currentIndex] != 4) && this.pages[this.currentIndex] < 2){ this.getNews(); }
}
},
methods:{
navChange: function (e) {
this.currentIndex = e;
},
swiperChange: function (e) {
var index = e.detail.current;
this.currentIndex = index;
},
//加载分类信息 初始化数据
getCate : function () {
uni.request({
// 此处可以获取分类 根据分类自己拼接请求地址
// 格式请参考 接口 [ 浏览器运行直接查看 ]
url : 'http://grace.hcoder.net/api/tabs',
method : 'GET',
data : {},
success : res => {
// 初始化新闻列表数组 元素数量与分类匹配
this.tabsAll = res.data.data;
for(var i = 0; i < this.tabsAll.length; i++){
this.newsAll.push([]);
this.tabs.push(this.tabsAll[i].txt);
this.pages.push(1);
this.loadingTypes.push(3);
this.scrollTops.push(0);
}
this.getNews();
}
});
},
// 加载新闻
getNews : function(isReload){
// 当前正在展示的 选项index 为 this.currentIndex
// 那么分类 id 应该为 this.tabsAll[this.currentIndex].id
//console.log('类型 : ' + this.tabs[this.currentIndex] + ' 第'+ this.pages[this.currentIndex] +'页');
// 使用内部变量记录 当前索引,避免加载时用户切换引起的变量变化
var currentIndex = this.currentIndex;
if(!isReload){this.loadingTypes.splice(currentIndex, 1, 1);}
//console.log('http://grace.hcoder.net/api/news/index/'+this.tabsAll[this.currentIndex].id+'/'+this.pages[this.currentIndex]);
uni.request({
// 此处可以获取分类 根据分类自己拼接请求地址
// 分类 id 、页码 都已经获取到了
url : 'http://grace.hcoder.net/api/news/index/'+this.tabsAll[currentIndex].id+'/'+this.pages[currentIndex],
method : 'GET',
data : {},
success : res => {
if(res.data.status == 'ok'){
// 第一页
if(this.pages[currentIndex] == 1){
this.newsAll.splice(currentIndex, 1 , res.data.data);
}
// 之后的加载页
else{
this.newsAll[currentIndex] = this.newsAll[currentIndex].concat(res.data.data);
}
// 页码增加
this.pages[currentIndex]++;
setTimeout(()=>{
this.loadingTypes.splice(currentIndex, 1, 0);
},300)
}else if(res.data.status == 'empty'){
console.log('empty');
this.newsAll[currentIndex] = 'empty';
this.loadingTypes.splice(currentIndex, 1, 4);
}else if(res.data.status == 'nomore'){
console.log('nomore');
this.loadingTypes.splice(currentIndex, 1, 2);
}
},
complete: () => {
if(isReload){
setTimeout(()=>{
this.$refs.graceReload[currentIndex].endReload();
},300)
}
}
});
},
// 点击新闻列表
newsinfo : function () {
uni.showToast({
title:"请在api接口返回新闻id,利用此函数打开新闻详情页面",
icon:"none"
})
},
// 加载更多
scrollend:function(e){
console.log(this.loadingTypes[this.currentIndex]);
// 判断加载状态避免多次滚动时有加载尚未完成
if( this.loadingTypes[this.currentIndex] != 0){ return false; }
console.log('loadmore.....');
this.getNews();
},
scroll:function(e){
if(this.scrollTimer != null){clearTimeout(this.scrollTimer);}
this.scrollTimer = setTimeout(()=>{this.scrollTops.splice(this.currentIndex, 1, e.contentOffset.y * -1);}, 100);
},
touchstart : function (e){
var touchObj = {scrollTop : this.scrollTops[this.currentIndex], moveY : e.changedTouches[0].pageY};
this.$refs.graceReload[this.currentIndex].touchstart(touchObj);
},
touchmove : function (e) {
var touchObj = {scrollTop : this.scrollTops[this.currentIndex], moveY : e.changedTouches[0].pageY};
this.$refs.graceReload[this.currentIndex].touchmove(touchObj);
},
touchend : function (e) {
var touchObj = {scrollTop : this.scrollTops[this.currentIndex], moveY : e.changedTouches[0].pageY};
this.$refs.graceReload[this.currentIndex].touchend(touchObj);
},
// 下拉刷新
reload:function(){
this.pages[this.currentIndex] = 1;
this.loadingTypes.splice(this.currentIndex, 1, 0);
this.getNews(1);
},
totop : function () {
this.scrollTops.splice(this.currentIndex, 1, 0);
}
},
components:{gracePage, graceNavBar, graceEmpty, graceLoading, graceReload}
}
</script>
<style>
.grace-news-img{width:220rpx; height:150rpx; overflow:hidden; font-size:0;}
.grace-news-img-in{width:100%;}
.grace-news-imgs-img{width:225rpx; height:380rpx;}
.empty-view{width:280rpx; height:280rpx; border-radius:280rpx; background-color:#F6F7F8; margin-top:300rpx;}
.empty-img{width:220rpx; height:200rpx; margin:40rpx; border-radius:200rpx;}
.grace-text-small{margin-top:10px;}
.to-top{width:80rpx; line-height:80rpx; height:80rpx; font-size:44rpx; text-align:center; background-color:#F5F6F8; border-radius:10rpx; color:#3688FF; opacity:0;}
.to-top-show{opacity:1;}
/* 此处 hx 误报样式错误,必须这样使用哦 */
.grace-news-body{align-self: flex-start;}
</style>
.wxml
<gracePage headerBG="#FFFFFF" rbWidth="{{80}}">
<view slot="gHeader" style="padding:0 25rpx;">
<graceNavBar items="{{tabs}}" currentIndex="{{currentIndex}}" bindchange="navChange"></graceNavBar>
</view>
<view slot="gBody" class="grace-flex-v1" id="gBody">
<swiper style="height:{{mainHeight+'px'}}" current="{{currentIndex}}" bindchange="swiperChange">
<swiper-item wx:for="{{newsAll}}" wx:for-item="news" wx:for-index="newsIndex" wx:key="newsIndex">
<scroll-view scroll-y="true" style="height:{{mainHeight+'px'}}; position:relative;"
bindscrolltolower="scrollend" bindscroll="scroll" scroll-anchoring="{{true}}" scroll-top="{{scrollTops[currentIndex]}}"
bindtouchstart="touchstart" bindtouchmove="touchmove" bindtouchend="touchend">
<view>
<graceReload id="{{'graceReload'+newsIndex}}" bind:reload="reload" width="700rpx" marginLeft="25rpx"></graceReload>
</view>
<graceEmpty wx:if="{{news == 'empty'}}">
<view slot="img" class="empty-view">
<!-- 请根据您的项目要求制作并更换为空图片 -->
<image class="empty-img" mode="widthFix" src="https://staticimgs.oss-cn-beijing.aliyuncs.com/empty.png"></image>
</view>
<text slot="text" class="grace-text-small grace-gray">本栏目暂无新闻</text>
</graceEmpty>
<view style="padding:0 25rpx;">
<block wx:for="{{news}}" wx:key="index" wx:if="{{news != 'empty'}}">
<view class="grace-news-list grace-border-b" wx:if="{{item.imgs.length < 3}}" bindtap="newsinfo">
<view class="grace-news-item">
<view class="grace-news-img grace-news-img-l">
<image src="{{item.imgs[0]}}" mode="widthFix" class="grace-news-img-in"></image>
</view>
<view class="grace-news-body">
<text class="grace-news-title">{{item.title}}</text>
<text class="grace-news-desc">{{item.desc}}</text>
</view>
</view>
<view class="grace-news-info">
<view style="width:500rpx;" class="grace-nowrap">
<text class="grace-icons icon-eye grace-news-info-text"> {{item.viewnum}}</text>
<text class="grace-icons icon-write grace-news-info-text" style="margin-left:30rpx;"> {{item.author}} · {{item.catename}}</text>
</view>
<text class="grace-icons icon-zan grace-news-info-text"> 208</text>
</view>
</view>
<view class="grace-news-list grace-border-b" wx:if="{{item.imgs.length >= 3}}" bindtap="newsinfo">
<text class="grace-news-title">{{item.title}}</text>
<view class="grace-news-img-list">
<view class="grace-news-imgs" wx:for="{{item.imgs}}" wx:for-item="img" wx:for-index="imgIndex" wx:key="imgIndex">
<image src="{{img}}" mode="widthFix" class="grace-news-imgs-img"></image>
</view>
</view>
<view class="grace-news-info">
<view style="width:500rpx;" class="grace-nowrap">
<text class="grace-icons icon-eye grace-news-info-text"> {{item.viewnum}}</text>
<text class="grace-icons icon-write grace-news-info-text" style="margin-left:30rpx;"> {{item.author}} · {{item.catename}}</text>
</view>
<text class="grace-icons icon-zan grace-news-info-text"> 208</text>
</view>
</view>
</block>
</view>
<graceLoading loadingType="{{loadingTypes[newsIndex]}}"></graceLoading>
</scroll-view>
</swiper-item>
</swiper>
</view>
<view slot="gRTArea">
<text class="to-top grace-icons icon-totop {{scrollTops[currentIndex] >= 100 ? 'to-top-show' : ''}}" bindtap="totop"></text>
</view>
</gracePage>.js
Page({
data: {
currentIndex: 0,
mainHeight: 600,
//分类数据
tabsAll: [],
tabs: [],
// 新闻信息保存数组
newsAll: [],
// 每个选项卡对应的分页
pages: [],
// 加载状态
loadingTypes: [],
// 每个滚动区域的滚动值
scrollTops: [],
scrollTimer: null
},
onReady: function () {
setTimeout(() => {
wx.createSelectorQuery().select('#gBody').fields(
{ size: true }, (res) => {
this.setData({ mainHeight: res.height });
}
).exec();
}, 1000);
//加载分类信息 初始化数据
this.getCate();
},
navChange: function (e) {
this.setData({ currentIndex: e.detail });
},
swiperChange: function (e) {
if(e.detail.source == "touch"){
var index = e.detail.current;
this.setData({ currentIndex: index });
}
if (this.data.loadingTypes[this.data.currentIndex] == 3 && this.data.pages[this.data.currentIndex] < 2) {
this.getNews();
}
},
//加载分类信息 初始化数据
getCate: function () {
wx.request({
// 此处可以获取分类 根据分类自己拼接请求地址
// 格式请参考 接口 [ 浏览器运行直接查看 ]
url: 'http://grace.hcoder.net/api/tabs',
method: 'GET',
data: {},
success: res => {
// 初始化新闻列表数组 元素数量与分类匹配
this.data.tabsAll = res.data.data;
for (var i = 0; i < this.data.tabsAll.length; i++) {
this.data.newsAll.push([]);
this.data.tabs.push(this.data.tabsAll[i].txt);
this.data.pages.push(1);
this.data.loadingTypes.push(3);
this.data.scrollTops.push(0);
}
this.setData({
newsAll: this.data.newsAll,
tabs: this.data.tabs,
pages: this.data.pages,
loadingTypes: this.data.loadingTypes,
scrollTops: this.data.scrollTops
});
this.getNews();
}
});
},
// 加载新闻
getNews: function (isReload) {
// 当前正在展示的 选项index 为 this.data.currentIndex
// 那么分类 id 应该为 this.data.tabsAll[this.data.currentIndex].id
//console.log('类型 : ' + this.tabs[this.data.currentIndex] + ' 第'+ this.data.pages[this.data.currentIndex] +'页');
// 使用内部变量记录 当前索引,避免加载时用户切换引起的变量变化
var currentIndex = this.data.currentIndex;
if (!isReload) {
this.data.loadingTypes.splice(currentIndex, 1, 1);
this.setData({ loadingTypes: this.data.loadingTypes });
}
//console.log('http://grace.hcoder.net/api/news/index/'+this.data.tabsAll[this.data.currentIndex].id+'/'+this.data.pages[this.data.currentIndex]);
wx.request({
// 此处可以获取分类 根据分类自己拼接请求地址
// 分类 id 、页码 都已经获取到了
url: 'http://grace.hcoder.net/api/news/index/' + this.data.tabsAll[currentIndex].id + '/' + this.data.pages[currentIndex],
method: 'GET',
data: {},
success: res => {
if (res.data.status == 'ok') {
// 第一页
if (this.data.pages[currentIndex] == 1) {
this.data.newsAll.splice(currentIndex, 1, res.data.data);
this.setData({ newsAll: this.data.newsAll });
}
// 之后的加载页
else {
this.data.newsAll[currentIndex] = this.data.newsAll[currentIndex].concat(res.data.data);
this.setData({ newsAll: this.data.newsAll });
}
// 页码增加
this.data.pages[currentIndex]++;
this.setData({ pages: this.data.pages });
setTimeout(() => {
this.data.loadingTypes.splice(currentIndex, 1, 3);
this.setData({ loadingTypes: this.data.loadingTypes });
}, 300)
} else if (res.data.status == 'empty') {
console.log('empty');
this.data.newsAll[currentIndex] = 'empty';
this.data.loadingTypes.splice(currentIndex, 1, 4);
this.setData({ newsAll: this.data.newsAll, loadingTypes: this.data.loadingTypes });
} else if (res.data.status == 'nomore') {
console.log('nomore');
this.data.loadingTypes.splice(currentIndex, 1, 2);
this.setData({ loadingTypes: this.data.loadingTypes });
}
},
complete: () => {
if (isReload) {
setTimeout(() => {
this.selectComponent("#graceReload" + currentIndex).endReload();
}, 300)
}
}
});
},
// 加载更多
scrollend: function (e) {
// 判断加载状态避免多次滚动时有加载尚未完成
if (this.data.loadingTypes[this.data.currentIndex] != 3) { return false; }
console.log('loadmore.....');
this.getNews();
},
scroll: function (e) {
if (this.data.scrollTimer) { clearTimeout(this.data.scrollTimer);}
this.data.scrollTimer = setTimeout(()=>{
this.data.scrollTops.splice(this.data.currentIndex, 1, e.detail.scrollTop);
this.setData({scrollTops: this.data.scrollTops });
},100);
this.setData({ scrollTimer : this.data.scrollTimer});
},
touchstart: function (e) {
var touchObj = { scrollTop: this.data.scrollTops[this.data.currentIndex], moveY: e.changedTouches[0].pageY };
this.selectComponent("#graceReload" + this.data.currentIndex).touchstart(touchObj);
},
touchmove: function (e) {
var touchObj = { scrollTop: this.data.scrollTops[this.data.currentIndex], moveY: e.changedTouches[0].pageY };
this.selectComponent("#graceReload" + this.data.currentIndex).touchmove(touchObj);
},
touchend: function (e) {
var touchObj = { scrollTop: this.data.scrollTops[this.data.currentIndex], moveY: e.changedTouches[0].pageY };
this.selectComponent("#graceReload" + this.data.currentIndex).touchend(touchObj);
},
// 下拉刷新
reload: function () {
this.data.pages[this.data.currentIndex] = 1;
this.data.loadingTypes.splice(this.data.currentIndex, 1, 3);
this.setData({
pages: this.data.pages,
loadingTypes: this.data.loadingTypes
});
this.getNews(1);
},
totop: function () {
this.data.scrollTops.splice(this.data.currentIndex, 1, 0);
this.setData({ scrollTops: this.data.scrollTops});
}
}).json
{
"usingComponents": {
"gracePage" : "../../graceUI/components/gracePage",
"graceNavBar": "../../graceUI/components/graceNavBar",
"graceLoading": "../../graceUI/components/graceLoading",
"graceEmpty": "../../graceUI/components/graceEmptyNew",
"graceReload": "../../graceUI/components/graceReload"
},
"navigationStyle": "custom"
}.wxss
.grace-news-img{width:220rpx; height:150rpx; overflow:hidden; font-size:0;}
.grace-news-img-in{width:100%;}
.grace-news-imgs-img{width:225rpx; height:380rpx;}
.empty-view{width:280rpx; height:280rpx; border-radius:280rpx; background-color:#F6F7F8; margin-top:300rpx;}
.empty-img{width:220rpx; height:200rpx; margin:40rpx; border-radius:200rpx;}
.grace-text-small{margin-top:10px;}
.to-top{width:80rpx; line-height:80rpx; height:80rpx; font-size:44rpx; text-align:center; display:block; background:#F5F6F8; border-radius:10rpx; color:#3688FF; opacity:0;}
.to-top-show{opacity:1;}