ali-oss-multi.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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" :rules="rules" label-width="100px">
  14. <el-form-item label="关联场景" prop="kindergartenId" required>
  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
  48. v-show="showProgress"
  49. :text-inside="true"
  50. :stroke-width="15"
  51. :percentage="progress"
  52. ></el-progress>
  53. </el-form-item>
  54. </el-form>
  55. </el-dialog>
  56. </template>
  57. <script>
  58. import { client, getFileNameUUID } from '@/utils/ali-oss'; // 前面的ali-js文件内的两个封装函数
  59. import { saveItem } from '@/api/photoWarehouse';
  60. export default {
  61. name: 'Upload',
  62. props: {
  63. limit: {
  64. type: Number,
  65. default: 50
  66. }
  67. },
  68. data() {
  69. return {
  70. title: '批量上传照片',
  71. modal: true,
  72. form: {
  73. kindergartenId: null,
  74. activityId: '',
  75. urls: []
  76. },
  77. rules: {
  78. kindergartenId: [
  79. { required: true, message: '请选择关联场景', trigger: 'blur' }
  80. ],
  81. urls: [{ required: true, message: '请上传图片', trigger: 'change' }]
  82. },
  83. fileList: [], // 文件列
  84. showProgress: false, // 进度条的显示
  85. dataObj: {
  86. region: 'oss-cn-guangzhou',
  87. accessKeyId: 'LTAI5t8FMLXGzU4pTi9GXWb6',
  88. accessKeySecret: 'Hz6WOXIVNfpdMphYtf3laHbVoasGlT',
  89. bucket: 'yxl-kindergarten'
  90. }, // 存签名信息 todo
  91. progress: 0 // 进度条数据
  92. };
  93. },
  94. methods: {
  95. // 文件超出个数限制时的钩子
  96. handleExceed(files, fileList) {
  97. this.$message.warning(`每次只能上传 ${this.limit} 个文件`);
  98. },
  99. // 点击文件列表中已上传的文件时的钩子
  100. handlePreview(file) {},
  101. // 删除文件之前的钩子
  102. beforeRemove(file, fileList) {
  103. if (file && file.status==="success") {
  104. return this.$confirm(`确定移除 ${file.name}?`);
  105. }
  106. },
  107. // 文件列表移除文件时的钩子
  108. handleRemove(file, fileList) {
  109. if (file && file.status==="success") {}
  110. },
  111. // 文件上传成功时的钩子
  112. handleSuccess(response, file, fileList) {
  113. this.fileList = fileList;
  114. // if (response.name) {
  115. // this.saveData(response.name);
  116. // }
  117. },
  118. // 文件上传前的校验
  119. beforeAvatarUpload(file) {
  120. // if (["video/mp4"].indexOf(file.type) == -1) {
  121. // this.$message.error("请上传正确的视频格式");
  122. // return false;
  123. // }
  124. const isLt100M = file.size / 1024 / 1024 > 5 && file.size / 1024 / 1024 < 1024;
  125. if (!isLt100M) {
  126. this.$message.error("上传照片大小要在5MB ~ 1GB之间哦!");
  127. return false;
  128. }
  129. const isLt30 = file.name.length < 30;
  130. if (!isLt30) {
  131. this.$message.error('上传视频文件名称长度必须要小于30个文字哦!');
  132. return false;
  133. }
  134. if (!this.form.kindergartenId) {
  135. this.$error('请先选择幼儿园');
  136. return false;
  137. }
  138. },
  139. // http-request属性来覆盖默认的上传行为(即action="url"),自定义上传的实现
  140. upload(file) {
  141. const _that = this;
  142. async function multipartUpload() {
  143. let temporary = file.file.name.lastIndexOf('.');
  144. let fileNameLength = file.file.name.length;
  145. let fileFormat = file.file.name.substring(
  146. temporary + 1,
  147. fileNameLength
  148. );
  149. let fileName = getFileNameUUID() + '.' + fileFormat;
  150. client(_that.dataObj)
  151. .multipartUpload(`tmp/${fileName}`, file.file, {
  152. progress: function(p) {
  153. // todo 每个文件一个进度条???如何限定关联的业务参数(幼儿园id、活动id)
  154. // p进度条的值
  155. console.log(p);
  156. _that.showProgress = true;
  157. _that.progress = Math.floor(p * 100);
  158. }
  159. })
  160. .then(result => {
  161. //上传成功返回值,可针对项目需求写其他逻辑
  162. // todo 这里每完成一个文件,会回调一次。这里请求服务端保存业务。照片信息 + 幼儿园 + 活动...
  163. console.log(result);
  164. _that.saveData(result);
  165. })
  166. .catch(err => {
  167. console.log('err:', err);
  168. });
  169. }
  170. multipartUpload();
  171. },
  172. async saveData(obj) {
  173. console.log(obj);
  174. const params = {
  175. kindergartenId: this.form.kindergartenId,
  176. activityId: this.form.activityId,
  177. fileRequest: {
  178. // todo 补充字段
  179. originalFilename: 'abc',
  180. size: 100,
  181. path: obj.name
  182. }
  183. };
  184. const { success, msg } = await saveItem(params);
  185. if (success) {
  186. }
  187. }
  188. }
  189. };
  190. </script>
  191. <style lang="scss" scoped></style>
  192. <style>
  193. .el-upload {
  194. width: 100%;
  195. }
  196. .el-upload-dragger {
  197. width: 100%;
  198. }
  199. </style>