import {url} from '../request'
Page({
  data: {
    myyuyue:[],
    
  },
  onShow() {
    console.log('jjjjjjjj');
    my.getStorage({
      key: 'userInfo',
      success: (res) => {
        const userInfo = res.data;
        this.setData({
          id: userInfo.id, // 获取 id
        });
  
        if (userInfo) {
          my.request({
            url: url + '/api/orders/my/page',
            method: 'POST',
            data: {
              claimStatus: 0,
              current: 1,
              endTime: "",
              id: "",
              pageSize: 1000,
              paymentStatus: "",
              sortField: "",
              sortOrder: "",
              startTime: "",
              userId: userInfo.id,
              userRole: 0,
            },
            headers: {
              'content-type': 'application/json',
              
            },
            dataType: 'json',
            success: (res) => {
              console.log(res);
              if (res.data.code === 0) {
                // 在成功返回数据后，修改数据结构
                const updatedDingdan = res.data.data.records.map(item => {
                  // 遍历 orderItemsVOList，为每个商品添加 orderNumber
                  item.orderItemsVOList = item.orderItemsVOList.map(orderItem => {
                    orderItem.commoditiesVO.orderNumber = item.orderNumber; // 添加 orderNumber
                    return orderItem;
                  });
                  return item;
                });
  
                // 更新数据到页面
                this.setData({
                  dingdan: updatedDingdan,
                });
              } else{
                my.alert({
                  content: '',
                });
                my.navigateTo({
                  url: '/pages/denglu/denglu',
                });
              }
              console.log(res); 
              console.log(this.data.dingdan);
            },
            fail: (error) => {
              console.error('请求失败: ', JSON.stringify(error));
              my.alert({ content: '请求失败，请稍后重试' });
            },
          });
        } else {
          my.alert({
            content: '您未登录，请先登录。',
            success: () => {
              my.navigateTo({
                url: '/pages/denglu/denglu',
              });
            },
          });
        }
      },
    });
  },
  
});
