index.vue 6.46 KB
<!--
 * @Date: 2023-12-13 11:13:13
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2023-12-27 19:30:49
 * @FilePath: /meihuaApp/src/pages/my/index.vue
 * @Description: 我的页面
-->
<template>
  <view class="my-page">
    <view id="avatar" class="user-wrapper">
      <view class="user-info">
        <nut-avatar size="60" style="border: 1px solid #fff;">
          <img style="border-radius: 50%;" :src="userInfo.avatar ? userInfo.avatar : avatar" />
        </nut-avatar>
        <text style="margin-left: 30rpx;">{{ userInfo.name }}</text>
      </view>
      <view class="edit">
        <IconFont name="edit" @tap="goToEdit"></IconFont>
      </view>
      <view class="wrapper-bottom"></view>
    </view>
    <view class="list-wrapper">
      <view id="title" class="title">我的订单</view>
      <nut-tabs v-model="tab_index" @click="onTabClick" title-scroll title-gutter="0" background="#FFF" color="#6A4925" animated-time="0">
        <nut-tab-pane v-for="item in tabList" :title="item.title" :pane-key="item.key" />
      </nut-tabs>
      <view v-if="showContent" class="book-list">
        <scroll-view v-if="order_list.length" ref="refScrollView" :style="scrollStyle" :scroll-y="true" :scroll-with-animation="true" @scrolltolower="onScrollToLower">
          <view v-for="(item, index) in order_list" :key="index">
            <order-card :key="index" :data="item" :sys="sys_data" @onPay="onPay"></order-card>
            <view v-if="index === order_list.length-1" style="height: 2rem;"></view>
          </view>
        </scroll-view>
        <nut-empty v-else image="https://static-ftcms.jd.com/p/files/61a9e3313985005b3958672e.png" description="暂无数据" ></nut-empty>
      </view>
    </view>
    <nav-bar activated="my" />
    <payCard :visible="show_pay" :data="payData" @close="onPayClose"/>
  </view>
</template>

<script setup>
import Taro from '@tarojs/taro'
import { ref, onMounted, computed } from "vue";
import { IconFont } from '@nutui/icons-vue-taro';
import navBar from '@/components/navBar.vue'
import orderCard from '@/components/orderCard.vue'
import payCard from '@/components/payCard.vue'
import avatar from '@/assets/images/avatar.png'
import { showMyInfoAPI, myOrderAPI, sysParamAPI } from '@/api/index'
import { getCurrentPageParam } from "@/utils/weapp";

const refScrollView = ref(null);

const show_pay = ref(false);
const payData = ref({});
const userInfo = ref({
  name: '',
  avatar: '',
  phone: ''
});

onMounted(async () => {
  // 获取用户信息
  const { code, data } = await showMyInfoAPI();
  if (code) {
    userInfo.value = {
      name: data.wxapp_user_name ? data.wxapp_user_name : '默认用户',
      avatar: data.wxapp_user_avatar,
      phone: data.wxapp_user_phone
    }
  }
});

const onPay = ({ id, remain_time }) => {
  console.warn(id);
  // Taro.showToast({
  //   title: '支付已超时',
  //   icon: 'error',
  //   duration: 2000
  // });
  show_pay.value = true;
  payData.value.id = '123';
  payData.value.price = 1200;
  payData.value.remain_time = remain_time;
}

const onPayClose = () => {
  show_pay.value = false;
}

const goToEdit = async () => {
  if (userInfo.value.phone) {
    Taro.navigateTo({
      url: '/pages/myInfo/index',
    });
  } else {
    Taro.navigateTo({
      url: '/pages/login/index?page=my',
    });
  }
}
</script>

<script>
import "./index.less";
import { $ } from '@tarojs/extend'
import mixin from '@/utils/mixin';

export default {
  name: "myPage",
  mixins: [mixin.init],
  computed: {
    scrollStyle() {
      return {
        height: this.indexCoverHeight + 'px',
      };
    },
  },
  async onShow () {
    Taro.showLoading({ mask: true, title: "加载中..." })
    // 获取活动和轮播信息
    const { code, data } = await myOrderAPI({ page: this.page, limit: this.limit, pay_type: this.tab_index });
    if (code) {
      this.order_list = data;
      this.page = this.page + 1;
      Taro.hideLoading()
    }
    // 获取系统参数
    const sysData = await sysParamAPI();
    if (sysData.code) {
      this.sys_data = sysData.data;
    }
  },
  onHide () { // 离开当前页面
    this.page = 1;
    this.flag = true;
  },
  mounted () {
    // 设置首页封面高度
    const windowHeight = wx.getSystemInfoSync().windowHeight;
    // 处理切换显示白屏问题
    setTimeout(() => {
      this.showContent = true;
    }, 100);
    // setTimeout(async () => {
    //   const navHeight = await $('#navbar-page').height();
    //   const avatarHeight = await $('#avatar').height();
    //   const titleHeight = await $('#title').height();
    //   this.indexCoverHeight = windowHeight - navHeight - avatarHeight - titleHeight - 50;
    // }, 500);
    this.$nextTick(async () => {
      const navHeight = await $('#navbar-page').height();
      const avatarHeight = await $('#avatar').height();
      const titleHeight = await $('#title').height();
      this.indexCoverHeight = windowHeight - navHeight - avatarHeight - titleHeight - 50;
      if (this.$refs.refScrollView) {
        Taro.hideLoading();
        console.warn('加载完成');
      }
    });
    // 默认勾选栏目
    let params = getCurrentPageParam();
    if (params.tab_index) {
      this.tab_index = params.tab_index;
    }
  },
  data() {
    return {
      showContent: false,
      indexCoverHeight: 0,
      order_list: [],
      sys_data: {},
      flag: true,
      page: 1,
      limit: 10,
      tab_index: 'all',
      // 房间状态(9=已入住,2=已取消,3=未支付,5=已支付)
      tabList: ([{
        title: '全部订单',
        key: 'all',
      }, {
        title: '待入住',
        key: '5',
      }, {
        title: '待支付',
        key: '3',
      }, {
        title: '已取消',
        key: '2',
      }])
    };
  },
  methods: {
    onTabClick ({ title, paneKey, disabled }) {
      this.tab_index = paneKey;
      this.order_list = [];
      this.page = 1;
      this.flag = true;
      this.getList();
    },
    onScrollToLower () {
      if(!this.flag){
        return
      }
      this.flag = false;
      this.getList();
    },
    async getList () {
      // 获取列表
      const { code, data } = await myOrderAPI({ page: this.page, limit: this.limit, pay_type: this.tab_index });
      if (code) {
        if (data.length) {
          this.order_list = this.order_list.concat(data);
          this.page = this.page + 1;
          this.flag = true;
        } else {
          Taro.showToast({
            title: '没有更多了',
            icon: 'none'
          });
        }
      }
    },
  }
};
</script>