forked from dompling/Scriptable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHistoryToday.js
162 lines (145 loc) · 4.64 KB
/
HistoryToday.js
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: green; icon-glyph: calendar-minus;
// 添加require,是为了vscode中可以正确引入包,以获得自动补全等功能
if (typeof require === "undefined") require = importModule;
const { DmYY, Runing } = require("./DmYY");
// @组件代码开始
class Widget extends DmYY {
constructor(arg) {
super(arg);
this.name = "历史上的今天";
this.en = "historyToday";
this.logo =
"https://raw.githubusercontent.com/Orz-3/task/master/historyToday.png";
config.runsInApp &&
this.registerAction("基础设置", this.setWidgetConfig);
this.cacheName = this.md5(`dataSouce_${this.en}`);
}
useBoxJS = false;
today = "";
dataSource = [];
init = async () => {
try {
const today = new Date();
const month = today.getMonth() + 1;
const day = today.getDate();
this.today = `${month}.${day}`;
await this.getHistoryList();
} catch (e) {
console.log(e);
}
};
getHistoryList = async () => {
const url = `http://code.lssdjt.com/jsondata/history.${this.today}.js`;
const response = await this.$request.get(url);
try {
if (response.d.length > 0) {
const dataSource = response.d;
this.dataSource = dataSource.filter((item) => item.j[0]).map((item) => ({ ...item, j: item.j[0] }));
}
return false;
} catch (e) {
return false;
}
};
setListCell = async (cell, data) => {
let { j, t, f } = data;
const [year, desc] = t.split(' ');
const imgUri = "http://img.lssdjt.com";
let body = cell.addStack();
body.url = `https://www.lssdjt.com/d/${f}.htm`;
const textView = body.addStack();
textView.layoutVertically();
const descText = textView.addText(desc);
descText.font = Font.boldSystemFont(14);
descText.textColor = this.widgetColor;
descText.lineLimit = 1;
textView.addSpacer();
const subContent = textView.addText(year);
subContent.font = Font.lightSystemFont(10);
subContent.textColor = this.widgetColor;
subContent.lineLimit = 1;
if (this.widgetFamily !== "small") {
body.addSpacer();
const imageView = body.addStack();
imageView.centerAlignContent();
imageView.size = new Size(43, 43);
imageView.cornerRadius = 5;
imageView.borderWidth = 1;
imageView.borderColor = this.widgetColor;
const image = await this.$request.get(`${imgUri}/${j}`, "IMG");
const imageItem = imageView.addImage(image);
imageItem.centerAlignImage();
body.addSpacer(10);
}
return cell;
};
setWidget = async (body, size) => {
const container = body.addStack();
container.layoutVertically();
const dataSource = this.getRandomArrayElements(this.dataSource, size);
for (let index = 0; index < dataSource.length; index++) {
const data = dataSource[index];
let listItem = container.addStack();
await this.setListCell(listItem, data);
container.addSpacer(10);
}
body.addSpacer();
return body;
};
renderSmall = async (w) => {
return await this.setWidget(w, 2);
};
renderLarge = async (w) => {
return await this.setWidget(w, 5);
};
renderMedium = async (w) => {
return await this.setWidget(w, 2);
};
/**
* 渲染函数,函数名固定
* 可以根据 this.widgetFamily 来判断小组件尺寸,以返回不同大小的内容
*/
async render() {
await this.init();
const widget = new ListWidget();
await this.getWidgetBackgroundImage(widget);
const header = widget.addStack();
if (this.widgetFamily !== "small") {
await this.renderNotSmallHeader(header);
} else {
await this.renderHeader(header, this.logo, this.name, this.widgetColor);
}
widget.addSpacer(10);
if (this.widgetFamily === "medium") {
return await this.renderMedium(widget);
} else if (this.widgetFamily === "large") {
return await this.renderLarge(widget);
} else {
return await this.renderSmall(widget);
}
}
renderNotSmallHeader = async (header) => {
header.centerAlignContent();
await this.renderHeader(header, this.logo, this.name, this.widgetColor);
header.addSpacer();
const headerMore = header.addStack();
let [month, day] = this.today.split(".");
month = month >= 10 ? month : `0${month}`;
day = day >= 10 ? day : `0${day}`;
headerMore.url = `https://m.8684.cn/today_d${month}${day}`;
headerMore.setPadding(1, 10, 1, 10);
headerMore.cornerRadius = 10;
headerMore.backgroundColor = new Color("#fff", 0.5);
const textItem = headerMore.addText(`更多`);
textItem.font = Font.boldSystemFont(12);
textItem.textColor = this.widgetColor;
textItem.lineLimit = 1;
textItem.rightAlignText();
return header;
};
}
// @组件代码结束
// await Runing(Widget, "", false); // 正式环境
await Runing(Widget, "", false); //远程开发环境