/
schedule.js
366 lines (325 loc) · 9.73 KB
/
schedule.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
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var days = ["一", "二", "三", "四", "五", "六", "日"];
var dayMap = {
1: "一",
2: "二",
3: "三",
4: "四",
5: "五",
6: "六",
7: "日"
};
var firstColWidth = 25;
var header = {
size: 10,
height: 50,
bg: "#f5f5f5",
color: "#555",
lineColor: "#d0d0d0"
};
var colHeader = {
size: 10,
width: 25,
bg: "#f5f5f5",
color: "#555",
lineColor: "#d0d0d0"
};
var courseStyle = {
color: "#fff",
size: 10,
top: 5
// title set
};var titleStyle = {
size: 20,
height: 50,
color: "#000",
bg: "#f5f5f5"
};
/**
* 微信小程序Canvas课程表,用于展示或者是分享图
* @class Schdule
*/
var Schedule = function () {
/**
* 对象初始化
* @param {Object} options 参数
*/
function Schedule(options) {
var _this = this;
_classCallCheck(this, Schedule);
// 必要参数,canvas id,长宽
["id", "width", "height"].forEach(function (e) {
_this.optionCheck(e, options);
}, this);
this.title = options.title || '';
// 不必要参数
// 日期
this.days = options.days || days;
// 标题,第一列,第一行样式设置
this.titleStyle = options.titleStyle || titleStyle;
this.colHeader = options.colHeader || colHeader;
this.header = options.header || header;
// 一天多少节课
this.sessions = options.sessions || 13;
// 课程宽度
this.courseWidth = options.courseWidth || (this.width - this.colHeader.width) / this.days.length;
if (options.courseHeight) {
this.courseHeight = options.courseHeight;
} else if (this.title != '') {
this.courseHeight = (this.height - this.titleStyle.height - this.header.height) / this.sessions;
} else {
this.courseHeight = (this.height - this.header.height) / this.sessions;
}
// 设置首行文字大小
if (!('header' in options) || !options.header) {
this.header = header;
this.header.size = this.courseWidth / 2 - 10;
}
// 课程样式
this.courseStyle = options.courseStyle || courseStyle;
// 获取canvas对象
this.ctx = wx.createCanvasContext(this.id);
//
this.startY = 0;
//
this.options = options;
}
/**
* 参数校验
* @param {String} props 属性
* @param {Object} option 参数
*/
_createClass(Schedule, [{
key: "optionCheck",
value: function optionCheck(props, option) {
if (!(props in option)) {
console.error("缺少" + props);
throw "缺少" + props;
}
this[props] = option[props];
}
/**
* 绘制课程表
* courses = [
* {
* name: '', // 课程名,
* address: '', // 课程地址,
* bg: '#000', //课程背景色,
* sessionArr: [1,2,3], // 上课节次
* day: 1, // 周几上课
* }
* ]
* @param {Array} courses 课程表
*/
}, {
key: "drawSchedule",
value: function drawSchedule(courses) {
var _this2 = this;
var ctx = this.ctx;
if (this.title) {
this.drawTitle();
}
// 绘制第一行
this.drawHeader();
this.startY += this.header.height;
// 第一列
this.drawFirstCol();
// 获得课程,绘制课程
courses.forEach(function (course) {
_this2.drawCourse(course);
});
ctx.draw();
}
/**
* 保存临时图片到相册
* @param {String} path
*/
}, {
key: "saveImage",
value: function saveImage(path) {
return new Promise(function (resolve, reject) {
wx.saveImageToPhotosAlbum({
filePath: path,
success: function success(res) {
return resolve(res);
},
fail: function fail(res) {
return reject(res);
}
});
});
}
/**
* canvas 转临时图片
* @return path
*/
}, {
key: "canvasToImg",
value: function canvasToImg() {
var id = this.id;
return new Promise(function (resolve, reject) {
wx.canvasToTempFilePath({
canvasId: id,
success: function success(res) {
return resolve(res.tempFilePath);
},
fail: function fail(res) {
return reject(res);
}
});
});
}
}, {
key: "drawTitle",
value: function drawTitle() {
var ctx = this.ctx;
// 绘制标题
ctx.setFillStyle(this.header.bg);
ctx.fillRect(0, 0, this.width, this.titleStyle.height);
ctx.beginPath();
ctx.moveTo(0, this.titleStyle.height);
ctx.lineTo(this.width, this.titleStyle.height);
ctx.setStrokeStyle(this.header.lineColor);
ctx.stroke();
ctx.closePath();
this.drawText(ctx, this.title, (this.width - this.courseWidth) / 2, this.startY, 20, titleStyle);
this.startY += this.titleStyle.height;
}
/**
* 绘制背景色
* @param {String} bg 背景色
*/
}, {
key: "drawBg",
value: function drawBg() {
var bg = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "#f8f8f5";
this.ctx.rect(0, 0, this.width, this.height);
this.ctx.setFillStyle(bg);
this.ctx.fill();
}
/**
* 绘制首行
*/
}, {
key: "drawHeader",
value: function drawHeader() {
var ctx = this.ctx;
ctx.setFillStyle(this.header.bg);
ctx.fillRect(0, this.startY, this.width, this.header.height);
ctx.setStrokeStyle(this.header.lineColor);
ctx.setLineWidth(1);
ctx.setTextAlign("center");
ctx.setFillStyle(this.header.color);
ctx.setFontSize(this.header.size);
for (var i = 0; i < this.days.length; i++) {
ctx.beginPath();
ctx.moveTo(this.colHeader.width + i * this.courseWidth, this.startY);
ctx.lineTo(this.colHeader.width + i * this.courseWidth, this.startY + this.header.height);
ctx.stroke();
ctx.closePath();
ctx.fillText("周" + days[i], this.colHeader.width + this.courseWidth / 2 + i * this.courseWidth, this.startY + (this.header.height + this.header.size) / 2);
}
}
/**
* 绘制第一列
*/
}, {
key: "drawFirstCol",
value: function drawFirstCol() {
var ctx = this.ctx;
ctx.setFillStyle(this.header.bg);
ctx.fillRect(0, this.startY, this.colHeader.width, this.height);
ctx.setStrokeStyle(this.header.lineColor);
ctx.setLineWidth(1);
ctx.setTextAlign("center");
ctx.setFillStyle(this.header.color);
ctx.setFontSize(this.header.size);
for (var i = 0; i <= this.sessions; i++) {
ctx.beginPath();
ctx.moveTo(0, this.courseHeight * i + this.startY);
ctx.lineTo(this.colHeader.width, this.courseHeight * i + this.startY);
ctx.stroke();
ctx.closePath();
if (i == this.sessions) continue;
ctx.fillText(i + 1, this.colHeader.width / 2, this.courseHeight * (i + 0.5) + this.startY + this.header.size / 2);
}
}
/**
* 绘制课程
* @param {Object} course 课程
*/
}, {
key: "drawCourse",
value: function drawCourse(course) {
var ctx = this.ctx;
// 定位
var x = this.colHeader.width + this.getCourseDayX(course.day) * this.courseWidth;
var y = (course.sessionArr[0] - 1) * this.courseHeight + this.startY;
ctx.setFillStyle(course.bg);
// 绘制背景
ctx.fillRect(x, y, this.courseWidth, this.courseHeight * course.sessionArr.length);
// 绘制文字
y = y + this.courseStyle.top;
y = this.drawText(ctx, course.name, x, y, 4, this.courseStyle);
this.drawText(ctx, "@" + course.address, x, y, 4, this.courseStyle);
}
/**
* 获取课程的偏移位置
* @param {Number} day 周几,1 = 周一
*/
}, {
key: "getCourseDayX",
value: function getCourseDayX(day) {
for (var i = 0; i < this.days.length; i++) {
if (this.days[i] === dayMap[day]) {
return i;
}
}
throw "课程时间和设置不匹配!";
}
}, {
key: "drawText",
value: function drawText(ctx, str, x, y, len, style) {
str = str.trim();
ctx.setFillStyle(style.color);
ctx.setTextAlign("center");
ctx.setFontSize(style.size);
for (var i = 0; i < str.length / len; i++) {
y = y + style.size * 1.5;
ctx.fillText(str.substr(i * len, len), x + this.courseWidth / 2, y);
}
return y;
}
}, {
key: "download",
value: function download(url) {
return new Promise(function (resolve, reject) {
wx.downloadFile({
url: url,
success: function success(res) {
if (res.statusCode === 200) {
resolve(res.tempFilePath);
} else {
reject(res);
}
},
fail: function fail(res) {
return reject(res);
}
});
});
}
}, {
key: "log",
value: function log() {
console.log(this.options);
}
}]);
return Schedule;
}();
exports.default = Schedule;