tabbarShared.uts
4.67 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { computed, ref } from "vue";
import { hideNativeTabBar } from "@/utils/tabBarShell.uts";
import { navigateToSelectStoreWithSessionStores } from "@/utils/shukeSelectStoreNav.uts";
export type HomeFrameTabbarSource = {
getTabIndex: () => number
getUserInfo: () => null | UTSJSONObject
}
/** TabBarShell `provide`:`Ref<UTSJSONObject>`;MainTabShell `inject`,与插槽同源 */
export const SCENE_SHELL_USER_INFO_INJECT_KEY = "homeFrame.sceneShellUserInfo"
/**
* 横屏侧栏头像文案:与 simgle_app `LeftSide` 使用 `userInfo.userName` 一致;
* 数科/ROP 返回里常见 `name`、`staffName`、`nickName`、`merchantName` 等作回退。
*/
export function resolveHomeFrameDisplayName(info: null | UTSJSONObject): string {
if (info == null) {
return "";
}
const obj = info as UTSJSONObject;
const userName = obj.userName as string | null;
if (userName != null && userName !== "") {
return userName;
}
const raw = obj.name ?? obj.staffName ?? obj.nickName ?? obj.merchantName;
if (raw == null) {
return "";
}
const s = `${raw}`;
return s !== "" ? s : "";
}
export type TabItem = {
label: string;
icon: string;
name: string;
/** 模板内仅用路径字段,避免 UTS 在 slot 中调用 import 的函数 */
iconSrcActive: string;
iconSrcInactive: string;
};
/** 与 pages.json tabBar 一致:首页 | 接待中心 | 我的(选店为普通页,见 shukeSelectStoreNav) */
export const HOME_FRAME_TABS: TabItem[] = [
{
label: "首页",
icon: "home",
name: "HomeSlot",
iconSrcActive: "/static/images/tabbar/home-active.png",
iconSrcInactive: "/static/images/tabbar/home.png"
},
{
label: "接待",
icon: "record",
name: "ReceptionCenter",
iconSrcActive: "/static/images/tabbar/record-active.png",
iconSrcInactive: "/static/images/tabbar/record.png"
},
{
label: "我的",
icon: "my",
name: "MyCenter",
iconSrcActive: "/static/images/tabbar/study-active.png",
iconSrcInactive: "/static/images/tabbar/study.png"
},
];
export function getTabIconSrc(icon: string, active: boolean): string {
if (icon === "home") {
return active ? "/static/images/tabbar/home-active.png" : "/static/images/tabbar/home.png";
}
if (icon === "record") {
return active ? "/static/images/tabbar/record-active.png" : "/static/images/tabbar/record.png";
}
if (icon === "study") {
return active ? "/static/images/tabbar/study-active.png" : "/static/images/tabbar/study.png";
}
if (icon === "my") {
return active ? "/static/images/tabbar/study-active.png" : "/static/images/tabbar/study.png";
}
return "/static/images/tabbar/home.png";
}
function reLaunchPage(pagesJsonPath: string): void {
const trimmed = pagesJsonPath.startsWith("/") ? pagesJsonPath.substring(1) : pagesJsonPath;
const withSlash = "/" + trimmed;
uni.reLaunch({
url: withSlash,
fail: (_err: any) => {
uni.reLaunch({ url: trimmed });
}
});
}
function switchTabPage(pagesJsonPath: string): void {
const trimmed = pagesJsonPath.startsWith("/") ? pagesJsonPath.substring(1) : pagesJsonPath;
const url = "/" + trimmed;
uni.switchTab({
url,
success: () => {
hideNativeTabBar();
},
fail: (_err: any) => {
reLaunchPage(trimmed);
}
});
}
export function useHomeFrameTabbar(source: HomeFrameTabbarSource) {
const showSetting = ref(false);
const currentTabIndex = computed((): number => {
return source.getTabIndex();
});
const userName = computed((): string => {
return resolveHomeFrameDisplayName(source.getUserInfo());
});
const navigateToTab = (index: number): void => {
const cur = source.getTabIndex();
if (index == cur) {
return;
}
if (index == 0) {
switchTabPage("pages/index/index");
return;
}
if (index == 1) {
switchTabPage("pages/receptionCenter/center");
return;
}
if (index == 2) {
switchTabPage("pages/person/myCenter");
return;
}
};
const onTabChange = (name: string | number): void => {
const idx = name as number;
navigateToTab(idx);
};
const onSettingVisible = (v: boolean): void => {
showSetting.value = v;
};
const onSetting = (): void => {
showSetting.value = true;
};
const jumpPersonCenter = (): void => {
uni.navigateTo({ url: "/pages/person/attendance" });
};
/** 横屏侧栏底栏「切换」:进选店页(非 tab) */
const onSwitchStore = async (): Promise<void> => {
try {
uni.showLoading({ title: "加载中", mask: true });
await navigateToSelectStoreWithSessionStores();
} catch (error) {
const msg = error != null ? `${error}` : "操作失败";
uni.showToast({ title: msg, icon: "none" });
} finally {
uni.hideLoading();
}
};
return {
showSetting,
currentTabIndex,
userName,
onTabChange,
onSettingVisible,
onSetting,
jumpPersonCenter,
onSwitchStore
};
}