赫艾前端
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.

313 lines
10 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <template>
  2. <div class="customer-css">
  3. <el-dialog :title="titleCon" :close-on-click-modal="false" :visible.sync="visible" width="390px" class="customer-dialog">
  4. <el-form :inline="true" label-position="top" label-width="80px">
  5. <div class="upload-toolbar">
  6. <el-button type="primary" @click="downloadFile()">下载文件模板</el-button>
  7. <el-select v-if="isMasterImport" filterable v-model="selectedSite" placeholder="请选择工厂" style="width: 180px">
  8. <el-option
  9. v-for="item in siteList"
  10. :key="item.siteID"
  11. :label="item.shortSiteName"
  12. :value="item.siteID">
  13. </el-option>
  14. </el-select>
  15. </div>
  16. <el-form-item v-if="needSelectBu" label="BU">
  17. <el-select v-model="bu" placeholder="请选择" style="width: 295px">
  18. <el-option
  19. v-for="i in userBuList"
  20. :key="i.buNo"
  21. :label="i.sitename"
  22. :value="i.buNo">
  23. <span style="float: left;width: 100px">{{ i.sitename }}</span>
  24. <span style="float: right; color: #8492a6;white-space:nowrap;overflow:hidden;text-overflow:ellipsis; font-size: 11px;">
  25. {{ i.buDesc }}
  26. </span>
  27. </el-option>
  28. </el-select>
  29. </el-form-item>
  30. <el-row>
  31. <el-col :span="24">
  32. <el-upload class="customer-upload" drag action="javascript:void(0);" ref="uploadFile" :limit="1" accept=".xlsx,.xls"
  33. :before-upload="beforeUploadHandle" :on-change="onChange" :on-remove="onRemove" :auto-upload="false" style="text-align: left;">
  34. <i class="el-icon-upload"></i>
  35. <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
  36. </el-upload>
  37. </el-col>
  38. </el-row>
  39. </el-form>
  40. <span slot="footer" class="dialog-footer">
  41. <el-button type="primary" :loading="saveLoading" @click="saveUploadFile()">保存</el-button>
  42. <el-button type="primary" @click="closeDialog">关闭</el-button>
  43. </span>
  44. </el-dialog>
  45. </div>
  46. </template>
  47. <script>
  48. import {
  49. uploadExcel, // 导入项目文件
  50. uploadTemplateExcel, // 导入模板文件
  51. uploadTemplateItemExcel, // 导入模板检验项目
  52. uploadMethodExcel, // 导入方法文件
  53. uploadPartAttributeExcel, // 导入物料属性文件
  54. uploadPartAttrExcel,
  55. uploadPartTemplateExcel,
  56. uploadPartStandardExcel,
  57. uploadFamilyAttributeExcel, // 导入类别属性文件
  58. downloadQcImportTemplate,
  59. queryFileId, // 查询文件ID
  60. getSiteAndBuByUserName
  61. } from "@/api/qc/qc.js"
  62. import { downLoadObjectFile } from '@/api/eam/eam_object_list.js'
  63. import { getSiteData } from '@/api/board.js'
  64. export default {
  65. data() {
  66. return {
  67. titleCon: '文件导入',
  68. visible: false,
  69. fileList: [],
  70. bu: '',
  71. selectedSite: '',
  72. siteList: [],
  73. userBuList: [],
  74. saveLoading: false,
  75. pageData: {
  76. flag: '',
  77. createBy: '',
  78. site: ''
  79. },
  80. }
  81. },
  82. computed: {
  83. isMasterImport() {
  84. return ['item', 'method', 'template', 'templateItem', 'partAttr', 'partTemplate', 'partStandard'].indexOf(this.pageData.flag) !== -1
  85. },
  86. needSelectBu() {
  87. return !this.isMasterImport
  88. },
  89. importSite() {
  90. return this.selectedSite || ''
  91. }
  92. },
  93. created(){
  94. this.getSiteAndBu();
  95. this.loadSiteList()
  96. },
  97. methods: {
  98. loadSiteList () {
  99. return getSiteData({ userID: this.$store.state.user.name }).then(({data}) => {
  100. this.siteList = (data && data.rows) ? data.rows : []
  101. if (!this.selectedSite && this.siteList.length) {
  102. this.selectedSite = this.siteList[0].siteID
  103. }
  104. })
  105. },
  106. getDefaultSite () {
  107. return (this.siteList && this.siteList.length) ? this.siteList[0].siteID : ''
  108. },
  109. getSiteAndBu () {
  110. let tempData = {
  111. username: this.$store.state.user.name,
  112. }
  113. getSiteAndBuByUserName(tempData).then(({data}) => {
  114. if (data.code === 0) {
  115. this.userBuList = data.rows
  116. }
  117. })
  118. },
  119. init(currentRow) {
  120. this.pageData = JSON.parse(JSON.stringify(currentRow))
  121. this.titleCon = this.pageData.title || '文件导入'
  122. this.fileList = []
  123. this.bu = ''
  124. this.selectedSite = this.getDefaultSite()
  125. this.saveLoading = false
  126. this.visible = true
  127. this.loadSiteList()
  128. this.$nextTick(() => {
  129. if (this.$refs.uploadFile) {
  130. this.$refs.uploadFile.clearFiles()
  131. }
  132. })
  133. },
  134. beforeUploadHandle(file) {
  135. const name = file && file.name ? file.name : ''
  136. let extName = name.substring(name.lastIndexOf('.')).toLowerCase()
  137. if (!(extName === '.xlsx' || extName === '.xls')) {
  138. this.$message.error('数据导入失败,请选择正确的xlsx模板文件')
  139. return false
  140. }
  141. },
  142. onChange(file, fileList) {
  143. this.fileList = fileList.slice(-1)
  144. },
  145. onRemove() {
  146. this.fileList = []
  147. },
  148. closeDialog(){
  149. this.fileList = []
  150. this.bu = ''
  151. this.selectedSite = this.getDefaultSite()
  152. this.saveLoading = false
  153. this.$emit('refreshPageTables');
  154. this.visible = false;
  155. },
  156. showImportError(msg) {
  157. const text = msg || '导入失败'
  158. if (text.indexOf('<br/>') !== -1 || text.indexOf('<br>') !== -1) {
  159. this.$alert(text, '导入失败', {
  160. confirmButtonText: '确定',
  161. dangerouslyUseHTMLString: true
  162. })
  163. } else {
  164. this.$message.warning(text)
  165. }
  166. },
  167. handleImportResult(data) {
  168. if (data && data.code === 0) {
  169. this.$message.success(data.msg || '导入成功')
  170. if (this.$refs.uploadFile) {
  171. this.$refs.uploadFile.clearFiles()
  172. }
  173. this.closeDialog()
  174. } else {
  175. this.showImportError(data && data.msg)
  176. }
  177. },
  178. saveUploadFile(){
  179. if (this.needSelectBu && (this.bu === '' || this.bu == null)){
  180. this.$message.warning("请选择BU!");
  181. return false;
  182. }
  183. if (this.isMasterImport && !this.importSite) {
  184. this.$message.warning("请选择工厂");
  185. return false;
  186. }
  187. if (this.fileList == null || this.fileList.length === 0){
  188. this.$message.error("请先上传文件!");
  189. return false;
  190. }
  191. const formData = new FormData();
  192. formData.append("file", this.fileList[0].raw);
  193. formData.append("createBy", this.pageData.createBy);
  194. if (this.isMasterImport) {
  195. formData.append("site", this.importSite);
  196. } else if (this.pageData.flag === 'partAttribute') {
  197. formData.append("site", this.bu);
  198. } else {
  199. formData.append("site", this.bu.split('_')[0]);
  200. }
  201. let request
  202. if (this.pageData.flag === 'item'){
  203. request = uploadExcel(formData)
  204. } else if (this.pageData.flag === 'method'){
  205. request = uploadMethodExcel(formData)
  206. } else if (this.pageData.flag === 'template'){
  207. request = uploadTemplateExcel(formData)
  208. } else if (this.pageData.flag === 'templateItem'){
  209. request = uploadTemplateItemExcel(formData)
  210. } else if (this.pageData.flag === 'partAttr'){
  211. request = uploadPartAttrExcel(formData)
  212. } else if (this.pageData.flag === 'partTemplate'){
  213. request = uploadPartTemplateExcel(formData)
  214. } else if (this.pageData.flag === 'partStandard'){
  215. request = uploadPartStandardExcel(formData)
  216. } else if (this.pageData.flag === 'partAttribute'){
  217. request = uploadPartAttributeExcel(formData)
  218. } else if (this.pageData.flag === 'familyAttribute'){
  219. request = uploadFamilyAttributeExcel(formData)
  220. } else {
  221. this.$message.warning('不支持的导入类型')
  222. return false
  223. }
  224. this.saveLoading = true
  225. request.then(({data}) => {
  226. this.saveLoading = false
  227. this.handleImportResult(data)
  228. }).catch(() => {
  229. this.saveLoading = false
  230. this.$message.error('导入失败,请检查文件格式和内容')
  231. })
  232. },
  233. bindDownloadBlob(data, fileName) {
  234. const blob = new Blob([data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"})
  235. const linkNode = document.createElement('a')
  236. linkNode.download = fileName
  237. linkNode.style.display = 'none'
  238. linkNode.href = URL.createObjectURL(blob)
  239. document.body.appendChild(linkNode)
  240. linkNode.click()
  241. URL.revokeObjectURL(linkNode.href)
  242. document.body.removeChild(linkNode)
  243. },
  244. async downloadFile(){
  245. const dynamicFlags = ['item', 'template', 'templateItem', 'method', 'partAttr', 'partTemplate', 'partStandard']
  246. if (dynamicFlags.indexOf(this.pageData.flag) !== -1) {
  247. const fileNameMap = {
  248. item: '检验项目导入模板.xlsx',
  249. template: '检验模板导入模板.xlsx',
  250. templateItem: '模板检验项目导入模板.xlsx',
  251. method: '检验方法导入模板.xlsx',
  252. partAttr: '物料属性导入模板.xlsx',
  253. partTemplate: '物料检验模板导入模板.xlsx',
  254. partStandard: '物料检验标准导入模板.xlsx'
  255. }
  256. if (!this.importSite) {
  257. this.$message.warning("请选择工厂")
  258. return
  259. }
  260. await downloadQcImportTemplate(this.pageData.flag, this.importSite).then(({data}) => {
  261. this.bindDownloadBlob(data, fileNameMap[this.pageData.flag])
  262. })
  263. return
  264. }
  265. let file = {
  266. id: 0,
  267. fileName: ''
  268. }
  269. let tempData = {
  270. orderRef1: '',
  271. orderRef2: ''
  272. }
  273. if (this.pageData.flag === 'partAttribute'){
  274. tempData.orderRef1 = 'qc';
  275. tempData.orderRef2 = 'partAttributeFormat';
  276. } else if (this.pageData.flag === 'familyAttribute'){
  277. tempData.orderRef1 = 'qc';
  278. tempData.orderRef2 = 'familyAttributeFormat';
  279. } else {
  280. this.$message.warning('未找到对应的导入模板')
  281. return
  282. }
  283. await queryFileId(tempData).then(({data}) => {
  284. if (data && data.code === 0) {
  285. file.id = data.data.id
  286. file.fileName = data.data.fileName
  287. } else {
  288. this.$alert(data.msg, '错误', {
  289. confirmButtonText: '确定'
  290. })
  291. }
  292. })
  293. if (!file.id) {
  294. return
  295. }
  296. await downLoadObjectFile(file).then(({data}) => {
  297. this.bindDownloadBlob(data, file.fileName)
  298. })
  299. },
  300. }
  301. }
  302. </script>
  303. <style scoped lang="scss">
  304. .upload-toolbar {
  305. display: flex;
  306. align-items: center;
  307. margin-bottom: 10px;
  308. .el-button {
  309. margin-right: 8px;
  310. }
  311. }
  312. </style>