kuaiying_wx/pages/details/details.js

246 lines
6.5 KiB
JavaScript
Raw Permalink Normal View History

2025-03-04 15:25:38 +08:00
var app = getApp();
Page({
data: {
currentPath: '', // 当前路径
breadcrumbPath: '', // 上一级路径
currentItems: [], // 当前目录内容
hasMore: false, // 是否有更多数据
showQRPopup: false, // 控制二维码弹窗显示
popupAnimation: {}, // 弹窗动画
page: 1, // 当前页码
pageSize: 20, // 每页大小
isRoot: true, // 是否是根目录
qrcodeUrl: 'https://file.guimiaokeji.com/%E5%B9%BF%E5%91%8A/C55BDC06151A606BDAEBCF787283874E.jpg', // 二维码图片链接
},
onLoad() {
wx.showShareMenu({
withShareTicket: !0,
menus: [ "shareAppMessage", "shareTimeline" ]
});
},
showVip(){
wx.navigateTo({
url: "../vip_recharge/vip_recharge?show=true"
})
}
,
// 默认加载第一个目录
loadDefaultDirectory() {
wx.showLoading({ title: '加载中...' });
app.apiRequest({
url: '/api/courses/',
method: 'GET',
data: {
path: '/', // 根路径
page_num: 1,
page_size: 1, // 仅获取第一个目录
},
success: (res) => {
if (res.data.status === 'success') {
const firstItem = res.data.data.files[0];
if (firstItem && firstItem.type === 1) {
// 如果第一个项目是目录,加载该目录
this.loadDirectory(`/${firstItem.name}`);
} else {
wx.showToast({ title: '没有可用的目录', icon: 'none' });
}
} else {
wx.showToast({ title: '获取数据失败', icon: 'none' });
}
},
fail: (err) => {
wx.showToast({ title: '加载失败,请重试', icon: 'none' });
console.error(err);
},
complete: () => wx.hideLoading(),
});
},
// 加载指定目录内容
loadDirectory(path, append = false) {
wx.showLoading({ title: '加载中...' });
// 确保路径不会出现多个斜杠
const sanitizedPath = path.replace(/\/+/g, '/');
app.apiRequest({
url: '/api/courses/',
method: 'GET',
data: {
path: sanitizedPath,
page_num: this.data.page,
page_size: this.data.pageSize,
},
success: (res) => {
if (res.data.status === 'success') {
const newItems = res.data.data.files.map((item) => ({
...item,
path: `${sanitizedPath}/${item.name}`,
}));
const paths = sanitizedPath.split('/').filter(Boolean);
const breadcrumbPath = paths.slice(0, -1).join('/') || '/';
this.setData({
currentItems: append ? this.data.currentItems.concat(newItems) : newItems,
currentPath: sanitizedPath,
breadcrumbPath,
hasMore: newItems.length === this.data.pageSize,
isRoot: sanitizedPath === '/', // 根目录判断
});
} else {
wx.showToast({ title: '获取数据失败', icon: 'none' });
}
},
fail: (err) => {
wx.showToast({ title: '加载失败,请重试', icon: 'none' });
console.error(err);
},
complete: () => wx.hideLoading(),
});
},
// 点击目录或文件
handleItemClick(e) {
const { type, path } = e.currentTarget.dataset;
if (type === 1) { // 目录
this.setData({ page: 1 });
this.loadDirectory(path);
} else if (type === 0) { // 文件
this.setData({
qrcodeUrl: this.data.qrcodeUrl, // 假设文件路径作为二维码图片链接
});
this.showQRCode();
}
},
// 返回上级目录
goToParent() {
const paths = this.data.currentPath.split('/').filter(Boolean);
paths.pop();
const parentPath = '/' + paths.join('/');
this.setData({ page: 1 });
this.loadDirectory(parentPath);
},
// 加载更多
loadMore() {
if (!this.data.hasMore) return;
this.setData({ page: this.data.page + 1 });
this.loadDirectory(this.data.currentPath, true);
},
// 显示二维码弹窗
showQRCode() {
this.setData({ showQRPopup: true });
const animation = wx.createAnimation({
duration: 300,
timingFunction: 'ease',
});
animation.scale(1).opacity(1).step();
this.setData({ popupAnimation: animation.export() });
},
// 关闭二维码弹窗
closeQRCode() {
const animation = wx.createAnimation({
duration: 300,
timingFunction: 'ease',
});
animation.scale(0.8).opacity(0).step();
this.setData({ popupAnimation: animation.export() });
setTimeout(() => {
this.setData({ showQRPopup: false });
}, 300);
},
// 防止点击弹窗内部关闭弹窗
preventClose() {
// 空函数,阻止事件冒泡
},
// 保存二维码到相册
saveQRCode() {
const { qrcodeUrl } = this.data;
wx.showLoading({ title: '正在保存...' });
wx.downloadFile({
url: qrcodeUrl,
success: (res) => {
if (res.statusCode === 200) {
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success: () => {
wx.showToast({ title: '二维码已保存', icon: 'success' });
},
fail: (err) => {
wx.showToast({ title: '保存失败,请重试', icon: 'none' });
console.error(err);
},
});
}
},
fail: (err) => {
wx.showToast({ title: '下载失败,请重试', icon: 'none' });
console.error(err);
},
complete: () => wx.hideLoading(),
});
},
onShareAppMessage: function () {
return {
title: '分享给你一份全链路的自媒体课程,赶快来领取吧',
path: '/pages/details/details?uuid=' + wx.getStorageSync('uuid'),
imageUrl: 'https://file.guimiaokeji.com/kecheng.png',
success: function (e) {
wx.showToast({
title: "分享成功",
icon: "success",
duration: 2e3
});
},
fail: function (e) {
wx.showToast({
title: "分享失败",
icon: "none",
duration: 2e3
});
}
}
},
onShareTimeline: function () {
return {
title: '分享给你一份全链路的自媒体课程,赶快来领取吧',
path: '/pages/details/details?uuid=' + wx.getStorageSync('uuid'),
imageUrl: 'https://file.guimiaokeji.com/kecheng.png',
success: function (e) {
wx.showToast({
title: "分享成功",
icon: "success",
duration: 2e3
});
},
fail: function (e) {
wx.showToast({
title: "分享失败",
icon: "none",
duration: 2e3
});
}
}
},
});