fullSignture.js
6.08 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
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
// import { commonApi, equityApi } from '../../../api/api'
import { uploadImg } from '~/api/api'
// import { quotationUpgrade, updateOrderStatus } from '../../../api'
Page({
data: {
tempFilePath: '',
hideModal: true,
hasDraw: false,
canvasName: '#handWriting',
ctx: '',
canvasWidth: 0,
canvasHeight: 0,
startPoint: {
x: 0,
y: 0
},
selectColor: 'black',
lineColor: '#282829', // 颜色
lineSize: 3, // 笔记倍数
radius: 1.5, //画圆的半径
showPlaceHoder: true,
signImage: ''
},
onRoute(options) {
let id = options.id || 10
let orderId = options.orderId || ''
this.setData({
id,
orderId
})
},
onLoad(options) {
let { windowWidth, windowHeight } = wx.getSystemInfoSync()
if (options.writeOffCode) {
this.writeOffCode = options.writeOffCode
}
let that = this
let query = wx.createSelectorQuery().in(this) //获取自定义组件的SelectQuery对象
this.canvasCtx = wx.createCanvasContext('signature', that)
// 设置线的样式
this.canvasCtx.setFillStyle('white')
this.canvasCtx.fillRect(0, 0, windowWidth, windowHeight)
this.canvasCtx.setLineCap('round')
this.canvasCtx.setLineJoin('round')
// 初始化颜色
this.canvasCtx.setStrokeStyle(that.data.selectColor)
query
.select('.modal-canvas')
.boundingClientRect(rect => {
this.setData({
canvasWidth: rect.width,
canvasHeight: rect.height
})
})
.exec()
},
scaleStart(event) {
if (event.type != 'touchstart') return false
let currentPoint = {
x: event.touches[0].x,
y: event.touches[0].y
}
this.drawCircle(currentPoint)
this.setData({
startPoint: currentPoint,
hasDraw: true,
showPlaceHoder: false
})
},
scaleMove(event) {
if (event.type != 'touchmove') return false
let { startPoint } = this.data
let currentPoint = {
x: event.touches[0].x,
y: event.touches[0].y
}
this.drawLine(startPoint, currentPoint)
this.setData({
startPoint: currentPoint
})
},
scaleEnd(e) {
this.setData({
isStart: true
})
},
drawCircle(point) {
//这里负责点
let ctx = this.canvasCtx
ctx.beginPath()
// ctx.fillStyle = this.data.lineColor;
ctx.setFillStyle(this.data.lineColor)
//笔迹粗细由圆的大小决定
ctx.arc(point.x, point.y, this.data.radius, 0, 2 * Math.PI)
ctx.fill()
ctx.closePath()
ctx.draw(true)
},
drawLine(sourcePoint, targetPoint) {
let ctx = this.canvasCtx
this.drawCircle(targetPoint)
ctx.beginPath()
// ctx.strokeStyle = this.data.lineColor;
ctx.setStrokeStyle(this.data.lineColor)
// ctx.lineWidth = this.data.radius * 2; //这里乘2是因为线条的粗细要和圆的直径相等
ctx.setLineWidth(this.data.radius * 2)
ctx.moveTo(sourcePoint.x, sourcePoint.y)
ctx.lineTo(targetPoint.x, targetPoint.y)
ctx.stroke()
ctx.closePath()
},
clearCanvas() {
//清空画布
let ctx = this.canvasCtx
ctx.clearRect(0, 0, this.data.canvasWidth, this.data.canvasHeight)
ctx.fillStyle = 'rgba(0, 0, 0, .1)'
ctx.draw()
this.setData({
hasDraw: false, //未签字
showPlaceHoder: true,
signImage: ''
})
},
getTempFile() {
return {
tempFilePath: this.data.tempFilePath
}
},
saveToImage() {
let { hasDraw, canvasHeight, canvasWidth } = this.data
let that = this
if (!hasDraw) {
wx.showToast({
title: '还未签字哟!',
icon: 'none',
mask: true
})
return
}
wx.canvasToTempFilePath(
{
x: 0,
y: 0,
width: canvasWidth,
height: canvasHeight,
canvasId: 'signature',
fileType: 'png',
success(res) {
// //将图片保存到本地相册
// wx.saveImageToPhotosAlbum({
// filePath: res.tempFilePath,
// success(res) {
// // wx.$util.showToast('已将图片保存到本地相册', res)
// },
// fail(err) {
// wx.$util.showToast('失败', err)
// console.log(err)
// }
// })
if (res.tempFilePath) {
that.toUploadImage(res.tempFilePath)
}
},
fail(err) {
console.log(err)
}
},
that
)
},
toBack() {
wx.navigateBack({
delta: 1
})
},
// 上传图片
toUploadImage(tempFilePaths) {
let _this = this
const tempFiles = tempFilePaths
let imgType = tempFiles.split('.')[1]
let type = imgType == 'unknown' ? 'jpg' : imgType
wx.getFileSystemManager().readFile({
filePath: tempFiles, //选择图片返回的相对路径
encoding: 'base64', //编码格式
success(_res) {
wx.$util.showLoading('上传中...')
uploadImg({
file: `${type}@${_res.data}`
}).then(res => {
// _this.saveToHttp(img)
let { data } = res
// _this.saveToHttp(data)
// console.log(data)
wx.$router.back(page => {
page.setSignture && page.setSignture(data)
})
// _this.changeOrderStatus(data)
})
},
fail() {
wx.$util.hideLoading()
}
})
}
// changeOrderStatus(signUrl) {
// let { id } = this.data
// updateOrderStatus({ id, orderStatus: 5, customerNameUrl: signUrl })
// .then(result => {
// this.navigator()
// })
// .catch(err => {})
// },
// navigator() {
// let { orderId } = this.data
// wx.$router.go('serviceInfo', {
// orderId: orderId,
// apiName: 'handler'
// })
// },
// 上传签名到后端
// saveToHttp(signUrl) {
// let { id } = this.data
// let para = {
// id,
// customerNameUrl: signUrl,
// orderStatus: 2
// }
// quotationUpgrade(para).then(res => {
// wx.$util.hideLoading()
// let { code, data } = res
// code == 0 && wx.$router.redirectTo('CarQuotationPay', { orderId:data.orderId, id })
// })
// }
})