// 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:2022-12-05 */ // ------------------------------------------------------- // 是否是开发环境,配合手机端调试使用,正式发布设置为false const isDev = true; console.log(`开发环境 👉👉👉 ${isDev ? 'DEV' : 'RELEASE'}`); console.log(`----------------------------------------`); // 依赖包目录 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 { writeWidgetSetting, generateAlert, generateInputAlert, presentSheet, readWidgetSetting, drawTextWithCustomFont, httpGet, getDateStr, getImageByUrl, presentViewInApp, } = require(dependencyFileName); // ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ // ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ // ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓START↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ //=========================组件默认配置========================= const defaultPreference = { caiyun: 'UR8ASaplvIwavDfR', }; const getValueByKey = (key) => readWidgetSetting()[key] ?? defaultPreference[key]; const caiyun = () => getValueByKey('caiyun'); // --------------------------NET START-------------------------- // --------------------------NET END-------------------------- // Widget创建 const createWidget = async (widgetSetting, family) => { //================================= const widget = new ListWidget(); widget.setPadding(0, 0, 0, 0); //================================= let stack = widget.addStack(); stack.layoutHorizontally(); //--------------------------------- const localQuickConfigs = widgetSetting.quickStart; const quickConfigJSON = localQuickConfigs ? localQuickConfigs : JSON.stringify(defaultPreference.quickConfigs); const quickConfigArr = JSON.parse(quickConfigJSON); await quickStack(stack, quickConfigArr); //--------------------------------- let rightStack = stack.addStack(); rightStack.layoutVertically(); rightStack.addSpacer(); await loadWeatherStack(rightStack); rightStack.addSpacer(); //================================= stack.addSpacer(); return widget; } //=========================配置界面========================= await presentViewInApp({ widgetProvider: { small: false, // 是否提供小号组件 medium: true, // 是否提供中号组件 large: false, // 是否提供大号组件 }, // 预览界面的组件设置item settingItems: [ { name: 'otherSetting', label: '其他设置', type: 'cell', icon: 'https://gitcode.net/4qiao/framework/raw/master/img/icon/setting.gif', needLoading: true, childItems: [ { name: 'quickFontSize', label: '启动文字大小', type: 'cell', icon: { name: 'pencil.and.outline', color: '#7743DB', }, needLoading: false, alert: { title: '启动文字大小', options: [ { key: 'quickFontSize', hint: '请输入字号', } ] }, default: quickFontSize(), }, { name: 'quickFontColor', label: '启动文字颜色', type: 'color', icon: { name: 'pencil.and.outline', color: '#7743DB', }, needLoading: false, default: quickFontColor(), }, { name: 'temperatureFontSize', label: '温度文字大小', type: 'cell', icon: { name: 'thermometer.medium', color: '#E0144C', }, needLoading: false, alert: { title: '温度文字大小', options: [ { key: 'temperatureFontSize', hint: '请输入字号', } ] }, default: temperatureFontSize(), }, { name: 'temperatureFontColor', label: '温度文字颜色', type: 'color', icon: { name: 'thermometer.medium', color: '#E0144C', }, needLoading: false, default: temperatureFontColor(), }, { name: 'dateFontSize', label: '日期文字大小', type: 'cell', icon: 'https://s3.uuu.ovh/imgs/2022/12/05/ac2c5b8843ff2719.gif', needLoading: false, alert: { title: '日期文字大小', options: [ { key: 'dateFontSize', hint: '请输入字号', } ] }, default: dateFontSize(), }, { name: 'dateFontColor', label: '日期文字颜色', type: 'color', icon: 'https://s3.uuu.ovh/imgs/2022/12/05/ac2c5b8843ff2719.gif', needLoading: false, default: dateFontColor(), }, { name: 'weatherFontSize', label: '天气文字大小', type: 'cell', icon: 'https://s3.uuu.ovh/imgs/2022/12/05/a95f9edce36891d9.gif', needLoading: false, alert: { title: '天气文字大小', options: [ { key: 'weatherFontSize', hint: '请输入字号', } ] }, default: weatherFontSize(), }, { name: 'weatherFontColor', label: '天气文字颜色', type: 'color', icon: 'https://s3.uuu.ovh/imgs/2022/12/05/a95f9edce36891d9.gif', needLoading: false, default: weatherFontColor(), }, { name: 'covFontSize', label: '疫情文字大小', type: 'cell', icon: 'https://s3.uuu.ovh/imgs/2022/12/05/2cd3dee47cd0824f.png', needLoading: false, alert: { title: '疫情文字大小', options: [ { key: 'covFontSize', hint: '请输入字号', } ] }, default: covFontSize(), }, { name: 'covFontColor', label: '疫情文字颜色', type: 'color', icon: 'https://s3.uuu.ovh/imgs/2022/12/05/2cd3dee47cd0824f.png', needLoading: false, default: covFontColor(), }, ] }, ], // cell类型的item点击回调 onItemClick: async (item) => { let widgetSetting = readWidgetSetting(); let insertDesc; switch (item.name) { case '': break } // 写入更新配置 writeWidgetSetting(widgetSetting); return { desc: { value: insertDesc }, }; }, // UI渲染 render: async ({ widgetSetting, family }) => { Object.assign(defaultPreference, widgetSetting); try { return await createWidget(defaultPreference, family); } catch (e) { if (config.runsInApp) { await generateAlert({ title: '❌运行错误', message: `${e}`, options: ['确定'] }); } console.error(e); } } }); // ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑END↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ // ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ // ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ // ================================================================================= // ================================================================================= async function downloadLSPDependency() { let fm = FileManager.local(); const dependencyURL = "https://gitee.com/enjoyee/scriptable/raw/develop/_LSP.js"; 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) { console.log(`🤖 DEV 开始下载依赖~`); await downloadFile2Scriptable('_LSP', dependencyURL); } return } ////////////////////////////////////////////////////////// console.log(`----------------------------------------`); const remoteDependencyExit = fm.fileExists(`${cacheDir}/_LSP.js`); console.log(`🚀 RELEASE依赖文件${remoteDependencyExit ? '已存在 ✅' : '不存在 🚫'}`); console.log(`----------------------------------------`); // ------------------------------ if (!remoteDependencyExit) { // 下载依赖 // 创建根目录 if (!fm.fileExists(cacheDir)) { fm.createDirectory(cacheDir, true); } // 下载 console.log('🤖 RELEASE开始下载依赖~'); console.log(`----------------------------------------`); const req = new Request(dependencyURL); const moduleJs = await req.load(); if (moduleJs) { fm.write(fm.joinPath(cacheDir, '/_LSP.js'), moduleJs); 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); }; // ================================================================================= // =================================================================================