duidui_mini_program/pages/index/index.js
2026-03-27 10:41:46 +08:00

484 lines
12 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const DEFAULT_PAGE = 0;
const baseUrl = getApp();
const api = require('../../config/api.js');
const util = require('../../config/util.js');
const { default: toast } = require('../../miniprogram_npm/@vant/weapp/toast/toast.js');
import { campApi } from '../../config/camp_api.js';
Page({
data: {
startPageX: 0,
currentView: DEFAULT_PAGE,
livesdata: [],
baseurl: baseUrl.globalData.baseimgurl,
movies: [],
active: 1,
challengeactive: 1,
toView: `card_${DEFAULT_PAGE}`,
list: [],
itemdata: [],
isdisplay: 1,
onelist: [], //一级数据
activeshow: 1,
currentid: 1,
// 小方块数据
blockItems: []
},
// 打卡按钮点击事件 - 跳转到打卡营列表页面
onCheckinClick() {
console.log('打卡按钮被点击');
// 跳转到打卡营列表页面
wx.navigateTo({
url: '/pages/camp_list/index',
success: () => {
console.log('跳转到打卡营列表页面成功');
},
fail: (err) => {
console.log('跳转失败:', err);
wx.showToast({
title: '页面跳转失败',
icon: 'none'
});
}
});
},
// 直播按钮点击事件
onLiveClick() {
console.log('直播按钮被点击');
wx.showToast({
title: '直播功能',
icon: 'none'
});
// 这里可以添加直播相关的逻辑
// 比如跳转到直播页面或调用直播接口
},
// 获取小方块数据(使用 Fiber API
getBlockItems() {
const that = this;
// 使用统一的 campApi 接口(推荐打卡营)
campApi.getCamps(null)
.then((res) => {
console.log('小方块数据请求成功:', res);
// Fiber API 返回格式:{ success: true, camps: [...] }
const camps = res.camps || [];
if (Array.isArray(camps) && camps.length > 0) {
// 处理接口返回的数据,转换为小方块格式
const blockItems = camps.map((item, index) => ({
id: item.id || index + 1,
name: item.title || '未知标题',
icon: item.cover_image || 'https://ndnd.duiduiedu.com/uploads/pics/default-icon.png',
description: item.description || '',
// 保存原始数据,用于后续处理
originalData: item
}));
that.setData({
blockItems: blockItems
});
console.log('小方块数据更新成功:', blockItems);
} else {
console.log('接口返回数据为空或格式异常:', res);
// 如果接口返回异常,显示空数据
that.setData({
blockItems: []
});
}
})
.catch((err) => {
console.log('小方块数据请求失败:', err);
// 请求失败时显示空数据
that.setData({
blockItems: []
});
});
},
// 小方块点击事件 - 跳转到打卡营详情页面
onBlockClick(e) {
const blockId = e.currentTarget.dataset.id;
const blockItem = this.data.blockItems.find(item => item.id == blockId);
// console.log('小方块被点击:', blockItem);
if (blockItem && blockItem.originalData) {
const camp = blockItem.originalData;
console.log('打卡营详情:', camp);
// 直接跳转到打卡营详情页面
wx.navigateTo({
url: `/pages/camp_detail/index?id=${camp.id}&title=${encodeURIComponent(camp.title || '打卡营')}`,
success: () => {
console.log('跳转到打卡营详情页面成功:', camp.id);
},
fail: (err) => {
console.log('跳转失败:', err);
wx.showToast({
title: '页面跳转失败',
icon: 'none'
});
}
});
} else {
wx.showToast({
title: '数据加载中...',
icon: 'none'
});
}
},
checkclassshow(event) {
//新增开关功能
console.log(event);
console.log(event.currentTarget.id);
const id = event.currentTarget.id;
let arr1 = [];
let arr = [];
arr = this.data.onelist;
// if (id == 1) {
// arr.forEach(function (item, index) {
// console.log(item);
// if (item.id == 2) {
// arr1[index] = item;
// }
// })
// this.setData({
// onelist: arr1
// })
// } else {
// arr.forEach(function (item, index) {
// console.log(item);
// if (item.id == 1) {
// arr1[index] = item;
// }
// })
// this.setData({
// onelist: arr1
// })
// }
// arr.forEach(function (item, index) {
// if (index == e.currentTarget.dataset.itemid) {
// item['liveclickopen'] = 2;
// arr[index] = item;
// } else {
// item['liveclickopen'] = 2;
// arr[index] = item
// }
// })
// this.setData({
// listdata: arr
// })
},
footeronChange(event) {
this.setData({
footeractive: event.detail
});
},
onChangefooter(event) {
console.log(event.detail);
this.setData({
footeractive: event.detail
});
},
onChange(event) {
wx.showToast({
title: `切换到标签 ${event.detail.name}`,
icon: 'none',
});
},
touchStart(e) {
this.startPageX = e.changedTouches[0].pageX;
},
touchEnd(e) {
const moveX = e.changedTouches[0].pageX - this.startPageX;
const maxPage = this.data.list.length - 1;
if (Math.abs(moveX) >= 150) {
if (moveX > 0) {
this.currentView = this.currentView !== 0 ? this.currentView - 1 : 0;
} else {
this.currentView = this.currentView !== maxPage ? this.currentView + 1 : maxPage;
}
}
this.setData({
toView: `card_${this.currentView}`
});
},
touchecard(e) {
console.log(e.currentTarget.id);
this.getindexcoursetwo(e.currentTarget.id);
this.setData({
active: e.currentTarget.id
})
},
loaditem(id) {
console.log(id);
},
cardaddclick(e) {
console.log(e);
},
challengeclickleft(e) {
console.log(e.currentTarget.id);
this.setData({
challengeactive: e.currentTarget.id
})
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
//baseUrl.islogin();
// 页面加载时获取小方块数据
this.getBlockItems();
},
getpopindexcouse: function (params) {
//顶级分类
const that = this;
let url = baseUrl.globalData.baseUrl + '/index/indexcoursetop';
var param = {
id: '1'
};
//此处为使用封装的post请求
util.get(url, param).then((res) => {
console.log(res);
that.setData({
list: res.data
});
}).catch((errMsg) => {
console.log(errMsg);
});
},
getindexcourse: function () {
const that = this;
let url = baseUrl.globalData.baseUrl + '/index/indexcourse';
var param = {
id: '123456789'
};
//此处为使用封装的post请求
util.get(url, param).then((res) => {
console.log(res);
that.setData({
list: res.data
});
}).catch((errMsg) => {
console.log(errMsg);
});
},
getindexcoursetwo: function (e) {
//得到二级课程的数据
const that = this;
let url = baseUrl.globalData.baseUrl + '/index/indexcoursepara';
var param = {
pareid: e
};
//此处为使用封装的post请求
util.post(url, param).then((res) => {
console.log('===================');
console.log(res.data);
that.setData({
onelist: res.data
});
}).catch((errMsg) => {
console.log(errMsg);
});
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
this.setData({
active: 1
});
// if (wx.getStorageSync('userinfo') != null) {
// baseUrl.globalData.currentuserinfo = wx.getStorageSync('userinfo');
// wx.setStorageSync('wxopenid', wx.getStorageSync('userinfo').openid);
// }
this.isdisplay();
wx.showShareMenu({
withShareTicket: true,
menus: ['shareAppMessage', 'shareTimeline']
})
this.getpopindexcouse(); //顶级分类
// this.getindexcourse(); //一级分类
this.getindexcoursetwo(1);
this.adsfun();
this.indexlives();
this.isjumpblack();
},
isjumpblack: function () {
console.log(wx.getStorageSync('userisblack'));
if (wx.getStorageSync('userisblack') == 2) {
wx.showToast({
title: "不允许访问该程序",//提示文字
duration: 5000,//显示时长
mask: true,//是否显示透明蒙层防止触摸穿透默认false
icon: 'none', //图标,支持"success"、"loading"
success: function () {
wx.reLaunch({
url: '/pages/userlogin/userlogin',
})
},//接口调用成功
fail: function () { }, //接口调用失败的回调函数
complete: function () { } //接口调用结束的回调函数
})
}
},
indexlives: function () {
//直播列表
const that = this;
let url = baseUrl.globalData.baseUrl + '/lives/indexlives';
var param = {
id: '123456789'
};
//此处为使用封装的post请求
util.get(url, param).then((res) => {
// console.log(res);
that.setData({
livesdata: res.data
});
}).catch((errMsg) => {
console.log(errMsg);
});
},
adsfun: function () {
//广告
const that = this;
let url = baseUrl.globalData.baseUrl + '/ads/index';
var param = {
id: '123456789'
};
//此处为使用封装的post请求
util.get(url, param).then((res) => {
// console.log(res);
that.setData({
movies: res.data
});
}).catch((errMsg) => {
console.log(errMsg);
});
},
livelistclick: function () {
if (this.data.isdisplay == 1) {
// 直播列表
wx.navigateTo({
url: '/pages/indexlives/indexlives',
})
} else {
wx.showToast({
title: `功能正在开发中`,
icon: 'none',
});
}
},
messageclick: function () {
wx.navigateTo({
url: '/pages/messagelist/messagelist',
})
},
resclick: function () {
wx.navigateTo({
url: '/pages/indexreslists/indexreslists',
})
},
courselearningclick: function (e) {
//课程学习
console.log(e.currentTarget.id);
wx.navigateTo({
url: '/pages/communityitemlist/communityitemlist?typeid=' + e.currentTarget.id,
})
},
picsclick: function (e) {
console.log(e);
if (this.data.isdisplay == 1) {
console.log(e.currentTarget.id);
wx.navigateTo({
url: '/pages/messageencyclopedias/messageencyclopedias?id=' + e.currentTarget.id,
})
}
},
isdisplay: function (params) {
const that = this;
let url = baseUrl.globalData.baseUrl + '/index/isdisplay';
var param = {
pareid: params
};
//此处为使用封装的post请求
util.get(url, param).then((res) => {
console.log(res);
that.setData({
isdisplay: res.data
});
}).catch((errMsg) => {
console.log(errMsg);
});
},
onShareAppMessage: function (options) {//分享onShareAppMessage
return {
title: '怼怼公考',
path: `/pages/index/index`,
success: function (res) {
console.log(res, '成功')
console.info(res + '成功');
wx.showToast({
title: '分享成功',
})
// 转发成功
},
fail: function (res) {
console.log(res + '失败');
// 转发失败
},
complete: function (res) {
// 不管成功失败都会执行
console.log(res, '成功或失败')
wx.showToast({
title: '成功或失败',
})
}
}
}
})