-
Notifications
You must be signed in to change notification settings - Fork 241
Expand file tree
/
Copy path「源码」彩云天气.js
More file actions
153 lines (139 loc) · 4.97 KB
/
「源码」彩云天气.js
File metadata and controls
153 lines (139 loc) · 4.97 KB
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
// 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 () {
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)
// 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(dataOfTempature["result"], dataOfWeather["result"])
case 'medium':
return await this.renderMedium(dataOfTempature["result"], dataOfWeather["result"])
default:
return await this.renderSmall(dataOfTempature["result"])
}
}
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["daily"]["temperature"][0]['avg'].toString())
t1.font = Font.lightSystemFont(16)
const t2 = w.addText("最高温度:" + data["daily"]["temperature"][0]['max'].toString())
t2.font = Font.lightSystemFont(16)
t2.textColor = Color.red()
const t3 = w.addText("最低温度:" + data["daily"]["temperature"][0]['min'].toString())
t3.font = Font.lightSystemFont(16)
t3.textColor = Color.blue()
w.addSpacer()
return w
}
/**
* 渲染中尺寸组件
*/
async renderMedium (data1, data2) {
let w = new ListWidget()
w.url = this.actionUrl('open-url')
await this.renderHeader(w, this.logo, this.name)
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 (data1, data2) {
return await this.renderMedium(data1, data2)
}
/**
* 获取近两天天气数据
*/
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 打开的链接
*/
async actionOpenUrl () {
Safari.openInApp("https://h5.caiyunapp.com/h5", false)
}
}
// @组件代码结束
const { Testing } = require("./「小件件」开发环境")
await Testing(Widget)