wx-guyantv/pages/home/index.js
2025-01-06 14:18:31 +08:00

189 lines
4.2 KiB
JavaScript

// index.js
const app = getApp();
import { pageList, moduleList } from "@/api/home";
import { PlayerManager } from "@/utils/playerManager";
Page({
data: {
navbarData: {
showCapsule: 1, //是否显示左上角图标
returnType: 1, //是否显示返回图标 不传不显示
homeType: 1, //是否显示主页图标 不传不显示
title: "古言网络", //中间标题
bgColor: "#fff",
},
opacity: 0,
capsuleObj: null,
bannerList: [],
bestList: [],
bannerIndex: 0,
starsList: [],
recommend: [],
newList: [],
history: null,
hotList: [], // 最热
newestList: [], // 最新
moreList: [], // 更多
showHistory: true,
page: 1,
top: 1,
loadStatus: "loading", // loading loadmore nomore
tabsActive: "1",
},
onShareAppMessage() {
return {
title: "古言网络",
path: "pages/home/index",
loadmore: "loadmore",
};
},
async onLoad() {
this.setData({
capsuleObj: app.globalData.capsuleObj,
});
},
async onShow() {
this.getList();
},
// 监听屏幕滚动,获取滚动距离
onPageScroll: function (e) {
let that = this;
var opacity,
scrollTop = e.scrollTop;
// 根据滚动距离设置渐隐渐显
if (scrollTop <= 100) {
opacity = 0;
} else if (scrollTop <= 200) {
opacity = (scrollTop - 100) / 100;
} else {
opacity = 1;
}
that.setData({
opacity: opacity,
});
},
//监听下拉刷新
onPullDownRefresh() {
console.log("监听用户下拉刷新");
var _this = this;
this.setData({
bannerList: [],
bestList: [],
starsList: [],
recommend: [],
newList: [],
history: null,
hotList: [], // 最热
newestList: [], // 最新
moreList: [], // 更多
bannerIndex: 0,
loadStatus: "loading",
});
setTimeout(async () => {
this.getList();
wx.stopPullDownRefresh();
}, 500);
},
// 监听上拉加载
async onReachBottom() {
console.log("监听用户上拉加载");
if (this.data.loadStatus !== "nomore") {
this.setData({
loadStatus: "loading",
});
const { module: newList } = await moduleList({
page: this.data.page,
top: 1,
});
this.setData({
loadStatus: newList.length < 5 ? "nomore" : "loadmore",
moreList: this.data.moreList.concat(newList),
page: this.data.page + 1,
});
}
},
async getList() {
const res = await pageList();
// 接口调用
const { module: hotList } = await moduleList({
page: 1,
top: 2,
});
const { module: newestList } = await moduleList({
page: 1,
top: 3,
});
const { module: moreList } = await moduleList({
page: 1,
top: 1,
});
this.setData({
bannerList: res.ads, // banner
bestList: res.module1, // 全网热播
starsList: res.module2, // 精品短剧
recommend: res.module3, // 特别推荐
newList: res.module4, // 新剧发布
hotList, // 最热
newestList, // 最新
moreList, // 更多
history: res.history,
tabsActive: "1",
page: 2,
loadStatus: moreList?.length < 5 ? "nomore" : "loadmore",
showHistory: true,
});
},
onTabsChange(event) {
this.setData({
tabsActive: event.detail.name,
});
},
// 点击顶部搜索
openSearch() {
console.log(454);
wx.navigateTo({
url: "/pages/search/index",
});
},
// banner改变
bannerChange(event) {
let { current, source } = event.detail;
if (source === "autoplay" || source === "touch") {
this.setData({
bannerIndex: current,
});
}
},
// 关闭历史弹框
historyChange() {
this.setData({
showHistory: !this.data.showHistory,
});
},
// 打开视频
openDetail(event) {
const data = event.currentTarget.dataset.item;
console.log(data);
if (data.wx_drama_id) {
PlayerManager.navigateToPlayer({
srcAppid: "wx2ab73633e3b9fcbf",
dramaId: data.wx_drama_id,
});
}
},
// 排行榜
openRecommend() {
console.log(456);
// 打开视频
wx.switchTab({
url: "/pages/recommend/index",
});
},
});