From 4e91a54c084959762dd7127e901caec392f57196 Mon Sep 17 00:00:00 2001 From: kngxscn Date: Sun, 5 Mar 2023 00:44:18 +0800 Subject: [PATCH 1/3] =?UTF-8?q?Create=20=E3=80=8C=E6=BA=90=E7=A0=81?= =?UTF-8?q?=E3=80=8D=E5=BD=A9=E4=BA=91=E5=A4=A9=E6=B0=94-=E6=B8=A9?= =?UTF-8?q?=E5=BA=A6.js?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...1\346\260\224-\346\270\251\345\272\246.js" | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 "Scripts/\343\200\214\346\272\220\347\240\201\343\200\215\345\275\251\344\272\221\345\244\251\346\260\224-\346\270\251\345\272\246.js" diff --git "a/Scripts/\343\200\214\346\272\220\347\240\201\343\200\215\345\275\251\344\272\221\345\244\251\346\260\224-\346\270\251\345\272\246.js" "b/Scripts/\343\200\214\346\272\220\347\240\201\343\200\215\345\275\251\344\272\221\345\244\251\346\260\224-\346\270\251\345\272\246.js" new file mode 100644 index 0000000..2c0b335 --- /dev/null +++ "b/Scripts/\343\200\214\346\272\220\347\240\201\343\200\215\345\275\251\344\272\221\345\244\251\346\260\224-\346\270\251\345\272\246.js" @@ -0,0 +1,130 @@ +// Variables used by Scriptable. +// These must be at the very top of the file. Do not edit. +// icon-color: orange; icon-glyph: comments; +// +// iOS 桌面组件脚本 @「小件件」 +// 开发说明:请从 Widget 类开始编写,注释请勿修改 +// https://x.im3x.cn +// + +// 添加require,是为了vscode中可以正确引入包,以获得自动补全等功能 +if (typeof require === 'undefined') require = importModule +const { Base } = require("./「小件件」开发环境") + +// @组件代码开始 +class Widget extends Base { + /** + * 传递给组件的参数,可以是桌面 Parameter 数据,也可以是外部如 URLScheme 等传递的数据 + * @param {string} arg 自定义参数 + */ + constructor (arg) { + super(arg) + this.name = '彩云天气-温度' + this.desc = '「小件件」—— 原创精美实用小组件' + this.logo = 'https://docs.caiyunapp.com/img/favicon.ico' + } + + /** + * 渲染函数,函数名固定 + * 可以根据 this.widgetFamily 来判断小组件尺寸,以返回不同大小的内容 + */ + async render () { + const data = await this.getData() + // try { + // if (data["status"] !== "ok") { + // return this.renderFail(data['error'], true); + // } + // } catch (e) { + // return this.renderFail("数据解析失败") + // } + switch (this.widgetFamily) { + case 'large': + return await this.renderLarge(data["result"]["daily"]) + case 'medium': + return await this.renderMedium(data["result"]["daily"]) + default: + return await this.renderSmall(data["result"]["daily"]) + } + } + + async renderFail (msg, login = false) { + const w = new ListWidget() + w.addText("⚠️") + w.addSpacer(10) + const t = w.addText(msg) + t.textColor = Color.red() + t.font = Font.boldSystemFont(14) + w.url = login ? this.actionUrl('login') : this.actionUrl() + return w + } + + /** + * 渲染小尺寸组件 + */ + async renderSmall (data) { + let w = new ListWidget() + w.url = this.actionUrl('open-url') + await this.renderHeader(w, this.logo, this.name) + const t1 = w.addText("平均温度:" + data["temperature"][0]['avg'].toString()) + t1.font = Font.lightSystemFont(16) + const t2 = w.addText("最高温度:" + data["temperature"][0]['max'].toString()) + t2.font = Font.lightSystemFont(18) + t2.textColor = Color.red() + const t3 = w.addText("最低温度:" + data["temperature"][0]['min'].toString()) + t3.font = Font.lightSystemFont(18) + t3.textColor = Color.blue() + w.addSpacer() + return w + } + /** + * 渲染中尺寸组件 + */ + async renderMedium (data, num = 3) { + let w = new ListWidget() + await this.renderHeader(w, this.logo, this.name) + data['data'].slice(0, num).map(d => { + const cell = w.addStack() + cell.centerAlignContent() + const cell_box = cell.addStack() + cell_box.size = new Size(3, 15) + cell_box.backgroundColor = new Color('#ff837a', 0.6) + cell.addSpacer(10) + const cell_text = cell.addText(d['title']) + cell_text.font = Font.lightSystemFont(16) + cell.url = this.actionUrl("open-url", d['url']) + cell.addSpacer() + w.addSpacer(10) + }) + w.addSpacer() + return w + } + /** + * 渲染大尺寸组件 + */ + async renderLarge (data) { + return await this.renderMedium(data, 10) + } + + /** + * 获取数据函数,函数名可不固定 + */ + async getData () { + let gps = await Location.current() + const api = `https://api.caiyunapp.com/v2.6/TAkhjf8d1nlSlspN/${gps["longitude"]},${gps["latitude"]}/daily?dailysteps=1` + console.log(api) + return await this.httpGet(api) + } + + /** + * 自定义注册点击事件,用 actionUrl 生成一个触发链接,点击后会执行下方对应的 action + * @param {string} url 打开的链接 + */ + async actionOpenUrl () { + Safari.openInApp("https://h5.caiyunapp.com/h5", false) + } + +} +// @组件代码结束 + +const { Testing } = require("./「小件件」开发环境") +await Testing(Widget) \ No newline at end of file From 262ffb3b4f45a846a8b8a71f807d07fca18d6422 Mon Sep 17 00:00:00 2001 From: kngxscn Date: Sun, 5 Mar 2023 11:35:14 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=9F=A5=E7=9C=8B?= =?UTF-8?q?=E7=BB=BC=E5=90=88=E5=A4=A9=E6=B0=94=E4=BF=A1=E6=81=AF=EF=BC=88?= =?UTF-8?q?=E6=96=87=E5=AD=97=E6=8F=8F=E8=BF=B0=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...1\346\260\224-\346\270\251\345\272\246.js" | 76 ++++++++++++------- 1 file changed, 47 insertions(+), 29 deletions(-) diff --git "a/Scripts/\343\200\214\346\272\220\347\240\201\343\200\215\345\275\251\344\272\221\345\244\251\346\260\224-\346\270\251\345\272\246.js" "b/Scripts/\343\200\214\346\272\220\347\240\201\343\200\215\345\275\251\344\272\221\345\244\251\346\260\224-\346\270\251\345\272\246.js" index 2c0b335..f7e09dc 100644 --- "a/Scripts/\343\200\214\346\272\220\347\240\201\343\200\215\345\275\251\344\272\221\345\244\251\346\260\224-\346\270\251\345\272\246.js" +++ "b/Scripts/\343\200\214\346\272\220\347\240\201\343\200\215\345\275\251\344\272\221\345\244\251\346\260\224-\346\270\251\345\272\246.js" @@ -29,7 +29,10 @@ class Widget extends Base { * 可以根据 this.widgetFamily 来判断小组件尺寸,以返回不同大小的内容 */ async render () { - const data = await this.getData() + const gps = await Location.current() // 获取当前位置 + const dataOfTempature = await this.getDataOfTempature(gps) + const dataOfWeather = await this.getDataOfWeather(gps) + // try { // if (data["status"] !== "ok") { // return this.renderFail(data['error'], true); @@ -39,11 +42,11 @@ class Widget extends Base { // } switch (this.widgetFamily) { case 'large': - return await this.renderLarge(data["result"]["daily"]) + return await this.renderLarge(dataOfTempature["result"], dataOfWeather["result"]) case 'medium': - return await this.renderMedium(data["result"]["daily"]) + return await this.renderMedium(dataOfTempature["result"], dataOfWeather["result"]) default: - return await this.renderSmall(data["result"]["daily"]) + return await this.renderSmall(dataOfTempature["result"]) } } @@ -65,13 +68,13 @@ class Widget extends Base { let w = new ListWidget() w.url = this.actionUrl('open-url') await this.renderHeader(w, this.logo, this.name) - const t1 = w.addText("平均温度:" + data["temperature"][0]['avg'].toString()) + const t1 = w.addText("平均温度:" + data["daily"]["temperature"][0]['avg'].toString()) t1.font = Font.lightSystemFont(16) - const t2 = w.addText("最高温度:" + data["temperature"][0]['max'].toString()) - t2.font = Font.lightSystemFont(18) + const t2 = w.addText("最高温度:" + data["daily"]["temperature"][0]['max'].toString()) + t2.font = Font.lightSystemFont(16) t2.textColor = Color.red() - const t3 = w.addText("最低温度:" + data["temperature"][0]['min'].toString()) - t3.font = Font.lightSystemFont(18) + const t3 = w.addText("最低温度:" + data["daily"]["temperature"][0]['min'].toString()) + t3.font = Font.lightSystemFont(16) t3.textColor = Color.blue() w.addSpacer() return w @@ -79,42 +82,57 @@ class Widget extends Base { /** * 渲染中尺寸组件 */ - async renderMedium (data, num = 3) { + async renderMedium (data1, data2) { let w = new ListWidget() + w.url = this.actionUrl('open-url') await this.renderHeader(w, this.logo, this.name) - data['data'].slice(0, num).map(d => { - const cell = w.addStack() - cell.centerAlignContent() - const cell_box = cell.addStack() - cell_box.size = new Size(3, 15) - cell_box.backgroundColor = new Color('#ff837a', 0.6) - cell.addSpacer(10) - const cell_text = cell.addText(d['title']) - cell_text.font = Font.lightSystemFont(16) - cell.url = this.actionUrl("open-url", d['url']) - cell.addSpacer() - w.addSpacer(10) - }) - w.addSpacer() + + const tempatureResult = `温度:${data1["daily"]["temperature"][0]['max'].toString()}~${data1["daily"]["temperature"][0]['min'].toString()} 平均:${data1["daily"]["temperature"][0]['avg'].toString()}` + const tempatureText = w.addText(tempatureResult) + tempatureText.font = Font.lightSystemFont(14) + tempatureText.textColor = Color.blue() + + let alert_md = ''; + if (data2.alert.content.length > 0) { + alert_md += '天气预警 ⚠\n'; + data2.alert.content.map(a => { + alert_md += `${a.title}\n${a.description}`; + }); + } + let result = `提醒:${data2.minutely.description.trim()};${data2.hourly.description.trim()}。\n${alert_md}`; + + w.addSpacer(10) + let t = w.addText(result) + t.font = Font.lightSystemFont(14) return w } /** * 渲染大尺寸组件 */ - async renderLarge (data) { - return await this.renderMedium(data, 10) + async renderLarge (data1, data2) { + return await this.renderMedium(data1, data2) } /** - * 获取数据函数,函数名可不固定 + * 获取近两天天气数据 */ - async getData () { - let gps = await Location.current() + async getDataOfTempature (gps) { const api = `https://api.caiyunapp.com/v2.6/TAkhjf8d1nlSlspN/${gps["longitude"]},${gps["latitude"]}/daily?dailysteps=1` console.log(api) return await this.httpGet(api) } + /** + * 获取天气综合数据 + * @returns JSON对象 + */ + async getDataOfWeather (gps) { + let api = `https://api.caiyunapp.com/v2.5/TAkhjf8d1nlSlspN/${gps["longitude"]},${gps["latitude"]}/weather.json?alert=true` + let req = new Request(api) + let res = await req.loadJSON() + return res + } + /** * 自定义注册点击事件,用 actionUrl 生成一个触发链接,点击后会执行下方对应的 action * @param {string} url 打开的链接 From 97253af1eb90e500cea08499ccefe23de271f59d Mon Sep 17 00:00:00 2001 From: kngxscn Date: Sat, 11 Mar 2023 20:06:02 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E4=BD=8D=E7=BD=AE=E5=BC=82=E5=B8=B8=E6=97=B6=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\345\275\251\344\272\221\345\244\251\346\260\224.js" | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) rename "Scripts/\343\200\214\346\272\220\347\240\201\343\200\215\345\275\251\344\272\221\345\244\251\346\260\224-\346\270\251\345\272\246.js" => "Scripts/\343\200\214\346\272\220\347\240\201\343\200\215\345\275\251\344\272\221\345\244\251\346\260\224.js" (93%) diff --git "a/Scripts/\343\200\214\346\272\220\347\240\201\343\200\215\345\275\251\344\272\221\345\244\251\346\260\224-\346\270\251\345\272\246.js" "b/Scripts/\343\200\214\346\272\220\347\240\201\343\200\215\345\275\251\344\272\221\345\244\251\346\260\224.js" similarity index 93% rename from "Scripts/\343\200\214\346\272\220\347\240\201\343\200\215\345\275\251\344\272\221\345\244\251\346\260\224-\346\270\251\345\272\246.js" rename to "Scripts/\343\200\214\346\272\220\347\240\201\343\200\215\345\275\251\344\272\221\345\244\251\346\260\224.js" index f7e09dc..b43b106 100644 --- "a/Scripts/\343\200\214\346\272\220\347\240\201\343\200\215\345\275\251\344\272\221\345\244\251\346\260\224-\346\270\251\345\272\246.js" +++ "b/Scripts/\343\200\214\346\272\220\347\240\201\343\200\215\345\275\251\344\272\221\345\244\251\346\260\224.js" @@ -19,7 +19,7 @@ class Widget extends Base { */ constructor (arg) { super(arg) - this.name = '彩云天气-温度' + this.name = '彩云天气' this.desc = '「小件件」—— 原创精美实用小组件' this.logo = 'https://docs.caiyunapp.com/img/favicon.ico' } @@ -29,7 +29,12 @@ class Widget extends Base { * 可以根据 this.widgetFamily 来判断小组件尺寸,以返回不同大小的内容 */ async render () { - const gps = await Location.current() // 获取当前位置 + let gps = {"longitude":"116", "latitude":"39"} // 因为经常出现获取位置权限失败的情况,因此先默认一个经纬度 + try { + gps = await Location.current() // 获取当前位置 + } catch (e) { + console.error("获取位置异常", e) + } const dataOfTempature = await this.getDataOfTempature(gps) const dataOfWeather = await this.getDataOfWeather(gps)