123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362 |
- import { models, db, _ } from '../../utils/cloudbase.js'
- Page({
- data: {
- categoriesindex: 1,
- categories: [
- {
- title: '全部',
- type: 1,
- },
- {
- title: '待支付',
- type: 2,
- },
- {
- title: '待收货',
- type: 3,
- },
- {
- title: '已完成',
- type: 4,
- }
- ],
- searchText: '', // 搜索关键词
- 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);
- },
- // 模糊搜索
- onSearchInput(e) {
- const value = e.detail.value.trim();
- this.setData({
- searchText: value,
- pageNumber: 1,
- hasMore: true,
- orders: []
- }, () => {
- this.getdatalist();
- });
- },
- async getdatalist(isLoadMore = false) {
- if (this.data.isLoading || !this.data.hasMore) return;
-
- this.setData({ isLoading: true });
-
- const { pageNumber, pageSize, categoriesindex, searchText, orders } = this.data;
-
- let dbRegExp = null;
- let jsRegExp = null;
-
- if (searchText) {
- dbRegExp = db.RegExp({
- regexp: searchText,
- options: 'i'
- });
- jsRegExp = new RegExp(searchText, 'i');
- }
-
- // 构造订单表的筛选条件
- // 获取本地存储的用户信息
- const userInfo = wx.getStorageSync('userInfo');
- const userId = userInfo && userInfo._id ? userInfo._id : ''; // 根据你的userInfo结构取ID
- const schoolId = userInfo && userInfo.school_id ? userInfo.school_id : ''; // 根据你的userInfo结构取ID
- let orderWhere = {
- user_id: userId, // 每次必传
- school_id: schoolId
- };
- if (categoriesindex !== 1) {
- orderWhere.status = categoriesindex - 2;
- }
- if (dbRegExp) {
- orderWhere = _.and([
- orderWhere,
- { order_id: dbRegExp }
- ]);
- }
-
- try {
- // 第一步:查订单表
- const { data } = await models.orders.list({
- filter: { where: orderWhere },
- pageSize,
- pageNumber,
- getCount: true,
- envType: 'prod'
- });
-
- const orderList = data.records || [];
- if (orderList.length === 0) {
- this.setData({ hasMore: false, isLoading: false });
- return;
- }
-
- // 第二步:查商品表
- const fileManagerIds = orderList.map(item => item.merchandise_id);
-
- const fileDetailPromises = fileManagerIds.map(id => {
- return models.wx_merchandise.list({
- filter: { where: { _id: id } },
- envType: 'prod'
- }).then(res => res.data?.records?.[0] || null)
- .catch(err => {
- console.error(`商品获取失败: ${id}`, err);
- return null;
- });
- });
-
- const fileDetails = await Promise.all(fileDetailPromises);
-
- // 第三步:合并数据,并进行模糊搜索筛选(匹配一个就保留)
- const mergedOrders = [];
-
- for (let i = 0; i < orderList.length; i++) {
- const order = orderList[i];
- const merch = fileDetails[i];
-
- if (!merch) continue;
-
- if (jsRegExp) {
- const matchOrderId = jsRegExp.test(order._id || '');
- const matchName = jsRegExp.test(merch.name || '');
-
- if (!matchOrderId && !matchName) {
- continue; // 都没匹配上,跳过
- }
- }
-
- // 先把 _id 改名为 order_id
- const { _id, school_id, ...restOrder } = order;
- const orderRenamed = {
- ...restOrder,
- orders_id: _id,
- schools_id: school_id
- };
- mergedOrders.push({
- ...orderRenamed,
- ...merch
- });
- }
-
- // 第四步:更新数据
- this.setData({
- orders: isLoadMore ? orders.concat(mergedOrders) : mergedOrders,
- pageNumber: pageNumber + 1,
- hasMore: orderList.length === 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,
- searchText: '', // ✅ 清空搜索关键词
- pageNumber: 1, // 重置页码
- hasMore: true, // 重置是否有更多数据
- orders: [], // 清空旧数据
- }, () => {
- console.log('categoriesindex:', this.data.categoriesindex);
- this.getdatalist(false);
- });
- },
-
- // 处理第一个按钮的点击事件
- handleAction1: function (event) {
- const index = event.currentTarget.dataset.index; // 获取点击的订单索引
- const status = this.data.orders[index].status; // 获取订单状态
- switch (status) {
- case 1:
- console.log("查看详情", index);
- const item = this.data.orders[index];
- wx.navigateTo({
- url: '/subpackages/orderdetails/orderdetails?data=' + encodeURIComponent(JSON.stringify(item))
- });
- // 执行查看详情的逻辑
- break;
- case 0:
- console.log("取消订单", index);
- // 执行取消订单的逻辑
- break;
- case 2:
- console.log("申请售后", index);
- // 执行申请售后的逻辑
- const items = this.data.orders[index];
- console.log(items, 'itemsitemsitemsitems');
- wx.navigateTo({
- url: '/subpackages/afterservice/afterservice?data=' + encodeURIComponent(JSON.stringify(items))
- });
- 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);
- this.getyesordels(this.data.orders[index])
- // 执行确认收货的逻辑
- break;
- case 0:
- console.log("立即支付", index);
- // 执行立即支付的逻辑
- break;
- case 2:
- console.log("再来一单", index);
- // 执行再来一单的逻辑
- break;
- default:
- console.log("未知状态");
- }
- },
- // 确认收货
- async getyesordels (list) {
- console.log(list, 'list');
- const userInfo = wx.getStorageSync('userInfo');
- const userId = userInfo && userInfo._id ? userInfo._id : ''; // 根据你的userInfo结构取ID
- const schoolId = userInfo && userInfo.school_id ? userInfo.school_id : ''; // 根据你的userInfo结构取ID
- const { data } = await models.orders.update({
- data: {
- status: 2, // 状态
- },
- filter: {
- where: {
- $and: [
- {
- merchandise_id: {
- $eq: list._id, // 推荐传入_id数据标识进行操作
- },
- },
- {
- user_id: {
- $eq: userId, // 推荐传入_id数据标识进行操作
- },
- },
- {
- school_id: {
- $eq: schoolId, // 推荐传入_id数据标识进行操作
- },
- },
- ]
- }
- },
- envType: "prod",
- });
- console.log(data.count, 'data.count');
- if (Number(data.count) > 0) {
- wx.showToast({
- title: '确认收货成功',
- icon: 'success',
- duration: 1500
- });
- // ✅ 重新获取数据
- this.setData({
- pageNumber: 1,
- hasMore: true,
- orders: []
- }, () => {
- this.getdatalist(false);
- });
- } else {
- wx.showToast({
- title: '确认收货失败',
- icon: 'error',
- duration: 1500
- });
- }
- },
- 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))
- });
- },
- // async adds () {
- // const { data } = await models.orders.create({
- // data: {
- // adresses_id: "BWSLJAGVZA", // 地址id
- // sys_updateby_id: "", // 更新人
- // merchandise_id: "BULGAD2DA8", // 商品id
- // real_money: 223.12, // 实际付款金额
- // updat_adress: "", // 修改后地址
- // update_phone: "", // 修改后电话
- // delete: 0, // 逻辑删除
- // way: 1, // 收货方式
- // update_name: "", // 修改后收货人
- // school_id: "BU5N9CFX2Q", // 学校_id
- // user_id: "BWYRRKQVNY", // 用户_id
- // num_index: 1, // 商品数据
- // tracking_number: "物流单号888", // 物流单号
- // order_id: "订单号888", // 订单号
- // status: 2, // 状态
- // },
- // // envType: pre 体验环境, prod 正式环境
- // envType: "prod",
- // });
-
- // // 返回创建的数据 id
- // console.log(data);
- // }
- });
|