Skip to content

Commit 32acb45

Browse files
author
unknown
committed
ss
1 parent e8fdba8 commit 32acb45

File tree

4 files changed

+92
-27
lines changed

4 files changed

+92
-27
lines changed

src/app/app.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ function App (data: object, watchs: object) {
1919
this.$router = Router;
2020
this.$route = URI.parse(window.location.href);
2121
this.$statistic = Statistic;
22-
this.$storage = Storage;
22+
this.$storage = new Storage();
2323
}
2424

2525
App.prototype = {
2626
bindEvents: EMPTY_FUNCTION,
2727

2828
init: EMPTY_FUNCTION,
2929

30-
$tip: function (options: TipOptions) {
30+
$tip: function (options: TipOptions) : void {
3131
new Tip(options);
3232
},
3333

@@ -43,7 +43,7 @@ App.prototype = {
4343
return new Confirm(options);
4444
},
4545

46-
_init: function () {
46+
_init: function () : void {
4747
this.bindEvents();
4848
// get or set a property on this, relate to data[propertyName]
4949
if (this.data) {

src/app/modules/logger.ts

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,34 @@
1-
export default class {
2-
3-
}
1+
const Logger = class {
2+
//
3+
static readonly reportUrl: 'http://www.shuxia123.com';
4+
5+
static readonly supportMethods = ['log', 'info', 'warn', 'debug', 'error'];
6+
7+
private reportUrl: string;
8+
private store: Array<any> = [];
9+
10+
constructor (reportUrl: string) {
11+
this.reportUrl = reportUrl;
12+
13+
Logger.supportMethods.forEach((methodName) => {
14+
this[methodName] = (...params) => {
15+
const method = console[methodName];
16+
method.apply(console, ...params);
17+
};
18+
})
19+
20+
this.addErrorListener();
21+
}
22+
23+
/**
24+
* 添加错误日志上报
25+
*/
26+
addErrorListener () :void {
27+
window.onerror = (msg, url, line, col, error) => {
28+
29+
};
30+
}
31+
32+
};
33+
34+
export default Logger;

src/app/modules/storage.ts

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,57 @@
11
class Storage {
2+
// `prefix`: 缓存前缀, 实现namespace的功能, 防止重名
23
static readonly prefix: string = '__';
3-
private readonly splitCode = '|_|';
4-
private readonly storage = localStorage || window.localStorage;
5-
static readonly defauleExpiredTime = 30 * 24 * 60 * 60 * 1000;
64

7-
constructor () {
5+
// `splitCode`: 过期时间 和 缓存值之间的切割符, 如: '60000|_|abc'
6+
static readonly splitCode: string = '|_|';
7+
8+
// storage support
9+
static readonly storage = localStorage || window.localStorage;
10+
11+
// `defaultExpiredTime`: 默认30天过期
12+
static readonly defaultExpiredTime = 30 * 24 * 60 * 60 * 1000;
13+
14+
private prefix: string;
15+
16+
private splitCode: string;
17+
18+
constructor (prefix?: string, splitCode?: string) {
19+
this.prefix = prefix || Storage.prefix;
20+
this.splitCode = splitCode || Storage.splitCode;
821
}
922

10-
private getStorageKey (key:string) {
11-
return `${Storage.prefix}${key}`;
23+
/**
24+
* 获取缓存key
25+
* @param key
26+
*/
27+
private getStorageKey (key:string): string {
28+
return `${this.prefix}${key}`;
1229
}
1330

14-
public setItem (key: string, value: any, expired: number = Storage.defauleExpiredTime) {
31+
/**
32+
* 设置缓存值
33+
* @param key, storage 的 key
34+
* @param value, storage 的 value
35+
* @param expired, storage 的过期时间
36+
*/
37+
public setItem (key: string, value: any, expired: number = Storage.defaultExpiredTime): void {
1538
const itemKey = this.getStorageKey(key);
16-
const expiredTimestamp = +(new Date()) + expired;
17-
this.storage.setItem(itemKey, `${expiredTimestamp}${this.splitCode}${value}`);
39+
const date = new Date();
40+
const expiredTimestamp = date.getTime() + expired;
41+
Storage.storage.setItem(itemKey, `${expiredTimestamp}${this.splitCode}${value}`);
1842
}
1943

20-
public getItem (key: string) {
44+
/**
45+
* 获取缓存值
46+
* @param key, 缓存的key
47+
*/
48+
public getItem (key: string): string {
2149
const itemKey = this.getStorageKey(key);
22-
const localValue = this.storage.getItem(itemKey);
50+
const value = Storage.storage.getItem(itemKey);
2351

24-
if (localValue) {
25-
const localValueSplits = localValue.split(this.splitCode);
52+
if (value) {
53+
const localValueSplits = value.split(this.splitCode);
54+
const currentTimestamp = (new Date()).getTime();
2655
if (+localValueSplits[0] > +(new Date())) {
2756
return localValueSplits[1];
2857
}
@@ -33,10 +62,14 @@ class Storage {
3362
return null;
3463
}
3564

36-
public removeItem (key: string) {
65+
/**
66+
* 删除缓存
67+
* @param key, 缓存的key
68+
*/
69+
public removeItem (key: string): void {
3770
const itemKey = this.getStorageKey(key);
38-
this.storage.removeItem(itemKey);
71+
Storage.storage.removeItem(itemKey);
3972
}
4073
}
4174

42-
export default new Storage();
75+
export default Storage;

src/pages/home/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,16 @@ new App({
107107
this.share = new Share('', null, shareInfo);
108108

109109
// demo code for ajax
110-
this.$ajax({
111-
url: 'http://www.shuxia123.com/services/demos',
110+
const result = this.$ajax({
111+
url: 'https://www.shuxia123.com/services/projects',
112112
method: 'get',
113113
data: {
114114
name: 'test'
115-
}
116-
}).then((result: any) => {
117-
console.log(result);
115+
},
116+
async: false
118117
});
118+
119+
JSON.parse(result)
119120

120121
// demo code for audio
121122
const that = this;

0 commit comments

Comments
 (0)