card.vue 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <!--证件照片上传(单张)-->
  2. <template>
  3. <view class="sfz" @click="chooseImage()">
  4. <image :src="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. let url= this.http.ip+'/sp-admin/upload/image'
  37. //照片选择
  38. uni.chooseImage({
  39. count: 1, //默认9
  40. sizeType: ['compressed'], //可以指定是原图还是压缩图,默认二者都有
  41. success: res => {
  42. res.tempFilePaths.forEach(path => {
  43. uni.showLoading({ title: '正在上传图片', mask: true });
  44. uni.uploadFile({
  45. url: url,
  46. filePath: path,
  47. name: 'file',
  48. header: { Authorization: this.getUser().token },
  49. success: res => {
  50. let data = JSON.parse(res.data);
  51. console.log(data);
  52. if (data.code == 200) {
  53. this.fileName = data.data;
  54. this.$emit('input', data.data);
  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">
  86. .sfz {
  87. text-align: center;
  88. border-radius: 5px;
  89. margin-top: 10px;
  90. overflow: hidden;
  91. image {
  92. width: 100%;
  93. margin: 0 auto;
  94. border-radius: 5px;
  95. }
  96. }
  97. </style>