index.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. // pages/sign-name/sign-name.js
  2. import api from '../api/api'
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. attachmentids: [],
  9. canvasName: 'signCanvas',
  10. ctx: '', // 上下文实例
  11. canvas: '', // 画布实例
  12. canvasWidth: 0,
  13. canvasHeight: 0,
  14. transparent: 1, // 透明度
  15. selectColor: 'black',
  16. lineColor: '#1A1A1A', // 颜色
  17. lineSize: 1.5, // 笔记倍数
  18. lineMin: 0.5, // 最小笔画半径
  19. lineMax: 4, // 最大笔画半径
  20. pressure: 1, // 默认压力
  21. smoothness: 60, //顺滑度,用60的距离来计算速度
  22. currentPoint: {},
  23. currentLine: [], // 当前线条
  24. firstTouch: true, // 第一次触发
  25. radius: 1, //画圆的半径
  26. cutArea: {
  27. top: 0,
  28. right: 0,
  29. bottom: 0,
  30. left: 0
  31. }, //裁剪区域
  32. bethelPoint: [], //保存所有线条 生成的贝塞尔点;
  33. lastPoint: 0,
  34. chirography: [], //笔迹
  35. currentChirography: {}, //当前笔迹
  36. linePrack: [], //划线轨迹 , 生成线条的实际点
  37. isDraw: false, // 判断用户是否绘制
  38. upPageNumber: 0,
  39. offCanvas: '',
  40. offCtx: '',
  41. dpr: ''
  42. },
  43. /**
  44. * 生命周期函数--监听页面加载
  45. */
  46. onLoad: function (options) {
  47. this.setData({
  48. id: options.id
  49. })
  50. this.data.dpr = wx.getSystemInfoSync().pixelRatio
  51. const query = wx.createSelectorQuery()
  52. query
  53. .select('.handCenter')
  54. .boundingClientRect(rect => {
  55. this.data.canvasWidth = rect.width * this.data.dpr
  56. this.data.canvasHeight = rect.height * this.data.dpr
  57. this.setData({
  58. canvasWidth:this.data.canvasWidth,
  59. canvasHeight:this.data.canvasHeight
  60. })
  61. })
  62. .exec()
  63. const query1 = wx.createSelectorQuery()
  64. query1
  65. .select('#signCanvas')
  66. .fields({
  67. node: true,
  68. size: true
  69. })
  70. .exec(res => {
  71. const canvas = res[0].node
  72. this.data.ctx = canvas.getContext('2d')
  73. canvas.width = this.data.canvasWidth
  74. canvas.height = this.data.canvasHeight
  75. /* 将canvas背景设置为 白底,不设置 导出的canvas的背景为透明 */
  76. // console.log(this, 'hahah');
  77. this.data.canvas = canvas
  78. this.data.ctx.scale(this.data.dpr, this.data.dpr)
  79. this.setCanvasBg('#fff')
  80. })
  81. const query2 = wx.createSelectorQuery()
  82. query2
  83. .select('#offCanvas')
  84. .fields({
  85. node: true,
  86. size: true
  87. })
  88. .exec(res => {
  89. const canvas = res[0].node
  90. const ctx = canvas.getContext('2d')
  91. canvas.width = this.data.canvasHeight
  92. canvas.height = this.data.canvasWidth
  93. this.data.offCanvas = canvas
  94. this.data.offCtx = ctx
  95. this.data.offCtx.scale(this.data.dpr, this.data.dpr)
  96. })
  97. },
  98. /**
  99. * 生命周期函数--监听页面初次渲染完成
  100. */
  101. onReady: function () {
  102. },
  103. /**
  104. * 生命周期函数--监听页面显示
  105. */
  106. onShow: function () {
  107. },
  108. // 重写事件(初始化画布)
  109. retDraw() {
  110. this.data.ctx.clearRect(0, 0, 700, 730)
  111. //设置canvas背景
  112. this.setCanvasBg('#fff')
  113. this.data.isDraw = false
  114. },
  115. // 完成事件
  116. subCanvas() {
  117. // 调用预览
  118. console.log('========sub')
  119. this.previewCanvasImg()
  120. console.log('========subOver')
  121. },
  122. //画两点之间的线条;参数为:line,会绘制最近的开始的两个点;
  123. pointToLine(line) {
  124. this.calcBethelLine(line)
  125. return
  126. },
  127. // 笔迹开始
  128. uploadScaleStart(e) {
  129. if (e.type != 'touchstart') return false
  130. let ctx = this.data.ctx
  131. ctx.fillStyle = this.data.lineColor // 初始线条设置颜色
  132. ctx.globalAlpha = this.data.transparent // 设置半透明
  133. let currentPoint = {
  134. x: e.touches[0].x,
  135. y: e.touches[0].y
  136. }
  137. let currentLine = this.data.currentLine
  138. currentLine.unshift({
  139. time: new Date().getTime(),
  140. dis: 0,
  141. x: currentPoint.x,
  142. y: currentPoint.y
  143. })
  144. this.setData({
  145. currentPoint
  146. // currentLine
  147. })
  148. if (this.data.firstTouch) {
  149. this.setData({
  150. cutArea: {
  151. top: currentPoint.y,
  152. right: currentPoint.x,
  153. bottom: currentPoint.y,
  154. left: currentPoint.x
  155. },
  156. firstTouch: false
  157. })
  158. }
  159. this.pointToLine(currentLine)
  160. },
  161. // 笔迹移动
  162. uploadScaleMove(e) {
  163. if (e.type != 'touchmove') return false
  164. if (e.cancelable) {
  165. // 判断默认行为是否已经被禁用
  166. if (!e.defaultPrevented) {
  167. e.preventDefault()
  168. }
  169. }
  170. let point = {
  171. x: e.touches[0].x,
  172. y: e.touches[0].y
  173. }
  174. //测试裁剪
  175. if (point.y < this.data.cutArea.top) {
  176. this.data.cutArea.top = point.y
  177. }
  178. if (point.y < 0) this.data.cutArea.top = 0
  179. if (point.x > this.data.cutArea.right) {
  180. this.data.cutArea.right = point.x
  181. }
  182. if (this.data.canvasWidth - point.x <= 0) {
  183. this.data.cutArea.right = this.data.canvasWidth
  184. }
  185. if (point.y > this.data.cutArea.bottom) {
  186. this.data.cutArea.bottom = point.y
  187. }
  188. if (this.data.canvasHeight - point.y <= 0) {
  189. this.data.cutArea.bottom = this.data.canvasHeight
  190. }
  191. if (point.x < this.data.cutArea.left) {
  192. this.data.cutArea.left = point.x
  193. }
  194. if (point.x < 0) this.data.cutArea.left = 0
  195. this.setData({
  196. lastPoint: this.data.currentPoint,
  197. currentPoint: point
  198. })
  199. let currentLine = this.data.currentLine
  200. currentLine.unshift({
  201. time: new Date().getTime(),
  202. dis: this.distance(this.data.currentPoint, this.data.lastPoint),
  203. x: point.x,
  204. y: point.y
  205. })
  206. // this.setData({
  207. // currentLine
  208. // })
  209. this.pointToLine(currentLine)
  210. },
  211. // 笔迹结束
  212. uploadScaleEnd(e) {
  213. if (e.type != 'touchend') return 0
  214. let point = {
  215. x: e.changedTouches[0].x,
  216. y: e.changedTouches[0].y
  217. }
  218. this.setData({
  219. lastPoint: this.data.currentPoint,
  220. currentPoint: point
  221. })
  222. let currentLine = this.data.currentLine
  223. currentLine.unshift({
  224. time: new Date().getTime(),
  225. dis: this.distance(this.data.currentPoint, this.data.lastPoint),
  226. x: point.x,
  227. y: point.y
  228. })
  229. // this.setData({
  230. // currentLine
  231. // })
  232. if (currentLine.length > 2) {
  233. var info =
  234. (currentLine[0].time - currentLine[currentLine.length - 1].time) / currentLine.length
  235. //$("#info").text(info.toFixed(2));
  236. }
  237. //一笔结束,保存笔迹的坐标点,清空,当前笔迹
  238. //增加判断是否在手写区域;
  239. this.pointToLine(currentLine)
  240. var currentChirography = {
  241. lineSize: this.data.lineSize,
  242. lineColor: this.data.lineColor
  243. }
  244. var chirography = this.data.chirography
  245. chirography.unshift(currentChirography)
  246. this.setData({
  247. chirography
  248. })
  249. var linePrack = this.data.linePrack
  250. linePrack.unshift(this.data.currentLine)
  251. this.setData({
  252. linePrack,
  253. currentLine: []
  254. })
  255. this.data.isDraw = true
  256. },
  257. //计算插值的方式;
  258. calcBethelLine(line) {
  259. if (line.length <= 1) {
  260. line[0].r = this.data.radius
  261. return
  262. }
  263. let x0,
  264. x1,
  265. x2,
  266. y0,
  267. y1,
  268. y2,
  269. r0,
  270. r1,
  271. r2,
  272. len,
  273. lastRadius,
  274. dis = 0,
  275. time = 0,
  276. curveValue = 0.5
  277. if (line.length <= 2) {
  278. x0 = line[1].x
  279. y0 = line[1].y
  280. x2 = line[1].x + (line[0].x - line[1].x) * curveValue
  281. y2 = line[1].y + (line[0].y - line[1].y) * curveValue
  282. //x2 = line[1].x;
  283. //y2 = line[1].y;
  284. x1 = x0 + (x2 - x0) * curveValue
  285. y1 = y0 + (y2 - y0) * curveValue
  286. } else {
  287. x0 = line[2].x + (line[1].x - line[2].x) * curveValue
  288. y0 = line[2].y + (line[1].y - line[2].y) * curveValue
  289. x1 = line[1].x
  290. y1 = line[1].y
  291. x2 = x1 + (line[0].x - x1) * curveValue
  292. y2 = y1 + (line[0].y - y1) * curveValue
  293. }
  294. //从计算公式看,三个点分别是(x0,y0),(x1,y1),(x2,y2) ;(x1,y1)这个是控制点,控制点不会落在曲线上;实际上,这个点还会手写获取的实际点,却落在曲线上
  295. len = this.distance({
  296. x: x2,
  297. y: y2
  298. }, {
  299. x: x0,
  300. y: y0
  301. })
  302. lastRadius = this.data.radius
  303. for (let n = 0; n < line.length - 1; n++) {
  304. dis += line[n].dis
  305. time += line[n].time - line[n + 1].time
  306. if (dis > this.data.smoothness) break
  307. }
  308. this.setData({
  309. radius: Math.min((time / len) * this.data.pressure + this.data.lineMin, this.data.lineMax) *
  310. this.data.lineSize
  311. })
  312. line[0].r = this.data.radius
  313. //计算笔迹半径;
  314. if (line.length <= 2) {
  315. r0 = (lastRadius + this.data.radius) / 2
  316. r1 = r0
  317. r2 = r1
  318. //return;
  319. } else {
  320. r0 = (line[2].r + line[1].r) / 2
  321. r1 = line[1].r
  322. r2 = (line[1].r + line[0].r) / 2
  323. }
  324. let n = 5
  325. let point = []
  326. for (let i = 0; i < n; i++) {
  327. let t = i / (n - 1)
  328. let x = (1 - t) * (1 - t) * x0 + 2 * t * (1 - t) * x1 + t * t * x2
  329. let y = (1 - t) * (1 - t) * y0 + 2 * t * (1 - t) * y1 + t * t * y2
  330. let r = lastRadius + ((this.data.radius - lastRadius) / n) * i
  331. point.push({
  332. x: x,
  333. y: y,
  334. r: r
  335. })
  336. if (point.length == 3) {
  337. let a = this.ctaCalc(
  338. point[0].x,
  339. point[0].y,
  340. point[0].r,
  341. point[1].x,
  342. point[1].y,
  343. point[1].r,
  344. point[2].x,
  345. point[2].y,
  346. point[2].r
  347. )
  348. a[0].color = this.data.lineColor
  349. // let bethelPoint = this.data.bethelPoint;
  350. // console.log(a)
  351. // console.log(this.data.bethelPoint)
  352. // bethelPoint = bethelPoint.push(a);
  353. this.bethelDraw(a, 1)
  354. point = [{
  355. x: x,
  356. y: y,
  357. r: r
  358. }]
  359. }
  360. }
  361. this.setData({
  362. currentLine: line
  363. })
  364. },
  365. //求两点之间距离
  366. distance(a, b) {
  367. let x = b.x - a.x
  368. let y = b.y - a.y
  369. return Math.sqrt(x * x + y * y)
  370. },
  371. ctaCalc(x0, y0, r0, x1, y1, r1, x2, y2, r2) {
  372. let a = [],
  373. vx01,
  374. vy01,
  375. norm,
  376. n_x0,
  377. n_y0,
  378. vx21,
  379. vy21,
  380. n_x2,
  381. n_y2
  382. vx01 = x1 - x0
  383. vy01 = y1 - y0
  384. norm = Math.sqrt(vx01 * vx01 + vy01 * vy01 + 0.0001) * 2
  385. vx01 = (vx01 / norm) * r0
  386. vy01 = (vy01 / norm) * r0
  387. n_x0 = vy01
  388. n_y0 = -vx01
  389. vx21 = x1 - x2
  390. vy21 = y1 - y2
  391. norm = Math.sqrt(vx21 * vx21 + vy21 * vy21 + 0.0001) * 2
  392. vx21 = (vx21 / norm) * r2
  393. vy21 = (vy21 / norm) * r2
  394. n_x2 = -vy21
  395. n_y2 = vx21
  396. a.push({
  397. mx: x0 + n_x0,
  398. my: y0 + n_y0,
  399. color: '#1A1A1A'
  400. })
  401. a.push({
  402. c1x: x1 + n_x0,
  403. c1y: y1 + n_y0,
  404. c2x: x1 + n_x2,
  405. c2y: y1 + n_y2,
  406. ex: x2 + n_x2,
  407. ey: y2 + n_y2
  408. })
  409. a.push({
  410. c1x: x2 + n_x2 - vx21,
  411. c1y: y2 + n_y2 - vy21,
  412. c2x: x2 - n_x2 - vx21,
  413. c2y: y2 - n_y2 - vy21,
  414. ex: x2 - n_x2,
  415. ey: y2 - n_y2
  416. })
  417. a.push({
  418. c1x: x1 - n_x2,
  419. c1y: y1 - n_y2,
  420. c2x: x1 - n_x0,
  421. c2y: y1 - n_y0,
  422. ex: x0 - n_x0,
  423. ey: y0 - n_y0
  424. })
  425. a.push({
  426. c1x: x0 - n_x0 - vx01,
  427. c1y: y0 - n_y0 - vy01,
  428. c2x: x0 + n_x0 - vx01,
  429. c2y: y0 + n_y0 - vy01,
  430. ex: x0 + n_x0,
  431. ey: y0 + n_y0
  432. })
  433. a[0].mx = a[0].mx.toFixed(1)
  434. a[0].mx = parseFloat(a[0].mx)
  435. a[0].my = a[0].my.toFixed(1)
  436. a[0].my = parseFloat(a[0].my)
  437. for (let i = 1; i < a.length; i++) {
  438. a[i].c1x = a[i].c1x.toFixed(1)
  439. a[i].c1x = parseFloat(a[i].c1x)
  440. a[i].c1y = a[i].c1y.toFixed(1)
  441. a[i].c1y = parseFloat(a[i].c1y)
  442. a[i].c2x = a[i].c2x.toFixed(1)
  443. a[i].c2x = parseFloat(a[i].c2x)
  444. a[i].c2y = a[i].c2y.toFixed(1)
  445. a[i].c2y = parseFloat(a[i].c2y)
  446. a[i].ex = a[i].ex.toFixed(1)
  447. a[i].ex = parseFloat(a[i].ex)
  448. a[i].ey = a[i].ey.toFixed(1)
  449. a[i].ey = parseFloat(a[i].ey)
  450. }
  451. return a
  452. },
  453. bethelDraw(point, is_fill, color) {
  454. let ctx = this.data.ctx
  455. ctx.beginPath()
  456. ctx.moveTo(point[0].mx, point[0].my)
  457. if (undefined != color) {
  458. ctx.fillStyle = color
  459. ctx.strokeStyle = color
  460. } else {
  461. ctx.fillStyle = point[0].color
  462. ctx.strokeStyle = point[0].color
  463. }
  464. for (let i = 1; i < point.length; i++) {
  465. ctx.bezierCurveTo(
  466. point[i].c1x,
  467. point[i].c1y,
  468. point[i].c2x,
  469. point[i].c2y,
  470. point[i].ex,
  471. point[i].ey
  472. )
  473. }
  474. ctx.stroke()
  475. if (undefined != is_fill) {
  476. ctx.fill() //填充图形 ( 后绘制的图形会覆盖前面的图形, 绘制时注意先后顺序 )
  477. }
  478. },
  479. //设置canvas背景色 不设置 导出的canvas的背景为透明
  480. //@params:字符串 color
  481. setCanvasBg(color) {
  482. console.log('开始设置背景色')
  483. /* 将canvas背景设置为 白底,不设置 导出的canvas的背景为透明 */
  484. //rect() 参数说明 矩形路径左上角的横坐标,左上角的纵坐标, 矩形路径的宽度, 矩形路径的高度
  485. //这里是 canvasHeight - 4 是因为下边盖住边框了,所以手动减了写
  486. this.data.ctx.rect(0, 0, this.data.canvasWidth, this.data.canvasHeight - 4)
  487. this.data.ctx.fillStyle = color
  488. this.data.ctx.fill() //设置填充
  489. },
  490. //预览
  491. async previewCanvasImg() {
  492. this.data.offCtx.rotate(-90 * Math.PI / 180);
  493. // offCtx.clearRect(0, 0, 100, 100)
  494. const srcImg = this.data.offCanvas.createImage()
  495. // 等待图片加载
  496. // console.log(this.data.canvas.toDataURL('image/png', 1))
  497. await new Promise(resolve => {
  498. srcImg.onload = resolve
  499. srcImg.src = this.data.canvas.toDataURL('image/png', 1) // 要加载的图片 url
  500. })
  501. let width = this.data.canvasWidth / this.data.dpr
  502. let height = this.data.canvasHeight / this.data.dpr
  503. this.data.offCtx.drawImage(srcImg, -width, 0, width, height)
  504. wx.canvasToTempFilePath({
  505. canvas: this.data.offCtx,
  506. fileType: 'jpg',
  507. quality: 1, //图片质量
  508. success(res) {
  509. console.log(res.tempFilePath, 'canvas生成图片地址')
  510. wx.previewImage({
  511. urls: [res.tempFilePath] //预览图片 数组
  512. })
  513. }
  514. })
  515. },
  516. //上传
  517. async uploadCanvasImg() {
  518. let that = this
  519. if (!this.data.isDraw) {
  520. wx.showToast({
  521. title: '请签名',
  522. icon: 'error'
  523. })
  524. return
  525. }
  526. // console.log('===============')
  527. const self = this
  528. this.data.offCtx.rotate(-90 * Math.PI / 180);
  529. // offCtx.clearRect(0, 0, 100, 100)
  530. const srcImg = this.data.offCanvas.createImage()
  531. // 等待图片加载
  532. // console.log(this.data.canvas.toDataURL('image/png', 1))
  533. await new Promise(resolve => {
  534. srcImg.onload = resolve
  535. srcImg.src = this.data.canvas.toDataURL('image/png', 1) // 要加载的图片 url
  536. console.log("await============")
  537. })
  538. console.log("await0============")
  539. console.log(srcImg)
  540. let width = this.data.canvasWidth / this.data.dpr
  541. let height = this.data.canvasHeight / this.data.dpr
  542. this.data.offCtx.drawImage(srcImg, -width, 0, width, height)
  543. console.log('===============2')
  544. console.log(srcImg)
  545. wx.canvasToTempFilePath({
  546. canvas: this.data.offCanvas,
  547. fileType: 'jpg', // png默认无背景是透明的,可以改jpg
  548. quality: 1, //图片质量
  549. success(res) {
  550. console.log(res)
  551. wx.getFileSystemManager().readFile({
  552. filePath: res.tempFilePath,
  553. success: result => {
  554. //返回临时文件路径
  555. console.log(res)
  556. const fileData = result.data;
  557. api._post({
  558. "classname": "system.attachment.huawei.OBS",
  559. "method": "getFileName",
  560. "content": {
  561. "filename": '客户签字',
  562. "filetype": 'jpg',
  563. "parentid": wx.getStorageSync('siteP').appfolderid
  564. }
  565. }).then(res => {
  566. if (res.msg == "成功") {
  567. that.uploadFile(res.data, fileData)
  568. }
  569. })
  570. },
  571. fail: console.error
  572. })
  573. },
  574. fail(res) {
  575. console.log(res)
  576. }
  577. })
  578. },
  579. uploadFile(res, data) {
  580. var that = this;
  581. wx.request({
  582. url: res.uploadurl,
  583. method: "PUT",
  584. data: data,
  585. header: {
  586. 'content-type': 'application/octet-stream'
  587. },
  588. success(a) {
  589. api._post({
  590. "classname": "system.attachment.huawei.OBS",
  591. "method": "uploadSuccess",
  592. "content": {
  593. "serialfilename": res.serialfilename
  594. }
  595. }).then(rs => {
  596. that.data.attachmentids.push(rs.data.attachmentids[0])
  597. that.filebindData()
  598. }).catch(err => {
  599. console.log(err)
  600. })
  601. }
  602. })
  603. },
  604. async filebindData() {
  605. let pages = getCurrentPages()[getCurrentPages().length - 2]
  606. const res = await api._post({
  607. "classname": "system.attachment.Attachment",
  608. "method": "createFileLink",
  609. "content": {
  610. "ownertable": pages.data.bindSignNameData.ownertable,
  611. "ownerid": pages.data.bindSignNameData.ownerid,
  612. "usetype": "signature",
  613. "attachmentids": this.data.attachmentids
  614. }
  615. })
  616. wx.navigateBack()
  617. },
  618. })