uploadLicense.vue 3.4 KB

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