ali-oss-multi.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <template>
  2. <el-dialog
  3. :title="title"
  4. :visible.sync="modal"
  5. :close-on-click-modal="false"
  6. width="960px"
  7. @close="
  8. res => {
  9. $emit('cancel');
  10. }
  11. "
  12. >
  13. <el-form ref="form" :model="form" label-width="100px">
  14. <el-form-item label="关联场景" prop="kindergartenId">
  15. <SceneSelect v-model="form.kindergartenId" :clearable="false" />
  16. </el-form-item>
  17. <el-form-item label="关联活动">
  18. <ActivitySelect
  19. v-model="form.activityId"
  20. :kindergartenId="form.kindergartenId"
  21. :firstLoad="false"
  22. :disabled="form.kindergartenId === null"
  23. />
  24. </el-form-item>
  25. <el-form-item label="图片" prop="urls" required>
  26. <el-upload
  27. action
  28. :http-request="Upload"
  29. :before-upload="beforeAvatarUpload"
  30. :on-preview="handlePreview"
  31. :before-remove="beforeRemove"
  32. :on-remove="handleRemove"
  33. :on-success="handleSuccess"
  34. :on-exceed="handleExceed"
  35. drag
  36. multiple
  37. :limit="limit"
  38. :file-list="fileList"
  39. >
  40. <i class="el-icon-upload"></i>
  41. <div class="el-upload__text">
  42. 将文件拖到此处,或
  43. <em>点击上传</em>
  44. </div>
  45. <div slot="tip" class="el-upload__tip">上传文件大小不能超过 1G</div>
  46. </el-upload>
  47. <el-progress v-show="showProgress" :text-inside="true" :stroke-width="15" :percentage="progress"></el-progress>
  48. </el-form-item>
  49. </el-form>
  50. </el-dialog>
  51. </template>
  52. <script>
  53. import { client , getFileNameUUID } from "@/utils/ali-oss"; // 前面的ali-js文件内的两个封装函数
  54. import { saveItem } from '@/api/photoWarehouse';
  55. export default {
  56. name: "Upload",
  57. props: {
  58. limit: {
  59. type: Number,
  60. default: 30
  61. }
  62. },
  63. data() {
  64. return {
  65. title: '批量上传照片',
  66. modal: true,
  67. form: {
  68. kindergartenId: null,
  69. activityId: '',
  70. urls: []
  71. },
  72. fileList: [], // 文件列
  73. showProgress: false, // 进度条的显示
  74. dataObj: {
  75. region: 'oss-cn-guangzhou',
  76. accessKeyId: 'LTAI5t8FMLXGzU4pTi9GXWb6',
  77. accessKeySecret: 'Hz6WOXIVNfpdMphYtf3laHbVoasGlT',
  78. bucket: 'yxl-kindergarten',
  79. }, //存签名信息
  80. progress: 0 //进度条数据
  81. };
  82. },
  83. methods: {
  84. // 文件超出个数限制时的钩子
  85. handleExceed(files, fileList) {
  86. this.$message.warning(`每次只能上传 ${this.limit} 个文件`);
  87. },
  88. // 点击文件列表中已上传的文件时的钩子
  89. handlePreview(file) {},
  90. // 删除文件之前的钩子
  91. beforeRemove(file, fileList) {
  92. return this.$confirm(`确定移除 ${file.name}?`);
  93. },
  94. // 文件列表移除文件时的钩子
  95. handleRemove(file, fileList) {},
  96. // 文件上传成功时的钩子
  97. handleSuccess(response, file, fileList) {
  98. this.fileList = fileList;
  99. // if (response.name) {
  100. // this.saveData(response.name);
  101. // }
  102. },
  103. // 文件上传前的校验
  104. beforeAvatarUpload(file) {
  105. // if (["video/mp4"].indexOf(file.type) == -1) {
  106. // this.$message.error("请上传正确的视频格式");
  107. // return false;
  108. // }
  109. // const isLt100M = file.size / 1024 / 1024 > 10 && file.size / 1024 / 1024 < 1024;
  110. // if (!isLt100M) {
  111. // this.$message.error("上传视频大小要在10MB~1GB之间哦!");
  112. // return false;
  113. // }
  114. if(!this.form.kindergartenId) {
  115. this.$error('请先选择幼儿园')
  116. }
  117. const isLt30 = file.name.length < 30;
  118. if (!isLt30) {
  119. this.$message.error("上传视频文件名称长度必须要小于30个文字哦!");
  120. return false;
  121. }
  122. },
  123. // http-request属性来覆盖默认的上传行为(即action="url"),自定义上传的实现
  124. Upload(file) {
  125. const _that = this;
  126. async function multipartUpload() {
  127. let temporary = file.file.name.lastIndexOf(".");
  128. let fileNameLength = file.file.name.length;
  129. let fileFormat = file.file.name.substring(
  130. temporary + 1,
  131. fileNameLength
  132. );
  133. let fileName = getFileNameUUID() + "." + fileFormat;
  134. client(_that.dataObj).multipartUpload(`tmp/${fileName}`, file.file, {
  135. progress: function(p) {
  136. // todo 每个文件一个进度条???如何限定关联的业务参数(幼儿园id、活动id)
  137. // p进度条的值
  138. console.log(p);
  139. _that.showProgress = true;
  140. _that.progress = Math.floor(p * 100);
  141. }
  142. })
  143. .then(result => {
  144. //上传成功返回值,可针对项目需求写其他逻辑
  145. // todo 这里每完成一个文件,会回调一次。这里请求服务端保存业务。照片信息 + 幼儿园 + 活动...
  146. console.log(result);
  147. this.saveData(result);
  148. })
  149. .catch(err => {
  150. console.log("err:", err);
  151. });
  152. }
  153. multipartUpload();
  154. },
  155. async saveData(obj) {
  156. const params = {
  157. kindergartenId: this.form.kindergartenId,
  158. activityId: this.form.activityId,
  159. fileRequest: {
  160. // todo 补充字段
  161. originalFilename: 'abc',
  162. size: 100,
  163. path: obj.name
  164. }
  165. }
  166. const { success, msg } = await saveItem(params);
  167. if (success) {
  168. }
  169. }
  170. }
  171. };
  172. </script>
  173. <style lang="scss" scoped></style>