zhengyiming
2025-03-27 538e4f454ba3126ef92278ab9cb675adb9e3b287
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import './index.scss';
 
import { SetupContext } from 'vue';
import { View, Text, Image } from '@tarojs/components';
import Taro from '@tarojs/taro';
import { useSystemStore } from '@/stores/modules/system';
import { storeToRefs } from 'pinia';
import { TabBarPageRouter } from '@/constants';
// import IconPublish from '@/assets/tabbar/icon-publish.png';
import { useUser, useIsLogin } from '@/hooks';
import { Message } from '@/utils';
 
type TarBarItemProps = {
  text: string;
  icon: string;
  activeIcon: string;
  pagePath: string;
  active: boolean;
  className: string;
  onClick?: (url: string, index: number) => any;
  index: number;
  badge?: number;
};
 
type TarBarItemEvents = {
  click: (url: string, index: number) => any;
};
 
const TarBarItem = function (props: TarBarItemProps, context: SetupContext<TarBarItemEvents>) {
  const _badge = props.badge > 99 ? '99+' : props.badge;
  return (
    <div
      class={['bottom-tab-item', { active: props.active }, props.className]}
      onClick={() => context.emit('click', props.pagePath, props.index)}
    >
      <div class="bottom-tab-item-icon-wrapper">
        {props.badge > 0 && <div class="bottom-tab-item-badge">{_badge}</div>}
        <img class="bottom-tab-item-icon" src={props.active ? props.activeIcon : props.icon} />
      </div>
      <div class="bottom-tab-item-text">{props.text}</div>
    </div>
  );
};
 
TarBarItem.emits = {
  click: (url: string, index: number) => typeof url === 'string',
};
 
const whitePageList = [RouterPath.home, RouterPath.mine];
 
export default {
  name: 'CustomTabBar',
  components: {
    View,
    Text,
    Image,
    TarBarItem,
  },
  setup() {
    const system = useSystemStore();
    const { IPhoneXPadding } = storeToRefs(system);
 
    const router = Taro.useRouter();
 
    const isLogin = useIsLogin();
 
    const loginTipShowed = ref(false);
 
    const switchTab = (url: string, index: number) => {
      if (!isLogin.value && whitePageList.every((x) => x !== url)) {
        if (!loginTipShowed.value) {
          loginTipShowed.value = true;
          Message.confirm({ message: '请前往登录' })
            .then(() => {
              RouteHelper.navigateTo({
                url: `${RouterPath.loginByForm}?redirect=${url}`,
              });
            })
            .finally(() => {
              loginTipShowed.value = false;
            });
        }
        return;
      }
      system.setTabIndex(index);
      RouteHelper.switchTab({ url });
    };
 
    function goPublish() {
      if (!isLogin.value) {
        if (!loginTipShowed.value) {
          loginTipShowed.value = true;
          Message.confirm({ message: '请前往登录' })
            .then(() => {
              RouteHelper.navigateTo({
                url: `${RouterPath.loginByForm}?redirect=${RouterPath.home}`,
              });
            })
            .finally(() => {
              loginTipShowed.value = false;
            });
        }
 
        return;
      }
      // RouteHelper.navigateTo({
      //   url: RouterPath.pulishCircleFriend,
      // });
    }
 
    return () => {
      return (
        <View
          class="bottom-tab"
          style={{
            paddingBottom: `${IPhoneXPadding.value}px`,
          }}
        >
          <TarBarItem
            text={'首页'}
            icon="../assets/tabbar/icon-home.png"
            activeIcon="../assets/tabbar/icon-home-active.png"
            pagePath={TabBarPageRouter.Home}
            index={0}
            active={system.activeTab === 0}
            className="home"
            onClick={switchTab}
          ></TarBarItem>
          <TarBarItem
            text="我的"
            icon="../assets/tabbar/icon-mine.png"
            activeIcon="../assets/tabbar/icon-mine-active.png"
            pagePath={TabBarPageRouter.Mine}
            active={system.activeTab === 1}
            className="mine"
            onClick={switchTab}
            index={1}
          ></TarBarItem>
        </View>
      );
    };
  },
};