index.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <!--
  2. * @FilePath: index.vue
  3. * @Author: 旭颖
  4. * @Date: 2023-02-03 11:12:51
  5. * @LastEditors: Please set LastEditors
  6. * @LastEditTime: 2023-05-26 20:42:41
  7. -->
  8. <!--批量导入-->
  9. <template>
  10. <el-dialog
  11. v-model="dialogFormVisible"
  12. :before-close="close"
  13. title="账号导入"
  14. width="800px"
  15. >
  16. <div>
  17. <el-upload
  18. accept=".xls,.xlsx"
  19. :action="action"
  20. class="upload-demo"
  21. :data="uploadData"
  22. drag
  23. :file-list="fileList"
  24. :headers="header"
  25. multiple
  26. :on-error="handleError"
  27. :on-remove="handleRemove"
  28. :on-success="handleSuccess"
  29. >
  30. <el-icon class="el-icon--upload"><upload-filled /></el-icon>
  31. <div class="el-upload__text">
  32. 将文件拖拽到此处或
  33. <em>点击上传</em>
  34. </div>
  35. </el-upload>
  36. <div class="model">
  37. <a :download="modelName" :href="modelUrl">
  38. 点击下载模板:{{ modelName }}
  39. </a>
  40. </div>
  41. </div>
  42. <div class="model">
  43. <div class="import-msg">
  44. <p style="margin-bottom: 5px">注意:</p>
  45. <p>1.请严格按照模板中提供的数据案例格式进行填写;</p>
  46. <p>2.填写错误可能会导致数据无法导入成功!</p>
  47. </div>
  48. </div>
  49. <template #footer>
  50. <el-button @click="close">取 消</el-button>
  51. <el-button type="primary" @click="batchImport">确 定</el-button>
  52. </template>
  53. </el-dialog>
  54. </template>
  55. <script>
  56. import { accountImport } from './api/index'
  57. import { baseURL, fileUrl } from '@/config'
  58. import { useUserStore } from '@/store/modules/user'
  59. export default defineComponent({
  60. name: 'BatchSend',
  61. emits: ['fetch-data'],
  62. setup(props, { emit }) {
  63. const $baseMessage = inject('$baseMessage')
  64. const state = reactive({
  65. formRef: null,
  66. treeRef: null,
  67. action: '', //上传文件地址
  68. uploadData: { file_type: 'file', dir: 'account' },
  69. fileList: [],
  70. header: {},
  71. modelUrl: '', //模版地址
  72. modelName: '', //模版名称
  73. title: '',
  74. dialogFormVisible: false,
  75. resource_id: '',
  76. apiUrl: '',
  77. type: -1, //类型供应商导入时需要
  78. })
  79. const showEdit = (row) => {
  80. const userStore = useUserStore()
  81. const { token } = userStore
  82. state.header.Authorization = token
  83. state.action = baseURL + '/base/resource/upload'
  84. state.modelUrl = fileUrl + row.modelUrl //模版地址
  85. state.modelName = row.name //模版名称
  86. state.apiUrl = row.url
  87. if (row.type) {
  88. state.type = row.type
  89. }
  90. state.dialogFormVisible = true
  91. }
  92. const close = () => {
  93. state.resource_id = ''
  94. state.fileList = []
  95. state.dialogFormVisible = false
  96. }
  97. //文件上传失败
  98. const handleError = () => {
  99. $baseMessage('文件上传失败', 'danger', 'vab-hey-message-danger')
  100. }
  101. //移除已上传文件
  102. const handleRemove = () => {
  103. state.resource_id = ''
  104. }
  105. //文件上传成功
  106. const handleSuccess = (res) => {
  107. state.resource_id = res.data.id
  108. }
  109. const batchImport = async () => {
  110. if (!state.resource_id) {
  111. $baseMessage('上传需要导入的文件', 'error', 'vab-hey-message-error')
  112. return
  113. }
  114. const url = state.apiUrl
  115. let data = {}
  116. if (state.type !== -1) {
  117. data = {
  118. resource_id: state.resource_id,
  119. type: state.type,
  120. }
  121. } else {
  122. data = {
  123. resource_id: state.resource_id,
  124. }
  125. }
  126. const { message } = await accountImport(url, data)
  127. $baseMessage(message, 'success', 'vab-hey-message-success')
  128. emit('fetch-data')
  129. close()
  130. }
  131. onMounted(() => {})
  132. return {
  133. ...toRefs(state),
  134. showEdit,
  135. close,
  136. batchImport,
  137. handleError,
  138. handleRemove,
  139. handleSuccess,
  140. }
  141. },
  142. })
  143. </script>
  144. <style lang="scss" scoped>
  145. .vab-tree-border {
  146. width: 100%;
  147. height: 250px;
  148. padding: $base-padding;
  149. overflow-y: auto;
  150. border: 1px solid #dcdfe6;
  151. border-radius: $base-border-radius;
  152. }
  153. .upload {
  154. display: flex;
  155. .model {
  156. margin-left: 15px;
  157. }
  158. }
  159. </style>