index.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // canvas 全局配置
  2. var util = require('../../../../utils/util.js');
  3. var api = require('../../../../config/api.js');
  4. var app = getApp();
  5. var context = null;// 使用 wx.createContext 获取绘图上下文 context
  6. var isButtonDown = false;
  7. var arrx = [];
  8. var arry = [];
  9. var arrz = [];
  10. var canvasw = 0;
  11. var canvash = 0;
  12. //获取系统信息
  13. wx.getSystemInfo({
  14. success: function (res) {
  15. canvasw = res.windowWidth;//设备宽度
  16. canvash = res.windowHeight;
  17. }
  18. });
  19. //注册页面
  20. Page({
  21. /**
  22. * 页面的初始数据
  23. */
  24. data: {
  25. hasWrited:false
  26. },
  27. canvasIdErrorCallback: function (e) {
  28. console.error(e.detail.errMsg)
  29. },
  30. canvasStart: function (event){
  31. isButtonDown = true;
  32. arrz.push(0);
  33. arrx.push(event.changedTouches[0].x);
  34. arry.push(event.changedTouches[0].y);
  35. //context.moveTo(event.changedTouches[0].x, event.changedTouches[0].y);
  36. },
  37. canvasMove: function (event) {
  38. if (isButtonDown) {
  39. arrz.push(1);
  40. arrx.push(event.changedTouches[0].x);
  41. arry.push(event.changedTouches[0].y);
  42. // context.lineTo(event.changedTouches[0].x, event.changedTouches[0].y);
  43. // context.stroke();
  44. // context.draw()
  45. };
  46. for (var i = 0; i < arrx.length; i++) {
  47. if (arrz[i] == 0) {
  48. context.moveTo(arrx[i], arry[i])
  49. } else {
  50. context.lineTo(arrx[i], arry[i])
  51. };
  52. };
  53. context.clearRect(0, 0, canvasw, canvash);
  54. context.setStrokeStyle('#000000');
  55. context.setLineWidth(4);
  56. context.setLineCap('round');
  57. context.setLineJoin('round');
  58. context.stroke();
  59. context.draw(false);
  60. },
  61. canvasEnd: function (event) {
  62. isButtonDown = false;
  63. },
  64. cleardraw: function () {
  65. //清除画布
  66. arrx = [];
  67. arry = [];
  68. arrz = [];
  69. context.clearRect(0, 0, canvasw, canvash);
  70. context.draw(true);
  71. },
  72. getimg: function(){
  73. if (arrx.length==0){
  74. wx.showModal({
  75. title: '提示',
  76. content: '签名内容不能为空!',
  77. showCancel: false
  78. });
  79. return false;
  80. };
  81. //生成图片
  82. wx.canvasToTempFilePath({
  83. canvasId: 'canvas',
  84. success: function (res) {
  85. console.log(res.tempFilePath);
  86. //存入服务器
  87. const uploadTask =wx.uploadFile({
  88. url: api.StorageUpload, //图片上传接口地址
  89. filePath: res.tempFilePath,
  90. name: 'file',
  91. success: function (_res) {
  92. console.log(_res);
  93. var _res = JSON.parse(_res.data);
  94. if (_res.errno === 0) { //图片上传成功 执行数据更新
  95. console.log( _res.data.url );
  96. util.request(api.DisCommissionSign,{
  97. picurl:_res.data.url
  98. }).then(res=>{
  99. if(res.errno===0){
  100. wx.showToast({
  101. title: "签名成功",
  102. icon: 'success',
  103. duration: 2000,
  104. complete: function () {
  105. wx.navigateTo({
  106. url: "/pages/ucenter/dis/index"
  107. })
  108. }
  109. })
  110. } else{
  111. wx.showModal({
  112. title: '错误',
  113. content: '签名失败 请重签',
  114. showCancel: false
  115. })
  116. }
  117. })
  118. }
  119. },
  120. fail: function (res) {
  121. wx.showModal({
  122. title: '错误',
  123. content: '上传失败',
  124. showCancel: false
  125. })
  126. }
  127. });
  128. uploadTask.onProgressUpdate((res) => {
  129. console.log('上传进度', res.progress)
  130. console.log('已经上传的数据长度', res.totalBytesSent)
  131. console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
  132. })
  133. }
  134. })
  135. },
  136. /**
  137. * 生命周期函数--监听页面加载
  138. */
  139. onLoad: function (options) {
  140. // 使用 wx.createContext 获取绘图上下文 context
  141. context = wx.createCanvasContext('canvas');
  142. //context.drawImage("/static/images/bk.jpg",0,0,wx.getSystemInfoSync().windowWidth,wx.getSystemInfoSync().windowHeight-40)
  143. //context.draw();
  144. context.beginPath()
  145. context.setStrokeStyle('#000000');
  146. context.setLineWidth(4);
  147. context.setLineCap('round');
  148. context.setLineJoin('round');
  149. }
  150. })