card.vue 1.9 KB

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