index.js 17 KB

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