tools.js
6.4 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
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
export default {
async checkImgHttp(imgPath, type) {
let res = await this.getImageInfo_PromiseFc(imgPath)
let newPath = '';
if (!res) {
uni.hideLoading()
switch (type) {
case 'avatar':
console.log('%cerr:海报头像图片错误: ' + imgPath, 'color:green;background:yellow');
break;
case 'bgImage':
console.log('%cerr:海报背景图片错误: ' + imgPath, 'color:green;background:yellow');
break;
case 'wxCode':
console.log('%cerr:海报微信二维码图片错误: ' + imgPath, 'color:green;background:yellow');
break;
case 'goodsImage':
console.log('%cerr:海报商品图片错误: ' + imgPath, 'color:green;background:yellow');
break;
default:
break;
}
} else {
let pathArr = imgPath.split('://');
// #ifdef H5
let ishttps = 'https:' == window.location.protocol ? true : false;
ishttps ? (pathArr[0] = 'https') : (pathArr[0] = 'http');
// #endif
// #ifdef MP-WEIXIN
pathArr[0] = 'https'
// #endif
newPath = pathArr.join('://');
}
return newPath;
},
// 检测图片是否可用
getImageInfo_PromiseFc(imgPath) {
return new Promise((rs, rj) => {
imgPath = this.checkMPUrl(imgPath);
uni.getImageInfo({
src: imgPath,
success: res => {
rs(1);
},
fail: err => {
console.log(err)
rs(0);
}
})
});
},
// 微信头像
checkMPUrl(url) {
// #ifdef MP
if (
url.substring(0, 4) === 'http' &&
url.substring(0, 5) !== 'https' &&
url.substring(0, 12) !== 'http://store' &&
url.substring(0, 10) !== 'http://tmp' &&
url.substring(0, 10) !== 'http://usr'
) {
url = 'https' + url.substring(4, url.length);
}
// #endif
return url;
},
/**
* 相对比例
* @param {Object} bgObj - 背景图片数据对象
* @param {Number} num - 需要计算的绘制模块数值
*/
getScale(bgObj, num = 0) {
return Number(bgObj.width * (num / bgObj.width).toFixed(2))
},
/**
* 绘制序列
* @param {Object} bgObj - 背景图片数据对象
* @param {Array} drawArr - 绘制元素数组对象
*/
initDrawArray(bgObj, drawArr) {
const that = this;
let arr = []
drawArr.forEach(item => {
switch (item.type) {
case 'text':
arr.push({
type: 'text',
text: item.text, //文本
size: that.getScale(bgObj, item.size) || 50, //大小
color: item.color || 'black', //颜色
alpha: item.alpha || 1, //透明度
textAlign: item.textAlign || 'left', //文字x对齐方式: 'left'、'middle'、'right'
textBaseline: item.textBaseline ||
'middle', //文字y对齐方式: 'top'、'bottom'、'middle'、'normal'
dx: that.getScale(bgObj, item.dx) || 0, //文字x轴位置
dy: that.getScale(bgObj, item.dy + item.size) || 0, //文字y轴位置
[item.lineFeed ? 'lineFeed' : '']: { //设置换行
maxWidth: item.lineFeed?.maxWidth && that.getScale(bgObj, item.lineFeed
.maxWidth) || bgObj
.width, //最大宽度
lineHeight: item.lineFeed?.lineHeight && that.getScale(bgObj, item
.lineFeed.lineHeight) || 50, //行高
lineNum: item.lineFeed?.lineNum || -1, // 最多行数,小于0为无限
dx: item.lineFeed?.dx || -1, //非第一行文字位置,小于0为默认位置
},
infoCallBack: (textLength) => { //回调函数,返回文本信息
if (item.isBgCenter) {
return {
dx: (bgObj.width - textLength) / 2,
}
}
return {}
},
[item.lineThrough ? 'lineThrough' : '']: { // 设置删除线,默认为字体样式
width: item.lineThrough?.width || item.size / 10,
style: item.lineThrough?.style || item.color,
alpha: item.lineThrough?.alpha || 1,
cap: item.lineThrough?.cap || 'butt',
},
fontStyle: item.fontStyle || 'normal', // 设置字体,跟随系统默认
fontVariant: item.fontVariant || 'normal',
fontWeight: item.fontWeight || 'normal',
fontFamily: item.fontFamily || 'sans-serif',
})
break;
case 'qrcode':
arr.push({
type: 'qrcode',
text: String(item.text || '') || '', // 生成内容
size: Number(that.getScale(bgObj, item.size) || 0) || 200, // 二维码大小
background: String(item.background || '') || '#ffffff', // 背景色
foreground: String(item.foreground || '') || '#000000', // 前景色
correctLevel: Number(item.correctLevel || 0) || 3, // 容错级别
image: String(item.image || '') || '', // 二维码图标
imageSize: Number(that.getScale(bgObj, item.imageSize) || 0) || 40, // 二维码图标大小
dx: item.isBgCenter ? (bgObj.width - item.size) / 2 : Number(that.getScale(
bgObj, item.dx) || 0) || 0,
dy: Number(that.getScale(bgObj, item.dy) || 0) || 0, // y轴距离
})
break;
case 'image':
arr.push({
type: 'image',
url: this.checkImgHttp(item.url, item.name), // 网络图片路径
dWidth: Number(that.getScale(bgObj, item.dWidth) || 0) || 100, //绘制图像的宽度,允许缩放
dHeight: Number(that.getScale(bgObj, item.dHeight) || 0) || 100, //绘制图像的高度,允许缩放
sWidth: Number(that.getScale(bgObj, item.sWidth) || 0) || 100, //绘制图像的宽度,允许缩放
sHeight: Number(that.getScale(bgObj, item.sHeight) || 0) || 100, //绘制图像的高度,允许缩放
dx: Number(that.getScale(bgObj, item.dx) || 0) || 0, // x轴距离
dy: Number(that.getScale(bgObj, item.dy) || 0) || 0, // y轴距离
sx: Number(that.getScale(bgObj, item.sx) || 0) || 0,
sy: Number(that.getScale(bgObj, item.sy) || 0) || 0,
[item.circleSet ? 'circleSet' : '']: { //圆形设置
r: Number(item.circleSet?.r && that.getScale(bgObj, item.circleSet
.r) || 0),
x: Number(item.circleSet?.x && that.getScale(bgObj, item.circleSet
.x) || 0),
y: Number(item.circleSet?.y && that.getScale(bgObj, item.circleSet
.y) || 0),
},
[item.roundRectSet ? 'roundRectSet' : '']: { //圆角设置
r: Number(item.roundRectSet?.r && that.getScale(bgObj, item
.roundRectSet.r) || 0),
},
infoCallBack: (imgInfo) => { //回调函数,返回图片信息
if (item.isBgCenter) {
return {
dx: (bgObj.width - item.dWidth) / 2,
}
}
},
})
break;
default:
_app.log('暂无次类型');
break;
}
})
return arr
}
}