-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunpalash.js
398 lines (384 loc) · 14 KB
/
unpalash.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// unsplash v1.2
// unsplash.js使用unsplash官方api
// api限制每小时50次请求
// author:extrastu
// site:blog.extrastu.xin
// 为了更好的图片体验加载的是full规格的图片,可能加载的有点慢,请见谅
let appID = '17805ef4205f7051084afdf56296a3811b0d98deb7cf68688554bc54562cf222'
let page = 1, perPage = 12, photos = [], currIndex = 0, res = '', searchTxt = '';
const sizes = [$size(550, 850), $size(1000, 665), $size(1024, 689), $size(640, 427),]
const CN_MENU = ['最新', '精选', '搜索']
const dw = $device.info.screen.width
const dh = $device.info.screen.height
let h = 250
$ui.render({
props: {
title: "unsplash",
navButtons: [{
title: "支持",
icon: "103",
handler: () => {
$ui.menu({
items: ["越花越有礼❤", "请我喝杯咖啡❤"],
handler: (title, idx) => {
switch (idx) {
case 0:
$clipboard.text = '小伙伴们,给大家发红包喽!领完上App Store绑定支付宝就能用。6zKICt25e8 用完还能赢4999元购物津贴!#吱口令#长按复制此消息,打开支付宝就能领取!';
$ui.alert({
title: "支持",
message: "apple store红包已复制到剪切板\n打开支付宝即可使用\n\n感谢您的支持❤"
})
break;
case 1:
$app.openURL("https://www.buymeacoffee.com/extra")
break;
}
}
})
}
}]
},
views: [
//loading入场动画
{
type: "image",
props: {
src: "http://oz53lzns9.bkt.clouddn.com/18-8-23/41250009.jpg"
},
layout: (make, view) => {
make.height.equalTo(view.super);
make.width.equalTo(view.super);
}
},
{
type: "label",
props: {
id: "header",
alpha: 0,
},
layout: function (make, view) {
make.height.equalTo(50)
make.width.equalTo(view.super);
},
views: [
{
type: "image",
props: {
src: "http://oz53lzns9.bkt.clouddn.com/18-8-4/86764088.jpg"
},
layout: (make, view) => {
make.height.equalTo(view.super);
make.width.equalTo(view.super);
}
},
{
type: "label",
props: {
text: "Photos for everyone",
font: $font("ChalkboardSE-Light", 35),
color: $color("#FFF")
},
layout: function (make, view) {
make.center.equalTo(view.super)
}
}
]
},
{
type: "matrix",
props: {
id: "listView",
columns: 2,
spacing: 10,
selectable: true,
waterfall: true,
square: false,
alpha: 0,
template: [
{
type: "image",
props: {
id: "image",
smoothRadius: 10,
info: "unsplash"
},
layout: $layout.fill
},
]
},
layout: function (make) {
make.left.bottom.right.equalTo(0)
make.top.equalTo(0)
},
events: {
itemSize: (sender, indexPath) => {
return sizes[indexPath.item % 4]
},
didSelect: function (sender, indexPath, object) {
h = 10
$ui.push({
props: {
title: "image"
},
views: [{
type: "image",
props: {
src: object.image.src,
scale: 0.8
},
layout: (make, view) => {
make.width.equalTo
},
events: {
tapped: function (sender) {
$http.download({
url: object.image.src,
handler: function (resp) {
$share.universal(resp.data)
$app.tips("下载完成")
}
})
}
}
}]
})
},
didReachBottom: function (sender) {
$ui.toast("请求数据中...")
$device.taptic(1)
page++;
if (currIndex == 0) {
fetchData(page, perPage, "https://api.unsplash.com/photos")
} else if (currIndex == 1) {
fetchData(page, perPage, "https://api.unsplash.com/collections/featured")
}
$delay(1.5, function () {
sender.endFetchingMore()
})
}
}
}
,
{
type: "menu",
props: {
id: "menuView",
items: CN_MENU,
alpha: 0,
},
layout: (make, view) => {
make.bottom.inset(0)
make.height.equalTo(44);
make.width.equalTo(view.super);
},
events: {
changed: function (sender) {
let items = sender.items
let index = sender.index
currIndex = index
photos.splice(0, photos.length)
if (index == 0) {
fetchData(page, perPage, "https://api.unsplash.com/photos")
} else if (index == 1) {
fetchData(page, perPage, "https://api.unsplash.com/collections/featured")
} else if (index == 2) {
searchPhotoView()
}
}
}
},
]
})
function searchPhotoView() {
$ui.push({
type: "view",
props: {
title: "搜索",
bgcolor: $color("#008080"),
},
views: [
{
type: "input",
props: {
id: "searchInput",
bgcolor: $color("#fff"),
placeholder: "输入图片/作者/类名进行搜索",
clearsOnBeginEditing: true,
font: $font(18),
radius: 0
},
layout: (make, view) => {
make.top.left.right.inset(0)
make.height.equalTo(35)
},
events: {
returned(sender) {
sender.blur()
if (sender.text) {
searchTxt = sender.text
$device.taptic(0);
$ui.toast("正在为你查找中...")
fetchData(page, perPage, 'https://api.unsplash.com/search/collections?query=' + sender.text)
} else {
sender.text = "输入图片/作者/类名进行搜索"
}
}
}
},
{
type: "label",
props: {
id: "photosCount",
bgcolor: $color("#fff"),
font: $font("bold", 30),
textColor: $color("#008080"),
autoFontSize: true
},
layout: function (make, view) {
make.top.equalTo($("searchInput").bottom).offset(1)
make.height.equalTo(35)
make.width.equalTo(view.super);
}
},
{
type: "matrix",
props: {
id: "searchListView",
columns: 1,
spacing: 10,
selectable: true,
waterfall: true,
square: false,
bgcolor: $color("#eeeeee"),
footer: {
type: "label",
props: {
id: "searchListFooter",
height: 50,
text: "加载中...",
textColor: $color("#008080"),
align: $align.center,
font: $font(12)
}
},
template: [
{
type: "image",
props: {
id: "searchRes",
smoothRadius: 10,
},
layout: $layout.fill
}
]
},
layout: (make, view) => {
make.top.equalTo($("photosCount").bottom)
make.bottom.inset(0)
make.left.right.inset(0)
},
events: {
itemSize: (sender, indexPath) => {
return sizes[indexPath.item % 4]
},
didSelect: function (sender, indexPath, object) {
console.log(object)
$ui.push({
props: {
title: "image"
},
views: [{
type: "image",
props: {
src: object.searchRes.src
},
layout: $layout.fill,
events: {
tapped: (sender) => {
$http.download({
url: object.searchRes.src,
handler: function (resp) {
$share.universal(resp.data)
$app.tips("下载完成")
}
})
}
}
}]
})
},
didReachBottom: (sender) => {
$ui.toast("请求数据中...")
$device.taptic(1)
page++;
fetchData(page, perPage, 'https://api.unsplash.com/search/collections?query=' + searchTxt)
$delay(1.5, function () {
sender.endFetchingMore()
})
}
}
}
]
})
fetchData(page, perPage, 'https://api.unsplash.com/search/collections?query=city')
$("photosCount").text = "本周热搜";
}
function fetchData(page, perPage, reqUrl) {
if (currIndex == 2) {
var url = reqUrl + "&client_id=" + appID + "&page=" + page
} else {
var url = reqUrl + "?client_id=" + appID + "&page=" + page + "&per_page=" + perPage
}
$ui.loading(true)
$http.get({
url: url,
handler: (resp) => {
$ui.loading(false)
photos = photos.concat(resp.data)
console.log(photos.length)
render(photos)
}
})
}
function render(data) {
if (data == "") {
console.log("暂未匹配到结果")
$ui.alert("暂未匹配到结果")
return false
} else {
if (currIndex == 2) {
data.map((item) => {
$("searchListView").data = item.results.map((i) => {
return { searchRes: { src: i.cover_photo.urls.regular } }
})
})
} else {
$("listView").data = data.map(function (item) {
$ui.animate({
duration: .4,
animation: () => {
$("listView").alpha = 1
$("menuView").alpha = 1
}
});
if (currIndex == 0) {
return { image: { src: item.urls.regular } }
} else if (currIndex == 1) {
return { image: { src: item.cover_photo.urls.regular } }
}
})
}
}
}
function fetchNetworkType() {
let networkType = $device.networkType
if (networkType == 0) {
$ui.alert("您当前无网络,请检查后再试")
} else if (networkType == 1) {
$ui.toast("您正在使用wifi网络")
} else {
$ui.toast("您正在使用蜂窝数据网络")
}
}
fetchNetworkType()
fetchData(page, perPage, "https://api.unsplash.com/photos")