ali-oss-multi.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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">
  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="图片">
  26. <el-row>
  27. <el-upload
  28. action
  29. :http-request="upload"
  30. :on-preview="handlePreview"
  31. :on-remove="handleRemove"
  32. :on-success="handleSuccess"
  33. :on-change="handleChange"
  34. :before-upload="beforeUpload"
  35. :before-remove="beforeRemove"
  36. multiple
  37. drag
  38. accept="image/*"
  39. :file-list="fileList"
  40. :limit="limit"
  41. :on-exceed="handleExceed"
  42. >
  43. <i class="el-icon-upload"></i>
  44. <div class="el-upload__text">
  45. 将照片拖到此处,或<em>点击上传</em>
  46. </div>
  47. <div slot="tip" class="el-upload__tip">
  48. <el-row>注意事项:</el-row>
  49. <el-row>1. 上传照片大小要在1MB ~ 100MB之间哦!</el-row><!-- todo -->
  50. </div>
  51. </el-upload>
  52. </el-row>
  53. <el-row style="text-align: right;">
  54. <el-button class="mt-40" size="small" type="primary" @click="close">确认</el-button>
  55. </el-row>
  56. </el-form-item>
  57. <el-progress
  58. v-show="showProgress"
  59. :text-inside="true"
  60. :stroke-width="15"
  61. :percentage="progress"
  62. ></el-progress>
  63. </el-form>
  64. </el-dialog>
  65. </template>
  66. <script>
  67. import { client, getFileNameUUID } from '@/utils/ali-oss'; // 前面的ali-js文件内的两个封装函数
  68. import { saveItem } from '@/api/photoWarehouse';
  69. export default {
  70. name: 'Upload',
  71. props: {
  72. limit: {
  73. type: Number,
  74. default: 100
  75. }
  76. },
  77. data() {
  78. return {
  79. title: '批量上传照片',
  80. modal: true,
  81. form: {
  82. kindergartenId: '',
  83. activityId: ''
  84. },
  85. rules: {
  86. kindergartenId: [
  87. { required: true, message: '请选择场景', trigger: 'blur' }
  88. ]
  89. },
  90. fileList: [], // 文件列
  91. showProgress: false, // 进度条的显示
  92. dataObj: {
  93. region: 'oss-cn-guangzhou',
  94. accessKeyId: 'LTAI5t8FMLXGzU4pTi9GXWb6',
  95. accessKeySecret: 'Hz6WOXIVNfpdMphYtf3laHbVoasGlT',
  96. bucket: 'yxl-kindergarten'
  97. }, // 存签名信息 todo
  98. progress: 0 // 进度条数据
  99. };
  100. },
  101. methods: {
  102. // 点击文件列表中已上传的文件时的钩子
  103. handlePreview(file) {},
  104. // 文件列表移除文件时的钩子
  105. handleRemove(file, fileList) {
  106. if (file && file.status==="success") {}
  107. },
  108. // 文件上传成功时的钩子
  109. handleSuccess(response, file, fileList) {
  110. this.fileList = fileList;
  111. // if (response.name) {
  112. // this.saveData(response.name);
  113. // }
  114. },
  115. handleChange(file, fileList) {
  116. },
  117. // 文件上传前的校验
  118. beforeUpload(file) {
  119. const isLt100M = file.size / 1024 / 1024 > 1 && file.size / 1024 / 1024 < 100;
  120. if (!isLt100M) {
  121. this.$message.error("上传照片大小要在1MB ~ 100MB之间哦!");
  122. return false;
  123. }
  124. const isLt30 = file.name.length < 30;
  125. if (!isLt30) {
  126. this.$message.error('上传照片名称长度必须要小于30个文字哦!');
  127. return false;
  128. }
  129. },
  130. // 删除文件之前的钩子
  131. beforeRemove(file, fileList) {
  132. if (file && file.status==="success") {
  133. return this.$confirm(`确定移除 ${file.name}?`);
  134. }
  135. },
  136. // 文件超出个数限制时的钩子
  137. handleExceed(files, fileList) {
  138. this.$message.warning(`每次只能上传 ${this.limit} 张照片`);
  139. },
  140. // http-request属性来覆盖默认的上传行为(即action="url"),自定义上传的实现
  141. upload(file) {
  142. const _that = this;
  143. this.$refs.form.validate(async valid => {
  144. if (!valid) {
  145. return false;
  146. }
  147. async function multipartUpload() {
  148. let temporary = file.file.name.lastIndexOf('.');
  149. let fileNameLength = file.file.name.length;
  150. let fileFormat = file.file.name.substring(
  151. temporary + 1,
  152. fileNameLength
  153. );
  154. let fileName = getFileNameUUID() + '.' + fileFormat;
  155. // https://github.com/ali-sdk/ali-oss#multipartuploadname-file-options
  156. client(_that.dataObj)
  157. .multipartUpload(`tmp/${fileName}`, file.file, {
  158. parallel: 4, // 同时处理分片
  159. partSize: 2 * 1024 * 1024, // 每个分片大小
  160. progress: function(p) { // p进度条的值
  161. // todo 每个文件一个进度条???如何限定的业务参数(幼儿园id、活动id)
  162. console.log(p);
  163. _that.showProgress = true;
  164. _that.progress = Math.floor(p * 100);
  165. }
  166. })
  167. .then(result => { // 上传成功返回值,可针对项目需求写其他逻辑
  168. _that.saveData(result, file.file.name, file.file.size);
  169. })
  170. .catch(err => {
  171. console.log('err:', err);
  172. });
  173. }
  174. multipartUpload();
  175. })
  176. },
  177. async saveData(obj, name, size) {
  178. const params = {
  179. kindergartenId: this.form.kindergartenId,
  180. activityId: this.form.activityId,
  181. fileRequest: {
  182. originalFilename: name,
  183. size: size,
  184. path: obj.name
  185. }
  186. };
  187. const { success, msg } = await saveItem(params);
  188. if (success) {
  189. }
  190. },
  191. close () {
  192. this.modal = false;
  193. this.$g_emit('photo_reload');
  194. }
  195. }
  196. };
  197. </script>
  198. <style lang="scss" scoped></style>
  199. <style>
  200. .el-upload {
  201. width: 100%;
  202. }
  203. .el-upload-dragger {
  204. width: 100%;
  205. }
  206. </style>