mini-system-examination-signup/pages/notices/noticeslist.js
2021-07-23 11:18:54 +08:00

129 lines
4.3 KiB
JavaScript

// pages/notices/noticeslist.js
const app = getApp()
Page({
/**
* 页面的初始数据
*/
data: {
noticeList: [],
CustomBar: app.globalData.CustomBar,
countTime: 2000, //延迟搜索 时间
searchWaiting: false, //是否等待搜索倒计时中,
searchKey: '',
contentHeight: app.globalData.windowHeight,
currentPage: 1,
totalSize: 0
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
this.getResultsList('')
},
showDetail(e) {
var item = e.currentTarget.dataset.item
wx.navigateTo({
url: './noticedetail?id=' + item.contentId,
})
},
doLoadMore() {
var _self = this
if (_self.data.totalSize > _self.data.noticeList.length) {
_self.setData({
currentPage: ++_self.data.currentPage
})
_self.getResultsList(_self.data.searchKey)
} else {
wx.showToast({
title: '无更多数据了',
icon: 'none'
})
}
},
doSearch(e) {
this.setData({
countTime: 2000,
searchKey: e.detail.value,
})
//是否处于搜索倒计时中
if (!this.data.searchWaiting) {
this.timer();
}
},
getResultsList(key) {
var _self = this
app.restAjax.get(app.restAjax.path(app.apis.getArticleList, [app.baseUrls.cardUrl]), {
categoryId: '2ff8f6be-a435-46c1-98b7-47b16c621395',
page: _self.data.currentPage,
rows: '10',
keywords: key
}, {},
function (code, data) {
wx.hideLoading({})
if (code == 200) {
for (var i = 0; i < data.rows.length; i++) {
var item = data.rows[i]
if (item.coverPhotos.length > 0) {
var photos = item.coverPhotos.split(',')
data.rows[i].showPhoto = app.baseUrls.cardUrl + app.baseUrls.baseImgUrl + photos[0]
} else {
data.rows[i].showPhoto = 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.czcaizhi.com%2FContent%2FUploadFiles%2Fimage%2F20180920%2F20180920133956_3429.jpg&refer=http%3A%2F%2Fwww.czcaizhi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1622110389&t=4ae9d4f943c30ac6d43801515eec97e5'
}
}
if (_self.data.currentPage > 1) {
if (data.rows.length > 0) {
_self.data.noticeList = _self.data.noticeList.concat(data.rows)
}
} else {
_self.data.noticeList = data.rows
}
_self.setData({
noticeList: _self.data.noticeList,
totalSize: data.total
})
}
},
function (code, err) {
wx.hideLoading({})
console.log(err)
})
},
/**
* 延迟搜索
*/
timer() {
var _self = this;
this.setData({
currentPage: 1,
totalSize: 0,
searchWaiting: true
})
let promise = new Promise((resolve, reject) => {
let setTimer = setInterval(
() => {
console.log('搜索倒计时: ' + _self.data.countTime);
_self.setData({
countTime: _self.data.countTime - 1000
})
if (_self.data.countTime <= 0) {
console.log('开始搜索: ' + _self.data.searchKey);
_self.setData({
countTime: 2000,
searchWaiting: false,
})
resolve(setTimer)
}
}, 1000)
})
promise.then((setTimer) => {
wx.showLoading({
title: '搜索中...',
})
_self.getResultsList(_self.data.searchKey)
clearInterval(setTimer) //清除计时器
})
},
})