-
Notifications
You must be signed in to change notification settings - Fork 118
/
写真随机.js
319 lines (294 loc) · 10.7 KB
/
写真随机.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: red; icon-glyph: user-astronaut;
/**
* Author:LSP
* Date:2023-05-15
*/
// -------------------------------------------------------
// 是否是开发环境,配合手机端调试使用,正式发布设置为false
const isDev = false;
const dependencyLSP = '20230512';
console.log(`当前环境 👉👉👉👉👉 ${isDev ? 'DEV' : 'RELEASE'}`);
console.log(`----------------------------------------`);
// 分支
const branch = 'v2';
// 仓库根目录
const remoteGithubRoot = `https://raw.githubusercontent.com/Enjoyee/Scriptable/${branch}`;
const remoteHomeLandRoot = `https://glimmerk.coding.net/p/Scriptable/shared-depot/source/git/raw/${branch}`;
// 依赖包目录
const fm = FileManager.local();
const rootDir = fm.documentsDirectory();
const cacheDir = fm.joinPath(rootDir, 'LSP');
const dependencyFileName = isDev ? "_LSP.js" : `${cacheDir}/_LSP.js`;
// 下载依赖包
await downloadLSPDependency();
// -------------------------------------------------------
if (typeof require === 'undefined') require = importModule
// 引入相关方法
const { BaseWidget } = require(dependencyFileName);
// @定义小组件
class Widget extends BaseWidget {
defaultPreference = {
pic_name: 'girls_pic_20230515.json',
};
getValueByKey = (key) => this.readWidgetSetting()[key] ?? this.defaultPreference[key];
constructor(scriptName) {
super(scriptName);
this.backgroundColor = '#03071e,#03071e';
this.picJsonArr = [];
}
async getAppViewOptions() {
return {
showWidgetBg: false,
widgetProvider: {
small: true, // 是否提供小号组件
medium: true, // 是否提供中号组件
large: true, // 是否提供大号组件
},
// 预览界面的组件设置item
settingItems: [
{
name: 'photoHalving',
label: '图片张数',
type: 'select',
icon: { name: 'photo.artframe', color: '#ef233c' },
options: [
{ label: '占满组件', value: '1' },
{ label: '左右互搏', value: '2' },
{ label: '三分天下', value: '3' },
{ label: '四平八稳', value: '4' },
],
default: "1"
},
],
};
}
async render({ widgetSetting, family }) {
//
let cacheJsonArr = this.useFileManager().readJSONCache(this.defaultPreference.pic_name);
if (JSON.stringify(cacheJsonArr) == '{}') {
const url = `${this.getRemoteRootPath()}/file/${this.defaultPreference.pic_name}`;
cacheJsonArr = await this.httpGet(url, { jsonFormat: false, useCache: false });
this.useFileManager().writeJSONCache(this.defaultPreference.pic_name, cacheJsonArr);
}
this.picJsonArr = JSON.parse(cacheJsonArr);
this.picJsonArr.sort(() => Math.random() - 0.5);
return await this.provideWidget(family, widgetSetting);
}
async provideWidget(widgetFamily, widgetSetting) {
const photoHalving = widgetSetting.photoHalving ?? 1;
let name = '大号';
switch (widgetFamily) {
case 'small':
name = '小号';
break;
case 'medium':
name = '中号';
break;
}
//=================================
const widget = new ListWidget();
widget.setPadding(0, 0, 0, 0);
//=================================
let stack = widget.addStack();
widget.backgroundColor = new Color('#03071e');
const imgRes = this.picJsonArr.slice(0, 10);
const widgetSize = this.getWidgetSize(name);
const widgetWidth = widgetSize.width + 10 * Device.screenScale();
const widgetHeight = widgetSize.height + 6 * Device.screenScale();
// 图片等分
const picHalvingCache = Number(photoHalving);
let imgStack;
const divider = 3;
switch (picHalvingCache) {
case 2:
stack.layoutHorizontally();
imgStack = stack.addStack();
imgStack.size = new Size(widgetWidth / 2, widgetHeight);
await this.addStackImg(imgRes, imgStack);
//
stack.addSpacer(divider);
imgStack = stack.addStack();
imgStack.size = new Size(widgetWidth / 2, widgetHeight);
await this.addStackImg(imgRes, imgStack);
break;
case 3:
stack.layoutHorizontally();
imgStack = stack.addStack();
imgStack.size = new Size(widgetWidth * 0.6, widgetHeight);
await this.addStackImg(imgRes, imgStack);
//
stack.addSpacer(divider);
//
imgStack = stack.addStack();
imgStack.layoutVertically();
let stack_3_1 = imgStack.addStack();
stack_3_1.size = new Size(widgetWidth * 0.4, widgetHeight / 2);
await this.addStackImg(imgRes, stack_3_1);
//
imgStack.addSpacer(divider);
let stack_3_2 = imgStack.addStack();
stack_3_2.size = new Size(widgetWidth * 0.4, widgetHeight / 2);
await this.addStackImg(imgRes, stack_3_2);
break;
case 4:
stack.layoutVertically();
imgStack = stack.addStack();
imgStack.layoutHorizontally();
let stack_4_1 = imgStack.addStack();
stack_4_1.size = new Size(widgetWidth / 2, widgetHeight / 2);
await this.addStackImg(imgRes, stack_4_1);
//
imgStack.addSpacer(divider);
let stack_4_2 = imgStack.addStack();
stack_4_2.size = new Size(widgetWidth / 2, widgetHeight / 2);
await this.addStackImg(imgRes, stack_4_2);
//
stack.addSpacer(divider);
imgStack = stack.addStack();
imgStack.layoutHorizontally();
let stack_4_3 = imgStack.addStack();
stack_4_3.size = new Size(widgetWidth / 2, widgetHeight / 2);
await this.addStackImg(imgRes, stack_4_3);
//
imgStack.addSpacer(divider);
let stack_4_4 = imgStack.addStack();
stack_4_4.size = new Size(widgetWidth / 2, widgetHeight / 2);
await this.addStackImg(imgRes, stack_4_4);
break;
default:
stack.layoutVertically();
imgStack = stack.addStack();
imgStack.size = new Size(widgetWidth, widgetHeight);
await this.addStackImg(imgRes, imgStack);
break;
}
//=================================
stack.addSpacer();
return widget;
}
addStackImg = async (imgRes, imgStack) => {
if (imgRes && imgRes.length > 0) {
const index = parseInt(Math.random() * imgRes.length)
let imgUrl = imgRes[index];
imgRes.splice(index, 1);
imgStack.url = imgUrl;
let img = await this.getImageByUrl(imgUrl, 'mirror', true);
let imgSpan = imgStack.addImage(img);
imgSpan.applyFillingContentMode();
}
}
// --------------------------NET START--------------------------
// --------------------------NET END--------------------------
}
await new Widget(Script.name()).run();
// =================================================================================
// =================================================================================
async function downloadLSPDependency() {
let fm = FileManager.local();
const fileName = fm.joinPath(fm.documentsDirectory(), `LSP/${Script.name()}/settings.json`);
const fileExists = fm.fileExists(fileName);
let cacheString = '{}';
if (fileExists) {
cacheString = fm.readString(fileName);
}
const use_github = JSON.parse(cacheString)['use_github'];
const dependencyURL = `${use_github ? remoteGithubRoot : remoteHomeLandRoot}/_LSP.js`;
const update = needUpdateDependency();
if (isDev) {
const iCloudPath = FileManager.iCloud().documentsDirectory();
const localIcloudDependencyExit = fm.isFileStoredIniCloud(`${iCloudPath}/_LSP.js`);
const localDependencyExit = fm.fileExists(`${rootDir}/_LSP.js`);
const fileExist = localIcloudDependencyExit || localDependencyExit;
console.log(`🚀 DEV开发依赖文件${fileExist ? '已存在 ✅' : '不存在 🚫'}`);
if (!fileExist || update) {
console.log(`🤖 DEV 开始${update ? '更新' + dependencyLSP : '下载'}依赖~`);
keySave('VERSION', dependencyLSP);
await downloadFile2Scriptable('_LSP', dependencyURL);
}
return;
}
//////////////////////////////////////////////////////////
console.log(`----------------------------------------`);
const remoteDependencyExit = fm.fileExists(`${cacheDir}/_LSP.js`);
console.log(`🚀 RELEASE依赖文件${remoteDependencyExit ? '已存在 ✅' : '不存在 🚫'}`);
// ------------------------------
if (!remoteDependencyExit || update) { // 下载依赖
// 创建根目录
if (!fm.fileExists(cacheDir)) {
fm.createDirectory(cacheDir, true);
}
// 下载
console.log(`🤖 RELEASE开始${update ? '更新' : '下载'}依赖~`);
console.log(`----------------------------------------`);
const req = new Request(dependencyURL);
const moduleJs = await req.load();
if (moduleJs) {
fm.write(fm.joinPath(cacheDir, '/_LSP.js'), moduleJs);
keySave('VERSION', dependencyLSP);
console.log('✅ LSP远程依赖环境下载成功!');
console.log(`----------------------------------------`);
} else {
console.error('🚫 获取依赖环境脚本失败,请重试!');
console.log(`----------------------------------------`);
}
}
}
/**
* 获取保存的文件名
* @param {*} fileName
* @returns
*/
function getSaveFileName(fileName) {
const hasSuffix = fileName.lastIndexOf(".") + 1;
return !hasSuffix ? `${fileName}.js` : fileName;
};
/**
* 保存文件到Scriptable软件目录,app可看到
* @param {*} fileName
* @param {*} content
* @returns
*/
function saveFile2Scriptable(fileName, content) {
try {
const fm = FileManager.iCloud();
let fileSimpleName = getSaveFileName(fileName);
const filePath = fm.joinPath(fm.documentsDirectory(), fileSimpleName);
fm.writeString(filePath, content);
return true;
} catch (error) {
return false;
}
};
/**
* 下载js文件到Scriptable软件目录
* @param {*} moduleName 名称
* @param {*} url 在线地址
* @returns
*/
async function downloadFile2Scriptable(moduleName, url) {
const req = new Request(url);
const content = await req.loadString();
return saveFile2Scriptable(`${moduleName}`, content);
};
/**
* 是否需要更新依赖版本
*/
function needUpdateDependency() {
return dependencyLSP != keyGet('VERSION');
};
function keySave(cacheKey, cache) {
if (cache) {
Keychain.set(Script.name() + cacheKey, cache);
}
}
function keyGet(cacheKey, defaultValue = '') {
if (Keychain.contains(Script.name() + cacheKey)) {
return Keychain.get(Script.name() + cacheKey);
} else {
return defaultValue;
}
}
// =================================================================================
// =================================================================================