-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
399 lines (343 loc) · 9.56 KB
/
index.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
397
398
399
/**
* 引入API
*/
const {API} = require('../../utils/api.js')
const TodayPoem = require('../../utils/thridApi/jinrishici.js')
/**
* 获取app对象
*/
const app = getApp()
/**
* 定义全局的配置变量
*/
const { ajaxData, href, text, thridApi} = app.globalData.config
/**
* 获取设备的屏幕的 宽度 - 高度 - 分辨率
*/
const { screenWidth, screenHeight, pixelRatio} = wx.getSystemInfoSync()
/**
* 初始化Page对象
*/
Page({
/**
* data信息
*/
data: {
topBgUrl: '', //顶部背景图URL
copyright: '',//版权信息
todayData: {}, //todayData当天的数据信息
isCopyrightHidden: true //显示版权信息
},
/**
* 加载的时候获取图片信息 获取今天的date 据
*/
onLoad: function () {
this.fetchImgData()
this.fetchTodayData()
//this.fetchPoem()
},
/**
* 超时处理
*/
onReady: function () {
setTimeout(() => {
this.setData({
isCopyrightHidden: false
})
}, 1000)
},
/**
* 分享的配置
*/
onShareAppMessage: function () {
return {
title: text.shareTitle,
path: '/pages/index/index'
}
},
/**
* 获取图片数据
*/
fetchImgData: function() {
/**
* 异步获取数据入参
* getTopImg : 顶部的图片Url
* topBgUrl : 顶部图片的地址
* copyright: 版权信息
*/
API(ajaxData.getTopImg, data => {
if (data.images.length > 0) { //存在图片的时候
/**
* 解析出来这里面的数据 然后拼接起来 请求访问
* 第一个图片[0]
* 第一个图片对应的版权信息
*/
this.setData({
topBgUrl: `https://cn.bing.com${data.images[0].url}`,
copyright: data.images[0].copyright,
thridApiUrl: thridApi.poemApiUrl
})
//console.log(this.data)
}else{ //如果不存在图片 -- or 解析异常 采用默认的方式设置几个图片
/**
* topBgUrl - 默认的图片地址
*/
this.setData({
topBgUrl: `https://www.baidu.com`, //可以采用默认的assets对应的文件地址
copyright: 'JasonInternational@LTD,.CD',
})
}
})
},
/**
* 获取诗句
*/
fetchPoem:function(){
TodayPoem.load(result => {
console.log(result)
try {
const slogan = wx.getStorageSync('slogan') //标号
} catch (e) {
console.log(e)
}
/**
* 今日诗词
*/
this.setData({ "jinrishici": result.data.content })
})
},
/**
* 获取当前的数据
*/
fetchTodayData: function() {
/**
* 获取当天的数据 -- 年月日 宜or忌 API数据结构
* {
* "avoid": {
* "msg": "搞学习"
* },
* "day": 02,
* "lunar": "You are what you want to be.",
* "month": 01,
* "slogan": "勿忘初心,方得始终!",
* "suitable": {
* "msg": "睡觉觉"
* },
* "time": 1546424942685,
* "week": "星期一",
* "year": "2019"
* }
*/
API(ajaxData.today, data => {
/**
* reqData:
* url: 'https://dev.xdooi.com/today',
* data:{ }
*/
/**
* 如果有数据返回
*/
if (data) {
var data = data.result; // data.result
try {
//const slogan = wx.getStorageSync('slogan') //标号
const suitable = wx.getStorageSync('suitable') //宜
const avoid = wx.getStorageSync('avoid')//忌
const time = wx.getStorageSync('time')//时间
//const testDate = wx.getStorageSync('testDate')
/**
* 当前的时间 == 请求的时间
*/
if (time === data.time) {
data.slogan = slogan
data.suitable = suitable
data.avoid = avoid
}
this.setData({
todayData:data
})
} catch (e) {
/**
* 打印异常信息
*/
console.log(e)
}
/**
* 将数据缓存下来
*/
try {
wx.setStorageSync('slogan', data.slogan)
wx.setStorageSync('suitable', data.suitable)
wx.setStorageSync('avoid', data.avoid)
wx.setStorageSync('time', data.time)
// wx.setStorageSync('testDate', data.testDate)
} catch (e) {
console.log(e)
}
}
/**
* 生成的分享的图片 数据
*/
// this.generalShareImg(this.data.todayData)
})
},
/**
* 预览图片
*/
bindImageTap: function() {
wx.previewImage({
urls: [this.data.topBgUrl],
})
},
/**
* generalShareImg -- 生成分享图片
*/
bindShareTap: function() {
this.generalShareImg(this.data.todayData)
},
/**
* generalShareImg
*/
generalShareImg: function(data) {
/**
* createCanvasContext 利用Canvas来生成分享图片.
*/
const ctx = wx.createCanvasContext('share-canvas', this)
/**
* 展示生成图片中
*/
wx.showLoading({
title: '佛系小吴生成图片中...',
mask: true
})
/**
* Promise
*/
new Promise(resolve => {
/**
* 获取image信息
* src: href.topImg : topImg: 'https://dev.xdooi.com/bingimage/bgTop.jpg'
*/
wx.getImageInfo({
src: href.topImg,
success: res => {
/**
* 画图 -- screenWidth 屏幕宽度
*/
ctx.drawImage(res.path, 0, 0, screenWidth, 210)
resolve()
}
})
}).then(() => new Promise(resolve => {
ctx.rect(0, 210, screenWidth, 470)
ctx.setFillStyle('#fff')
ctx.fill() //刷流
ctx.beginPath()
ctx.rect(20, 156, screenWidth*0.893, 409)
ctx.setFillStyle('#fff')
ctx.setShadow(0, 5, 25, 'rgba(0, 42, 110, 0.15)')
ctx.fill()
ctx.setFontSize(15)
ctx.setTextAlign('center')
ctx.setFillStyle('#444')
ctx.fillText(`${data.year}年${data.month}月`, screenWidth / 2, 195)
ctx.setFontSize(12)
ctx.setTextAlign('center')
ctx.setFillStyle('#444')
ctx.fillText(data.lunar, screenWidth / 2, 215)
ctx.setFontSize(100)
ctx.setTextAlign('center')
ctx.setFillStyle('#444')
ctx.fillText(data.day, screenWidth / 2, 315)
ctx.setFontSize(15)
ctx.setTextAlign('center')
ctx.setFillStyle('#444')
ctx.fillText(data.week, screenWidth / 2, 350)
ctx.drawImage('/assets/quo-left.png', 45, 390, 11, 11)
const lineNum = screenWidth > 320 ? 14 : 11
/**
* 如果标号的长度>lineNum
*/
if (data.slogan.length > lineNum) {
/**
* 对标语进行文字处理
*/
const text1 = data.slogan.substring(0, lineNum)
const text2Temp = data.slogan.substring(lineNum, data.slogan.length)
const text2 = text2Temp.length > lineNum ? `${text2Temp.substring(0, lineNum)}...` : text2Temp
ctx.setFontSize(17)
ctx.setTextAlign('center')
ctx.setFillStyle('#333')
ctx.fillText(text1, screenWidth / 2, 405, 247)
ctx.setFontSize(17)
ctx.setTextAlign('center')
ctx.setFillStyle('#333')
ctx.fillText(text2, screenWidth / 2, 432, 247)
} else {
ctx.fillText(data.slogan, screenWidth / 2, 405, 247)
}
ctx.drawImage('/assets/quo-right.png', screenWidth * 0.85, 408, 11, 11)
ctx.beginPath()
ctx.rect(47.5, 465, screenWidth * 0.747, 1)
ctx.setFillStyle('#C7C7C7')
ctx.fill()
ctx.beginPath()
ctx.rect(47.5, 468, screenWidth * 0.747, 0.5)
ctx.setFillStyle('#C7C7C7')
ctx.fill()
ctx.drawImage('/assets/yi-icon.png', 60, 492, 30, 30)
ctx.beginPath()
ctx.rect(100, 505, 4, 4)
ctx.setFillStyle('#4B4B4B')
ctx.fill()
ctx.setFontSize(screenWidth > 320 ? 17 : 14)
ctx.setFillStyle('#333')
ctx.fillText(data.suitable, 110 + (data.suitable.length * 17) / 2, 513)
ctx.drawImage('/assets/ji-icon.png', screenWidth > 320 ? 210 : 190, 492, 30, 30)
ctx.beginPath()
ctx.rect(screenWidth > 320 ? 250 : 230, 505, 4, 4)
ctx.setFillStyle('#4B4B4B')
ctx.fill()
ctx.setFontSize(screenWidth > 320 ? 17 : 14)
ctx.setFillStyle('#333')
ctx.fillText(data.avoid, (screenWidth > 320 ? 260 : 235) + (data.avoid.length * 17) / 2, 513)
ctx.drawImage('/assets/qrcode.png', screenWidth / 2 - 40, 575, 80, 80)
ctx.setFontSize(12)
ctx.setTextAlign('center')
ctx.setFillStyle('#333')
ctx.fillText(text.shareTitle2, screenWidth / 2, 670)
ctx.draw()
setTimeout(() => resolve(), 1000) //设置超时1000ms
})).then(() => {
//隐藏加载loading Tips
wx.hideLoading()
wx.canvasToTempFilePath({
canvasId: 'share-canvas',
x: 0,
y: 0,
width: screenWidth,
height: 736,
success: res => {
/**
* tempFilePath 图片指向一个临时的文件路径 -- 保存到相册
*/
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success(res) {
wx.showToast({
title: '生成图片成功',
duration: 2000
})
}
})
},
fail: () => {
wx.showToast({
title: '生成图片失败',
duration: 2000
})
}
}, this)
})
}
})