upload.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <template>
  2. <div>
  3. <el-dialog
  4. v-model="dialogVisible"
  5. width="750px"
  6. title="Upload Statement"
  7. :show-close="false"
  8. :close-on-click-modal="false"
  9. :close-on-press-escape="false"
  10. :before-close="handleClose"
  11. >
  12. <div
  13. v-loading="loading"
  14. class="flex start"
  15. >
  16. <el-form
  17. ref="mainForm"
  18. :rules="formRule"
  19. class="flex-auto"
  20. :model="form"
  21. label-width="150px"
  22. >
  23. <el-form-item
  24. label="Statement Name"
  25. prop="statement_name"
  26. >
  27. <el-input
  28. v-model="form.statement_name"
  29. style="width: 190px"
  30. type="textarea"
  31. rows="3"
  32. />
  33. </el-form-item>
  34. <el-form-item label="Currency">
  35. <el-select v-model="form.currency">
  36. <el-option
  37. v-for="option in currencyList as IOptionItem[]"
  38. :key="option.value"
  39. :label="option.label"
  40. :value="option.value"
  41. ></el-option>
  42. </el-select>
  43. </el-form-item>
  44. <el-form-item label="Payment Type">
  45. <el-select
  46. v-model="form.paymentType"
  47. disabled
  48. >
  49. <el-option
  50. v-for="option in paymentOption"
  51. :key="option.value"
  52. :label="option.label"
  53. :value="option.value"
  54. ></el-option>
  55. </el-select>
  56. </el-form-item>
  57. <el-form-item
  58. v-if="form.paymentType === '国内运费'"
  59. label="Weight Unit"
  60. >
  61. <el-select v-model="form.weightUnit">
  62. <el-option
  63. v-for="option in weightOption"
  64. :key="option.value"
  65. :label="option.label"
  66. :value="option.value"
  67. ></el-option>
  68. </el-select>
  69. </el-form-item>
  70. <el-form-item label="Upload Mode">
  71. <el-select
  72. v-model="form.mode"
  73. disabled
  74. >
  75. <el-option
  76. v-for="option in uploadOption"
  77. :key="option.value"
  78. :label="option.label"
  79. :value="option.value"
  80. ></el-option>
  81. </el-select>
  82. </el-form-item>
  83. </el-form>
  84. <div
  85. class="flex-auto drag-area"
  86. @dragenter="stop"
  87. @dragover="stop"
  88. @dragleave="stop"
  89. @drop="processExcel"
  90. >
  91. <label for="fileInput">
  92. <div
  93. class="flex column stretch"
  94. style="text-align: center; padding: 44px 20px; cursor: pointer"
  95. >
  96. <div>
  97. <el-icon
  98. size="60px"
  99. color="#999"
  100. >
  101. <upload-filled />
  102. </el-icon>
  103. </div>
  104. <div class="el-upload__text">拖动文件到这或者点击选择</div>
  105. <br />
  106. <div class="el-upload__text">
  107. 注意, 点确认前确保Payment Type选了正确的类型;
  108. </div>
  109. <br />
  110. <div class="el-upload">
  111. 单个Excel数据最好控制在100行内, 处理起来会慢;
  112. </div>
  113. <br />
  114. <div
  115. v-if="fileContainer"
  116. style="color: green"
  117. >
  118. 读取文件成功!
  119. </div>
  120. </div>
  121. </label>
  122. </div>
  123. </div>
  124. <br />
  125. <div class="flex end">
  126. <el-button
  127. :loading="loading"
  128. @click="handleClose"
  129. >
  130. 关闭
  131. </el-button>
  132. <el-tooltip content="注意, 点确认前确保Payment Type选了正确的类型">
  133. <el-button
  134. type="primary"
  135. :loading="loading"
  136. @click="next(mainForm)"
  137. >
  138. 确认
  139. </el-button>
  140. </el-tooltip>
  141. </div>
  142. </el-dialog>
  143. <!-- multiple -->
  144. <input
  145. id="fileInput"
  146. type="file"
  147. style="display: none"
  148. accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel"
  149. @change="processExcel"
  150. />
  151. </div>
  152. </template>
  153. <script lang="ts">
  154. import { defineComponent } from 'vue'
  155. export default defineComponent({
  156. name: 'DialogUploadExcel',
  157. })
  158. </script>
  159. <script lang="ts" setup>
  160. import { watchEffect, ref } from 'vue'
  161. import {
  162. ElDialog,
  163. ElMessage,
  164. ElForm,
  165. ElFormItem,
  166. ElSelect,
  167. ElOption,
  168. ElInput,
  169. ElButton,
  170. ElIcon,
  171. ElTooltip,
  172. } from 'element-plus'
  173. import { UploadFilled } from '@element-plus/icons-vue'
  174. import * as XLSX from 'xlsx'
  175. import type { FormInstance, FormRules } from 'element-plus'
  176. import { IPoItem, IOptionItem } from '../inteface'
  177. // import request from '@/utils/axios'
  178. import utils from '@/utils/index'
  179. const props = defineProps({
  180. visible: {
  181. type: Boolean,
  182. default: false,
  183. },
  184. currencyList: {
  185. type: Array,
  186. default: () => {
  187. return []
  188. },
  189. },
  190. defaultFileType: {
  191. type: String,
  192. default: '国内运费',
  193. },
  194. })
  195. const emit = defineEmits(['update:visible', 'update-table-data'])
  196. const dialogVisible = ref(false)
  197. watchEffect(() => {
  198. dialogVisible.value = props.visible
  199. })
  200. const tableData = ref([] as any[])
  201. const handleClose = function (done: any) {
  202. emit('update:visible', false)
  203. tableData.value = []
  204. form.value = {
  205. statement_name: '',
  206. currency: 'CNY',
  207. mode: 'Replace',
  208. paymentType: props.defaultFileType,
  209. weightUnit: 'Kg',
  210. }
  211. const target = document.getElementById('fileInput') as HTMLInputElement
  212. if (target) {
  213. target.value = ''
  214. }
  215. fileContainer.value = null
  216. if (typeof done === 'function') {
  217. done()
  218. }
  219. }
  220. const mainForm = ref<FormInstance>()
  221. const formRule = ref<FormRules>({
  222. statement_name: {
  223. required: true,
  224. message: '必填项',
  225. trigger: 'blur',
  226. },
  227. })
  228. const form = ref({
  229. statement_name: '',
  230. currency: 'CNY',
  231. mode: 'Replace',
  232. paymentType: props.defaultFileType,
  233. weightUnit: 'Kg',
  234. })
  235. watchEffect(() => {
  236. form.value.paymentType = props.defaultFileType
  237. })
  238. const uploadOption = [
  239. {
  240. label: '追加(Append)',
  241. value: 'Append',
  242. },
  243. {
  244. label: '替换(Replace)',
  245. value: 'Replace',
  246. },
  247. ]
  248. const paymentOption = [
  249. {
  250. label: '货款',
  251. value: '货款',
  252. },
  253. {
  254. label: '国内运费',
  255. value: '国内运费',
  256. },
  257. ]
  258. const weightOption = [
  259. {
  260. label: 'Kg',
  261. value: 'Kg',
  262. },
  263. {
  264. label: 'Lb',
  265. value: 'Lb',
  266. },
  267. ]
  268. const stop = (e: any) => {
  269. e.preventDefault()
  270. e.stopPropagation()
  271. }
  272. const fileContainer = ref(null as any)
  273. const processExcel = (event: any) => {
  274. const files = event.target.files || event.dataTransfer.files
  275. let str = ''
  276. // let arr: IPoItem[] = []
  277. tableData.value = []
  278. try {
  279. for (let i = 0; i < files.length; i++) {
  280. if (i === files.length - 1) {
  281. fileContainer.value = files[i]
  282. }
  283. if (
  284. ![
  285. 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  286. 'application/vnd.ms-excel',
  287. ].includes(files[i].type)
  288. ) {
  289. ElMessage.error('读取数据出错, 请确认选择了正确的Excel文件')
  290. return
  291. }
  292. str =
  293. str +
  294. `${str.length ? ', ' : ''}` +
  295. (files[i].name.replace(/\.xlsx?/, '') || 'unNameFile')
  296. }
  297. form.value.statement_name = str
  298. } catch (error) {
  299. console.log('读取文件出错:', error)
  300. }
  301. event.preventDefault()
  302. event.stopPropagation()
  303. }
  304. const loading = ref(false)
  305. const next = (formEl: FormInstance | undefined) => {
  306. if (!formEl) return
  307. formEl.validate((valid, fields) => {
  308. if (valid) {
  309. loading.value = true
  310. try {
  311. const fileReader = new FileReader()
  312. fileReader.onload = (e: any) => {
  313. const data = XLSX.read(e.target.result, { type: 'binary' })
  314. // 重命名列名
  315. if (form.value.paymentType === '国内运费') {
  316. // todo
  317. const jsonData = XLSX.utils.sheet_to_json(
  318. data.Sheets[data.SheetNames[0]],
  319. ) as IPoItem[]
  320. jsonData.forEach((i) => {
  321. tableData.value.push({
  322. ...i,
  323. Weight_Unit: form.value.weightUnit,
  324. Currency: form.value.currency,
  325. payment_type: form.value.paymentType,
  326. statement_name: form.value.statement_name,
  327. })
  328. })
  329. }
  330. let result = {
  331. mode: form.value.mode,
  332. data: tableData.value,
  333. }
  334. emit('update-table-data', result, fileContainer.value)
  335. handleClose(false)
  336. }
  337. fileReader.readAsBinaryString(fileContainer.value)
  338. } catch (e) {
  339. console.log(e, '处理文件出错')
  340. }
  341. loading.value = false
  342. } else {
  343. console.log('check form has not pass!', fields)
  344. ElMessage.error('请检查表单必填项')
  345. }
  346. })
  347. }
  348. form.value.paymentType = props.defaultFileType
  349. </script>
  350. <style lang="scss" scoped>
  351. .drag-area {
  352. height: 240px;
  353. border: 1px solid #ddd;
  354. border-radius: 4px;
  355. margin-left: 24px;
  356. }
  357. </style>