sign.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // pages/sign.js
  2. var context = null; // 使用 wx.createContext 获取绘图上下文 context
  3. var isButtonDown = false; //是否在绘制中
  4. var arrx = []; //动作横坐标
  5. var arry = []; //动作纵坐标
  6. var arrz = []; //总做状态,标识按下到抬起的一个组合
  7. var canvasw = 0; //画布宽度
  8. var canvash = 0; //画布高度
  9. Page({
  10. /**
  11. * 页面的初始数据
  12. */
  13. data: {
  14. canvasw: '',
  15. canvash: '',
  16. imgUrl: '',
  17. info: {},
  18. signBase64: '',
  19. sysType: '' // 判断机型
  20. },
  21. /**
  22. * 生命周期函数--监听页面加载
  23. */
  24. onLoad: function (options) {
  25. let that = this
  26. let res = wx.getSystemInfoSync()
  27. const system = res.system.split(' ')
  28. that.setData({
  29. sysType: system[0],
  30. })
  31. // let params = JSON.parse(options.params)
  32. // that.setData({
  33. // info: params,
  34. // })
  35. that.startCanvas();
  36. that.initCanvas()
  37. },
  38. /**
  39. * 以下 - 手写签名 / 上传签名
  40. */
  41. startCanvas() { //画布初始化执行
  42. var that = this;
  43. //获取系统信息
  44. wx.getSystemInfo({
  45. success: function (res) {
  46. canvasw = res.windowWidth;
  47. canvash = res.windowHeight;
  48. that.setData({
  49. canvasw: canvasw
  50. });
  51. that.setData({
  52. canvash: canvash
  53. });
  54. }
  55. });
  56. this.initCanvas();
  57. this.cleardraw();
  58. },
  59. //初始化函数
  60. initCanvas() {
  61. context = wx.createCanvasContext('canvas');
  62. context.beginPath()
  63. if (this.data.sysType === 'iOS') {
  64. context.fillStyle = 'rgba(255, 255, 255, 1)';
  65. context.setStrokeStyle('#444');
  66. } else {
  67. context.fillStyle = 'rgba(0, 0, 0, 1)';
  68. context.setStrokeStyle('#aaa');
  69. }
  70. context.setLineWidth(4);
  71. context.setLineCap('round');
  72. context.setLineJoin('round');
  73. },
  74. canvasStart(event) {
  75. isButtonDown = true;
  76. arrz.push(0);
  77. arrx.push(event.changedTouches[0].x);
  78. arry.push(event.changedTouches[0].y);
  79. },
  80. canvasMove(event) {
  81. if (isButtonDown) {
  82. arrz.push(1);
  83. arrx.push(event.changedTouches[0].x);
  84. arry.push(event.changedTouches[0].y);
  85. }
  86. for (var i = 0; i < arrx.length; i++) {
  87. if (arrz[i] == 0) {
  88. context.moveTo(arrx[i], arry[i])
  89. } else {
  90. context.lineTo(arrx[i], arry[i])
  91. }
  92. }
  93. context.clearRect(0, 0, canvasw, canvash);
  94. if (this.data.sysType === 'iOS') {
  95. context.fillStyle = 'rgba(255, 255, 255, 1)';
  96. context.setStrokeStyle('#444');
  97. } else {
  98. context.fillStyle = 'rgba(0, 0, 0, 1)';
  99. context.setStrokeStyle('#aaa');
  100. }
  101. context.setLineWidth(3);
  102. context.setLineCap('round');
  103. context.setLineJoin('round');
  104. context.stroke();
  105. context.draw(false);
  106. },
  107. canvasEnd(event) {
  108. isButtonDown = false;
  109. },
  110. //清除画布
  111. cleardraw() {
  112. arrx = [];
  113. arry = [];
  114. arrz = [];
  115. context.clearRect(0, 0, canvasw, canvash);
  116. if (this.data.sysType === 'iOS') {
  117. context.fillStyle = 'rgba(255, 255, 255, 1)';
  118. context.setStrokeStyle('#444');
  119. } else {
  120. context.fillStyle = 'rgba(0, 0, 0, 1)';
  121. context.setStrokeStyle('#aaa');
  122. }
  123. context.draw(true);
  124. },
  125. uploadImg() {
  126. var that = this
  127. //生成图片
  128. setTimeout(() => {
  129. wx.canvasToTempFilePath({
  130. canvasId: 'canvas',
  131. //设置输出图片的宽高
  132. fileType: 'jpg',
  133. quality: 1,
  134. success: function (res) {
  135. // canvas图片地址 res.tempFilePath
  136. // let imgBase64 = wx.getFileSystemManager().readFileSync(res.tempFilePath, 'base64')
  137. // that.setData({
  138. // imgUrl: res.tempFilePath,
  139. // signBase64: imgBase64
  140. // })
  141. // that.submitSign()
  142. // console.log('imgBase64', 'data:image/jpeg;base64,' + imgBase64)
  143. wx.saveImageToPhotosAlbum({
  144. filePath: res.tempFilePath,
  145. success(res4) {
  146. console.log(res,'保存res4');
  147. wx.showToast( {
  148. title: '已成功保存到相册',
  149. duration: 2000
  150. } );
  151. }
  152. })
  153. },
  154. fail: function () {
  155. wx.showModal({
  156. title: '提示',
  157. content: 'canvas生成图片失败。微信当前版本不支持,请更新到最新版本!',
  158. showCancel: false
  159. });
  160. },
  161. complete: function () {}
  162. }, 5000)
  163. })
  164. },
  165. // 提交签名
  166. submitSign() {
  167. let that = this
  168. wx.showLoading({
  169. title: '正在提交...',
  170. mask: true
  171. })
  172. let type = '1'
  173. if (that.data.sysType === 'iOS') {
  174. type = '0'
  175. } else {
  176. type = '1'
  177. }
  178. wx.$getWxLoginCode(resp => {
  179. const params = {
  180. qmbase64: that.data.signBase64,
  181. }
  182. console.info("入参", params)
  183. wx.kservice.yyyurl(params, res => {
  184. wx.hideLoading()
  185. if (res.statusCode === '200') {
  186. wx.showModal({
  187. title: '提示',
  188. content: res.message,
  189. showCancel: false,
  190. confirmText: '返回首页',
  191. success(res) {
  192. if (res.confirm) {
  193. wx.reLaunch({
  194. url: '/pages/index/index'
  195. })
  196. }
  197. }
  198. })
  199. } else {
  200. wx.showModal({
  201. title: '提示',
  202. content: res.message,
  203. showCancel: true,
  204. cancelText: '返回首页',
  205. confirmText: '重新提交',
  206. success: (result) => {
  207. if (result.cancel) {
  208. // 取消停留
  209. wx.reLaunch({
  210. url: '/pages/index/index'
  211. })
  212. } else if (result.confirm) {
  213. //重新提交
  214. that.submitSign()
  215. }
  216. },
  217. });
  218. }
  219. }, {}, true, true)
  220. })
  221. },
  222. /**
  223. * 生命周期函数--监听页面初次渲染完成
  224. */
  225. onReady: function () {
  226. },
  227. /**
  228. * 生命周期函数--监听页面显示
  229. */
  230. onShow: function () {
  231. },
  232. /**
  233. * 生命周期函数--监听页面隐藏
  234. */
  235. onHide: function () {
  236. },
  237. /**
  238. * 生命周期函数--监听页面卸载
  239. */
  240. onUnload: function () {
  241. },
  242. /**
  243. * 页面相关事件处理函数--监听用户下拉动作
  244. */
  245. onPullDownRefresh: function () {
  246. },
  247. /**
  248. * 页面上拉触底事件的处理函数
  249. */
  250. onReachBottom: function () {
  251. },
  252. /**
  253. * 用户点击右上角分享
  254. */
  255. onShareAppMessage: function () {
  256. }
  257. })