signature.vue 6.0 KB

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