123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- // subpackages/dahome/dahome.js
- // import { models, db, _ } from '../../utils/cloudbase.js'
- import { getDB, getModels, getCommand, getTempFileURLs } from '../../utils/cloudbase.js'
- Page({
- data: {
- levelOptions: ['初级', '中级', '高级'],
- selectedLevelIndex: 0,
- typeOptions: ['1', '2', '3'],
- selectedTypeIndex: 0,
- items: [],
- souimg: '',
- xiaziimg: '',
- showimg: '',
- xialaimg: '',
- pageNumber: 1,
- pageSize: 10,
- hasMore: true, // 是否还有更多数据
- isLoading: false, // 防止多次触发
- searchText: '', // 搜索关键词
- inputFocus: false,
- // goods_9: '',
- },
- async onLoad(options) {
- // 存储 options 到 this 中
- this.options = options;
- if (options.autofocus === '1') {
- // 延迟设置 focus,让页面完成渲染
- setTimeout(() => {
- this.setData({
- inputFocus: true
- });
- }, 300); // 300ms 通常足够
- }
- // 列表数据
- this.getdatalist()
- // 获取图片
- const fileIDs = [
- 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/sou.png',
- 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/xiazi.png',
- 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/show_1.png',
- 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/xiala.png',
- ];
- const fileList = await getTempFileURLs(fileIDs)
- this.setData({
- homepage_8: fileList[0].tempFileURL,
- homepage_9: fileList[1].tempFileURL,
- souimg: fileList[2].tempFileURL,
- souimg: fileList[0].tempFileURL,
- xiaziimg: fileList[1].tempFileURL,
- showimg: fileList[2].tempFileURL,
- xialaimg: fileList[3].tempFileURL,
- // goods_9: fileList[4].tempFileURL,
- })
-
- // // 并发下载多个 fileID
- // Promise.all(
- // fileIDs.map(fileID => wx.cloud.downloadFile({ fileID }))
- // ).then(results => {
- // // 每个 result 对应一个下载结果
- // const tempFilePaths = results.map(r => r.tempFilePath);
- // console.log('全部下载成功:', tempFilePaths);
- // this.setData({
- // souimg: tempFilePaths[0],
- // xiaziimg: tempFilePaths[1],
- // showimg: tempFilePaths[2],
- // xialaimg: tempFilePaths[3],
- // // goods_9: tempFilePaths[4],
- // });
- // }).catch(err => {
- // console.error('有文件下载失败:', err);
- // });
- },
- onReachBottom() {
- // 上拉触底事件的处理函数
- this.loadMore();
- },
- loadMore() {
- // 加载更多数据的逻辑
- console.log('加载更多');
- // this.getdatalist(true);
- if (this.data.isLoading || !this.data.hasMore) return;
- this.setData({ isLoading: true });
- this.getdatalist(true);
- },
- // 输入即搜索
- onSearchInput(e) {
- const searchText = e.detail.value;
- this.setData({
- searchText,
- pageNumber: 1,
- items: [],
- hasMore: true,
- isLoading: false
- });
- this.getdatalist(false);
- },
- // 列表数据
- async getdatalist(isLoadMore = false) {
- const models = await getModels()
- const { pageNumber, pageSize, selectedLevelIndex, selectedTypeIndex, searchText } = this.data;
-
- // 构造过滤条件
- const whereFilter = {
- tag_id: 'C05HDKNBDS',
- level: Number(selectedTypeIndex) + 1,
- dan: Number(selectedLevelIndex),
- };
-
- // 添加模糊搜索
- if (searchText.trim()) {
- whereFilter.name = {
- $regex_ci: searchText
- };
- }
-
- try {
- const { data } = await models.file_manage.list({
- filter: {
- where: whereFilter
- },
- pageSize,
- pageNumber,
- getCount: true,
- envType: "prod",
- });
-
- let collectList = data.records || []; // 注意用 let,不是 const
- // 处理云路径图片
- const coverFileIDs = collectList
- .map(item => item.cover)
- .filter(Boolean);
- let newList = collectList;
- if (coverFileIDs.length > 0) {
- const tempFiles = await getTempFileURLs(coverFileIDs);
- newList = collectList.map((item, index) => ({
- ...item,
- cover: tempFiles[index]?.tempFileURL || item.cover
- }));
- }
-
- this.setData({
- items: isLoadMore ? this.data.items.concat(newList) : newList,
- pageNumber: pageNumber + 1,
- hasMore: collectList.length === pageSize,
- isLoading: false
- });
- } catch (err) {
- console.error('数据获取失败:', err);
- }
- },
- handleLevelChange(e) {
- const index = Number(e.detail.value);
- this.setData({
- selectedLevelIndex: index,
- pageNumber: 1, // 重置分页,从第一页查
- hasMore: true,
- isLoading: false,
- items: [] // 可选:清空旧数据避免闪烁
- });
- this.getdatalist(false)
- },
- handleTypeChange(e) {
- const index = Number(e.detail.value);
- this.setData({
- selectedTypeIndex: index,
- pageNumber: 1, // 重置分页,从第一页查
- hasMore: true,
- isLoading: false,
- items: [] // 可选:清空旧数据避免闪烁
- });
- this.getdatalist(false)
- },
- goToGoodsList(event) {
- // 获取绑定的数据
- const item = event.currentTarget.dataset.item;
- // 将数据转换为 JSON 字符串并传递
- const itemStr = encodeURIComponent(JSON.stringify(item));
- wx.navigateTo({
- url: `/subpackages/details/details?item=${itemStr}`
- });
- }
- // // 新增数据 async
- // async dianji() {
- // const { data } = await models.file_manage.create({
- // data: {
- // level: 1, // 级别
- // range: ['大班'], // 适用范围
- // remark: "文本", // 备注
- // type: 0, // 文件类型
- // url: ['cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/123123.mp4'], // 路径
- // download_count: 1, // 下载人数
- // dan: 1, // 段位
- // cover: "cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/未命名的设计.png", // 封面
- // size: "123.12KB", // 文件大小
- // name: "动手动脑图库5", // 文件名
- // tag_id: "BULYF5VJ9W", // 标签id
- // publisher: "李老师", // 发布者
- // describe: "文本", // 描述
- // },
- // // envType: pre 体验环境, prod 正式环境
- // envType: "prod",
- // });
-
- // // 返回创建的数据 id
- // console.log(data);
- // },
- })
|