ocr.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <!--证件照片上传(单张)-->
  2. <template>
  3. <view class="msf">
  4. <view class="sfz" @click="chooseImage()" :style="{ border: fileName ? '' : '1px solid #eeeeee' }">
  5. <image :src="ip + fileName" mode="widthFix" v-if="fileName"></image>
  6. <view class="uploads" v-else>
  7. <view class="icon" v-html="icon"></view>
  8. </view>
  9. </view>
  10. <view class="text" v-if="!read">{{ text }}</view>
  11. </view>
  12. </template>
  13. <script>
  14. export default {
  15. name: 'card',
  16. props: {
  17. icon: {
  18. type: String
  19. },
  20. text: {
  21. type: String
  22. },
  23. value: {
  24. type: String
  25. },
  26. side: {
  27. type: String
  28. },
  29. read: {
  30. type: Boolean,
  31. default: false
  32. }
  33. },
  34. data() {
  35. return {
  36. fileName: this.value,
  37. ip: this.http.ip
  38. };
  39. },
  40. watch: {
  41. value(newValue) {
  42. this.fileName = newValue;
  43. }
  44. },
  45. methods: {
  46. chooseImage() {
  47. if (this.read) {
  48. this.preview(this.fileName);
  49. return;
  50. }
  51. //照片选择
  52. uni.chooseImage({
  53. count: 1, //默认9
  54. sizeType: ['compressed'], //可以指定是原图还是压缩图,默认二者都有
  55. success: (res) => {
  56. res.tempFilePaths.forEach((path) => {
  57. uni.showLoading({ title: '正在上传图片', mask: true });
  58. uni.uploadFile({
  59. url: this.ip + '/app/common/upload',
  60. filePath: path,
  61. name: 'file',
  62. header: { Authorization: this.getUser().token },
  63. success: (res) => {
  64. let data = JSON.parse(res.data);
  65. if (data.code == 200) {
  66. this.fileName = data.fileName;
  67. this.ocr({ imageUrl: this.ip + data.fileName, side: this.side });
  68. } else {
  69. uni.showModal({ content: data.msg, showCancel: false });
  70. }
  71. uni.hideLoading();
  72. },
  73. fail: (res) => {
  74. uni.hideLoading();
  75. uni.showModal({ content: '图片上传失败', showCancel: false });
  76. }
  77. });
  78. });
  79. }
  80. });
  81. },
  82. ocr(item) {
  83. this.http.request({
  84. url: '/app/common/ocr',
  85. data: item,
  86. method: 'POST',
  87. success: (res) => {
  88. if (this.side == 'face') {
  89. if (res.data.data) {
  90. this.$emit('input', this.fileName);
  91. this.$emit('success', { side: this.side, data: res.data.data });
  92. } else {
  93. uni.showModal({ content: '请上传正确的身份证人像面', showCancel: false });
  94. }
  95. }
  96. if (this.side == 'back') {
  97. if (res.data.data) {
  98. this.$emit('input', this.fileName);
  99. this.$emit('success', { side: this.side, data: res.data.data });
  100. } else {
  101. uni.showModal({ content: '请上传正确的身份证国徽面', showCancel: false });
  102. }
  103. }
  104. }
  105. });
  106. },
  107. // 预览图片
  108. preview(item) {
  109. uni.previewImage({
  110. urls: [this.ip + item],
  111. current: this.ip + item,
  112. success: (res) => {}
  113. });
  114. },
  115. del(item) {
  116. this.value.splice(this.value.indexOf(item), 1);
  117. this.$emit('input', this.value);
  118. this.$forceUpdate();
  119. }
  120. }
  121. };
  122. </script>
  123. <style lang="scss" scoped>
  124. .msf {
  125. padding: 0px 5px 5px 5px;
  126. text-align: center;
  127. .sfz {
  128. text-align: center;
  129. border-radius: 5px;
  130. overflow: hidden;
  131. background-color: $bg;
  132. height: 100px;
  133. image {
  134. width: 100%;
  135. margin: 0 auto;
  136. border-radius: 5px;
  137. }
  138. .uploads {
  139. width: 100%;
  140. text-align: center;
  141. .icon {
  142. font-size: 50px;
  143. line-height: 100px;
  144. color: $main-color;
  145. }
  146. }
  147. }
  148. .text {
  149. font-size: 14px;
  150. padding-top: 15px;
  151. color: $main-color;
  152. }
  153. }
  154. </style>