封装组件

image.png

单文件上传组件-singleUpload.vue

  1. <template>
  2. <div>
  3. <el-upload
  4. action="http://gulimall-hello.oss-cn-beijing.aliyuncs.com"
  5. :data="dataObj"
  6. list-type="picture"
  7. :multiple="false" :show-file-list="showFileList"
  8. :file-list="fileList"
  9. :before-upload="beforeUpload"
  10. :on-remove="handleRemove"
  11. :on-success="handleUploadSuccess"
  12. :on-preview="handlePreview">
  13. <el-button size="small" type="primary">点击上传</el-button>
  14. <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过10MB</div>
  15. </el-upload>
  16. <el-dialog :visible.sync="dialogVisible">
  17. <img width="100%" :src="fileList[0].url" alt="">
  18. </el-dialog>
  19. </div>
  20. </template>
  21. <script>
  22. import {policy} from './policy'
  23. import { getUUID } from '@/utils'
  24. export default {
  25. name: 'singleUpload',
  26. props: {
  27. value: String
  28. },
  29. computed: {
  30. imageUrl() {
  31. return this.value;
  32. },
  33. imageName() {
  34. if (this.value != null && this.value !== '') {
  35. return this.value.substr(this.value.lastIndexOf("/") + 1);
  36. } else {
  37. return null;
  38. }
  39. },
  40. fileList() {
  41. return [{
  42. name: this.imageName,
  43. url: this.imageUrl
  44. }]
  45. },
  46. showFileList: {
  47. get: function () {
  48. return this.value !== null && this.value !== ''&& this.value!==undefined;
  49. },
  50. set: function (newValue) {
  51. }
  52. }
  53. },
  54. data() {
  55. return {
  56. dataObj: {
  57. policy: '',
  58. signature: '',
  59. key: '',
  60. ossaccessKeyId: '',
  61. dir: '',
  62. host: '',
  63. // callback:'',
  64. },
  65. dialogVisible: false
  66. };
  67. },
  68. methods: {
  69. emitInput(val) {
  70. this.$emit('input', val)
  71. },
  72. handleRemove(file, fileList) {
  73. this.emitInput('');
  74. },
  75. handlePreview(file) {
  76. this.dialogVisible = true;
  77. },
  78. beforeUpload(file) {
  79. let _self = this;
  80. return new Promise((resolve, reject) => {
  81. policy().then(response => {
  82. console.log("响应的数据",response);
  83. _self.dataObj.policy = response.data.policy;
  84. _self.dataObj.signature = response.data.signature;
  85. _self.dataObj.ossaccessKeyId = response.data.accessid;
  86. _self.dataObj.key = response.data.dir +getUUID()+'_${filename}';
  87. _self.dataObj.dir = response.data.dir;
  88. _self.dataObj.host = response.data.host;
  89. console.log("响应的数据222。。。",_self.dataObj);
  90. resolve(true)
  91. }).catch(err => {
  92. reject(false)
  93. })
  94. })
  95. },
  96. handleUploadSuccess(res, file) {
  97. console.log("上传成功...")
  98. this.showFileList = true;
  99. this.fileList.pop();
  100. this.fileList.push({name: file.name, url: this.dataObj.host + '/' + this.dataObj.key.replace("${filename}",file.name) });
  101. this.emitInput(this.fileList[0].url);
  102. }
  103. }
  104. }
  105. </script>
  106. <style>
  107. </style>

多文件上传组件-multiUpload.vue

<template>
  <div>
    <el-upload
      action="http://gulimall-hello.oss-cn-beijing.aliyuncs.com"
      :data="dataObj"
      :list-type="listType"
      :file-list="fileList"
      :before-upload="beforeUpload"
      :on-remove="handleRemove"
      :on-success="handleUploadSuccess"
      :on-preview="handlePreview"
      :limit="maxCount"
      :on-exceed="handleExceed"
      :show-file-list="showFile"
    >
      <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt />
    </el-dialog>
  </div>
</template>
<script>
import { policy } from "./policy";
import { getUUID } from '@/utils'
export default {
  name: "multiUpload",
  props: {
    //图片属性数组
    value: Array,
    //最大上传图片数量
    maxCount: {
      type: Number,
      default: 30
    },
    listType:{
      type: String,
      default: "picture-card"
    },
    showFile:{
      type: Boolean,
      default: true
    }

  },
  data() {
    return {
      dataObj: {
        policy: "",
        signature: "",
        key: "",
        ossaccessKeyId: "",
        dir: "",
        host: "",
        uuid: ""
      },
      dialogVisible: false,
      dialogImageUrl: null
    };
  },
  computed: {
    fileList() {
      let fileList = [];
      for (let i = 0; i < this.value.length; i++) {
        fileList.push({ url: this.value[i] });
      }

      return fileList;
    }
  },
  mounted() {},
  methods: {
    emitInput(fileList) {
      let value = [];
      for (let i = 0; i < fileList.length; i++) {
        value.push(fileList[i].url);
      }
      this.$emit("input", value);
    },
    handleRemove(file, fileList) {
      this.emitInput(fileList);
    },
    handlePreview(file) {
      this.dialogVisible = true;
      this.dialogImageUrl = file.url;
    },
    beforeUpload(file) {
      let _self = this;
      return new Promise((resolve, reject) => {
        policy()
          .then(response => {
            console.log("这是什么${filename}");
            _self.dataObj.policy = response.data.policy;
            _self.dataObj.signature = response.data.signature;
            _self.dataObj.ossaccessKeyId = response.data.accessid;
            _self.dataObj.key = response.data.dir +getUUID()+"_${filename}";
            _self.dataObj.dir = response.data.dir;
            _self.dataObj.host = response.data.host;
            resolve(true);
          })
          .catch(err => {
            console.log("出错了...",err)
            reject(false);
          });
      });
    },
    handleUploadSuccess(res, file) {
      this.fileList.push({
        name: file.name,
        // url: this.dataObj.host + "/" + this.dataObj.dir + "/" + file.name; 替换${filename}为真正的文件名
        url: this.dataObj.host + "/" + this.dataObj.key.replace("${filename}",file.name)
      });
      this.emitInput(this.fileList);
    },
    handleExceed(files, fileList) {
      this.$message({
        message: "最多只能上传" + this.maxCount + "张图片",
        type: "warning",
        duration: 1000
      });
    }
  }
};
</script>
<style>
</style>

获取服务端签名-policy.js

import http from '@/utils/httpRequest.js'
export function policy() {
   return  new Promise((resolve,reject)=>{
        http({
            url: http.adornUrl("/third-party/oss/policy"),
            method: "get",
            params: http.adornParams({})
        }).then(({ data }) => {
            resolve(data);
        })
    });
}

品牌管理页面

品牌管理完整页面参考:品牌管理-前端路由及逆向生成代码