25d8b336741d9239c352aca6473a9958286c514b.svn-base 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <template>
  2. <a-modal
  3. title="导入EXCEL"
  4. :width="600"
  5. :visible="visible"
  6. :confirmLoading="uploading"
  7. @cancel="handleClose">
  8. <div style="margin: 0px 0px 5px 1px" v-if="online">
  9. <span style="display: inline-block;height: 32px;line-height: 32px;vertical-align: middle;">是否开启校验:</span>
  10. <span style="display: inline-block;height: 32px;margin-left: 6px">
  11. <a-switch :checked="validateStatus==1" @change="handleChangeValidateStatus" checked-children="是" un-checked-children="否" size="small"/>
  12. </span>
  13. </div>
  14. <a-upload
  15. name="file"
  16. :multiple="true"
  17. accept=".xls,.xlsx"
  18. :fileList="fileList"
  19. :remove="handleRemove"
  20. :beforeUpload="beforeUpload">
  21. <a-button>
  22. <a-icon type="upload" />
  23. 选择导入文件
  24. </a-button>
  25. </a-upload>
  26. <template slot="footer">
  27. <a-button @click="handleClose">关闭</a-button>
  28. <a-button
  29. type="primary"
  30. @click="handleImport"
  31. :disabled="fileList.length === 0"
  32. :loading="uploading">
  33. {{ uploading ? '上传中...' : '开始上传' }}
  34. </a-button>
  35. </template>
  36. </a-modal>
  37. </template>
  38. <script>
  39. import { postAction } from '@/api/manage'
  40. export default {
  41. name: 'JImportModal',
  42. props:{
  43. url:{
  44. type: String,
  45. default: '',
  46. required: false
  47. },
  48. biz:{
  49. type: String,
  50. default: '',
  51. required: false
  52. },
  53. //是否online导入
  54. online:{
  55. type: Boolean,
  56. default: false,
  57. required: false
  58. }
  59. },
  60. data(){
  61. return {
  62. visible:false,
  63. uploading:false,
  64. fileList:[],
  65. uploadAction:'',
  66. foreignKeys:'',
  67. validateStatus: 0
  68. }
  69. },
  70. watch: {
  71. url (val) {
  72. if(val){
  73. this.uploadAction = window._CONFIG['domianURL']+val
  74. }
  75. }
  76. },
  77. created () {
  78. this.uploadAction = window._CONFIG['domianURL']+this.url
  79. },
  80. methods:{
  81. handleClose(){
  82. this.visible=false
  83. },
  84. show(arg){
  85. this.fileList = []
  86. this.uploading = false
  87. this.visible = true
  88. this.foreignKeys = arg;
  89. this.validateStatus = 0
  90. },
  91. handleRemove(file) {
  92. const index = this.fileList.indexOf(file);
  93. const newFileList = this.fileList.slice();
  94. newFileList.splice(index, 1);
  95. this.fileList = newFileList
  96. },
  97. beforeUpload(file) {
  98. this.fileList = [...this.fileList, file]
  99. return false;
  100. },
  101. handleImport() {
  102. const { fileList } = this;
  103. const formData = new FormData();
  104. if(this.biz){
  105. formData.append('isSingleTableImport',this.biz);
  106. }
  107. if(this.foreignKeys && this.foreignKeys.length>0){
  108. formData.append('foreignKeys',this.foreignKeys);
  109. }
  110. if(this.online==true){
  111. formData.append('validateStatus',this.validateStatus);
  112. }
  113. fileList.forEach((file) => {
  114. formData.append('files[]', file);
  115. });
  116. this.uploading = true
  117. postAction(this.uploadAction, formData).then((res) => {
  118. this.uploading = false
  119. if(res.success){
  120. if(res.code == 201){
  121. this.errorTip(res.message, res.result)
  122. }else{
  123. this.$message.success(res.message)
  124. }
  125. this.visible=false
  126. this.$emit('ok')
  127. }else{
  128. this.$message.warning(res.message)
  129. }
  130. })
  131. },
  132. // 是否开启校验 开关改变事件
  133. handleChangeValidateStatus(checked){
  134. this.validateStatus = checked==true?1:0
  135. },
  136. // 错误信息提示
  137. errorTip(tipMessage, fileUrl) {
  138. const h = this.$createElement;
  139. let href = window._CONFIG['domianURL'] + fileUrl
  140. this.$warning({
  141. title: '导入成功,但是有错误数据!',
  142. content: h('div', {}, [
  143. h('div', tipMessage),
  144. h('span', '具体详情请 '),
  145. h('a', {
  146. attrs: {
  147. href: href,
  148. target: '_blank'
  149. },
  150. },'点击下载'),
  151. ]),
  152. onOk() {},
  153. });
  154. },
  155. }
  156. }
  157. </script>
  158. <style scoped>
  159. </style>