card.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <!--证件照片上传(单张)-->
  2. <template>
  3. <view class="sfz" @click="chooseImage()" :style="{ border: fileName ? '' : '1px solid #eeeeee' }">
  4. <image :src="ip + fileName" mode="widthFix" v-if="fileName"></image>
  5. <view class="uploads" v-else>
  6. <view class="bw">
  7. <text class="icon">&#xe603;</text>
  8. <view class="text">选择图片</view>
  9. </view>
  10. </view>
  11. </view>
  12. </template>
  13. <script>
  14. export default {
  15. name: 'card',
  16. props: {
  17. value: {
  18. type: String
  19. },
  20. read: {
  21. type: Boolean,
  22. default: false
  23. }
  24. },
  25. data() {
  26. return {
  27. fileName: this.value,
  28. ip: this.http.ip
  29. };
  30. },
  31. watch: {
  32. value(newValue) {
  33. this.fileName = newValue;
  34. }
  35. },
  36. methods: {
  37. chooseImage() {
  38. //照片选择
  39. uni.chooseImage({
  40. count: 1, //默认9
  41. sizeType: ['compressed'], //可以指定是原图还是压缩图,默认二者都有
  42. success: res => {
  43. res.tempFilePaths.forEach(path => {
  44. uni.showLoading({ title: '正在上传图片', mask: true });
  45. uni.uploadFile({
  46. url: '/app/common/upload',
  47. filePath: path,
  48. name: 'file',
  49. header: { Authorization: this.getUser().token },
  50. success: res => {
  51. let data = JSON.parse(res.data);
  52. if (data.code == 200) {
  53. this.fileName = data.fileName;
  54. this.$emit('input', data.fileName);
  55. } else {
  56. uni.showModal({ content: data.msg, showCancel: false });
  57. }
  58. uni.hideLoading();
  59. },
  60. fail: res => {
  61. uni.hideLoading();
  62. uni.showModal({ content: '图片上传失败', showCancel: false });
  63. }
  64. });
  65. });
  66. }
  67. });
  68. },
  69. // 预览图片
  70. preview(item) {
  71. uni.previewImage({
  72. urls: [this.ip + item],
  73. current: this.ip + item,
  74. success: res => {}
  75. });
  76. },
  77. del(item) {
  78. this.value.splice(this.value.indexOf(item), 1);
  79. this.$emit('input', this.value);
  80. this.$forceUpdate();
  81. }
  82. }
  83. };
  84. </script>
  85. <style lang="scss" scoped>
  86. .sfz {
  87. text-align: center;
  88. border-radius: 5px;
  89. margin-top: 10px;
  90. color: $fc;
  91. overflow: hidden;
  92. image {
  93. width: 100%;
  94. margin: 0 auto;
  95. border-radius: 5px;
  96. }
  97. .uploads {
  98. width: 100%;
  99. text-align: center;
  100. background-color: $bc1;
  101. .bw {
  102. padding: 20px;
  103. .icon {
  104. font-size: 50px;
  105. }
  106. .text {
  107. color: $fc;
  108. }
  109. }
  110. }
  111. }
  112. </style>