@tmsfe/tms-core 0.0.22 → 0.0.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmsfe/tms-core",
3
- "version": "0.0.22",
3
+ "version": "0.0.23",
4
4
  "description": "tms运行时框架",
5
5
  "repository": {
6
6
  "type": "git",
@@ -11,7 +11,7 @@
11
11
  "build": "rimraf dist && rollup -c --environment TARGET:tms-core,INTER_ENV:public"
12
12
  },
13
13
  "main": "dist/index",
14
- "miniprogram": "dist",
14
+ "miniprogram": "src",
15
15
  "buildOptions": {
16
16
  "formats": [
17
17
  "esm"
@@ -1,60 +0,0 @@
1
- import { a as getAuthInfo, g as getEnvInfo } from './env-c7da70e1.js';
2
-
3
- /**
4
- * callCloudFunc 方法 调用云函数
5
- * @param {String} name 要调用的云函数
6
- * @param {Object} data 需要传给云函数的参数
7
- * @param {Boolean} withAuth 是否需要补充userId,token参数,当云函数需要调用SinanServer时,需要这些参数来完成鉴权
8
- * @returns {viod} 云函数
9
- */
10
-
11
- const callCloudFunc = async (name = '', data = {}, withAuth = true) => {
12
- const {
13
- cloudEnvId,
14
- appVersion
15
- } = getEnvInfo();
16
- const timestamp = Date.now();
17
- const random = Math.random().toString().slice(2, 7);
18
- const sourceId = 7; // 6 未知 7 云函数 8 出行 9 我的车
19
-
20
- const mergedData = { ...data,
21
- timestamp,
22
- seqId: `${timestamp}${sourceId}${random}`,
23
- // 补充seqId参数
24
- appVersion // 补充appVersion参数
25
-
26
- };
27
-
28
- if (withAuth) {
29
- // 补充userId, token参数
30
- const {
31
- userId,
32
- token
33
- } = await getAuthInfo();
34
- Object.assign(mergedData, {
35
- userId,
36
- token
37
- });
38
- }
39
-
40
- const res = await new Promise((resolve, reject) => {
41
- wx.cloud.callFunction({
42
- name,
43
- data: mergedData,
44
- config: {
45
- env: cloudEnvId
46
- },
47
- success: resolve,
48
- fail: err => {
49
- reject({
50
- err,
51
- name,
52
- mergedData
53
- });
54
- }
55
- });
56
- });
57
- return res;
58
- };
59
-
60
- export { callCloudFunc };
@@ -1,146 +0,0 @@
1
- const env = {
2
- wxAppId: '',
3
- // 当前运行时小程序的appId
4
- appVersion: '',
5
- // 当前运行时所在项目的版本号
6
- appEnv: '',
7
- // 运行环境 test - 测试、production - 正式
8
- client: '',
9
- // 运行时项目名,sinan - 腾讯出行服务; mycar - 我的车小程序
10
- cloudEnvId: '' // 运行云函数的环境id
11
-
12
- };
13
- let baseAuthInfo = undefined;
14
- const getAuthInfoQueue = [];
15
- /**
16
- * @description 设置用户信息
17
- * @param {Object} authInfo 用户信息
18
- * @param {Object} err err
19
- * @returns {Void} 无返回值
20
- */
21
-
22
- const setAuthInfo = (authInfo, err) => {
23
- baseAuthInfo = authInfo;
24
-
25
- while (getAuthInfoQueue.length) {
26
- const pro = getAuthInfoQueue.shift();
27
-
28
- if (err) {
29
- pro.reject(err);
30
- } else {
31
- pro.resolve(baseAuthInfo);
32
- }
33
- }
34
- };
35
- /**
36
- * 获取登录状态信息
37
- * @returns {Object} 当前用户状态参数
38
- */
39
-
40
- const getAuthInfo = async () => {
41
- if (baseAuthInfo !== undefined) {
42
- return baseAuthInfo;
43
- }
44
-
45
- return new Promise((resolve, reject) => getAuthInfoQueue.push({
46
- resolve,
47
- reject
48
- }));
49
- };
50
- /**
51
- * tms.getEnvInfo 用于获取运行时所在的环境信息
52
- * @returns {object} 运行时环境信息.
53
- */
54
-
55
- const getEnvInfo = () => env;
56
- /**
57
- * 设置环境变量
58
- * @param {object} envInfo 环境变量
59
- * @returns {undefined} 无.
60
- */
61
-
62
- const setEnvInfo = envInfo => {
63
- const {
64
- wxAppId,
65
- appVersion,
66
- appEnv,
67
- client,
68
- cloudEnvId
69
- } = envInfo;
70
- env.wxAppId = wxAppId;
71
- env.appVersion = appVersion;
72
- env.appEnv = appEnv;
73
- env.client = client;
74
- env.cloudEnvId = cloudEnvId;
75
- };
76
- let appPagePaths = []; // app页面路径集合
77
-
78
- let homePageInfo = null; // 首页信息 { isTab, path, tabs }
79
- /**
80
- * 设置app内所有页面的路径
81
- * @param {Array<String>} paths 页面路径集合
82
- * @param {Object|null} homePage 首页信息
83
- * @param {Boolean} homePath.isTab 首页是否tab页
84
- * @param {String} homePath.path 首页页面路径(isTab=false时使用此字段)
85
- * @param {Array<String>} homePath.tabs tab页面路径列表(isTab=true时使用自此段)
86
- * @returns {void}
87
- */
88
-
89
- const setAppPagePaths = (paths, homePage) => {
90
- appPagePaths = paths;
91
- const {
92
- isTab = false,
93
- path = '',
94
- tabs = []
95
- } = homePage || {};
96
- homePageInfo = {
97
- isTab,
98
- path,
99
- tabs
100
- };
101
- };
102
- /**
103
- * 判断页面是否存在于当前app中
104
- * @param {String} page 页面路径
105
- * @returns {Boolean} 页面是否存在app中
106
- */
107
-
108
- const isAppPageExist = page => {
109
- const route = !page ? '' : String(page).split('?')[0];
110
- if (!route || !Array.isArray(appPagePaths)) return false;
111
- const routeWithoutPrefixSlash = route[0] === '/' ? route.substring(1) : route;
112
- return appPagePaths.some(path => path === routeWithoutPrefixSlash || path === `/${routeWithoutPrefixSlash}`);
113
- }; // 以下变量标识各小程序appId
114
-
115
- const MOBILITY_APPID = 'wx65cc950f42e8fff1'; // 出行服务小程序AppId
116
-
117
- const MOBILITY_DEMO_APPID = 'wxa7ce727b525f80b0'; // 出行服务demo小程序AppId
118
-
119
- const SINAN_HOME = '/modules/index/carlife/pages/index/index'; // 出行首页地址
120
-
121
- const MYCAR_HOME = '/modules/car/index/index'; // 我的车首页地址
122
-
123
- /**
124
- * 获取首页信息
125
- * @returns {Object|null} homePage 首页信息
126
- * @returns {Boolean} homePath.isTab 首页是否tab页
127
- * @returns {String} homePath.path 首页页面路径(isTab=false时使用此字段)
128
- * @returns {Array<String>} homePath.tabs tab页面路径列表(isTab=true时使用自此段)
129
- */
130
-
131
- const getHomePage = () => {
132
- var _wx$getAccountInfoSyn, _wx$getAccountInfoSyn2;
133
-
134
- if (homePageInfo) return homePageInfo; // 返回默认信息
135
-
136
- const mpAppId = (_wx$getAccountInfoSyn = wx.getAccountInfoSync()) === null || _wx$getAccountInfoSyn === void 0 ? void 0 : (_wx$getAccountInfoSyn2 = _wx$getAccountInfoSyn.miniProgram) === null || _wx$getAccountInfoSyn2 === void 0 ? void 0 : _wx$getAccountInfoSyn2.appId;
137
- const homePagePath = [MOBILITY_APPID, MOBILITY_DEMO_APPID].indexOf(mpAppId) > -1 ? SINAN_HOME : MYCAR_HOME;
138
- homePageInfo = {
139
- isTab: false,
140
- path: homePagePath,
141
- tabs: []
142
- };
143
- return homePageInfo;
144
- };
145
-
146
- export { getAuthInfo as a, getHomePage as b, setEnvInfo as c, setAppPagePaths as d, getEnvInfo as g, isAppPageExist as i, setAuthInfo as s };
@@ -1,243 +0,0 @@
1
- import { o as rpxToPx, p as storage, s as syncApi, q as stringUtils, t as timeUtils, u as ipxHelper } from './storage-094c6ddb.js';
2
- import { m as md5, s as serialize } from './objUtils-154b94db.js';
3
-
4
- /**
5
- * 由于需要把tms-core、tms-runtime从主包中移到vendor分包中使用分包异步化加载,
6
- * 但是很多分包中都会立即调用getApp().tms的接口,为了保证调用不出错,
7
- * 特地新增此代理js,用于在app.js中先挂在到app.tms下,等vendor加载完再替换掉。
8
- * 注意:tms-core、tms-runtime有改动都要来这里确认proxy有没有问题。
9
- */
10
- let app = null;
11
- let initOptions = null;
12
- let tmsPromise = null;
13
- let tms = null;
14
- let logger = null;
15
-
16
- function invoke(obj, funcName, args) {
17
- if (obj[funcName]) {
18
- return obj[funcName](...args);
19
- } // 非法调用的时候就要及时上报日志,方便解决问题
20
-
21
-
22
- if (!logger) {
23
- logger = wx.getRealtimeLogManager();
24
- }
25
-
26
- const msg = `tms-proxy: Cannot read property ${funcName}`;
27
- logger.addFilterMsg('tms-proxy');
28
- logger.error(msg);
29
- console.error(msg);
30
- return Promise.reject(msg);
31
- }
32
-
33
- function initProxy(appObj, options) {
34
- app = appObj;
35
- initOptions = options;
36
- }
37
-
38
- function awaitTMS() {
39
- if (tmsPromise === null) {
40
- tmsPromise = new Promise(resolve => {
41
- app.initPromise.then(res => {
42
- tms = res;
43
- resolve(res);
44
- });
45
- });
46
- }
47
-
48
- return tmsPromise;
49
- } // 返回Promise的函数,无返回结果的也可以放这里
50
-
51
-
52
- const asyncFuncs = {};
53
- const asyncFuncNames = [// 这行是tms-ui的
54
- 'showDrawer', 'hideDrawer', 'showModal', 'hideModal', // 这行是tms-runtime的
55
- 'getPhone', 'login', 'getLoginInfo', 'getOpenId', 'getMycarPubOpenId', 'getSinanPubOpenId', // tms-core
56
- 'setAuthInfo', 'getConfig', 'navigateToWebview', 'callCloudFunc', 'setUserLocation', 'getUserLocation', 'getMpOpenId', 'getOuterOpenId'];
57
- asyncFuncNames.forEach(funcName => {
58
- asyncFuncs[funcName] = function (...args) {
59
- return awaitTMS().then(tms => invoke(tms, funcName, args));
60
- };
61
- }); // 返回对象的函数,并且对象的所有接口都可以以promise的形式返回
62
-
63
- const objFuncs = {};
64
- const objFuncNames = ['getLogManager', 'getRealtimeLogManager', 'getReporter', 'getFastReporter', 'getEventDispatcher'];
65
- objFuncNames.forEach(funcName => {
66
- objFuncs[funcName] = function (...args1) {
67
- const ps = awaitTMS().then(tms => invoke(tms, funcName, args1));
68
- return new Proxy({}, {
69
- get(obj, name) {
70
- return function (...args2) {
71
- return ps.then(funcObj => invoke(funcObj, name, args2));
72
- };
73
- }
74
-
75
- });
76
- };
77
- }); // tms-runtime
78
-
79
- function getCarManager() {
80
- return {
81
- getCarInfo() {
82
- if (tms) {
83
- return tms.getCarManager().getCarInfo();
84
- }
85
-
86
- return wx.getStorageSync('currentCar') || {};
87
- },
88
-
89
- setCarInfo(param = {}) {
90
- awaitTMS().then(tms => tms.getCarManager().setCarInfo(param));
91
- },
92
-
93
- getCarList() {
94
- return awaitTMS().then(tms => tms.getCarManager().getCarList());
95
- }
96
-
97
- };
98
- }
99
-
100
- function getLocationManager() {
101
- let manager = null;
102
- const ps = awaitTMS().then(tms => {
103
- manager = tms.getLocationManager();
104
- });
105
- let currLoc = null;
106
-
107
- const getUserLocation = () => {
108
- if (manager) {
109
- return manager.getUserLocation();
110
- }
111
-
112
- return currLoc;
113
- };
114
-
115
- const setUserLocation = loc => {
116
- if (manager) {
117
- manager.setUserLocation(loc);
118
- } else if (loc && typeof loc === 'object') {
119
- currLoc = loc;
120
- ps.then(() => manager.setUserLocation(loc));
121
- }
122
- };
123
-
124
- return new Proxy({}, {
125
- get(obj, name) {
126
- if (name === 'getUserLocation') {
127
- return getUserLocation;
128
- }
129
-
130
- if (name === 'setUserLocation') {
131
- return setUserLocation;
132
- }
133
-
134
- if (name === 'locForNoAuth') {
135
- return {
136
- province: '广东省',
137
- cityCode: '440300',
138
- cityName: '深圳市',
139
- latitude: 22.54286,
140
- longitude: 114.05956
141
- };
142
- }
143
-
144
- return function (...args) {
145
- return ps.then(() => invoke(manager, name, args));
146
- };
147
- }
148
-
149
- });
150
- }
151
-
152
- function createRequest(config = {}) {
153
- let request = null;
154
- const ps = awaitTMS().then(tms => {
155
- request = tms.createRequest(config);
156
- });
157
-
158
- const makeUrl = path => {
159
- if (request) {
160
- return request.makeUrl(path);
161
- }
162
-
163
- if (/^http/i.test(path)) {
164
- return path;
165
- }
166
-
167
- const host = (config === null || config === void 0 ? void 0 : config.host) || initOptions.defaultHost;
168
- const validHost = /^http/i.test(host) ? host : `https://${host}`;
169
- return `${validHost}/${path}`;
170
- };
171
-
172
- return new Proxy({}, {
173
- get(obj, name) {
174
- if (name === 'makeUrl') {
175
- return makeUrl;
176
- }
177
-
178
- return function (...args) {
179
- return ps.then(() => invoke(request, name, args));
180
- };
181
- }
182
-
183
- });
184
- }
185
-
186
- function getEnvInfo() {
187
- if (tms) {
188
- return tms.getEnvInfo();
189
- }
190
-
191
- const {
192
- wxAppId,
193
- appVersion,
194
- appEnv,
195
- client
196
- } = initOptions;
197
- return {
198
- wxAppId,
199
- appVersion,
200
- appEnv,
201
- client
202
- };
203
- }
204
-
205
- function isAppPageExist(...args) {
206
- if (tms) {
207
- return tms.isAppPageExist(...args);
208
- }
209
-
210
- return true; // todo: 可能不正确
211
- }
212
-
213
- function getHomePage() {
214
- if (tms) {
215
- return tms.getHomePage();
216
- }
217
-
218
- return initOptions.homePage;
219
- }
220
-
221
- const api = {
222
- isProxy: true,
223
- // 方便定位问题时判断是否proxy
224
- initProxy,
225
- md5,
226
- getCarManager,
227
- getLocationManager,
228
- rpxToPx,
229
- serialize,
230
- createRequest,
231
- getEnvInfo,
232
- isAppPageExist,
233
- getHomePage,
234
- storage,
235
- ...asyncFuncs,
236
- ...objFuncs,
237
- ...syncApi,
238
- ...stringUtils,
239
- ...timeUtils,
240
- ...ipxHelper
241
- };
242
-
243
- export { api as default };