uploadLicense.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <!-- @format -->
  2. <template>
  3. <view class="container">
  4. <view style="color: #3c9cff" class="uploadTitle">上传营业执照</view>
  5. <view class="updateContent">
  6. <view style="margin: 0 auto">
  7. <u-upload
  8. :fileList="fileList"
  9. @afterRead="afterRead"
  10. @delete="deletePic"
  11. :maxCount="1"
  12. width="280"
  13. height="180"
  14. >
  15. <view class="uploadTipcContent">
  16. <view>
  17. <view style="display: flex; justify-content: center">
  18. <u-icon name="arrow-upward" color="#2979ff" size="28"></u-icon>
  19. </view>
  20. <u-button
  21. :customStyle="{ color: '#3c9cff', fontSize: '26rpx' }"
  22. type="text"
  23. size="small"
  24. >点击上传</u-button
  25. >
  26. </view>
  27. <view class="topic">
  28. 请上传营业执照正面照片,限制照片大小30MB。
  29. </view>
  30. </view>
  31. </u-upload>
  32. </view>
  33. </view>
  34. </view>
  35. </template>
  36. <script>
  37. export default {
  38. data() {
  39. return {
  40. fileList: [],
  41. };
  42. },
  43. props: {
  44. formdData: {
  45. type: Object,
  46. default: () => ({}),
  47. },
  48. },
  49. onLoad() {},
  50. methods: {
  51. deletePic(event) {
  52. this[`fileList${event.name}`].splice(event.index, 1);
  53. },
  54. // 新增图片
  55. async afterRead(event) {
  56. console.log(this.$baseUrl);
  57. // 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式
  58. let lists = [].concat(event.file);
  59. let fileListLen = this[`fileList${event.name}`].length;
  60. lists.map((item) => {
  61. this[`fileList${event.name}`].push({
  62. ...item,
  63. status: "uploading",
  64. message: "上传中",
  65. });
  66. });
  67. for (let i = 0; i < lists.length; i++) {
  68. const result = await this.uploadFilePromise(lists[i].url);
  69. let item = this[`fileList${event.name}`][fileListLen];
  70. this[`fileList${event.name}`].splice(
  71. fileListLen,
  72. 1,
  73. Object.assign(item, {
  74. status: "success",
  75. message: "",
  76. url: JSON.parse(result.data).fileName,
  77. })
  78. );
  79. fileListLen++;
  80. }
  81. this.formdData.imageUrl = this.fileList[0].url;
  82. this.$emit(
  83. "update:formdData",
  84. Object.assign({ ...this.formdData, imageUrl: this.formdData.imageUrl })
  85. );
  86. },
  87. //上传
  88. uploadFilePromise(url) {
  89. return new Promise((resolve, reject) => {
  90. let a = uni.uploadFile({
  91. url: `${this.$baseUrl}/api/common/upload`, // 仅为示例,非真实的接口地址
  92. filePath: url,
  93. name: "file",
  94. success: (res) => {
  95. setTimeout(() => {
  96. resolve(res);
  97. }, 1000);
  98. },
  99. });
  100. });
  101. },
  102. },
  103. };
  104. </script>
  105. <style lang="scss">
  106. .uploadTipcContent {
  107. width: 555rpx;
  108. height: 260rpx;
  109. padding: 30rpx;
  110. border: 1px dotted#909399;
  111. }
  112. .topic {
  113. color: #909399;
  114. font-size: 24rpx;
  115. width: 400rpx;
  116. margin: 20rpx auto 0;
  117. font-family: "宋体";
  118. }
  119. .container {
  120. width: 100%;
  121. }
  122. .updateContent {
  123. display: flex;
  124. background: #fff;
  125. padding: 30rpx;
  126. }
  127. .uploadTitle {
  128. margin-bottom: 30rpx;
  129. }
  130. </style>