takeBill.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import {
  2. uploadFile
  3. } from "../../utils/upload"
  4. Page({
  5. data: {
  6. avatar: "https://6875-huihenduo-2gx127w7f837b584-1255802371.tcb.qcloud.la/miniapp-static/avatar-icon.jpg?sign=f5c66c94d189436f82353eb48cb01f08&t=1634538864",
  7. cameraIcon: "https://6875-huihenduo-2gx127w7f837b584-1255802371.tcb.qcloud.la/miniapp-static/camera-icon.png?sign=11a65871a2800cd04ecaa8991687fccd&t=1634607415",
  8. newCameraIcon: "https://6875-huihenduo-2gx127w7f837b584-1255802371.tcb.qcloud.la/miniapp-static/camera.png?sign=ad0fe8bead6a46cb20f45f792d4bed67&t=1645502416",
  9. userName: "",
  10. phone: "",
  11. shipName: "",
  12. shipMmsi: "",
  13. authModal: false,
  14. modalText: "位置",
  15. cw: '',
  16. ch: '',
  17. disabled: false,
  18. imgCache:''
  19. },
  20. takeBill() {
  21. if (this.data.disabled) return
  22. wx.showLoading({
  23. title: '正在上传...',
  24. })
  25. this.data.disabled = true
  26. const ctx = wx.createCameraContext()
  27. ctx.takePhoto({
  28. quality: 'high',
  29. success: (res) => {
  30. let src = res.tempImagePath
  31. let ch = res.height * 2
  32. let cw = res.width * 2
  33. wx.getImageInfo({
  34. src,
  35. success: e => {
  36. this.setData({
  37. ch,
  38. cw
  39. })
  40. const query = wx.createSelectorQuery();
  41. query.select('#myCanvas').node().exec((res) => {
  42. const canvas = res[0].node;
  43. const ctx = canvas.getContext('2d');
  44. const img = canvas.createImage();
  45. let h = ch / 2
  46. let w = cw / 2
  47. canvas.height = h * 0.85
  48. canvas.width = w * 0.9
  49. img.src = src
  50. img.onload = () => {
  51. ctx.drawImage(img, -w * 0.05, -h * 0.05);
  52. }
  53. this.saveImg(src)
  54. })
  55. }
  56. })
  57. }
  58. })
  59. },
  60. goBack() {
  61. wx.redirectTo({
  62. url: '/pages/takePhoto/takePhoto',
  63. })
  64. },
  65. async saveImg(src) {
  66. const query = wx.createSelectorQuery();
  67. const canvas = await new Promise((resolve, reject) => {
  68. query.select('#myCanvas')
  69. .fields({
  70. node: true,
  71. size: true
  72. })
  73. .exec(async (res) => {
  74. resolve(res[0].node);
  75. })
  76. });
  77. wx.canvasToTempFilePath({
  78. canvas,
  79. success: async e => {
  80. // let path = e.tempFilePath
  81. // wx.navigateTo({
  82. // url: `/pages/takeBill/success/success?src=${src}&path=${path}`
  83. // })
  84. // return
  85. this.setData({
  86. imgCache:e.tempFilePath
  87. })
  88. let res = await uploadFile(e.tempFilePath, {
  89. type: 0,
  90. userId: wx.getStorageSync('userId'),
  91. // userId: 89,
  92. location: ''
  93. })
  94. wx.hideLoading({
  95. success: (res) => {},
  96. })
  97. wx.showToast({
  98. title: res.msg,
  99. })
  100. setTimeout(() => {
  101. this.data.disabled = false
  102. this.goBack()
  103. }, 1000)
  104. },
  105. fail(res) {
  106. console.log('fail');
  107. }
  108. })
  109. },
  110. onLoad() {
  111. let userName = wx.getStorageSync('userName')
  112. let userId = wx.getStorageSync('userId')
  113. let phone = wx.getStorageSync('phone')
  114. let shipId = wx.getStorageSync('shipId')
  115. this.setData({
  116. userName,
  117. phone,
  118. userId,
  119. shipId
  120. })
  121. }
  122. })