upload.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <template>
  2. <div class="addFileUpdate">
  3. <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
  4. <!-- <el-form-item label="版本号" prop="version">
  5. <el-input v-model="ruleForm.version" style="width: 200px;"></el-input>
  6. </el-form-item> -->
  7. <!-- <el-form-item label="更新日志" prop="log">
  8. <el-input type="textarea" v-model="ruleForm.log" style="width:500px;"></el-input>
  9. </el-form-item> -->
  10. <el-form-item label="gameid" prop="game_id">
  11. <el-input type="textarea" v-model="ruleForm.game_id" style="width:500px;"></el-input>
  12. </el-form-item>
  13. <el-form-item label="上传文件" prop="file">
  14. <el-upload ref="upload" class="upload-demo" action="/manager/appupdate/addFileUpdate" :headers="headers"
  15. :http-request="httpRequest" :before-remove="beforeRemove" :before-upload="beforeUploadFile" :on-exceed="handleExceed"
  16. multiple :limit="1" :auto-upload="false" :on-change="getFile" :data="ruleForm" :file-list="fileList" name="annexFile"
  17. style="width: 500px;">
  18. <el-button size="small" type="primary">点击上传</el-button>
  19. <div slot="tip" class="el-upload__tip">{{message}}</div>
  20. </el-upload>
  21. </el-form-item>
  22. <el-form-item>
  23. <el-button type="primary" @click="submitForm('ruleForm')" v-loading.fullscreen.lock="fullscreenLoading">立即创建</el-button>
  24. <el-button @click="resetForm('ruleForm')">重置</el-button>
  25. </el-form-item>
  26. </el-form>
  27. </div>
  28. </template>
  29. <script>
  30. import {
  31. addFileUpdate
  32. } from "../../api/upload.js"
  33. export default {
  34. name: 'AddFileUpdate',
  35. data() {
  36. return {
  37. fullscreenLoading: false,
  38. //表单
  39. ruleForm: {
  40. game_id: '' //游戏id
  41. },
  42. //上传
  43. headers: {
  44. // token: getStore('zxdAdmintoken'),
  45. "content-type": "multipart/form-data"
  46. },
  47. message: '请上传dll文件',
  48. fileList: [], //文件列表
  49. fd: {}, //用于放数据 FormData类型
  50. //校验规则
  51. rules: {
  52. game_id: [{
  53. required: true,
  54. message: '请输入游戏id',
  55. trigger: 'blur'
  56. }, ],
  57. // log: [{
  58. // required: true,
  59. // message: '请填写更新日志',
  60. // trigger: 'blur'
  61. // }]
  62. }
  63. }
  64. },
  65. methods: {
  66. //上传
  67. getFile(file, fileList) {
  68. this.fileList = fileList;
  69. // console.log(this.fileList)
  70. const fd = new FormData() // FormData 对象
  71. this.fd = fd
  72. },
  73. //上传前
  74. beforeUploadFile(file) {
  75. let extension = file.name.substring(file.name.lastIndexOf('.') + 1);
  76. console.log(extension)
  77. if (extension !== 'dll') {
  78. // this.$message.warning('只能上传后缀是.zip/.rar/.apk的文件'); //控制文件类型
  79. this.$message.warning('文件类型不对'); //控制文件类型
  80. return false
  81. }
  82. },
  83. //超限制
  84. handleExceed(files, fileList) {
  85. this.$message.warning("目前只能上传一款脚本")
  86. },
  87. //移除
  88. beforeRemove(file, fileList) {
  89. let extension = file.name.substring(file.name.lastIndexOf('.') + 1);
  90. if (extension !== 'dll') {
  91. return
  92. }
  93. return this.$confirm(`确定移除 ${file.name}?`)
  94. },
  95. httpRequest(param) {
  96. const fileObj = param.file // 相当于input里取得的files
  97. this.fd.append('file', fileObj) // 文件对象
  98. console.log("文件包" + this.fd.get('file'));
  99. },
  100. //提交
  101. submitForm(formName) {
  102. // let fileArr = this.$refs.upload.uploadFiles;
  103. // console.log(fileArr)
  104. this.$refs[formName].validate((valid) => {
  105. if (valid) {
  106. if (this.fileList.length <= 0) {
  107. this.$message.error("至少上传一款脚本!");
  108. return;
  109. }
  110. this.$refs.upload.submit();
  111. //换行自动添加为<br/>
  112. // this.ruleForm.log= this.ruleForm.log.replace(/\n/g,"<br/>");
  113. // console.log(this.ruleForm.log)
  114. //将表单内其他内容添加进fd
  115. this.fd.append('game_id', this.ruleForm.game_id)
  116. // this.fd.append('log', this.ruleForm.log)
  117. this.fd.append('type', "0")
  118. this.fullscreenLoading = true;
  119. //调用后端接口,提交即可
  120. addFileUpdate(this.fd).then(data => {
  121. console.log(data)
  122. if (data.code == 0) {
  123. this.fullscreenLoading = false;
  124. this.$message({
  125. message: '上传成功',
  126. type: 'success'
  127. });
  128. this.fd = {}
  129. this.fileList = []
  130. resetForm(formName)
  131. } else {
  132. this.$message.error("上传失败");
  133. }
  134. })
  135. } else {
  136. // console.log('error submit!!');
  137. return false;
  138. }
  139. });
  140. },
  141. //重置
  142. resetForm(formName) {
  143. this.$refs[formName].resetFields();
  144. this.$refs.upload.clearFiles()
  145. },
  146. }
  147. }
  148. </script>