card.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. read: {
  27. type: Boolean,
  28. default: false
  29. }
  30. },
  31. data() {
  32. return {
  33. fileName: this.value,
  34. ip: this.http.ip
  35. };
  36. },
  37. watch: {
  38. value(newValue) {
  39. this.fileName = newValue;
  40. }
  41. },
  42. methods: {
  43. chooseImage() {
  44. if (this.read) {
  45. this.preview(this.fileName);
  46. return;
  47. }
  48. //照片选择
  49. uni.chooseImage({
  50. count: 1, //默认9
  51. sizeType: ['compressed'], //可以指定是原图还是压缩图,默认二者都有
  52. success: (res) => {
  53. res.tempFilePaths.forEach((path) => {
  54. uni.showLoading({ title: '正在上传图片', mask: true });
  55. uni.uploadFile({
  56. url: this.http.ip + '/app/common/upload',
  57. filePath: path,
  58. name: 'file',
  59. header: { Authorization: this.getUser().token },
  60. success: (res) => {
  61. let data = JSON.parse(res.data);
  62. if (data.code == 200) {
  63. this.fileName = data.fileName;
  64. this.$emit('input', data.fileName);
  65. } else {
  66. uni.showModal({ content: data.msg, showCancel: false });
  67. }
  68. uni.hideLoading();
  69. },
  70. fail: (res) => {
  71. uni.hideLoading();
  72. uni.showModal({ content: '图片上传失败', showCancel: false });
  73. }
  74. });
  75. });
  76. }
  77. });
  78. },
  79. // 预览图片
  80. preview(item) {
  81. uni.previewImage({
  82. urls: [this.ip + item],
  83. current: this.ip + item,
  84. success: (res) => {}
  85. });
  86. },
  87. del(item) {
  88. this.value.splice(this.value.indexOf(item), 1);
  89. this.$emit('input', this.value);
  90. this.$forceUpdate();
  91. }
  92. }
  93. };
  94. </script>
  95. <style lang="scss" scoped>
  96. .msf {
  97. padding: 0px 5px 5px 5px;
  98. text-align: center;
  99. .sfz {
  100. text-align: center;
  101. border-radius: 5px;
  102. overflow: hidden;
  103. background-color: $bg;
  104. height: 100px;
  105. image {
  106. width: 100%;
  107. margin: 0 auto;
  108. border-radius: 5px;
  109. }
  110. .uploads {
  111. width: 100%;
  112. text-align: center;
  113. .icon {
  114. font-size: 50px;
  115. line-height: 100px;
  116. color: $main-color;
  117. }
  118. }
  119. }
  120. .text {
  121. font-size: 14px;
  122. padding-top: 15px;
  123. color: $main-color;
  124. }
  125. }
  126. </style>