295 lines
6.7 KiB
JavaScript
295 lines
6.7 KiB
JavaScript
// index.js
|
|
const MyApi = require('../../Api/api.js');
|
|
const updateManager = wx.getUpdateManager();
|
|
Page({
|
|
|
|
/**
|
|
* 页面的初始数据
|
|
*/
|
|
data: {
|
|
BaseURL: getApp().globalData.BaseURL,
|
|
imageList: [],
|
|
WebPages: [],
|
|
CourseList: [],
|
|
FreeCourseList: [],
|
|
webPageCount: 1,
|
|
hasTouchBottom:false,
|
|
bannerImgName:[],
|
|
text1: wx.getStorageSync('Config')[2].value,
|
|
text2: wx.getStorageSync('Config')[3].value,
|
|
text3: wx.getStorageSync('Config')[4].value,
|
|
text4: wx.getStorageSync('Config')[11].value,
|
|
},
|
|
|
|
/**
|
|
* 生命周期函数--监听页面加载
|
|
*/
|
|
onLoad(options) {
|
|
//获取配置
|
|
MyApi.ApiGetConfig()
|
|
.then(result => {
|
|
// 将信息存储到本地缓存中
|
|
wx.setStorageSync('Config', result);
|
|
})
|
|
.catch(err => {
|
|
// 失败后执行的代码
|
|
console.error("请求失败:"+err);
|
|
});
|
|
|
|
//获取首页banner图片
|
|
MyApi.ApiGetBannerImg()
|
|
.then(result => {
|
|
this.setData({
|
|
bannerImgName: result
|
|
});
|
|
})
|
|
.catch(err => {
|
|
console.error("请求失败:" + err);
|
|
});
|
|
|
|
|
|
//获取首页课程列表
|
|
wx.setStorageSync('IndexNewCourseData', []);
|
|
|
|
MyApi.ApiGetIndexCourseList()
|
|
.then(result => {
|
|
wx.setStorageSync('IndexNewCourseData', result.ListData);
|
|
this.setData({
|
|
CourseList: result.ListData
|
|
});
|
|
})
|
|
.catch(err => {
|
|
console.error("请求失败:" + err);
|
|
});
|
|
|
|
MyApi.ApiGetIndexFreeCourseList()
|
|
.then(result => {
|
|
wx.setStorageSync('IndexFreeCourseData', result.ListData);
|
|
this.setData({
|
|
FreeCourseList: result.ListData
|
|
});
|
|
})
|
|
.catch(err => {
|
|
console.error("请求失败:" + err);
|
|
});
|
|
|
|
//获取十个页面数据
|
|
MyApi.ApiGetWebPagesFromDB(1)
|
|
.then(result => {
|
|
wx.setStorageSync('WebPagesContent', result);
|
|
var tempWebPagesContent = wx.getStorageSync('WebPagesContent');
|
|
this.setData({
|
|
WebPages: tempWebPagesContent,
|
|
});
|
|
})
|
|
.catch(err => {
|
|
console.error("请求失败:" + err);
|
|
});
|
|
},
|
|
|
|
/**
|
|
* 生命周期函数--监听页面初次渲染完成
|
|
*/
|
|
onReady() {
|
|
|
|
},
|
|
|
|
/**
|
|
* 生命周期函数--监听页面显示
|
|
*/
|
|
onShow() {
|
|
//微信小程序更新
|
|
updateManager.onCheckForUpdate(function (res) {
|
|
// 请求完新版本信息的回调
|
|
//console.log("是否有新版本:"+res.hasUpdate)
|
|
})
|
|
|
|
updateManager.onUpdateReady(function () {
|
|
wx.showModal({
|
|
title: '更新提示',
|
|
showCancel:false,
|
|
content: '新版本已准备好,即将重启应用',
|
|
success: function (res) {
|
|
if (res.confirm) {
|
|
// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
|
|
updateManager.applyUpdate()
|
|
}
|
|
}
|
|
})
|
|
})
|
|
|
|
updateManager.onUpdateFailed(function () {
|
|
// 新版本下载失败
|
|
})
|
|
|
|
|
|
|
|
},
|
|
|
|
/**
|
|
* 生命周期函数--监听页面隐藏
|
|
*/
|
|
onHide() {
|
|
|
|
},
|
|
|
|
/**
|
|
* 生命周期函数--监听页面卸载
|
|
*/
|
|
onUnload() {
|
|
|
|
},
|
|
|
|
/**
|
|
* 页面相关事件处理函数--监听用户下拉动作
|
|
*/
|
|
onPullDownRefresh() {
|
|
//逻辑同onLoad()一样,略有修改
|
|
//获取配置
|
|
MyApi.ApiGetConfig()
|
|
.then(result => {
|
|
// 将信息存储到本地缓存中
|
|
wx.setStorageSync('Config', result);
|
|
})
|
|
.catch(err => {
|
|
// 失败后执行的代码
|
|
console.error("请求失败:"+err);
|
|
});
|
|
|
|
|
|
//获取小知识
|
|
MyApi.ApiGetTips()
|
|
.then(result => {
|
|
// 将信息存储到本地缓存中
|
|
wx.setStorageSync('Tips', result);
|
|
})
|
|
.catch(err => {
|
|
// 失败后执行的代码
|
|
console.error("请求失败:"+err);
|
|
});
|
|
|
|
|
|
//获取首页banner图片
|
|
MyApi.ApiGetBannerImg()
|
|
.then(result => {
|
|
//增加停止下拉刷新显示
|
|
wx.stopPullDownRefresh();
|
|
|
|
this.setData({
|
|
bannerImgName: result
|
|
});
|
|
})
|
|
.catch(err => {
|
|
console.error("请求失败:" + err);
|
|
});
|
|
|
|
//获取首页课程列表
|
|
wx.setStorageSync('IndexNewCourseData', []);
|
|
|
|
MyApi.ApiGetIndexCourseList()
|
|
.then(result => {
|
|
|
|
//增加停止下拉刷新显示
|
|
wx.stopPullDownRefresh();
|
|
|
|
wx.setStorageSync('IndexNewCourseData', result.ListData);
|
|
this.setData({
|
|
CourseList: result.ListData
|
|
});
|
|
})
|
|
.catch(err => {
|
|
console.error("请求失败:" + err);
|
|
});
|
|
|
|
MyApi.ApiGetIndexFreeCourseList()
|
|
.then(result => {
|
|
wx.setStorageSync('IndexFreeCourseData', result.ListData);
|
|
this.setData({
|
|
FreeCourseList: result.ListData
|
|
});
|
|
})
|
|
.catch(err => {
|
|
console.error("请求失败:" + err);
|
|
});
|
|
|
|
//获取十个页面数据
|
|
MyApi.ApiGetWebPagesFromDB(1)
|
|
.then(result => {
|
|
//增加停止下拉刷新显示
|
|
wx.stopPullDownRefresh();
|
|
|
|
wx.setStorageSync('WebPagesContent', result);
|
|
var tempWebPagesContent = wx.getStorageSync('WebPagesContent');
|
|
this.setData({
|
|
WebPages: tempWebPagesContent,
|
|
});
|
|
})
|
|
.catch(err => {
|
|
console.error("请求失败:" + err);
|
|
});
|
|
},
|
|
|
|
/**
|
|
* 页面上拉触底事件的处理函数
|
|
*/
|
|
onReachBottom() {
|
|
MyApi.ApiGetWebPagesFromDB(this.data.webPageCount + 1)
|
|
.then(result => {
|
|
if (result) {
|
|
//使用展开运算符将两个JSON数据拼接在一起
|
|
var combinedJson = this.data.WebPages.concat(result);
|
|
var requireCount = this.data.webPageCount;
|
|
this.setData({
|
|
hasTouchBottom: false,
|
|
webPageCount: requireCount + 1,
|
|
WebPages: combinedJson
|
|
});
|
|
|
|
//更新缓存
|
|
wx.setStorageSync('WebPagesContent', combinedJson)
|
|
|
|
} else {
|
|
this.setData({
|
|
hasTouchBottom: true,
|
|
});
|
|
}
|
|
|
|
})
|
|
.catch(err => {
|
|
console.error("请求失败:" + err);
|
|
});
|
|
|
|
},
|
|
|
|
/**
|
|
* 用户点击右上角分享
|
|
*/
|
|
onShareAppMessage() {
|
|
|
|
},
|
|
NavigatorToWebPageDetails(e) {
|
|
var index = e.currentTarget.dataset.index;
|
|
wx.navigateTo({
|
|
url: '/pages/WebPageDetails/WebPageDetails?index=' + index,
|
|
})
|
|
},
|
|
|
|
NavigatorToIndexNewCourseDetails(e) {
|
|
var number = e.currentTarget.dataset.number;
|
|
wx.navigateTo({
|
|
url: '/pages/IndexNewCourseDetails/IndexNewCourseDetails?number=' + number,
|
|
})
|
|
},
|
|
|
|
NavigatorToIndexFreeCourseDetails(e) {
|
|
var number = e.currentTarget.dataset.number;
|
|
wx.navigateTo({
|
|
url: '/pages/IndexFreeCourseDetails/IndexFreeCourseDetails?number=' + number,
|
|
})
|
|
},
|
|
|
|
//允许分享到朋友圈
|
|
onShareTimeline(e) {
|
|
|
|
}
|
|
}) |