封装组件

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

  1. <template>
  2. <div>
  3. <el-upload
  4. action="http://gulimall-hello.oss-cn-beijing.aliyuncs.com"
  5. :data="dataObj"
  6. :list-type="listType"
  7. :file-list="fileList"
  8. :before-upload="beforeUpload"
  9. :on-remove="handleRemove"
  10. :on-success="handleUploadSuccess"
  11. :on-preview="handlePreview"
  12. :limit="maxCount"
  13. :on-exceed="handleExceed"
  14. :show-file-list="showFile"
  15. >
  16. <i class="el-icon-plus"></i>
  17. </el-upload>
  18. <el-dialog :visible.sync="dialogVisible">
  19. <img width="100%" :src="dialogImageUrl" alt />
  20. </el-dialog>
  21. </div>
  22. </template>
  23. <script>
  24. import { policy } from "./policy";
  25. import { getUUID } from '@/utils'
  26. export default {
  27. name: "multiUpload",
  28. props: {
  29. //图片属性数组
  30. value: Array,
  31. //最大上传图片数量
  32. maxCount: {
  33. type: Number,
  34. default: 30
  35. },
  36. listType:{
  37. type: String,
  38. default: "picture-card"
  39. },
  40. showFile:{
  41. type: Boolean,
  42. default: true
  43. }
  44. },
  45. data() {
  46. return {
  47. dataObj: {
  48. policy: "",
  49. signature: "",
  50. key: "",
  51. ossaccessKeyId: "",
  52. dir: "",
  53. host: "",
  54. uuid: ""
  55. },
  56. dialogVisible: false,
  57. dialogImageUrl: null
  58. };
  59. },
  60. computed: {
  61. fileList() {
  62. let fileList = [];
  63. for (let i = 0; i < this.value.length; i++) {
  64. fileList.push({ url: this.value[i] });
  65. }
  66. return fileList;
  67. }
  68. },
  69. mounted() {},
  70. methods: {
  71. emitInput(fileList) {
  72. let value = [];
  73. for (let i = 0; i < fileList.length; i++) {
  74. value.push(fileList[i].url);
  75. }
  76. this.$emit("input", value);
  77. },
  78. handleRemove(file, fileList) {
  79. this.emitInput(fileList);
  80. },
  81. handlePreview(file) {
  82. this.dialogVisible = true;
  83. this.dialogImageUrl = file.url;
  84. },
  85. beforeUpload(file) {
  86. let _self = this;
  87. return new Promise((resolve, reject) => {
  88. policy()
  89. .then(response => {
  90. console.log("这是什么${filename}");
  91. _self.dataObj.policy = response.data.policy;
  92. _self.dataObj.signature = response.data.signature;
  93. _self.dataObj.ossaccessKeyId = response.data.accessid;
  94. _self.dataObj.key = response.data.dir +getUUID()+"_${filename}";
  95. _self.dataObj.dir = response.data.dir;
  96. _self.dataObj.host = response.data.host;
  97. resolve(true);
  98. })
  99. .catch(err => {
  100. console.log("出错了...",err)
  101. reject(false);
  102. });
  103. });
  104. },
  105. handleUploadSuccess(res, file) {
  106. this.fileList.push({
  107. name: file.name,
  108. // url: this.dataObj.host + "/" + this.dataObj.dir + "/" + file.name; 替换${filename}为真正的文件名
  109. url: this.dataObj.host + "/" + this.dataObj.key.replace("${filename}",file.name)
  110. });
  111. this.emitInput(this.fileList);
  112. },
  113. handleExceed(files, fileList) {
  114. this.$message({
  115. message: "最多只能上传" + this.maxCount + "张图片",
  116. type: "warning",
  117. duration: 1000
  118. });
  119. }
  120. }
  121. };
  122. </script>
  123. <style>
  124. </style>

获取服务端签名-policy.js

  1. import http from '@/utils/httpRequest.js'
  2. export function policy() {
  3. return new Promise((resolve,reject)=>{
  4. http({
  5. url: http.adornUrl("/third-party/oss/policy"),
  6. method: "get",
  7. params: http.adornParams({})
  8. }).then(({ data }) => {
  9. resolve(data);
  10. })
  11. });
  12. }

品牌管理页面

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