main.ts
4.74 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
import {createSSRApp} from "vue";
import App from "./App.vue";
import {createI18n, useI18n} from "vue-i18n";
import "./main.scss";
// import "./components/uni-plate-input/uni-plate-input.css";
import messages from '@/common/i18n/index';
import type {AxiosStatic} from "axios";
import axios from "axios";
import filterValueData from "@/common/filters";
import uviewPlus from 'uview-plus';
import {AuthService} from "@/common/AuthService";
import dayjs from "dayjs";
import duration from "dayjs/plugin/duration";
import {CustomTokenInterceptor} from "@/common/CustomTokenInterceptor";
import store from "@/store";
import mpAdapter from 'axios-miniprogram-adapter'
import {BurialPoint} from "@/common/BurialPoint";
import {resolveApiConfig} from "@/config/resolveApiConfig";
/// <reference types="@dcloudio/types" /> // 引入 uni 类型
const i18n = createI18n({
locale: 'zh-cn',
legacy: false,
globalInjection: true,
fallbackLocale: 'zh-cn',
messages: messages
});
const systemI18n = {
setup() {
const {locale, t} = useI18n();
return {locale, t};
}
};
dayjs.extend(duration);
export function createApp() {
const {apiUrl, baseUrl, miniProgramAppId, envKey, source, runtime} = resolveApiConfig();
console.log(
'[runtime] API',
`source=${source ?? 'unknown'}`,
`envKey=${envKey ?? 'env-default'}`,
apiUrl,
miniProgramAppId ?? '',
runtime?.envVersion ?? '',
runtime?.appId ?? '',
);
const authService: AuthService = new AuthService();
// authService.deleteAuthentication();
// authService.removeToken();
// 重写路由,全局实现埋点
// 保存原始跳转方法
const originalNavigateTo: typeof uni.navigateTo = uni.navigateTo;
const originalRedirectTo = uni.redirectTo;
const originalSwitchTab = uni.switchTab;
const originalNavigateBack = uni.navigateBack;
// 重写 navigateTo(打开新页面)
uni.navigateTo = (options: UniApp.NavigateToOptions) => {
const fromPage = getCurrentPages().slice(-1)[0]; // 当前页面
const toPage = options.url; // 目标页面路径
// 可在此处记录日志(如发送到后台)
return originalNavigateTo.call(uni, options); // 执行原始跳转
};
// 重写 redirectTo(关闭当前页,打开新页面)
uni.redirectTo = (options: UniApp.RedirectToOptions) => {
const fromPage = getCurrentPages().slice(-1)[0];
const toPage = options.url;
return originalRedirectTo.call(uni, options);
};
// 重写 switchTab(切换底部 tab)
uni.switchTab = (options: UniApp.SwitchTabOptions) => {
const fromPage = getCurrentPages().slice(-1)[0];
const toPage = options.url as string;
// 埋点
switch (toPage) {
case 'pages/tabbar/HomePage':
BurialPoint.burialPoint(authService.getAuthentication(), 'auction_nav_hometab_click', '0', toPage);
break;
case 'pages/tabbar/AuctionPage':
BurialPoint.burialPoint(authService.getAuthentication(), 'auction_nav_auctiontab_click', '0', toPage);
break;
case 'pages/tabbar/ProfilePage':
BurialPoint.burialPoint(authService.getAuthentication(), 'auction_nav_mytab_click', '0', toPage);
break;
}
return originalSwitchTab.call(uni, options);
};
// // 重写 navigateBack(返回上一页)
// uni.navigateBack = function (options: UniApp.NavigateBackOptions) {
// const fromPage = getCurrentPages().slice(-1)[0];
// const delta = options.delta || 1;
// const toPageIndex = getCurrentPages().length - 1 - delta;
// const toPage = toPageIndex >= 0 ? getCurrentPages()[toPageIndex].route : '首页';
// console.log(`从 ${fromPage.route} 返回至 ${toPage}(返回层级:${delta})`);
// return originalNavigateBack.call(this, options);
// };
const axiosInstance = axios.create({
timeout: 1000 * 60, baseURL: apiUrl, headers: {},
// @ts-ignore
adapter: mpAdapter
});
const tokenInterceptor: CustomTokenInterceptor = new CustomTokenInterceptor(axiosInstance, authService);
const app = createSSRApp(App, systemI18n);
app.use(i18n).use(store).use(uviewPlus);
// 挂载到全局的每一个Vue实例上
app.config.globalProperties.axios = axiosInstance as AxiosStatic;
app.config.globalProperties.$apiUrl = apiUrl;
app.config.globalProperties.$baseUrl = baseUrl;
app.config.globalProperties.$miniProgramAppId = miniProgramAppId;
app.config.globalProperties.$filters = filterValueData;
app.config.globalProperties.$store = store;
app.config.globalProperties.$auth = authService;
return {
app,
};
}