index.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <!--
  2. * @FilePath: index.vue
  3. * @Author: 旭颖
  4. * @Date: 2023-02-03 11:12:51
  5. * @LastEditors: Please set LastEditors
  6. * @LastEditTime: 2023-04-29 22:55:58
  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-form ref="formRef" label-width="110px" :model="form" :rules="rules">
  18. <el-form-item label="文件模版类型" prop="type">
  19. <el-select
  20. v-model="form.type"
  21. placeholder="请选择文件导入模版类型"
  22. style="width: 100%"
  23. @change="changeType"
  24. >
  25. <el-option
  26. v-for="item in typeList"
  27. :key="item.id"
  28. :label="item.name"
  29. :value="item.id"
  30. />
  31. </el-select>
  32. </el-form-item>
  33. <el-form-item label="文件名称" prop="name">
  34. <el-input v-model="form.name" placeholder="请输入文件名称" />
  35. </el-form-item>
  36. <el-form-item>
  37. <div style="width: 100%">
  38. <el-upload
  39. accept=".xlsx"
  40. :action="action"
  41. class="upload-demo"
  42. :data="uploadData"
  43. drag
  44. :file-list="fileList"
  45. :headers="header"
  46. multiple
  47. :on-error="handleError"
  48. :on-remove="handleRemove"
  49. :on-success="handleSuccess"
  50. >
  51. <el-icon class="el-icon--upload"><upload-filled /></el-icon>
  52. <div class="el-upload__text">
  53. 将文件拖拽到此处或
  54. <em>点击上传</em>
  55. </div>
  56. </el-upload>
  57. </div>
  58. </el-form-item>
  59. <el-form-item>
  60. <div class="model">
  61. <a :download="modelName" :href="modelUrl">
  62. 点击下载模板:{{ modelName }}
  63. </a>
  64. </div>
  65. </el-form-item>
  66. </el-form>
  67. </div>
  68. <div class="model">
  69. <div class="import-msg">
  70. <p style="margin-bottom: 5px">注意:</p>
  71. <p style="color:red">1.只可上传xlsx类型的表格文件</p>
  72. <p>2.请严格按照模板中提供的数据案例格式进行填写;</p>
  73. <p>3.填写错误可能会导致数据无法导入成功!</p>
  74. </div>
  75. </div>
  76. <template #footer>
  77. <el-button @click="close">取 消</el-button>
  78. <el-button type="primary" @click="batchImport">确 定</el-button>
  79. </template>
  80. </el-dialog>
  81. </template>
  82. <script>
  83. import { accountImport, trmplateOptions } from './api/index'
  84. import { baseURL, fileUrl } from '@/config'
  85. import { useUserStore } from '@/store/modules/user'
  86. export default defineComponent({
  87. name: 'BatchSend',
  88. emits: ['fetch-data'],
  89. setup(props, { emit }) {
  90. const $baseMessage = inject('$baseMessage')
  91. const state = reactive({
  92. formRef: null,
  93. treeRef: null,
  94. action: '', //上传文件地址
  95. uploadData: { file_type: 'file', dir: 'account' },
  96. fileList: [],
  97. header: {},
  98. modelUrl: '', //模版地址
  99. modelName: '', //模版名称
  100. form: {
  101. name: '',
  102. type: 1,
  103. resource_id: '',
  104. status: 1,
  105. },
  106. title: '',
  107. dialogFormVisible: false,
  108. apiUrl: '',
  109. rules: {
  110. name: [
  111. { required: true, trigger: 'blur', message: '请输入文件名称' },
  112. ],
  113. type: [
  114. {
  115. required: true,
  116. trigger: 'change',
  117. message: '请选择文件导入模版类型',
  118. },
  119. ],
  120. },
  121. typeList: [
  122. {
  123. id: '',
  124. name: '标准模版',
  125. },
  126. {
  127. id: '',
  128. name: '信访投诉导入模版',
  129. },
  130. {
  131. id: '',
  132. name: '市12345投诉导入末班',
  133. },
  134. ], //类型列表
  135. })
  136. const templateOptionsList = async () => {
  137. const { data } = await trmplateOptions()
  138. state.typeList = data
  139. }
  140. //选择上传文件末班类型
  141. const changeType = (e) => {
  142. state.typeList.map((item) => {
  143. if (item.id == e) {
  144. state.type = e
  145. state.modelName = item.name
  146. state.modelUrl = item.url
  147. }
  148. })
  149. }
  150. const showEdit = (row) => {
  151. const userStore = useUserStore()
  152. templateOptionsList()
  153. const { token } = userStore
  154. state.header.Authorization = token
  155. state.action = `${baseURL}/base/resource/upload`
  156. state.modelUrl = fileUrl + row.modelUrl //模版地址
  157. state.modelName = row.name //模版名称
  158. state.uploadData.dir = row.dirName
  159. state.apiUrl = row.url
  160. state.dialogFormVisible = true
  161. }
  162. const close = () => {
  163. state.form={
  164. name: '',
  165. type: 1,
  166. resource_id: '',
  167. status: 1,
  168. }
  169. state.fileList = []
  170. state.dialogFormVisible = false
  171. }
  172. //文件上传失败
  173. const handleError = () => {
  174. $baseMessage('文件上传失败', 'danger', 'vab-hey-message-danger')
  175. }
  176. //移除已上传文件
  177. const handleRemove = () => {
  178. state.form.resource_id = ''
  179. }
  180. //文件上传成功
  181. const handleSuccess = (res) => {
  182. state.form.resource_id = res.data.id
  183. }
  184. const batchImport = async () => {
  185. if (!state.form.resource_id) {
  186. $baseMessage('上传需要导入的文件', 'error', 'vab-hey-message-error')
  187. return
  188. }
  189. const url = state.apiUrl
  190. const { message } = await accountImport(url, state.form)
  191. $baseMessage(message, 'success', 'vab-hey-message-success')
  192. emit('fetch-data')
  193. close()
  194. }
  195. onMounted(() => {})
  196. return {
  197. ...toRefs(state),
  198. showEdit,
  199. close,
  200. batchImport,
  201. handleError,
  202. handleRemove,
  203. changeType,
  204. templateOptionsList,
  205. handleSuccess,
  206. }
  207. },
  208. })
  209. </script>
  210. <style lang="scss" scoped>
  211. .vab-tree-border {
  212. width: 100%;
  213. height: 250px;
  214. padding: $base-padding;
  215. overflow-y: auto;
  216. border: 1px solid #dcdfe6;
  217. border-radius: $base-border-radius;
  218. }
  219. .upload {
  220. display: flex;
  221. .model {
  222. margin-left: 15px;
  223. }
  224. }
  225. </style>