祥兆质量前端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
7.0 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. <template>
  2. <div class="customer-css">
  3. <el-dialog :title="titleCon" :close-on-click-modal="false" :visible.sync="visible" width="390px" style="height: 520px;" class="customer-dialog">
  4. <el-form :inline="true" label-position="top" label-width="80px">
  5. <el-button type="primary" @click="downloadFile()">下载文件模板</el-button>
  6. <el-row>
  7. <el-col :span="24">
  8. <el-upload class="customer-upload" drag action="javascript:void(0);" ref="uploadFile" :limit="1" accept=".xlsx,.xls"
  9. :before-upload="beforeUploadHandle" :on-change="onChange" :auto-upload="false" style="text-align: left;">
  10. <i class="el-icon-upload"></i>
  11. <div class="el-upload__text">将文件拖到此处<em>点击上传</em></div>
  12. </el-upload>
  13. </el-col>
  14. </el-row>
  15. </el-form>
  16. <span slot="footer" class="dialog-footer">
  17. <el-button type="primary" :loading="uploadLoading" @click="saveUploadFile()">保存</el-button>
  18. <el-button type="primary" @click="closeDialog">关闭</el-button>
  19. </span>
  20. </el-dialog>
  21. </div>
  22. </template>
  23. <script>
  24. import {
  25. uploadExcel, // 导入项目文件
  26. uploadTemplateExcel, // 导入模板文件
  27. uploadPartAttributeExcel, // 导入物料属性文件
  28. uploadFamilyAttributeExcel, // 导入类别属性文件
  29. queryFileId, // 查询文件ID
  30. } from "@/api/qc/qc.js"
  31. import { downLoadObjectFile } from '@/api/eam/eam_object_list.js'
  32. import axios from 'axios'
  33. import Vue from 'vue'
  34. export default {
  35. data() {
  36. return {
  37. titleCon: '文件导入',
  38. visible: false,
  39. fileList: [],
  40. uploadLoading: false,
  41. pageData: {
  42. flag: '',
  43. createBy: '',
  44. site: '',
  45. buNo: '',
  46. },
  47. }
  48. },
  49. methods: {
  50. // 初始化组件的参数
  51. init (currentRow) {
  52. // 获得类别
  53. this.pageData = JSON.parse(JSON.stringify(currentRow))
  54. // 打开页面
  55. this.visible = true
  56. },
  57. // 上传之前
  58. beforeUploadHandle (file) {
  59. let extName = file[0].name.substring(file[0].name.lastIndexOf('.')).toLowerCase()
  60. if (!(extName === '.xlsx' || extName === '.xls')) {
  61. this.$message.error('数据导入失败,请选择正确的xlsx模板文件')
  62. return false
  63. }
  64. },
  65. /*选择上传文件时*/
  66. onChange (file) {
  67. this.fileList.push(file)
  68. },
  69. /*关闭modal*/
  70. closeDialog () {
  71. this.fileList = []
  72. this.$refs.uploadFile.clearFiles()
  73. // 刷新报工的页面
  74. this.$emit('refreshPageTables')
  75. // 关闭当前的页面
  76. this.visible = false
  77. },
  78. /*保修当前的数据*/
  79. saveUploadFile () {
  80. this.uploadLoading = true
  81. /*判断文件是否上传*/
  82. if (null == this.fileList || 0 == this.fileList.length) {
  83. this.$message.error("请先上传文件!")
  84. this.uploadLoading = false
  85. return false
  86. }
  87. const formData = new FormData()
  88. formData.append("file", this.fileList[0].raw)
  89. formData.append("createBy", this.pageData.createBy)
  90. formData.append("site", this.pageData.site)
  91. formData.append("buNo", this.pageData.buNo)
  92. // 根据 flag 发不同的接口
  93. let promise
  94. if (this.pageData.flag === 'item') {
  95. promise = uploadExcel(formData)
  96. } else if (this.pageData.flag === 'template') {
  97. promise = uploadTemplateExcel(formData)
  98. } else if (this.pageData.flag === 'partAttribute') {
  99. promise = uploadPartAttributeExcel(formData)
  100. } else if (this.pageData.flag === 'familyAttribute') {
  101. promise = uploadFamilyAttributeExcel(formData)
  102. }
  103. promise
  104. .then(({ data }) => {
  105. if (data.code === 0) {
  106. this.$message.success(data.msg)
  107. this.$refs.uploadFile.clearFiles()
  108. this.closeDialog()
  109. } else {
  110. this.$message.warning(data.msg)
  111. }
  112. })
  113. .catch(err => {
  114. this.$message.error("上传失败,请稍后重试")
  115. console.error(err)
  116. })
  117. .finally(() => {
  118. this.uploadLoading = false // 请求结束时再关 loading
  119. })
  120. },
  121. // 下载
  122. async downloadFile () {
  123. let file = {
  124. id: 0,
  125. fileName: ''
  126. }
  127. let tempData = {
  128. orderRef1: '',
  129. orderRef2: ''
  130. }
  131. if (this.pageData.flag === 'item') { // 检验项目
  132. tempData.orderRef1 = 'qc';
  133. tempData.orderRef2 = 'itemFormat';
  134. await queryFileId(tempData).then(({data}) => {
  135. if (data && data.code === 0) {
  136. file.id = data.data.id
  137. file.fileName = data.data.fileName
  138. } else {
  139. this.$alert(data.msg, '错误', {
  140. confirmButtonText: '确定'
  141. })
  142. }
  143. })
  144. }else if (this.pageData.flag === 'template'){ // 检验模板
  145. tempData.orderRef1 = 'qc';
  146. tempData.orderRef2 = 'templateFormat';
  147. await queryFileId(tempData).then(({data}) => {
  148. if (data && data.code === 0) {
  149. file.id = data.data.id
  150. file.fileName = data.data.fileName
  151. } else {
  152. this.$alert(data.msg, '错误', {
  153. confirmButtonText: '确定'
  154. })
  155. }
  156. })
  157. }else if (this.pageData.flag === 'partAttribute'){ // 物料属性设置
  158. tempData.orderRef1 = 'qc';
  159. tempData.orderRef2 = 'partAttributeFormat';
  160. await queryFileId(tempData).then(({data}) => {
  161. if (data && data.code === 0) {
  162. file.id = data.data.id
  163. file.fileName = data.data.fileName
  164. } else {
  165. this.$alert(data.msg, '错误', {
  166. confirmButtonText: '确定'
  167. })
  168. }
  169. })
  170. }else if (this.pageData.flag === 'familyAttribute'){ // 类别属性设置
  171. tempData.orderRef1 = 'qc';
  172. tempData.orderRef2 = 'familyAttributeFormat';
  173. await queryFileId(tempData).then(({data}) => {
  174. if (data && data.code === 0) {
  175. file.id = data.data.id
  176. file.fileName = data.data.fileName
  177. } else {
  178. this.$alert(data.msg, '错误', {
  179. confirmButtonText: '确定'
  180. })
  181. }
  182. })
  183. }
  184. await downLoadObjectFile(file).then(({data}) => {
  185. // 不限制文件下载类型
  186. const blob = new Blob([data], {type: "application/octet-stream"})
  187. // 下载文件名称
  188. const fileName = file.fileName
  189. // a标签下载
  190. const linkNode = document.createElement('a')
  191. // a标签的download属性规定下载文件的名称
  192. linkNode.download = fileName
  193. linkNode.style.display = 'none'
  194. // 生成一个Blob URL
  195. linkNode.href = URL.createObjectURL(blob)
  196. document.body.appendChild(linkNode)
  197. // 模拟在按钮上的一次鼠标单击
  198. linkNode.click()
  199. // 释放URL 对象
  200. URL.revokeObjectURL(linkNode.href)
  201. document.body.removeChild(linkNode)
  202. })
  203. },
  204. }
  205. }
  206. </script>
  207. <style scoped lang="scss">
  208. </style>