signature.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <template>
  2. <view class="signature_info">
  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. <lsj-upload v-if="fileList.length == 0" ref="lsjUpload" childId="upload1" width="250rpx" height="200rpx" :option="option"
  10. :debug="false" :instantly="false" @uploadEnd="onuploadEnd" @change="afterRead">
  11. <view class="addfile flex">
  12. <u-icon size="20" bold name="plus"></u-icon>
  13. </view>
  14. </lsj-upload>
  15. <u-upload :fileList="fileList" @delete="deletePic" :maxCount="fileList.length"
  16. :previewFullImage="true"></u-upload>
  17. </u-form-item>
  18. <u--text size="10" type="warning" text="有签名图片,请上传,无,请点击下面签字生成"></u--text>
  19. <u-form-item label="签字" borderBottom prop="signPic">
  20. <signInput ref="sign" canvasId="twoDrowCanvas" canvasIds="twoRotateCanvas" :header="header"
  21. :action="action" @signToUrl="signToUrl">
  22. </signInput>
  23. </u-form-item>
  24. <view class="padding-xl">
  25. <u-button type="primary" @click="formSubmit" text="提交"></u-button>
  26. </view>
  27. </u--form>
  28. </view>
  29. </template>
  30. <script>
  31. import {
  32. isImageFormat
  33. } from "@/common/util.js"
  34. import userService from "@/api/sys/userService"
  35. import * as $auth from "@/common/auth.js"
  36. import BASE_URL from '@/config.js'
  37. import signInput from "@/components/am-sign-input/am-sign-input.vue"
  38. export default {
  39. components: {
  40. signInput
  41. },
  42. data() {
  43. return {
  44. action: this.BASE_URL + '/gwfile/upload?uploadPath=sign', //上传服务器的地址
  45. header: {
  46. "token": this.$auth.getUserToken()
  47. }, //图片上传携带头部信息
  48. loading: false,
  49. inputForm: this.$auth.getUserInfo(),
  50. fileList: [],
  51. files:[],
  52. signPic: '',
  53. // 上传接口参数
  54. option: {
  55. url: BASE_URL + '/gwfile/upload?uploadPath=sign',
  56. name: 'file',
  57. header: {
  58. "token": $auth.getUserToken()
  59. },
  60. },
  61. }
  62. },
  63. methods: {
  64. /**
  65. * @param {Object} e
  66. * 签名完成回调
  67. */
  68. signToUrl(e) {
  69. this.signPic = e.data
  70. if (e.error_code && e.error_code === '201') {
  71. uni.showToast({
  72. title: e.msg,
  73. icon: 'none'
  74. })
  75. return
  76. }
  77. },
  78. // 删除图片
  79. deletePic(event) {
  80. this.fileList.splice(event.index, 1)
  81. },
  82. // 新增图片
  83. afterRead(files) {
  84. uni.showLoading({
  85. title: "上传中"
  86. })
  87. let lists = [...files.values()]
  88. this.files = files
  89. for (let i = 0; i < lists.length; i++) {
  90. if (lists[i].type == 'success') continue;
  91. if (!isImageFormat(lists[i].name)) {
  92. uni.showToast({
  93. title: '请上传图片',
  94. icon: 'error'
  95. })
  96. } else {
  97. if (lists[i].size > 200 * 1024) {
  98. this.compressAPP(lists[i], 150).then(res => {
  99. this.files.get(lists[i].name).file = res
  100. this.upload(lists[i].name)
  101. })
  102. } else {
  103. this.upload(lists[i].name)
  104. }
  105. }
  106. }
  107. },
  108. // app图片压缩
  109. compressAPP(urlData, targetSizeKB, initialQuality = 1.0) {
  110. const maxQuality = 1.0;
  111. const minQuality = 0.0;
  112. const tolerance = 0.01; // 根据需要调整公差
  113. let that = this
  114. return new Promise((resolve, reject) => {
  115. let binarySearch = (min, max) => {
  116. const midQuality = (min + max) / 2;
  117. uni.getImageInfo({
  118. src: urlData.path,
  119. success: function(res) {
  120. let img = new Image()
  121. img.src = res.path
  122. img.onload = function() {
  123. const canvas = document.createElement('canvas');
  124. const ctx = canvas.getContext('2d');
  125. canvas.width = img.width;
  126. canvas.height = img.height;
  127. ctx.clearRect(0, 0, canvas.width, canvas.height);
  128. ctx.drawImage(img, 0, 0, canvas.width, canvas
  129. .height);
  130. // 使用异步的 toBlob 方法
  131. canvas.toBlob(async (blob) => {
  132. const fileSizeKB = blob.size / 1024;
  133. if (Math.abs(fileSizeKB -
  134. targetSizeKB) <
  135. tolerance || max - min <
  136. tolerance) {
  137. // 当前质量足够接近目标大小,使用当前质量解析
  138. const file = new File([blob], urlData
  139. .name, {
  140. type: blob.type,
  141. lastModified: new Date()
  142. .getTime(), // 使用当前时间作为最后修改时间
  143. });
  144. setTimeout(() => {
  145. resolve(file)
  146. }, 300)
  147. } else if (fileSizeKB >
  148. targetSizeKB) {
  149. // 如果文件大小太大,降低质量,继续二分查找
  150. binarySearch(min, midQuality);
  151. } else {
  152. // 如果文件大小太小,增加质量,继续二分查找
  153. binarySearch(midQuality, max);
  154. }
  155. }, urlData.file.type, midQuality);
  156. };
  157. },
  158. fail: function(err) {
  159. resolve(false);
  160. }
  161. });
  162. }
  163. // 开始二分查找
  164. binarySearch(minQuality, maxQuality);
  165. })
  166. },
  167. // APP手动上传
  168. upload(name) {
  169. // name=指定文件名,不指定则上传所有type等于waiting和fail的文件
  170. this.$refs['lsjUpload'].upload(name);
  171. },
  172. onuploadEnd(item) {
  173. console.log(`${item.name}已上传结束,上传状态=${item.type}`);
  174. // 更新当前窗口状态变化的文件
  175. this.files.set(item.name, item);
  176. let file = {
  177. name: item.name,
  178. path: item.responseText
  179. }
  180. this.fileList.push(file)
  181. uni.hideLoading()
  182. // 微信小程序Map对象for循环不显示,所以转成普通数组,
  183. // 如果你用不惯Map对象,也可以像这样转普通数组,组件使用Map主要是避免反复文件去重操作
  184. // #ifdef MP-WEIXIN
  185. this.wxFiles = [...this.files.values()];
  186. // #endif
  187. // 强制更新视图
  188. this.$forceUpdate();
  189. // ---可删除--演示判断是否所有文件均已上传成功
  190. let isAll = [...this.files.values()].find(item => item.type !== 'success');
  191. if (!isAll) {
  192. } else {
  193. console.log(isAll.name + '待上传');
  194. }
  195. },
  196. formSubmit: function(e) {
  197. //定义表单规则
  198. // this.$refs.inputForm.validate().then(res => {
  199. uni.showLoading()
  200. let files = []
  201. if (this.fileList.length > 0) {
  202. this.fileList.forEach(item => {
  203. files.push(item.path)
  204. })
  205. this.inputForm.signPic = files.join(",");
  206. } else {
  207. this.inputForm.signPic = this.signPic
  208. }
  209. userService.saveInfo(this.inputForm).then((data) => {
  210. $auth.setUserInfo(this.inputForm)
  211. uni.showToast({
  212. title: data,
  213. icon: "success",
  214. complete: function(res) {
  215. uni.redirectTo({
  216. url: '/pages/index/index'
  217. })
  218. }
  219. });
  220. })
  221. }
  222. }
  223. }
  224. </script>
  225. <style>
  226. .btn-logout {
  227. margin-top: 100upx;
  228. width: 80%;
  229. border-radius: 50upx;
  230. font-size: 16px;
  231. color: #fff;
  232. background: linear-gradient(to right, #365fff, #36bbff);
  233. }
  234. .addfile {
  235. width: 80px;
  236. height: 80px;
  237. background-color: #eee;
  238. padding-left: 24%;
  239. margin: 10px;
  240. }
  241. .signature_info .takephoto {
  242. padding-left: 12%;
  243. }
  244. .btn-logout-hover {
  245. background: linear-gradient(to right, #365fdd, #36bbfa);
  246. }
  247. .cu-form-group .title {
  248. min-width: calc(4em + 40px);
  249. }
  250. </style>