forked from dompling/Scriptable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PoisonCalendar.js
231 lines (206 loc) · 7.01 KB
/
PoisonCalendar.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: yellow; icon-glyph: calendar-alt;
// 添加require,是为了vscode中可以正确引入包,以获得自动补全等功能
if (typeof require === 'undefined') require = importModule;
const { DmYY, Runing } = require('./DmYY');
const { Calendar } = require('./Calendar');
const $calendar = new Calendar();
// @组件代码开始
class Widget extends DmYY {
constructor(arg) {
super(arg);
this.name = '毒汤日历';
this.en = 'PoisonCalendar';
this.Run();
}
cookie = '';
date = new Date();
format = new DateFormatter();
userInfo = {};
dataSource = [];
baseUrl = 'http://www.dutangapp.cn';
init = async () => {
try {
await this.getUserInfo();
await this.getDaysInfo();
} catch (e) {
console.log(e);
}
};
getUserInfo = async () => {
try {
const url = `${this.baseUrl}/u/wx_login?code=&os=iOS&unid=${this.cookie}&version=3.5.2`;
const response = await this.$request.get(url);
if (response.code === 0) {
console.log('✅用户信息获取成功');
const { data } = response;
this.userInfo = data;
} else {
console.log('❌用户信息获取失败');
}
} catch (e) {
console.log('❌用户信息获取失败' + e);
}
};
getDaysInfo = async () => {
try {
const today = `${this.date.getFullYear()}-${
this.date.getMonth() + 1
}-${this.date.getDate()}`;
const url = `${this.baseUrl}/u/v2/days_info?days=${today}`;
console.log(url);
const response = await this.$request.get(url);
if (response.code === 0) {
console.log(`✅今日${this.name}获取成功`);
const { data } = response;
this.format.dateFormat = 'YYYY-MM-dd';
const key = this.format.string(this.date);
this.dataSource = data[key].toxicList;
} else {
console.log(`❌今日${this.name}获取成功`);
}
} catch (e) {
console.log(`❌今日${this.name}获取成功` + e);
}
};
setAvatar = async (stack) => {
stack.size = new Size(50, 50);
stack.cornerRadius = 5;
const imgLogo = await this.$request.get(this.userInfo.avatar, 'IMG');
const imgLogoItem = stack.addImage(imgLogo);
imgLogoItem.imageSize = new Size(50, 50);
return stack;
};
setTitleStack = (stack) => {
const textFormatNumber = this.textFormat.title;
textFormatNumber.color = this.backGroundColor;
const title = this.userInfo.nick;
textFormatNumber.size =
title.length > 20 || this.widgetFamily === 'small' ? 16 : 20;
const titleItem = this.provideText(title, stack, textFormatNumber);
titleItem.lineLimit = 1;
};
setPathStack = (stack) => {
const textFormatNumber = this.textFormat.defaultText;
textFormatNumber.color = new Color('#2481cc');
textFormatNumber.size = 12;
this.format.dateFormat = 'HH:mm';
let simpleText = '更新:' + this.format.string(this.date);
this.format.dateFormat = 'YYYY-MM-dd';
const titleItem = this.provideText(simpleText, stack, textFormatNumber);
titleItem.lineLimit = 1;
};
setFooterCell = async (stack) => {
const dayInfos = this.getRandomArrayElements(this.dataSource || [], 1);
const info = dayInfos[0] || {};
const title = this.textFormat.title;
title.color = this.backGroundColor;
title.size = 12;
const stackContent = stack.addStack();
stackContent.layoutVertically();
stackContent.centerAlignContent();
stackContent.addSpacer();
this.provideText(info.data, stackContent, title);
stackContent.addSpacer();
};
setCalendar(stack) {
const today = this.format.string(this.date);
const todays = today.split('-');
const response = $calendar.solar2lunar(todays[0], todays[1], todays[2]);
stack.layoutVertically();
const stackCalendar = stack.addStack();
stackCalendar.setPadding(5, 0, 5, 0);
stackCalendar.centerAlignContent();
stackCalendar.cornerRadius = 4;
stackCalendar.backgroundColor = this.backGroundColor;
const title = this.textFormat.title;
title.color = this.widgetColor;
title.size = 24;
this.provideText(todays[2], stackCalendar, title);
const stackYear = stackCalendar.addStack();
stackYear.layoutVertically();
const text = this.textFormat.defaultText;
text.color = this.widgetColor;
text.size = 8;
const animal = $calendar.getAnimalZodiacToEmoji(response.Animal);
this.provideText(response.Animal + animal, stackYear, text);
stackYear.addSpacer(2);
this.provideText(response.IMonthCn, stackYear, text);
stackYear.addSpacer(2);
this.provideText(response.IDayCn, stackYear, text);
}
renderSmall = async (w) => {
return w;
};
renderMedium = async (w) => {
const stackBody = w.addStack();
stackBody.layoutVertically();
const stackHeader = stackBody.addStack();
stackHeader.setPadding(5, 10, 5, 10);
stackHeader.cornerRadius = 10;
stackHeader.backgroundColor = this.widgetColor;
stackHeader.centerAlignContent();
const stackLeft = stackHeader.addStack();
await this.setAvatar(stackLeft);
stackHeader.addSpacer(20);
const stackRight = stackHeader.addStack();
stackRight.layoutVertically();
this.setTitleStack(stackRight);
stackRight.addSpacer(5);
this.setPathStack(stackRight);
stackHeader.addSpacer();
const stackDay = stackHeader.addStack();
this.setCalendar(stackDay);
stackBody.addSpacer();
const stackFooter = stackBody.addStack();
stackFooter.setPadding(10, 0, 10, 0);
stackFooter.cornerRadius = 10;
stackFooter.backgroundColor = this.widgetColor;
stackFooter.addSpacer();
await this.setFooterCell(stackFooter);
stackFooter.addSpacer();
return w;
};
renderLarge = async (w) => {
return w;
};
Run() {
if (config.runsInApp) {
const widgetInitConfig = {
cookie: '@DJT.unid',
};
this.registerAction('账号设置', async () => {
await this.setAlertInput(
`${this.name}账号`,
'读取 BoxJS 缓存信息',
widgetInitConfig,
);
});
this.registerAction('代理缓存', async () => {
await this.setCacheBoxJSData(widgetInitConfig);
});
this.registerAction('基础设置', this.setWidgetConfig);
}
this.cookie = this.settings.cookie || this.cookie;
}
/**
* 渲染函数,函数名固定
* 可以根据 this.widgetFamily 来判断小组件尺寸,以返回不同大小的内容
*/
async render() {
await this.init();
const widget = new ListWidget();
await this.getWidgetBackgroundImage(widget);
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);
}
}
}
// @组件代码结束
// await Runing(Widget, "", true); // 正式环境
await Runing(Widget, args.widgetParameter, false); //远程开发环境