user.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <template>
  2. <view class="content">
  3. <user-page ref='userPage' v-show="isUserPage" @tab='changeTab'>
  4. </user-page>
  5. <shop-page ref='shopPage' v-show="!isUserPage" @tab='changeTab'>
  6. </shop-page>
  7. </view>
  8. </template>
  9. <script>
  10. import userPage from "./userPage.vue"
  11. import shopPage from "./shopPage.vue"
  12. import {
  13. mapState,
  14. mapMutations
  15. } from 'vuex';
  16. import {
  17. orderData,
  18. getUserInfo
  19. } from '@/api/user.js';
  20. export default {
  21. components: {
  22. userPage,
  23. shopPage
  24. },
  25. data() {
  26. return {
  27. isUserPage: true //默认用户页面
  28. };
  29. },
  30. computed: {
  31. ...mapState('user', [ 'hasLogin']),
  32. },
  33. onShow() {
  34. uni.setTabBarStyle({
  35. backgroundColor: '#FFFFFF'
  36. })
  37. // 判断是否已经登录
  38. this.loadBaseData();
  39. // 判断是否用户页面
  40. if (this.isUserPage&&this.hasLogin) {
  41. this.$nextTick(() => {
  42. this.$refs.userPage.getUser();
  43. })
  44. } else if(this.hasLogin){
  45. this.$nextTick(() => {
  46. this.$refs.shopPage.getInit();
  47. })
  48. }
  49. },
  50. methods: {
  51. toJSON(){
  52. return this;
  53. },
  54. ...mapMutations('user', ['setUserInfo', 'setOrderInfo']),
  55. changeTab(bool) {
  56. if (bool) {
  57. this.$refs.shopPage.getInit();
  58. }
  59. if (!bool) {
  60. this.$refs.userPage.getUser();
  61. }
  62. // 判断 是否切换为商家
  63. this.isUserPage = !bool;
  64. },
  65. // 加载初始数据
  66. loadBaseData() {
  67. getUserInfo({})
  68. .then(({
  69. data
  70. }) => {
  71. this.setUserInfo(data);
  72. // 获取用户数据完毕后在获取订单数据防止多次跳转到登录页
  73. orderData({})
  74. .then(({
  75. data
  76. }) => {
  77. this.setOrderInfo(data);
  78. })
  79. .catch(e => {
  80. this.setOrderInfo({
  81. complete_count: 0, //完成
  82. received_count: 0, //待收货
  83. unshipped_count: 0, //待发货
  84. order_count: 0, //订单总数
  85. unpaid_count: 0 //待付款
  86. });
  87. });
  88. })
  89. .catch(e => {
  90. console.log(e);
  91. });
  92. },
  93. /**
  94. * 统一跳转接口,拦截未登录路由
  95. * navigator标签现在默认没有转场动画,所以用view
  96. */
  97. }
  98. };
  99. </script>
  100. <style lang="scss">
  101. .content {
  102. /* #ifdef H5 */
  103. padding-bottom: 150rpx;
  104. /* #endif */
  105. }
  106. </style>