signature.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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": $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. async afterRead(event) {
  64. // 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式
  65. let lists = [].concat(event.file)
  66. let fileListLen = this.fileList.length
  67. lists.map((item) => {
  68. this.fileList.push({
  69. ...item,
  70. status: 'uploading',
  71. message: '上传中'
  72. })
  73. })
  74. for (let i = 0; i < lists.length; i++) {
  75. const result = await this.uploadFilePromise(lists[i])
  76. let item = this.fileList[fileListLen]
  77. this.fileList.splice(fileListLen, 1, Object.assign(item, {
  78. status: 'success',
  79. message: '',
  80. url: result
  81. }))
  82. console.log("result", result);
  83. fileListLen++
  84. }
  85. },
  86. uploadFilePromise(url) {
  87. return new Promise((resolve, reject) => {
  88. let a = uni.uploadFile({
  89. url: this.BASE_URL + '/gwfile/upload?uploadPath=signature',
  90. filePath: url.url,
  91. name: 'file',
  92. header: {
  93. "token": $auth.getUserToken()
  94. },
  95. formData: url,
  96. success: (res) => {
  97. setTimeout(() => {
  98. resolve(res.data)
  99. }, 1000)
  100. }
  101. });
  102. })
  103. },
  104. formSubmit: function(e) {
  105. //定义表单规则
  106. // this.$refs.inputForm.validate().then(res => {
  107. uni.showLoading()
  108. let files = []
  109. if (this.fileList.length > 0) {
  110. this.fileList.forEach(item => {
  111. files.push(item.url)
  112. })
  113. this.inputForm.signPic = files.join(",");
  114. }
  115. userService.saveInfo(this.inputForm).then((data) => {
  116. uni.showToast({
  117. title: data,
  118. icon: "success",
  119. complete: function(res) {
  120. uni.redirectTo({
  121. url: '/pages/index/index'
  122. })
  123. }
  124. });
  125. })
  126. }
  127. }
  128. }
  129. </script>
  130. <style>
  131. .btn-logout {
  132. margin-top: 100upx;
  133. width: 80%;
  134. border-radius: 50upx;
  135. font-size: 16px;
  136. color: #fff;
  137. background: linear-gradient(to right, #365fff, #36bbff);
  138. }
  139. .btn-logout-hover {
  140. background: linear-gradient(to right, #365fdd, #36bbfa);
  141. }
  142. .cu-form-group .title {
  143. min-width: calc(4em + 40px);
  144. }
  145. </style>