@tmsfe/tms-core 0.0.1 → 0.0.5

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/src/index.js ADDED
@@ -0,0 +1,181 @@
1
+ import Request from './request';
2
+ import FastReport from './fastreport';
3
+ import { getConfig } from './config';
4
+ import syncApi from './syncfnmanager';
5
+ import nav from './navigator';
6
+ import { getLogManager, getRealtimeLogManager } from './log';
7
+ import { setEnvInfo, getEnvInfo, setAuthInfo, setAppPagePaths, isAppPageExist, getHomePage } from './env';
8
+ import md5 from './md5';
9
+ import { callCloudFunc } from './cloudService';
10
+ import { cache as report, startListenApp } from './report';
11
+ import EventDispatcher from './eventDispatcher';
12
+ import { serialize } from './objUtils';
13
+ import { rpxToPx } from './rpx';
14
+ import {
15
+ formatPlate,
16
+ subStr,
17
+ hidePhoneCenter,
18
+ isValidPhone,
19
+ isValidPlate,
20
+ isValidAuthCode,
21
+ roundStr,
22
+ } from './stringUtils';
23
+ import {
24
+ formatTime,
25
+ formatTimeStr,
26
+ formatTimeWithDetails,
27
+ dateToString,
28
+ } from './timeUtils';
29
+ import {
30
+ ipxInit,
31
+ isIPX,
32
+ getIpxClass,
33
+ getIpxConfig,
34
+ } from './ipxHelper';
35
+ import getLocInstance from './location/index.ts';
36
+ import LocationBase from './location/base.ts';
37
+ import { getMpOpenId, getOuterOpenId } from './mpInfo';
38
+
39
+ /**
40
+ * @public
41
+ * @description 创建网络请求对象,用于向腾讯出行服务平台后台发送网络请求
42
+ * @param {Object} [config] 参数配置
43
+ * @param {Boolean} [config.withAuth=true] 是否填充登录态参数
44
+ * @param {String} [config.host] 自定义的host域名
45
+ * @param {Object} [config.baseParam] 默认携带的参数
46
+ * @returns {Object} [Request实例](#class-request)
47
+ * @example
48
+ * const $ = getApp().tms.createRequest();
49
+ * $.get(apiPath)
50
+ * .then((resp) => {
51
+ * // ...
52
+ * })
53
+ * .catch((err) => {
54
+ * // ...
55
+ * });
56
+ */
57
+ const createRequest = (config = {}) => new Request(config);
58
+ /**
59
+ * @description 埋点上报
60
+ * @returns {Object} 包含[report方法](#report)的对象
61
+ * @example
62
+ * getReporter().report(reportData, reportNow);
63
+ */
64
+ const getReporter = () => ({ report });
65
+
66
+ /**
67
+ * @description 埋点上报(快速上报,不依赖用户userId标识)
68
+ * @returns {Class} [FastReporter类](#class-fastreport)
69
+ */
70
+ const getFastReporter = () => FastReport;
71
+
72
+ /**
73
+ * @description 自定义事件机制
74
+ * @returns {Object} [EventDispatcher实例](#class-eventdispatcher)
75
+ */
76
+ const getEventDispatcher = () => new EventDispatcher();
77
+
78
+ /**
79
+ * @description 获取地理位置方法的集合
80
+ * @returns {Class} [Location类](#class-location)
81
+ */
82
+ const getLocationManager = () => getLocInstance();
83
+
84
+ /**
85
+ * @description 获取位置信息base类(业务无关)
86
+ * @returns {Class} [Location类](#class-location)
87
+ */
88
+ const getLocationBaseClass = () => LocationBase;
89
+
90
+ /**
91
+ * @description init core包初始化, 小程序已在app.js中对齐初始化
92
+ * @param {Object} options 初始化
93
+ * @returns {undefined} 无返回值.
94
+ */
95
+ const init = (options = {}) => {
96
+ startListenApp();
97
+ const { appVersion, wxAppId, client, defaultHost, cloudEnvId, appEnv, appPagePaths, homePage } = options;
98
+ setEnvInfo({
99
+ wxAppId,
100
+ appVersion,
101
+ appEnv,
102
+ client,
103
+ cloudEnvId,
104
+ });
105
+ setAppPagePaths(appPagePaths, homePage);
106
+ Request.defaultHost = defaultHost;
107
+ // 初始化云环境
108
+ wx.cloud.init({ env: cloudEnvId });
109
+ };
110
+
111
+ /**
112
+ * @description 获取用户位置信息 -- 兼容版 现有业务中有的使用promise, 有的没有, 做一下兼容
113
+ * @returns { Promise } 位置信息
114
+ */
115
+ const getUserLocation = () => {
116
+ const userLocation = getLocInstance().getUserLocation();
117
+ if (userLocation) {
118
+ return Promise.resolve(userLocation);
119
+ }
120
+ return getLocInstance().getLocationDetail(false);
121
+ };
122
+
123
+ const api = {
124
+ init,
125
+ createRequest,
126
+ setAuthInfo,
127
+ getLogManager,
128
+ getRealtimeLogManager,
129
+ md5,
130
+ getReporter,
131
+ getFastReporter,
132
+ getLocationManager,
133
+ getLocationBaseClass,
134
+ getEventDispatcher,
135
+ getEnvInfo,
136
+ getConfig,
137
+ navigateToWebview: nav.navigateToWebview,
138
+ isAppPageExist,
139
+ getHomePage,
140
+ callCloudFunc,
141
+ setUserLocation: (loc) => {
142
+ getLocInstance().setUserLocation(loc);
143
+ },
144
+ getUserLocation,
145
+
146
+ /* 字符串方法 */
147
+ formatPlate,
148
+ subStr,
149
+ hidePhoneCenter,
150
+ isValidPhone,
151
+ isValidPlate,
152
+ isValidAuthCode,
153
+ roundStr,
154
+
155
+ /* 时间方法 */
156
+ formatTime,
157
+ formatTimeStr,
158
+ formatTimeWithDetails,
159
+ dateToString,
160
+
161
+ /* IPX方法 */
162
+ ipxInit,
163
+ isIPX,
164
+ getIpxClass,
165
+ getIpxConfig,
166
+
167
+ /* 处理对象方法 */
168
+ serialize,
169
+
170
+ /* 获取外部合作商openid */
171
+ getMpOpenId, // 变更为 getOuterOpenId
172
+ getOuterOpenId,
173
+
174
+ /** rpx转px */
175
+ rpxToPx,
176
+
177
+ ...syncApi,
178
+ };
179
+
180
+
181
+ export default api;
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Tencent Inc. All Rights Reserved.
3
+ * @author: petegao@tencent.com
4
+ * Created: 2019-01-14.
5
+ *
6
+ */
7
+
8
+ import syncfnmanager from './syncfnmanager';
9
+
10
+ let isInit = false;
11
+ let isIPXSign = false;
12
+ let ipxClass = '';
13
+
14
+ /**
15
+ * @returns {undefined}
16
+ */
17
+ function init() {
18
+ if (isInit) {
19
+ return;
20
+ }
21
+ isInit = true;
22
+ const sysInfo = syncfnmanager.getSystemInfoSync();
23
+ const { model, platform: pl } = sysInfo;
24
+ if (pl !== 'ios') {
25
+ return;
26
+ }
27
+ if (model.search('iPhone X') > -1 || model.search('iPhone 11') > -1) {
28
+ isIPXSign = true;
29
+ ipxClass = 'view__ipx';
30
+ }
31
+ }
32
+
33
+ /**
34
+ * @returns {Boolean} 判断是否是 iPhoneX
35
+ */
36
+ function isIPX() {
37
+ init();
38
+ return isIPXSign;
39
+ }
40
+
41
+ /**
42
+ * @returns {String} 返回 ipxClass
43
+ */
44
+ function getIpxClass() {
45
+ init();
46
+ return ipxClass;
47
+ }
48
+
49
+ /**
50
+ * @param {Object} config 用户需要合并的IPX的配置
51
+ * @returns {Object} 合并后的IPX的配置
52
+ */
53
+ function getIpxConfig(config) {
54
+ init();
55
+ return {
56
+ ...config,
57
+ data: {
58
+ ...config.data,
59
+ isIPX: isIPXSign,
60
+ ipxClass,
61
+ },
62
+ };
63
+ }
64
+
65
+ // 兼容旧接口,已经没什么用了
66
+ function ipxInit() {
67
+
68
+ }
69
+
70
+ export {
71
+ ipxInit,
72
+ isIPX,
73
+ getIpxClass,
74
+ getIpxConfig,
75
+ };
@@ -0,0 +1,236 @@
1
+ import { getSetting, updateLocStatus, getSystemInfo, getTextByReason, openSetting } from './utils';// eslint-disable-line
2
+ import EventDispatcher from '../eventDispatcher';
3
+ import { WxPostionType } from '../types/object'; // eslint-disable-line
4
+
5
+ const event = new EventDispatcher();
6
+
7
+ let getLocPromise: null | Promise<any> = null;
8
+ // 用户位置信息缓存
9
+ let userLocationCache: WxPostionType;
10
+ // 默认获取经纬度坐标的名称
11
+ const defaultLocationType = 'gcj02';
12
+ // 派发事件的名称
13
+ const EventName = 'loc_status_changed';
14
+
15
+ /**
16
+ * @private 将wx.getLocation获取位置方法用promise封装
17
+ * @param { string } type 获取位置类型
18
+ * @returns { Promise<any> } 用户位置信息
19
+ */
20
+ function getWxLocation(type: string = defaultLocationType): Promise<WxPostionType> {
21
+ return new Promise((resolve, reject) => {
22
+ wx.getLocation({
23
+ type,
24
+ success: (res: WxPostionType) => {
25
+ resolve(res);
26
+ },
27
+ fail: reject,
28
+ });
29
+ });
30
+ }
31
+
32
+ /**
33
+ * @class Location
34
+ * @classdesc 基于微信api,封装location相关的接口。 包括监听位置变化, 获取用户位置信息
35
+ * 获取位置授权等等。业务无关
36
+ */
37
+ class LocationBase {
38
+ /**
39
+ * @private 构造函数
40
+ */
41
+ constructor() {
42
+ wx.onLocationChange(this.subscribeLocationChnage);
43
+ wx.startLocationUpdate({});
44
+ }
45
+
46
+ /**
47
+ * 获取用户当前位置(经纬度)
48
+ * @memberof Location
49
+ * @param {boolean} showModalWhenCloseAuth 获取位置时,如果用户关闭了位置授权,是否弹窗提示用户打开授权
50
+ * @param {string} type 坐标类型
51
+ * @param {string} content 弹窗内容
52
+ * @param {boolean} showCancel 是否显示取消按钮
53
+ * @returns {Promise<LOC|ERR>} 返回对象
54
+ * @returns {Promise<LOC|ERR>} 返回对象
55
+ */
56
+ public getLocation(showModalWhenCloseAuth: boolean, type: string, content = '', showCancel = true): Promise<WxPostionType> {
57
+ // 如果缓存中没有, 并且getLocPromise有值, 说明正在请求中。 返回请求的promise
58
+ if (!userLocationCache && getLocPromise) { // eslint-disable-line
59
+ return getLocPromise;
60
+ }
61
+
62
+ // 如果缓存中没有, 并且getLocPromise没有值,新建一个请求
63
+ if (!userLocationCache && !getLocPromise) { // eslint-disable-line
64
+ getLocPromise = this.getWxLocationPromise(showModalWhenCloseAuth, type, content, showCancel);
65
+ return getLocPromise;
66
+ }
67
+
68
+ // 缓存中有数据, 证明用户已经获取过, 那么直接返回缓存中的位置
69
+
70
+ return Promise.resolve(userLocationCache);
71
+ }
72
+
73
+ /**
74
+ * @description 获取位置(经纬度)底层封装
75
+ * @memberof Location
76
+ * @param {boolean} showModalWhenCloseAuth 获取位置时,如果用户关闭了位置授权,是否弹窗提示用户打开授权
77
+ * @param {string} type 坐标类型
78
+ * @param {string} content 弹窗内容
79
+ * @param {boolean} showCancel 是否显示取消按钮
80
+ * @returns {Promise<LOC|ERR>} 返回对象
81
+ * @example
82
+ * <caption>LOC类型示例</caption>
83
+ * {
84
+ * cityName: '北京市',
85
+ * cityCode: '100100',
86
+ * latitude: 325.255333,
87
+ * longitude: 116.2545454,
88
+ * adCode: 1212144,
89
+ * poi: {
90
+ * id: '1114545554511',
91
+ * title: '腾讯北京总部大厦',
92
+ * address : '北京市海淀区东北旺西路',
93
+ * }
94
+ */
95
+ public getWxLocationPromise(showModalWhenCloseAuth: boolean, type: string, content = '', showCancel = true) {
96
+ return new Promise((resolve, reject) => {
97
+ getWxLocation(type).then((res: WxPostionType) => {
98
+ userLocationCache = res;
99
+ // 更新用户状态 -- todo
100
+ updateLocStatus(true);
101
+ resolve(res);
102
+ })
103
+ .catch((err) => {
104
+ getLocPromise = null;
105
+ updateLocStatus(false);
106
+
107
+ this.openLocationAuth(showModalWhenCloseAuth, type, content, showCancel, err).then((res) => {
108
+ resolve(res);
109
+ }, (err) => {
110
+ reject(err);
111
+ });
112
+ });
113
+ });
114
+ }
115
+
116
+ /**
117
+ * 如果没有权限,自动开启授权弹窗
118
+ * @memberof Location
119
+ * @param {boolean} showModalWhenCloseAuth 获取位置时,如果用户关闭了位置授权,是否弹窗提示用户打开授权
120
+ * @param {string} type 坐标类型
121
+ * @param {string} content 弹窗内容
122
+ * @param {boolean} showCancel 是否显示取消按钮
123
+ * @param {object} err 错误信息
124
+ * @returns {Promise} 返回对象
125
+ */
126
+ openLocationAuth(showModalWhenCloseAuth: boolean, type: string, content: string, showCancel: boolean, err: any) {
127
+ return new Promise((resolve, reject) => {
128
+ const { locationEnabled, locationAuthorized } = getSystemInfo()!;
129
+ // 系统位置定位开关打开 && 允许微信使用位置定位能力开关打开
130
+ if (locationEnabled && locationAuthorized) {
131
+ if (err.errMsg.search(/auth [deny|denied]|authorize/) < 0) {
132
+ reject();
133
+ return;
134
+ }
135
+ }
136
+
137
+ if (!showModalWhenCloseAuth) {
138
+ const rejectData = { ...err, unAuthed: true };
139
+ reject(rejectData);
140
+ return;
141
+ }
142
+
143
+ // 控制地理位置开关的名称(系统控制的开关,还是微信层面的开关)
144
+ const isSysAndWechatAllowedGetLoc = (locationEnabled && locationAuthorized);
145
+ const { authLevelName, confirmText } = getTextByReason(locationEnabled, isSysAndWechatAllowedGetLoc);
146
+ wx.showModal({
147
+ confirmText,
148
+ title: `未开启${authLevelName}位置信息权限`,
149
+ content: content || `开启${authLevelName}位置信息权限\n体验更顺畅的出行服务`,
150
+ showCancel: (showCancel && isSysAndWechatAllowedGetLoc),
151
+ confirmColor: '#4875fd',
152
+ success: (res) => {
153
+ // 用户拒绝去授权
154
+ if (!res.confirm || !locationEnabled || !locationAuthorized) {
155
+ const rejectData = { ...err, unAuthed: true };
156
+ return reject(rejectData);
157
+ }
158
+
159
+ // 用户同意去打开授权(只是打开小程序设置页,用户不一定打开了位置授权,所以需要重新进行获取位置)
160
+ openSetting()
161
+ .then(() => {
162
+ resolve(this.getLocation(showModalWhenCloseAuth, type, content, showCancel));
163
+ })
164
+ .catch((err) => {
165
+ const rejectData = { ...err, unAuthed: true };
166
+ reject(rejectData);
167
+ });
168
+ },
169
+ });
170
+ });
171
+ }
172
+
173
+ /**
174
+ * 订阅用户位置信息变化
175
+ * @param { object } res 用户位置信息对象
176
+ * @returns { undefined } no retrurn
177
+ */
178
+ public subscribeLocationChnage(res: WxPostionType) {
179
+ userLocationCache = res;
180
+ }
181
+
182
+ /**
183
+ * @description
184
+ * 以静默方式获取用户位置(经纬度),说明如下:<br>
185
+ * 1、从未授权情况下 | 用户删除小程序之后调用该方法不会弹微信自带的授权弹窗;<br>
186
+ * 2、拒绝授权后,调用该方法不会弹窗提示用户去授权(wx.showModal);<br>
187
+ * @memberof Location
188
+ * @param {String} type 非必填。坐标类型,默认'gcj02'
189
+ * @returns {Promise<LOC|ERR>} 返回位置信息
190
+ * @example
191
+ * <caption>LOC类型示例</caption>
192
+ * {
193
+ * latitude: 33.253321,
194
+ * longitude: 115.2444,
195
+ * }
196
+ * @example
197
+ * <caption>ERR类型示例</caption>
198
+ * {
199
+ * err,
200
+ * unAuthed: true,
201
+ * locationScopeStatus: false
202
+ * }
203
+ */
204
+ async getLocationSilent(type: string) {
205
+ let err = null;
206
+ let locationScopeStatus;
207
+ try {
208
+ locationScopeStatus = await getSetting();
209
+ // 用户已授权. locationScopeStatus = true
210
+ if (locationScopeStatus) {
211
+ return this.getLocation(false, type);
212
+ }
213
+ } catch (ex) {
214
+ err = ex;
215
+ }
216
+
217
+ updateLocStatus(false);
218
+ // 用户关闭了授权. locationScopeStatus = false
219
+ // 用户未授权过(删除小程序之后再次打开小程序). locationScopeStatus = undefined
220
+ return Promise.reject({ err, unAuthed: true, locationScopeStatus });
221
+ }
222
+
223
+ /**
224
+ * @memberof Location
225
+ * @description 监听获取位置状态变化(目前只有失败->成功时会触发事件)
226
+ * @param {Function} cb 监听事件回调函数
227
+ * @returns {void}
228
+ */
229
+ onLocStatusChange(cb: () => void) {
230
+ if (cb && typeof cb === 'function') {
231
+ event.bind(EventName, cb);
232
+ }
233
+ }
234
+ }
235
+
236
+ export default LocationBase;