@haluo/util 2.0.33 → 2.0.34

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.
Files changed (38) hide show
  1. package/dist/index.d.ts +3 -0
  2. package/dist/index.js +23 -19
  3. package/dist/modules/app-call/configs.d.ts +9 -0
  4. package/dist/modules/app-call/configs.js +44 -0
  5. package/dist/modules/app-call/core.d.ts +53 -0
  6. package/dist/modules/app-call/core.js +175 -0
  7. package/dist/modules/app-call/extensions.d.ts +187 -0
  8. package/dist/modules/app-call/extensions.js +1297 -0
  9. package/dist/modules/app-call/index.d.ts +16 -0
  10. package/dist/modules/app-call/index.js +84 -0
  11. package/dist/modules/app-call/offline.d.ts +12 -0
  12. package/dist/modules/app-call/offline.js +191 -0
  13. package/dist/modules/app-call/types.d.ts +67 -0
  14. package/dist/modules/app-call/types.js +4 -0
  15. package/dist/modules/cookie/index.js +22 -17
  16. package/dist/modules/date/index.js +54 -48
  17. package/dist/modules/dom/index.js +21 -15
  18. package/dist/modules/filter/index.js +14 -8
  19. package/dist/modules/format/index.js +9 -5
  20. package/dist/modules/match/index.js +8 -5
  21. package/dist/modules/monitor/lib/jsError.js +27 -38
  22. package/dist/modules/monitor/lib/timing.js +15 -17
  23. package/dist/modules/monitor/lib/xhr.js +26 -25
  24. package/dist/modules/monitor/utils/tracker.js +22 -10
  25. package/dist/modules/number/index.js +33 -30
  26. package/dist/modules/open-app/index.d.ts +3 -7
  27. package/dist/modules/open-app/index.js +52 -62
  28. package/dist/modules/sentry/index.js +16 -12
  29. package/dist/modules/tools/index.js +164 -154
  30. package/dist/modules/track/index.d.ts +122 -0
  31. package/dist/modules/track/index.js +421 -0
  32. package/dist/modules/track/types.d.ts +108 -0
  33. package/dist/modules/track/types.js +4 -0
  34. package/dist/modules/upload/aliOss.d.ts +52 -5
  35. package/dist/modules/upload/aliOss.js +589 -378
  36. package/dist/modules/upload/index.js +32 -29
  37. package/dist/tsconfig.tsbuildinfo +1 -1
  38. package/package.json +27 -2
@@ -0,0 +1,16 @@
1
+ import { createProjectConfig, projectConfigs } from './configs';
2
+ import type { AppCallConfig, AppCallInstance } from './types';
3
+ /**
4
+ * 创建 AppCall 实例
5
+ */
6
+ export declare function createAppCall(config: AppCallConfig): AppCallInstance;
7
+ /**
8
+ * 快速创建项目 AppCall(使用预设配置)
9
+ */
10
+ export declare function createProjectAppCall(projectType: keyof typeof projectConfigs, overrides?: Partial<AppCallConfig>): AppCallInstance;
11
+ /**
12
+ * 导出配置和类型
13
+ */
14
+ export { projectConfigs, createProjectConfig };
15
+ export { createOfflineAppCall, initOfflineAppCall } from './offline';
16
+ export type { AppCallConfig, AppCallInstance, AppCallCore } from './types';
@@ -0,0 +1,84 @@
1
+ /**
2
+ * AppCall 统一模块
3
+ * H5调用APP方法的统一接口
4
+ */
5
+ import { AppCallCoreClass } from './core';
6
+ import { createAllExtensions } from './extensions';
7
+ import { createProjectConfig, projectConfigs } from './configs';
8
+ /**
9
+ * 创建 AppCall 实例
10
+ */
11
+ export function createAppCall(config) {
12
+ var core = new AppCallCoreClass(config);
13
+ var appCall = core;
14
+ appCall.__core = core;
15
+ // 添加所有扩展方法(所有项目共享)
16
+ appCall.extend(createAllExtensions(appCall, config));
17
+ // 添加初始化方法
18
+ appCall.init = function () {
19
+ return new Promise(function (resolve) {
20
+ if (!core.getIsClient()) {
21
+ resolve();
22
+ return;
23
+ }
24
+ if (window.isIOS) {
25
+ // iOS Bridge 初始化
26
+ window.setupWebViewJavascriptBridge = function (callback) {
27
+ if (window.WebViewJavascriptBridge) {
28
+ return typeof callback === 'function' && callback(window.WebViewJavascriptBridge);
29
+ }
30
+ if (window.WVJBCallbacks) {
31
+ return window.WVJBCallbacks.push(callback);
32
+ }
33
+ window.WVJBCallbacks = [callback];
34
+ var WVJBIframe = document.createElement('iframe');
35
+ WVJBIframe.style.display = 'none';
36
+ WVJBIframe.src = 'motor://__BRIDGE_LOADED__';
37
+ document.documentElement.appendChild(WVJBIframe);
38
+ setTimeout(function () {
39
+ document.documentElement.removeChild(WVJBIframe);
40
+ }, 0);
41
+ };
42
+ appCall.callIOSHandler('getInterface', [], function (data) {
43
+ core.setIOSInterface(data);
44
+ if (window.location.href.includes('machine-verification')) {
45
+ resolve();
46
+ return;
47
+ }
48
+ appCall.getUserData(function () {
49
+ resolve();
50
+ });
51
+ appCall.getDeviceData();
52
+ });
53
+ }
54
+ else if (window.isAndroid || window.isHarmonyos) {
55
+ if (window.location.href.includes('machine-verification')) {
56
+ resolve();
57
+ return;
58
+ }
59
+ appCall.getUserData(function () {
60
+ resolve();
61
+ });
62
+ appCall.getDeviceData();
63
+ }
64
+ else {
65
+ resolve();
66
+ }
67
+ });
68
+ };
69
+ // 挂载到 window
70
+ window.AppCall = appCall;
71
+ return appCall;
72
+ }
73
+ /**
74
+ * 快速创建项目 AppCall(使用预设配置)
75
+ */
76
+ export function createProjectAppCall(projectType, overrides) {
77
+ var config = createProjectConfig(projectType, overrides);
78
+ return createAppCall(config);
79
+ }
80
+ /**
81
+ * 导出配置和类型
82
+ */
83
+ export { projectConfigs, createProjectConfig };
84
+ export { createOfflineAppCall, initOfflineAppCall } from './offline';
@@ -0,0 +1,12 @@
1
+ import type { AppCallConfig, AppCallInstance } from './types';
2
+ /**
3
+ * 创建离线包专用的 AppCall
4
+ * @param config 配置项
5
+ */
6
+ export declare function createOfflineAppCall(config?: Partial<AppCallConfig>): AppCallInstance;
7
+ /**
8
+ * 初始化离线包 AppCall
9
+ * 注意:全局变量已在 AppCallCoreClass 构造函数中通过 initGlobalVars() 初始化
10
+ * 这里直接使用已初始化的全局变量,避免重复计算和设置
11
+ */
12
+ export declare function initOfflineAppCall(appCall: AppCallInstance): Promise<void>;
@@ -0,0 +1,191 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ /**
13
+ * AppCall for offline package
14
+ * 离线包专用的 AppCall,不依赖项目特定的配置
15
+ */
16
+ import { AppCallCoreClass } from './core';
17
+ import { createAllExtensions } from './extensions';
18
+ /**
19
+ * 将获取到的设备信息放到本地存储
20
+ */
21
+ var setDeviceinfo = function (devicedata) {
22
+ if (devicedata === void 0) { devicedata = ''; }
23
+ if (devicedata && typeof devicedata === 'string') {
24
+ devicedata = devicedata.replace('undefined', '');
25
+ devicedata = JSON.parse(devicedata || '{}');
26
+ }
27
+ devicedata && window.localStorage.setItem('deviceData', JSON.stringify(devicedata));
28
+ };
29
+ /**
30
+ * 创建离线包专用的 AppCall
31
+ * @param config 配置项
32
+ */
33
+ export function createOfflineAppCall(config) {
34
+ var _a;
35
+ if (config === void 0) { config = {}; }
36
+ var offlineConfig = __assign({ projectName: config.projectName || '摩托范', domain: config.domain || '58moto.com', defaultImage: config.defaultImage || 'https://wap.58moto.com/static/img/common/motuofan_small.png', defaultDesc: config.defaultDesc || '摩托范,摩托发烧友的交流的平台,摩托维修、保养、配件等知识与经验分享社区。', showTip: (_a = config.showTip) !== null && _a !== void 0 ? _a : false, env: config.env || {},
37
+ // 离线包不需要这些依赖,提供空函数
38
+ getCircleIdByName: function () { return Promise.resolve({ id: 0, type: '' }); }, setUserinfo: function (data) {
39
+ console.log('setUserinfo', data);
40
+ if (data && typeof data === 'string') {
41
+ data = JSON.parse(data || '{}');
42
+ }
43
+ if (data && data.token) {
44
+ window.localStorage.setItem('user', JSON.stringify(data));
45
+ window.localStorage.setItem('token', data.token);
46
+ }
47
+ }, fileToHttps: config.fileToHttps || (function (url) {
48
+ if (url.includes('file://')) {
49
+ var suffix = location.href.split('html#')[1];
50
+ return "https://wap.58moto.com".concat(suffix);
51
+ }
52
+ return url;
53
+ }) }, config);
54
+ // 直接使用核心类创建实例,避免循环依赖
55
+ var core = new AppCallCoreClass(offlineConfig);
56
+ var appCall = core;
57
+ appCall.__core = core;
58
+ // 添加所有扩展方法(所有项目共享)
59
+ appCall.extend(createAllExtensions(appCall, offlineConfig));
60
+ // 添加初始化方法(简化版,离线包不需要完整的 init)
61
+ appCall.init = function () {
62
+ return Promise.resolve();
63
+ };
64
+ // 挂载到 window(离线包会在外部再次挂载,这里先不挂载)
65
+ // window.AppCall = appCall
66
+ // 扩展离线包特定的方法
67
+ appCall.extend({
68
+ // 文章 HTML 操作
69
+ articleHtmlAction: function (action, callback) {
70
+ if (action === void 0) { action = {}; }
71
+ ;
72
+ window.enableBridgeLog = false;
73
+ window.enableBridgeLog && appCall.alert('1.1、articleHtmlAction');
74
+ Object.assign((window.bridge = window.bridge || {}), {
75
+ articleHtmlActionCallback: function (res) {
76
+ if (res === void 0) { res = {}; }
77
+ console.log('articleHtmlActionCallback', res);
78
+ window.enableBridgeLog && appCall.alert('1.2、articleHtmlActionCallback');
79
+ if (typeof res === 'string') {
80
+ try {
81
+ // 直接使用已初始化的全局变量,避免重复计算
82
+ if ((window.isAndroid || window.isHarmonyos) && action.action === 'getDetail') {
83
+ res = appCall.replaceJsonValue(res);
84
+ }
85
+ res = JSON.parse(res);
86
+ window.enableBridgeLog && appCall.alert('1.3、articleHtmlAction JSON.parse');
87
+ }
88
+ catch (error) {
89
+ res = null;
90
+ console.log(error.message);
91
+ appCall.alert('html:' + error.message);
92
+ window.enableBridgeLog && appCall.alert('1.4、articleHtmlAction error' + error.message);
93
+ }
94
+ }
95
+ if (typeof callback === 'function') {
96
+ callback(res);
97
+ }
98
+ }
99
+ });
100
+ return appCall.call('articleHtmlAction', JSON.stringify(action), window.bridge.articleHtmlActionCallback);
101
+ },
102
+ // 地图导航
103
+ navigateMap: function (params) {
104
+ if (params === void 0) { params = {}; }
105
+ return appCall.call('navigateMap', JSON.stringify(params));
106
+ },
107
+ // 获取app信息
108
+ getDeviceData: function (callback) {
109
+ function _callback(data) {
110
+ // 直接使用已初始化的全局变量
111
+ if (window.isIOS) {
112
+ if (data) {
113
+ data = data.replace(/\n/g, '').replace(/\s*/g, '');
114
+ data = JSON.parse(data) || '';
115
+ window.localStorage.setItem('deviceData', JSON.stringify(data));
116
+ }
117
+ }
118
+ typeof callback === 'function' && callback(data);
119
+ }
120
+ var deviceData = appCall.call('getDeviceData', _callback);
121
+ // 安卓没有触发回调,所以自己调用设置一下
122
+ if (window.isAndroid || window.isHarmonyos) {
123
+ setDeviceinfo(deviceData);
124
+ }
125
+ return deviceData;
126
+ },
127
+ login: function (params) {
128
+ if (params === void 0) { params = {}; }
129
+ // 非客户端环境,跳转H5登录
130
+ if (!window.isClient) {
131
+ return (window.location.href = '/login');
132
+ }
133
+ appCall.call('login', JSON.stringify(params));
134
+ },
135
+ });
136
+ return appCall;
137
+ }
138
+ /**
139
+ * 初始化离线包 AppCall
140
+ * 注意:全局变量已在 AppCallCoreClass 构造函数中通过 initGlobalVars() 初始化
141
+ * 这里直接使用已初始化的全局变量,避免重复计算和设置
142
+ */
143
+ export function initOfflineAppCall(appCall) {
144
+ return new Promise(function (resolve) {
145
+ // 获取 core 实例(全局变量已在构造函数中初始化)
146
+ var core = appCall.__core;
147
+ if (!core) {
148
+ resolve();
149
+ return;
150
+ }
151
+ // 直接使用已初始化的全局变量,避免重复计算
152
+ var isClient = core.getIsClient();
153
+ if (isClient) {
154
+ if (window.isIOS) {
155
+ // iOS Bridge 初始化(与 index.ts 中的逻辑保持一致)
156
+ window.setupWebViewJavascriptBridge = function (callback) {
157
+ if (window.WebViewJavascriptBridge) {
158
+ return typeof callback === 'function' && callback(window.WebViewJavascriptBridge);
159
+ }
160
+ if (window.WVJBCallbacks) {
161
+ return window.WVJBCallbacks.push(callback);
162
+ }
163
+ window.WVJBCallbacks = [callback];
164
+ var WVJBIframe = document.createElement('iframe');
165
+ WVJBIframe.style.display = 'none';
166
+ WVJBIframe.src = 'motor://__BRIDGE_LOADED__';
167
+ document.documentElement.appendChild(WVJBIframe);
168
+ setTimeout(function () {
169
+ document.documentElement.removeChild(WVJBIframe);
170
+ }, 0);
171
+ };
172
+ appCall.callIOSHandler('getInterface', [], function (data) {
173
+ core.setIOSInterface(data);
174
+ appCall.getUserData(function () {
175
+ resolve();
176
+ });
177
+ appCall.getDeviceData();
178
+ });
179
+ }
180
+ else if (window.isAndroid || window.isHarmonyos) {
181
+ appCall.getUserData(function () {
182
+ resolve();
183
+ });
184
+ appCall.getDeviceData();
185
+ }
186
+ }
187
+ else {
188
+ resolve();
189
+ }
190
+ });
191
+ }
@@ -0,0 +1,67 @@
1
+ /**
2
+ * AppCall 类型定义
3
+ */
4
+ export interface AppCallConfig {
5
+ /** 项目名称,用于默认标题等 */
6
+ projectName: string;
7
+ /** 项目域名 */
8
+ domain: string;
9
+ /** 默认图片URL */
10
+ defaultImage?: string;
11
+ /** 默认描述 */
12
+ defaultDesc?: string;
13
+ /** BundleId */
14
+ bundleId?: string;
15
+ /** iOS Scheme */
16
+ iosScheme?: string;
17
+ /** Android Scheme */
18
+ androidScheme?: string;
19
+ /** App Store 链接 */
20
+ appStoreUrl?: string;
21
+ /** Android 应用市场链接 */
22
+ androidStoreUrl?: string;
23
+ /** 是否显示调试提示 */
24
+ showTip?: boolean;
25
+ /** 环境配置对象(从 config/env 导入) */
26
+ env: any;
27
+ /** 项目依赖函数:根据圈子名称获取圈子id */
28
+ getCircleIdByName?: (params: {
29
+ title: string;
30
+ }) => Promise<{
31
+ id: number;
32
+ type: string;
33
+ }>;
34
+ /** 项目依赖函数:设置用户信息 */
35
+ setUserinfo?: (data: any) => void;
36
+ /** 项目依赖函数:文件路径转https */
37
+ fileToHttps?: (url: string) => string;
38
+ }
39
+ export interface BridgeCallback {
40
+ [key: string]: (...args: any[]) => any;
41
+ }
42
+ export interface AppCallCore {
43
+ call: (name: string, ...args: any[]) => any;
44
+ has: (name: string) => boolean;
45
+ extend: (obj: Record<string, any>) => AppCallCore;
46
+ callIOSHandler: (name: string, params: any[], callback?: Function) => boolean;
47
+ allParamsToString: (params: Record<string, any>) => Record<string, string>;
48
+ }
49
+ /**
50
+ * 所有扩展方法的类型(由真实代码自动推导)
51
+ * - createAllExtensions 返回的对象就是最终 extend 到 AppCall 上的能力集合
52
+ */
53
+ export type AppCallExtensions = ReturnType<typeof import('./extensions').createAllExtensions>;
54
+ export interface AppCallInstance extends AppCallCore, AppCallExtensions {
55
+ /**
56
+ * 在 index.ts 里注入,用于桥接初始化(部分项目可能不调用)
57
+ */
58
+ init?: () => Promise<void>;
59
+ /**
60
+ * 内部引用,供 extensions.ts 取 core 能力使用
61
+ */
62
+ __core?: any;
63
+ /**
64
+ * 兜底索引(不要用 any,否则会吞掉所有智能提示)
65
+ */
66
+ [key: string]: unknown;
67
+ }
@@ -0,0 +1,4 @@
1
+ /**
2
+ * AppCall 类型定义
3
+ */
4
+ export {};
@@ -4,46 +4,51 @@
4
4
  * @createBy: @2021.01.21
5
5
  */
6
6
  'use strict';
7
- class CookieClass {
7
+ var CookieClass = /** @class */ (function () {
8
+ function CookieClass() {
9
+ }
8
10
  /**
9
11
  * 获取cookie
10
12
  * @param {String} name
11
13
  * @return {String}
12
14
  */
13
- getCookie(name) {
14
- const _name = name + '=';
15
- const ca = document.cookie.split(';');
16
- for (let i = 0; i < ca.length; i++) {
17
- let c = ca[i];
15
+ CookieClass.prototype.getCookie = function (name) {
16
+ var _name = name + '=';
17
+ var ca = document.cookie.split(';');
18
+ for (var i = 0; i < ca.length; i++) {
19
+ var c = ca[i];
18
20
  while (c.charAt(0) === ' ')
19
21
  c = c.substring(1);
20
22
  if (c.includes(_name))
21
23
  return c.substring(_name.length, c.length);
22
24
  }
23
25
  return '';
24
- }
26
+ };
25
27
  /**
26
28
  * 设置cookie
27
29
  * @param {Object} ICookie
28
30
  */
29
- setCookie({ name = '', value = '', exdays = -1, path = '/', domain = '.jddmoto.com', }) {
31
+ CookieClass.prototype.setCookie = function (_a) {
32
+ var _b = _a.name, name = _b === void 0 ? '' : _b, _c = _a.value, value = _c === void 0 ? '' : _c, _d = _a.exdays, exdays = _d === void 0 ? -1 : _d, _e = _a.path, path = _e === void 0 ? '/' : _e, _f = _a.domain, domain = _f === void 0 ? '.jddmoto.com' : _f;
30
33
  var d = new Date();
31
34
  d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
32
- var expires = `expires=${d.toUTCString()}`;
33
- document.cookie = `${name}=${value};${expires};path=${path};domain=${domain};`;
34
- }
35
+ var expires = "expires=".concat(d.toUTCString());
36
+ document.cookie = "".concat(name, "=").concat(value, ";").concat(expires, ";path=").concat(path, ";domain=").concat(domain, ";");
37
+ };
35
38
  /**
36
39
  * 清除Cookie
37
40
  * @param {String} name
38
41
  */
39
- clearCookie({ name = '', path = '/', domain = '.jddmoto.com', }) {
42
+ CookieClass.prototype.clearCookie = function (_a) {
43
+ var _b = _a.name, name = _b === void 0 ? '' : _b, _c = _a.path, path = _c === void 0 ? '/' : _c, _d = _a.domain, domain = _d === void 0 ? '.jddmoto.com' : _d;
40
44
  this.setCookie({
41
- name,
45
+ name: name,
42
46
  value: '',
43
47
  exdays: -1,
44
- path,
45
- domain
48
+ path: path,
49
+ domain: domain
46
50
  });
47
- }
48
- }
51
+ };
52
+ return CookieClass;
53
+ }());
49
54
  export default new CookieClass();
@@ -13,7 +13,7 @@
13
13
  function replacementDate(data, fmt) {
14
14
  for (var k in data) {
15
15
  if (new RegExp('(' + k + ')').test(fmt)) {
16
- fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (data[k]) : ((`00${data[k]}`).substr(('' + data[k]).length)));
16
+ fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (data[k]) : (("00".concat(data[k])).substr(('' + data[k]).length)));
17
17
  }
18
18
  }
19
19
  return fmt;
@@ -30,7 +30,9 @@ function replacementYear(date, fmt) {
30
30
  }
31
31
  return fmt;
32
32
  }
33
- class DateClass {
33
+ var DateClass = /** @class */ (function () {
34
+ function DateClass() {
35
+ }
34
36
  /**
35
37
  * 格式化时间
36
38
  * @param {Date|Number|String} date 需要格式化的时间 2017-11-11、2017/11/11、linux time
@@ -40,10 +42,11 @@ class DateClass {
40
42
  * date.format(new Date(), 'YYYY:MM:DD') // 自定义格式 'YYYY:MM:DD'
41
43
  * @return {String} fmt 'YYYY-MM-DD HH:mm:ss'
42
44
  */
43
- format(date, fmt = 'YYYY-MM-DD HH:mm:ss') {
45
+ DateClass.prototype.format = function (date, fmt) {
46
+ if (fmt === void 0) { fmt = 'YYYY-MM-DD HH:mm:ss'; }
44
47
  if (!date)
45
48
  return '';
46
- let timeData = typeof date === 'string' ? new Date(date.replace(/-/g, '/')) : date;
49
+ var timeData = typeof date === 'string' ? new Date(date.replace(/-/g, '/')) : date;
47
50
  timeData = typeof date === 'number' ? new Date(date) : timeData;
48
51
  var o = {
49
52
  'M+': timeData.getMonth() + 1,
@@ -55,7 +58,7 @@ class DateClass {
55
58
  'q+': Math.floor((timeData.getMonth() + 3) / 3),
56
59
  'S': timeData.getMilliseconds()
57
60
  };
58
- const week = {
61
+ var week = {
59
62
  '0': '\u65e5',
60
63
  '1': '\u4e00',
61
64
  '2': '\u4e8c',
@@ -66,10 +69,10 @@ class DateClass {
66
69
  };
67
70
  fmt = replacementYear(timeData, fmt);
68
71
  if (/(E+)/.test(fmt)) {
69
- fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? '\u661f\u671f' : '\u5468') : '') + week[`${timeData.getDay()} `]);
72
+ fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? '\u661f\u671f' : '\u5468') : '') + week["".concat(timeData.getDay(), " ")]);
70
73
  }
71
74
  return replacementDate(o, fmt);
72
- }
75
+ };
73
76
  /**
74
77
  * 天数加减
75
78
  * @param {string | Date} date 传入的时间 2020-10-15 or Date
@@ -78,11 +81,11 @@ class DateClass {
78
81
  * addDaysToDate('2020-10-15', -10) // '2020-10-05'
79
82
  * @return {String} fmt 'YYYY-MM-DD'
80
83
  */
81
- addDaysToDate(date, days) {
82
- const d = typeof date === 'object' ? date : new Date(date);
84
+ DateClass.prototype.addDaysToDate = function (date, days) {
85
+ var d = typeof date === 'object' ? date : new Date(date);
83
86
  d.setDate(d.getDate() + days);
84
87
  return d.toISOString().split('T')[0];
85
- }
88
+ };
86
89
  /**
87
90
  * 获取倒计时剩余时间
88
91
  * @param {Date | Number} endTime 截止时间
@@ -91,55 +94,56 @@ class DateClass {
91
94
  * date.format(1586840260500) // 返回 {dd: '天', hh: '时', mm: '分', ss: '秒'}
92
95
  * @return {object | boolean} {dd: '天', hh: '时', mm: '分', ss: '秒'}
93
96
  */
94
- remainTime(endTime, startTime = new Date()) {
95
- const ts = Number(endTime) - Number(startTime); // 计算剩余的毫秒数
96
- let dd = Math.floor(ts / 1000 / 60 / 60 / 24); // 计算剩余的天数
97
- let hh = Math.floor(ts / 1000 / 60 / 60 % 24); // 计算剩余的小时数
98
- let mm = Math.floor(ts / 1000 / 60 % 60); // 计算剩余的分钟数
99
- let ss = Math.floor(ts / 1000 % 60); // 计算剩余的秒数
97
+ DateClass.prototype.remainTime = function (endTime, startTime) {
98
+ if (startTime === void 0) { startTime = new Date(); }
99
+ var ts = Number(endTime) - Number(startTime); // 计算剩余的毫秒数
100
+ var dd = Math.floor(ts / 1000 / 60 / 60 / 24); // 计算剩余的天数
101
+ var hh = Math.floor(ts / 1000 / 60 / 60 % 24); // 计算剩余的小时数
102
+ var mm = Math.floor(ts / 1000 / 60 % 60); // 计算剩余的分钟数
103
+ var ss = Math.floor(ts / 1000 % 60); // 计算剩余的秒数
100
104
  if (ts <= 0)
101
105
  return false;
102
106
  return {
103
- dd: (dd < 10 ? `0${dd}` : dd),
104
- hh: (hh < 10 ? `0${hh}` : hh),
105
- mm: (mm < 10 ? `0${mm}` : mm),
106
- ss: (ss < 10 ? `0${ss}` : ss)
107
+ dd: (dd < 10 ? "0".concat(dd) : dd),
108
+ hh: (hh < 10 ? "0".concat(hh) : hh),
109
+ mm: (mm < 10 ? "0".concat(mm) : mm),
110
+ ss: (ss < 10 ? "0".concat(ss) : ss)
107
111
  };
108
- }
112
+ };
109
113
  /**
110
114
  * 格式化现在的已过时间
111
115
  * @param {Number} startTime
112
116
  * @return {String} *年前 *个月前 *天前 *小时前 *分钟前 刚刚
113
117
  */
114
- formatPassTime(startTime) {
115
- const currentTime = new Date();
116
- const time = currentTime - startTime;
117
- const year = Math.floor(time / (1000 * 60 * 60 * 24) / 30 / 12);
118
+ DateClass.prototype.formatPassTime = function (startTime) {
119
+ var currentTime = new Date();
120
+ var time = currentTime - startTime;
121
+ var year = Math.floor(time / (1000 * 60 * 60 * 24) / 30 / 12);
118
122
  if (year)
119
- return `${year}年前`;
120
- const month = Math.floor(time / (1000 * 60 * 60 * 24) / 30);
123
+ return "".concat(year, "\u5E74\u524D");
124
+ var month = Math.floor(time / (1000 * 60 * 60 * 24) / 30);
121
125
  if (month)
122
- return `${month}个月前`;
123
- const day = Math.floor(time / (1000 * 60 * 60 * 24));
126
+ return "".concat(month, "\u4E2A\u6708\u524D");
127
+ var day = Math.floor(time / (1000 * 60 * 60 * 24));
124
128
  if (day)
125
- return `${day}天前`;
126
- const hour = Math.floor(time / (1000 * 60 * 60));
129
+ return "".concat(day, "\u5929\u524D");
130
+ var hour = Math.floor(time / (1000 * 60 * 60));
127
131
  if (hour)
128
- return `${hour}小时前`;
129
- const min = Math.floor(time / (1000 * 60));
132
+ return "".concat(hour, "\u5C0F\u65F6\u524D");
133
+ var min = Math.floor(time / (1000 * 60));
130
134
  if (min)
131
- return `${min}分钟前`;
135
+ return "".concat(min, "\u5206\u949F\u524D");
132
136
  else
133
137
  return '刚刚';
134
- }
138
+ };
135
139
  /**
136
140
  * 格式化时间 列表里的时间内容格式 待废弃,统一时间格式
137
141
  * @param {Number} time 1494141000*1000
138
142
  * @return {String} *年*月*日 *月*日 刚刚(1-60秒) 1-60分钟前 1-24小时前 1-3天前
139
143
  */
140
- formatPassTimeForList(time) {
144
+ DateClass.prototype.formatPassTimeForList = function (time) {
141
145
  return DateClass.prototype.formatPassTimeForDetail(time, 'YYYY年MM月DD日', true);
142
- }
146
+ };
143
147
  /**
144
148
  * 格式化时间 详情内容里的时间格式
145
149
  * @param {Number} time 1494141000*1000
@@ -147,24 +151,25 @@ class DateClass {
147
151
  * @param {Boolean} noYear 是否显示年
148
152
  * @return {String} *年*月*日 *月*日 刚刚(1-60秒) 1-60分钟前 1-24小时前 1-3天前
149
153
  */
150
- formatPassTimeForDetail(time, fmt = 'YYYY-MM-DD', noYear) {
151
- const date = (typeof time === 'number') ? new Date(time) : new Date((time || '').replace(/-/g, '/'));
152
- const diff = (((new Date()).getTime() - date.getTime()) / 1000);
153
- const dayDiff = Math.floor(diff / 86400);
154
- const isValidDate = Object.prototype.toString.call(date) === '[object Date]' && !isNaN(date.getTime());
154
+ DateClass.prototype.formatPassTimeForDetail = function (time, fmt, noYear) {
155
+ if (fmt === void 0) { fmt = 'YYYY-MM-DD'; }
156
+ var date = (typeof time === 'number') ? new Date(time) : new Date((time || '').replace(/-/g, '/'));
157
+ var diff = (((new Date()).getTime() - date.getTime()) / 1000);
158
+ var dayDiff = Math.floor(diff / 86400);
159
+ var isValidDate = Object.prototype.toString.call(date) === '[object Date]' && !isNaN(date.getTime());
155
160
  if (!isValidDate)
156
161
  return '';
157
- const formatDate = function () {
158
- const today = new Date(date);
162
+ var formatDate = function () {
163
+ var today = new Date(date);
159
164
  var o = {
160
165
  'Y+': today.getFullYear(),
161
166
  'M+': ('0' + (today.getMonth() + 1)).slice(-2),
162
167
  'D+': ('0' + today.getDate()).slice(-2)
163
168
  };
164
169
  fmt = replacementYear(date, fmt);
165
- const year = today.getFullYear();
170
+ var year = today.getFullYear();
166
171
  if (!(new Date().getFullYear() > year) && noYear) {
167
- const backData = replacementDate(o, fmt);
172
+ var backData = replacementDate(o, fmt);
168
173
  return backData.split('年')[1];
169
174
  }
170
175
  return replacementDate(o, fmt);
@@ -180,6 +185,7 @@ class DateClass {
180
185
  (diff < 3600 && Math.floor(diff / 60) + '分钟前') ||
181
186
  (diff < 7200 && '1小时前') ||
182
187
  (diff < 86400 && Math.floor(diff / 3600) + '小时前'))) || (dayDiff < 16 && dayDiff + '天前');
183
- }
184
- }
188
+ };
189
+ return DateClass;
190
+ }());
185
191
  export default new DateClass();