signature.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <template>
  2. <view>
  3. <u--form :model="inputForm" labelWidth="100px" class="u-form" labelPosition="left" ref="inputForm">
  4. <u-form-item label="旧签名" borderBottom prop="signPic">
  5. <u--image :showLoading="true" :src="BASE_URL + inputForm.signPic " width="100px"
  6. height="40px"></u--image>
  7. </u-form-item>
  8. <u-form-item label="上传签字图片" borderBottom prop="signPic">
  9. <u-upload :fileList="fileList" @afterRead="afterRead" @delete="deletePic" :maxCount="1"></u-upload>
  10. </u-form-item>
  11. <u--text size="10" type="warning" text="有签名图片,请上传,无,请点击下面签字生成"></u--text>
  12. <u-form-item label="签字" borderBottom prop="signPic">
  13. <signInput ref="sign" canvasId="twoDrowCanvas" canvasIds="twoRotateCanvas" :header="header"
  14. :action="action" @signToUrl="signToUrl">
  15. </signInput>
  16. </u-form-item>
  17. <view class="padding-xl">
  18. <u-button type="primary" @click="formSubmit" text="提交"></u-button>
  19. </view>
  20. </u--form>
  21. </view>
  22. </template>
  23. <script>
  24. import userService from "@/api/sys/userService"
  25. import * as $auth from "@/common/auth.js"
  26. import BASE_URL from '@/config.js'
  27. import signInput from "@/components/am-sign-input/am-sign-input.vue"
  28. export default {
  29. components: {
  30. signInput
  31. },
  32. data() {
  33. return {
  34. action: this.BASE_URL + '/gwfile/upload?uploadPath=sign', //上传服务器的地址
  35. header: {
  36. "token": this.$auth.getUserToken()
  37. }, //图片上传携带头部信息
  38. loading: false,
  39. inputForm: this.$auth.getUserInfo(),
  40. fileList: [],
  41. signPic: ''
  42. }
  43. },
  44. methods: {
  45. /**
  46. * @param {Object} e
  47. * 签名完成回调
  48. */
  49. signToUrl(e) {
  50. this.signPic = e.data
  51. if (e.error_code && e.error_code === '201') {
  52. uni.showToast({
  53. title: e.msg,
  54. icon: 'none'
  55. })
  56. return
  57. }
  58. },
  59. // 删除图片
  60. deletePic(event) {
  61. this.fileList.splice(event.index, 1)
  62. },
  63. // 图片压缩
  64. compressH5(urlData, targetSizeKB, initialQuality = 1.0) {
  65. const maxQuality = 1.0;
  66. const minQuality = 0.0;
  67. const tolerance = 0.01; // 根据需要调整公差
  68. let that = this
  69. return new Promise((resolve, reject) => {
  70. let binarySearch = (min, max) => {
  71. const midQuality = (min + max) / 2;
  72. uni.getImageInfo({
  73. src: urlData.url,
  74. success(res) {
  75. let img = new Image()
  76. img.src = res.path
  77. img.onload = function() {
  78. const canvas = document.createElement('canvas');
  79. const ctx = canvas.getContext('2d');
  80. canvas.width = img.width;
  81. canvas.height = img.height;
  82. ctx.clearRect(0, 0, canvas.width, canvas.height);
  83. ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
  84. // 使用异步的 toBlob 方法
  85. canvas.toBlob(async (blob) => {
  86. const fileSizeKB = blob.size / 1024;
  87. if (Math.abs(fileSizeKB - targetSizeKB) <
  88. tolerance || max - min < tolerance) {
  89. // 当前质量足够接近目标大小,使用当前质量解析
  90. const dataUrl = canvas.toDataURL(urlData
  91. .type, midQuality);
  92. let result = that
  93. .uploadFilePromise(dataUrl)
  94. setTimeout(() => {
  95. resolve(
  96. result)
  97. }, 1000)
  98. } else if (fileSizeKB > targetSizeKB) {
  99. // 如果文件大小太大,降低质量,继续二分查找
  100. binarySearch(min, midQuality);
  101. } else {
  102. // 如果文件大小太小,增加质量,继续二分查找
  103. binarySearch(midQuality, max);
  104. }
  105. }, urlData.type, midQuality);
  106. };
  107. },
  108. fail() {
  109. uni.showModal({
  110. title: '提示',
  111. content: '图片压缩失败',
  112. showCancel: false
  113. });
  114. }
  115. })
  116. }
  117. // 开始二分查找
  118. binarySearch(minQuality, maxQuality);
  119. })
  120. },
  121. // 新增图片
  122. async afterRead(event) {
  123. // 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式
  124. let lists = [].concat(event.file)
  125. let fileListLen = this.fileList.length
  126. lists.map((item) => {
  127. this.fileList.push({
  128. ...item,
  129. status: 'uploading',
  130. message: '上传中'
  131. })
  132. })
  133. for (let i = 0; i < lists.length; i++) {
  134. let result = ""
  135. if (lists[i].size > (200 * 1024)) {
  136. result = await this.compressH5(lists[i], 150);
  137. } else {
  138. result = await this.uploadFilePromise(lists[i].url)
  139. }
  140. let item = this.fileList[fileListLen]
  141. this.fileList.splice(fileListLen, 1, Object.assign(item, {
  142. status: 'success',
  143. message: '',
  144. url: result
  145. }))
  146. console.log("result", result);
  147. fileListLen++
  148. }
  149. },
  150. uploadFilePromise(url) {
  151. return new Promise((resolve, reject) => {
  152. let a = uni.uploadFile({
  153. url: this.BASE_URL + '/gwfile/upload?uploadPath=signature',
  154. filePath: url,
  155. name: 'file',
  156. header: {
  157. "token": $auth.getUserToken()
  158. },
  159. success: (res) => {
  160. setTimeout(() => {
  161. resolve(res.data)
  162. }, 1000)
  163. }
  164. });
  165. })
  166. },
  167. formSubmit: function(e) {
  168. //定义表单规则
  169. // this.$refs.inputForm.validate().then(res => {
  170. uni.showLoading()
  171. let files = []
  172. if (this.fileList.length > 0) {
  173. this.fileList.forEach(item => {
  174. files.push(item.url)
  175. })
  176. this.inputForm.signPic = files.join(",");
  177. } else {
  178. this.inputForm.signPic = this.signPic
  179. }
  180. userService.saveInfo(this.inputForm).then((data) => {
  181. $auth.setUserInfo(this.inputForm)
  182. uni.showToast({
  183. title: data,
  184. icon: "success",
  185. complete: function(res) {
  186. uni.redirectTo({
  187. url: '/pages/index/index'
  188. })
  189. }
  190. });
  191. })
  192. }
  193. }
  194. }
  195. </script>
  196. <style>
  197. .btn-logout {
  198. margin-top: 100upx;
  199. width: 80%;
  200. border-radius: 50upx;
  201. font-size: 16px;
  202. color: #fff;
  203. background: linear-gradient(to right, #365fff, #36bbff);
  204. }
  205. .btn-logout-hover {
  206. background: linear-gradient(to right, #365fdd, #36bbfa);
  207. }
  208. .cu-form-group .title {
  209. min-width: calc(4em + 40px);
  210. }
  211. </style>