card.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.http.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.$emit('input', data.fileName);
  68. this.ocr({ imageUrl: this.side == 'face' ? 'http://123.60.57.26/api/profile/upload/2024/04/22/1713726675590.jpg' : 'http://123.60.57.26/api/profile/upload/2024/04/22/1713749068044.jpg', side: this.side });
  69. } else {
  70. uni.showModal({ content: data.msg, showCancel: false });
  71. }
  72. uni.hideLoading();
  73. },
  74. fail: (res) => {
  75. uni.hideLoading();
  76. uni.showModal({ content: '图片上传失败', showCancel: false });
  77. }
  78. });
  79. });
  80. }
  81. });
  82. },
  83. ocr(item) {
  84. this.http.request({
  85. url: '/app/common/ocr',
  86. data: item,
  87. method: 'POST',
  88. success: (res) => {
  89. this.$emit('success', { side: this.side, data: res.data.data });
  90. }
  91. });
  92. },
  93. // 预览图片
  94. preview(item) {
  95. uni.previewImage({
  96. urls: [this.ip + item],
  97. current: this.ip + item,
  98. success: (res) => {}
  99. });
  100. },
  101. del(item) {
  102. this.value.splice(this.value.indexOf(item), 1);
  103. this.$emit('input', this.value);
  104. this.$forceUpdate();
  105. }
  106. }
  107. };
  108. </script>
  109. <style lang="scss" scoped>
  110. .msf {
  111. padding: 0px 5px 5px 5px;
  112. text-align: center;
  113. .sfz {
  114. text-align: center;
  115. border-radius: 5px;
  116. overflow: hidden;
  117. background-color: $bg;
  118. height: 100px;
  119. image {
  120. width: 100%;
  121. margin: 0 auto;
  122. border-radius: 5px;
  123. }
  124. .uploads {
  125. width: 100%;
  126. text-align: center;
  127. .icon {
  128. font-size: 50px;
  129. line-height: 100px;
  130. color: $main-color;
  131. }
  132. }
  133. }
  134. .text {
  135. font-size: 14px;
  136. padding-top: 15px;
  137. color: $main-color;
  138. }
  139. }
  140. </style>