fa-upload-image.vue 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <template>
  2. <view>
  3. <u-upload :fileList="fileList" @afterRead="afterRead" @delete="deletePic" multiple :maxCount="imgType == 'single' ? 1 : 99" :previewFullImage="true"></u-upload>
  4. </view>
  5. </template>
  6. <script>
  7. // import Emitter from '@/uni_modules/uview-ui/libs/util/emitter.js';
  8. export default {
  9. name: 'fa-upload-image',
  10. // mixins: [Emitter],
  11. props: {
  12. value: {
  13. type: String,
  14. default: ''
  15. },
  16. imgType: {
  17. type: String,
  18. default: 'single'
  19. },
  20. fileList: {
  21. type: Array,
  22. default() {
  23. return [];
  24. }
  25. }
  26. },
  27. created() {},
  28. data() {
  29. return {
  30. header: {},
  31. formdata: {},
  32. vuex_token: '',
  33. vuex_user: {}
  34. };
  35. },
  36. methods: {
  37. // 删除图片
  38. deletePic(event) {
  39. this.fileList.splice(event.index, 1);
  40. },
  41. // 新增图片
  42. async afterRead(event) {
  43. // 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式
  44. console.log(event);
  45. let lists = [].concat(event.file);
  46. let fileListLen = this.fileList.length;
  47. lists.map((item) => {
  48. this.fileList.push({
  49. ...item,
  50. status: 'uploading',
  51. message: '上传中'
  52. });
  53. });
  54. for (let i = 0; i < lists.length; i++) {
  55. const result = await this.uploadFilePromise(lists[i].url);
  56. let item = this.fileList[fileListLen];
  57. this.fileList.splice(
  58. fileListLen,
  59. 1,
  60. Object.assign(item, {
  61. status: 'success',
  62. message: '',
  63. url: result
  64. })
  65. );
  66. fileListLen++;
  67. }
  68. console.log(this.fileList);
  69. },
  70. uploadFilePromise(url) {
  71. return new Promise((resolve, reject) => {
  72. let userToken = '';
  73. let auth = this.$db.get('auth');
  74. userToken = auth.token;
  75. let a = uni.uploadFile({
  76. url: this.$config.baseUrl + 'api/common/upload?token=' + userToken, // 仅为示例,非真实的接口地址
  77. filePath: url,
  78. name: 'file',
  79. formData: {},
  80. success: (res) => {
  81. setTimeout(() => {
  82. console.log(res);
  83. resolve(JSON.parse(res.data).data.fullurl);
  84. }, 1000);
  85. }
  86. });
  87. });
  88. }
  89. }
  90. };
  91. </script>
  92. <style lang="scss"></style>