card.vue 2.3 KB

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