card.vue 2.5 KB

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