小程序-canvas签字功能
WXML
<view class="qianzi"> <view class="paper"> <canvas class="handWriting" disable-scroll="true" bindtouchstart="touchstart1" bindtouchmove="touchmove1" canvas-id="handWriting1"> </canvas> </view> <view class="signBtn"> <button class="btn1" size="" bindtap="sign1ok">完成签字</button> <button class="btn2" size="" bindtap="reSign1">清除签名</button> </view> </view> <view class="image" hidden="{{src?false:true}}"> <image src="{{src}}"></image> </view>
JS
data: {
optionsTaskid: '', //接收到的任务id
orderKey: '',
context1: null,
hasDraw: false, //默认没有画
src: null,
},
touchstart1: function (e) { var context1 = this.data.context1; context1.moveTo(e.touches[0].x, e.touches[0].y); this.setData({ context1: context1, hasDraw: true, //要签字了 }); }, touchmove1: function (e) { var x = e.touches[0].x; var y = e.touches[0].y; var context1 = this.data.context1; context1.setLineWidth(3); context1.lineTo(x, y); context1.stroke(); context1.setLineCap('round'); context1.draw(true); context1.moveTo(x, y); }, reSign1: function () { //重新画 var that = this; var context1 = that.data.context1; context1.draw(); //清空画布 that.setData({ hasDraw: false, //没有画 src: null }); },
sign1ok: function () {
var that = this;
if (!that.data.hasDraw) {
return app.Tips({
title: "您还未签名"
})
};
var context1 = that.data.context1;
context1.draw(true, wx.canvasToTempFilePath({
canvasId: 'handWriting1',
success(res) {
console.log("res:", res)
console.log(res.tempFilePath) //得到了图片下面自己写上传吧
let Url = getApp().globalData.url,
header = HEADER;
console.log("Url:", Url)
if (getApp().globalData.token) header[TOKENNAME] = 'Bearer ' + getApp().globalData.token;
wx.uploadFile({
url: Url + "/api/task/upload/contract",
filePath: res.tempFilePath,
name: "file",
formData: {
file: "image",
key: that.data.orderKey
},
header: {
"Content-Type": "multipart/form-data",
[TOKENNAME]: 'Bearer ' + getApp().globalData.token
},
success: function (result) {
console.log(result)
var data = JSON.parse(result.data)
if (data.status == 200) {
app.Tips({
title: data.msg
});
setTimeout(function () {
wx.redirectTo({
url: '/pages/shouerlige/taskOrder_pay/index?key=' + that.data.orderKey + "&taskid=" + that.data.optionsTaskid,
})
}, 1200)
}else{
return app.Tips({title:data.msg})
}
},
fail:function(err){
}
})
}
}))
},
分类:
小程序1
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具