123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- import { models, db, _ } from '../../utils/cloudbase.js'
- Page({
- data: {
- categoriesindex: 1,
- categories: [
- {
- title: '全部',
- type: 1,
- },
- {
- title: '待支付',
- type: 2,
- },
- {
- title: '待收货',
- type: 3,
- },
- {
- title: '已完成',
- type: 4,
- }
- ],
- orders: [],
- souimg: '',
- pageNumber: 1,
- pageSize: 10,
- hasMore: true, // 是否还有更多数据
- isLoading: false, // 防止多次触发
- },
- onLoad(options) {
- const type = Number(options.type) || 1;
- console.log('收到的 type 参数:', type);
- // 根据 type 加载数据
- this.setData({
- categoriesindex: type
- }, () => {
- this.getdatalist(); // 确保 this 指向正确
- });
- const fileIDs = [
- 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/sou.png',
- ];
- // 并发下载多个 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]
- });
- }).catch(err => {
- console.error('有文件下载失败:', err);
- });
- },
-
- onReachBottom() {
- // 上拉触底事件的处理函数
- this.loadMore();
- },
- loadMore() {
- // 加载更多数据的逻辑
- console.log('加载更多');
- this.getdatalist(true);
- },
- async getdatalist(isLoadMore = false) {
- if (this.data.isLoading || !this.data.hasMore) return;
-
- this.setData({ isLoading: true });
- const { pageNumber, pageSize } = this.data;
- try {
- // 根据 categoriesindex 的值动态设置 filter
- const filter = {};
- if (this.data.categoriesindex !== 1) {
- filter.where = {
- status: this.data.categoriesindex - 2, // 减去 2
- };
- }
- const { data } = await models.orders.list({
- filter, // 动态传递 filter
- pageSize,
- pageNumber,
- getCount: true, // 开启用来获取总数
- envType: 'prod', // 正式环境
- });
- // 返回查询到的数据列表 records 和 总数 total
- const collectList = data.records || [];
- if (collectList.length === 0) {
- this.setData({ hasMore: false, isLoading: false });
- return;
- }
- console.log(collectList, 'collectList');
- const fileManagerIds = collectList.map(item => item.merchandise_id);
- console.log(fileManagerIds, 'fileManagerIds');
- // 第二步:循环获取每个文件数据(单条查询)
- const fileDetailPromises = fileManagerIds.map(id => {
- return models.wx_merchandise.list({
- filter: {
- where: {
- _id: id
- }
- },
- envType: "prod"
- }).then(res => {
- const record = res.data?.records?.[0];
- if (!record) {
- console.warn(`未找到 _id 为 ${id} 的文件`);
- }
- return record || null;
- })
- .catch(err => {
- console.error(`获取文件 ${id} 失败`, err);
- return null;
- });
- });
- // 第三步:等待所有请求完成
- const fileDetails = await Promise.all(fileDetailPromises);
- console.log(fileDetails, 'fileDetails');
- // 第四步:过滤无效项,并添加 checked 字段
- const newFiles = fileDetails
- .filter(Boolean) // 去掉 null 或 undefined
- .map((file, index) => {
- const historyRecord = collectList[index]; // 获取原 download_history 的记录
- if (!file) return null;
- return {
- ...file,
- ...historyRecord
- };
- });
- this.setData({
- orders: isLoadMore ? this.data.orders.concat(newFiles) : newFiles,
- pageNumber: pageNumber + 1,
- hasMore: collectList.length === pageSize, // 如果返回的数量小于 pageSize,说明已经到底
- isLoading: false
- });
- } catch (err) {
- console.error('获取失败:', err);
- this.setData({ isLoading: false });
- }
- },
- tabcategories(e) {
- const type = e.currentTarget.dataset.type;
- console.log('type:', type);
- this.setData({
- categoriesindex: type,
- pageNumber: 1, // 重置页码
- hasMore: true, // 重置是否有更多数据
- orders: [], // 清空旧数据
- }, () => {
- console.log('categoriesindex:', this.data.categoriesindex);
- this.getdatalist(false);
- });
- },
- handleAction1(e) {
- console.log('查看详情', e);
- },
- handleAction2(e) {
- console.log('处理其他操作', e);
- },
- // 处理第一个按钮的点击事件
- handleAction1: function (event) {
- const index = event.currentTarget.dataset.index; // 获取点击的订单索引
- const status = this.data.orders[index].status; // 获取订单状态
- switch (status) {
- case 1:
- console.log("查看详情", index);
- // 执行查看详情的逻辑
- break;
- case 2:
- console.log("取消订单", index);
- // 执行取消订单的逻辑
- break;
- case 3:
- console.log("申请售后", index);
- // 执行申请售后的逻辑
- break;
- default:
- console.log("未知状态");
- }
- },
- // 处理第二个按钮的点击事件
- handleAction2: function (event) {
- const index = event.currentTarget.dataset.index; // 获取点击的订单索引
- const status = this.data.orders[index].status; // 获取订单状态
- switch (status) {
- case 1:
- console.log("确认收货", index);
- // 执行确认收货的逻辑
- break;
- case 2:
- console.log("立即支付", index);
- // 执行立即支付的逻辑
- break;
- case 3:
- console.log("再来一单", index);
- // 执行再来一单的逻辑
- break;
- default:
- console.log("未知状态");
- }
- },
- goToGoodsList (e) {
- const index = e.currentTarget.dataset.type;
- const item = this.data.orders[index];
- // console.log(e.currentTarget, 'item');
- wx.navigateTo({
- url: '/subpackages/orderdetails/orderdetails?data=' + encodeURIComponent(JSON.stringify(item))
- });
- }
- });
|