|
@@ -1,182 +1,182 @@
|
1
|
|
-<template>
|
2
|
|
- <div>
|
3
|
|
- <el-dialog
|
4
|
|
- :visible.sync="msgTipVisible"
|
5
|
|
- width="40%"
|
6
|
|
- :modal-append-to-body='false'
|
7
|
|
- :close-on-click-modal="false"
|
8
|
|
- >
|
9
|
|
- <span>若执行排班导入,系统将会清除当前已有临时排班数据,是否继续导入?</span>
|
10
|
|
- <span slot="footer" class="dialog-footer">
|
11
|
|
- <el-button @click="msgTipVisible = false">取 消</el-button>
|
12
|
|
- <el-button type="primary" @click="handleUpload()">确 定</el-button>
|
13
|
|
- </span>
|
14
|
|
- </el-dialog>
|
15
|
|
- <input id="excel-upload-input" ref="excel-upload-input" type="file" accept=".xlsx,.xls,.xltx," @change="handleClick" >
|
16
|
|
- <el-button :loading="loading" style="margin-left:16px;" size="mini" type="primary" @click="msgTipVisible = true">导入排班
|
17
|
|
- </el-button>
|
18
|
|
- </div>
|
19
|
|
-</template>
|
20
|
|
-
|
21
|
|
-<script>
|
22
|
|
- import XLSX from 'xlsx'
|
23
|
|
-
|
24
|
|
- export default {
|
25
|
|
- name: "scheduleUploadExcel",
|
26
|
|
- props: {
|
27
|
|
- beforeUpload: Function,
|
28
|
|
- onSuccess: Function
|
29
|
|
- },
|
30
|
|
- data() {
|
31
|
|
- return {
|
32
|
|
- loading: false,
|
33
|
|
- msgTipVisible:false,
|
34
|
|
- excelData: {
|
35
|
|
- header: null,
|
36
|
|
- results: null
|
37
|
|
- }
|
38
|
|
- }
|
39
|
|
- },
|
40
|
|
- methods: {
|
41
|
|
- generateDate({ header, results }) {
|
42
|
|
- console.log("header",header)
|
43
|
|
- console.log("results",results)
|
44
|
|
- this.excelData.header = header
|
45
|
|
- this.excelData.results = results
|
46
|
|
-
|
47
|
|
- this.onSuccess && this.onSuccess(this.excelData)
|
48
|
|
- },
|
49
|
|
- handleDrop(e) {
|
50
|
|
- e.stopPropagation()
|
51
|
|
- e.preventDefault()
|
52
|
|
- if (this.loading) return
|
53
|
|
- const files = e.dataTransfer.files
|
54
|
|
- if (files.length !== 1) {
|
55
|
|
- this.$message.error('Only support uploading one file!')
|
56
|
|
- return
|
57
|
|
- }
|
58
|
|
- const rawFile = files[0] // only use files[0]
|
59
|
|
-
|
60
|
|
- if (!this.isExcel(rawFile)) {
|
61
|
|
- this.$message.error('Only supports upload .xlsx, .xls, .csv suffix files')
|
62
|
|
- return false
|
63
|
|
- }
|
64
|
|
- this.upload(rawFile)
|
65
|
|
- e.stopPropagation()
|
66
|
|
- e.preventDefault()
|
67
|
|
- },
|
68
|
|
- handleDragover(e) {
|
69
|
|
- e.stopPropagation()
|
70
|
|
- e.preventDefault()
|
71
|
|
- e.dataTransfer.dropEffect = 'copy'
|
72
|
|
- },
|
73
|
|
- handleUpload() {
|
74
|
|
- this.msgTipVisible = false
|
75
|
|
-
|
76
|
|
- document.getElementById('excel-upload-input').click()
|
77
|
|
- },
|
78
|
|
- handleClick(e) {
|
79
|
|
-
|
80
|
|
- const files = e.target.files
|
81
|
|
- const rawFile = files[0] // only use files[0]
|
82
|
|
- console.log("rawfiel",rawFile)
|
83
|
|
- if (!rawFile)
|
84
|
|
- return
|
85
|
|
- this.upload(rawFile)
|
86
|
|
-
|
87
|
|
- },
|
88
|
|
- upload(rawFile) {
|
89
|
|
-
|
90
|
|
- this.$refs['excel-upload-input'].value = null // fix can't select the same excel
|
91
|
|
- console.log("3333",this.beforeUpload)
|
92
|
|
- if (!this.beforeUpload) {
|
93
|
|
- this.readerData(rawFile)
|
94
|
|
- return
|
95
|
|
- }
|
96
|
|
- const before = this.beforeUpload(rawFile)
|
97
|
|
- console.log("before",before)
|
98
|
|
- if (before) {
|
99
|
|
- this.readerData(rawFile)
|
100
|
|
- }
|
101
|
|
- },
|
102
|
|
- readerData(rawFile) {
|
103
|
|
-
|
104
|
|
- this.loading = true
|
105
|
|
- return new Promise((resolve, reject) => {
|
106
|
|
- const reader = new FileReader()
|
107
|
|
- reader.onload = e => {
|
108
|
|
-
|
109
|
|
- const data = e.target.result
|
110
|
|
- console.log("data22222",data)
|
111
|
|
- const fixedData = this.fixdata(data)
|
112
|
|
- const workbook = XLSX.read(btoa(fixedData), { type: 'base64' })
|
113
|
|
- const firstSheetName = workbook.SheetNames[0]
|
114
|
|
- const worksheet = workbook.Sheets[firstSheetName]
|
115
|
|
-
|
116
|
|
- const header = this.get_header_row(worksheet)
|
117
|
|
-
|
118
|
|
- const results = XLSX.utils.sheet_to_json(worksheet)
|
119
|
|
- this.generateDate({ header, results })
|
120
|
|
- this.loading = false
|
121
|
|
- resolve()
|
122
|
|
- }
|
123
|
|
- reader.readAsArrayBuffer(rawFile)
|
124
|
|
- })
|
125
|
|
- },
|
126
|
|
- fixdata(data) {
|
127
|
|
- let o = ''
|
128
|
|
- let l = 0
|
129
|
|
- const w = 10240
|
130
|
|
- for (; l < data.byteLength / w; ++l) o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w, l * w + w)))
|
131
|
|
- o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)))
|
132
|
|
- return o
|
133
|
|
- },
|
134
|
|
- get_header_row(sheet) {
|
135
|
|
-
|
136
|
|
- if(sheet['!ref'] == undefined){
|
137
|
|
- this.loading = false
|
138
|
|
- return
|
139
|
|
- }
|
140
|
|
-
|
141
|
|
- const headers = []
|
142
|
|
- const range = XLSX.utils.decode_range(sheet['!ref'])
|
143
|
|
- let C
|
144
|
|
- const R = range.s.r /* start in the first row */
|
145
|
|
- for (C = range.s.c; C <= range.e.c; ++C) { /* walk every column in the range */
|
146
|
|
- var cell = sheet[XLSX.utils.encode_cell({ c: C, r: R })] /* find the cell in the first row */
|
147
|
|
- var hdr = 'UNKNOWN ' + C // <-- replace with your desired default
|
148
|
|
- if (cell && cell.t) hdr = XLSX.utils.format_cell(cell)
|
149
|
|
- headers.push(hdr)
|
150
|
|
- }
|
151
|
|
- return headers
|
152
|
|
-
|
153
|
|
-
|
154
|
|
-
|
155
|
|
- },
|
156
|
|
- isExcel(file) {
|
157
|
|
-
|
158
|
|
- return /\.(xlsx|xls|csv)$/.test(file.name)
|
159
|
|
- }
|
160
|
|
- }
|
161
|
|
- }
|
162
|
|
-</script>
|
163
|
|
-
|
164
|
|
-<style scoped>
|
165
|
|
- #excel-upload-input {
|
166
|
|
- display: none;
|
167
|
|
- z-index: -9999;
|
168
|
|
- }
|
169
|
|
-
|
170
|
|
- #drop {
|
171
|
|
- border: 2px dashed #bbb;
|
172
|
|
- width: 600px;
|
173
|
|
- height: 160px;
|
174
|
|
- line-height: 160px;
|
175
|
|
- margin: 0 auto;
|
176
|
|
- font-size: 24px;
|
177
|
|
- border-radius: 5px;
|
178
|
|
- text-align: center;
|
179
|
|
- color: #bbb;
|
180
|
|
- position: relative;
|
181
|
|
- }
|
182
|
|
-</style>
|
|
1
|
+<template>
|
|
2
|
+ <div>
|
|
3
|
+ <el-dialog
|
|
4
|
+ :visible.sync="msgTipVisible"
|
|
5
|
+ width="40%"
|
|
6
|
+ :modal-append-to-body='false'
|
|
7
|
+ :close-on-click-modal="false"
|
|
8
|
+ >
|
|
9
|
+ <span>若执行排班导入,系统将会清除当前已有临时排班数据,是否继续导入?</span>
|
|
10
|
+ <span slot="footer" class="dialog-footer">
|
|
11
|
+ <el-button @click="msgTipVisible = false">取 消</el-button>
|
|
12
|
+ <el-button type="primary" @click="handleUpload()">确 定</el-button>
|
|
13
|
+ </span>
|
|
14
|
+ </el-dialog>
|
|
15
|
+ <input id="excel-upload-input" ref="excel-upload-input" type="file" accept=".xlsx,.xls,.xltx," @change="handleClick" >
|
|
16
|
+ <el-button :loading="loading" style="margin-left:16px;" size="mini" type="primary" @click="msgTipVisible = true">导入排班
|
|
17
|
+ </el-button>
|
|
18
|
+ </div>
|
|
19
|
+</template>
|
|
20
|
+
|
|
21
|
+<script>
|
|
22
|
+ import XLSX from 'xlsx'
|
|
23
|
+
|
|
24
|
+ export default {
|
|
25
|
+ name: "scheduleUploadExcel",
|
|
26
|
+ props: {
|
|
27
|
+ beforeUpload: Function,
|
|
28
|
+ onSuccess: Function
|
|
29
|
+ },
|
|
30
|
+ data() {
|
|
31
|
+ return {
|
|
32
|
+ loading: false,
|
|
33
|
+ msgTipVisible:false,
|
|
34
|
+ excelData: {
|
|
35
|
+ header: null,
|
|
36
|
+ results: null
|
|
37
|
+ }
|
|
38
|
+ }
|
|
39
|
+ },
|
|
40
|
+ methods: {
|
|
41
|
+ generateDate({ header, results }) {
|
|
42
|
+ console.log("header",header)
|
|
43
|
+ console.log("results",results)
|
|
44
|
+ this.excelData.header = header
|
|
45
|
+ this.excelData.results = results
|
|
46
|
+
|
|
47
|
+ this.onSuccess && this.onSuccess(this.excelData)
|
|
48
|
+ },
|
|
49
|
+ handleDrop(e) {
|
|
50
|
+ e.stopPropagation()
|
|
51
|
+ e.preventDefault()
|
|
52
|
+ if (this.loading) return
|
|
53
|
+ const files = e.dataTransfer.files
|
|
54
|
+ if (files.length !== 1) {
|
|
55
|
+ this.$message.error('Only support uploading one file!')
|
|
56
|
+ return
|
|
57
|
+ }
|
|
58
|
+ const rawFile = files[0] // only use files[0]
|
|
59
|
+
|
|
60
|
+ if (!this.isExcel(rawFile)) {
|
|
61
|
+ this.$message.error('Only supports upload .xlsx, .xls, .csv suffix files')
|
|
62
|
+ return false
|
|
63
|
+ }
|
|
64
|
+ this.upload(rawFile)
|
|
65
|
+ e.stopPropagation()
|
|
66
|
+ e.preventDefault()
|
|
67
|
+ },
|
|
68
|
+ handleDragover(e) {
|
|
69
|
+ e.stopPropagation()
|
|
70
|
+ e.preventDefault()
|
|
71
|
+ e.dataTransfer.dropEffect = 'copy'
|
|
72
|
+ },
|
|
73
|
+ handleUpload() {
|
|
74
|
+ this.msgTipVisible = false
|
|
75
|
+
|
|
76
|
+ document.getElementById('excel-upload-input').click()
|
|
77
|
+ },
|
|
78
|
+ handleClick(e) {
|
|
79
|
+
|
|
80
|
+ const files = e.target.files
|
|
81
|
+ const rawFile = files[0] // only use files[0]
|
|
82
|
+ console.log("rawfiel",rawFile)
|
|
83
|
+ if (!rawFile)
|
|
84
|
+ return
|
|
85
|
+ this.upload(rawFile)
|
|
86
|
+
|
|
87
|
+ },
|
|
88
|
+ upload(rawFile) {
|
|
89
|
+
|
|
90
|
+ this.$refs['excel-upload-input'].value = null // fix can't select the same excel
|
|
91
|
+ console.log("3333",this.beforeUpload)
|
|
92
|
+ if (!this.beforeUpload) {
|
|
93
|
+ this.readerData(rawFile)
|
|
94
|
+ return
|
|
95
|
+ }
|
|
96
|
+ const before = this.beforeUpload(rawFile)
|
|
97
|
+ console.log("before",before)
|
|
98
|
+ if (before) {
|
|
99
|
+ this.readerData(rawFile)
|
|
100
|
+ }
|
|
101
|
+ },
|
|
102
|
+ readerData(rawFile) {
|
|
103
|
+
|
|
104
|
+ this.loading = true
|
|
105
|
+ return new Promise((resolve, reject) => {
|
|
106
|
+ const reader = new FileReader()
|
|
107
|
+ reader.onload = e => {
|
|
108
|
+
|
|
109
|
+ const data = e.target.result
|
|
110
|
+ console.log("data22222",data)
|
|
111
|
+ const fixedData = this.fixdata(data)
|
|
112
|
+ const workbook = XLSX.read(btoa(fixedData), { type: 'base64' })
|
|
113
|
+ const firstSheetName = workbook.SheetNames[0]
|
|
114
|
+ const worksheet = workbook.Sheets[firstSheetName]
|
|
115
|
+
|
|
116
|
+ const header = this.get_header_row(worksheet)
|
|
117
|
+
|
|
118
|
+ const results = XLSX.utils.sheet_to_json(worksheet)
|
|
119
|
+ this.generateDate({ header, results })
|
|
120
|
+ this.loading = false
|
|
121
|
+ resolve()
|
|
122
|
+ }
|
|
123
|
+ reader.readAsArrayBuffer(rawFile)
|
|
124
|
+ })
|
|
125
|
+ },
|
|
126
|
+ fixdata(data) {
|
|
127
|
+ let o = ''
|
|
128
|
+ let l = 0
|
|
129
|
+ const w = 10240
|
|
130
|
+ for (; l < data.byteLength / w; ++l) o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w, l * w + w)))
|
|
131
|
+ o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)))
|
|
132
|
+ return o
|
|
133
|
+ },
|
|
134
|
+ get_header_row(sheet) {
|
|
135
|
+
|
|
136
|
+ if(sheet['!ref'] == undefined){
|
|
137
|
+ this.loading = false
|
|
138
|
+ return
|
|
139
|
+ }
|
|
140
|
+
|
|
141
|
+ const headers = []
|
|
142
|
+ const range = XLSX.utils.decode_range(sheet['!ref'])
|
|
143
|
+ let C
|
|
144
|
+ const R = range.s.r /* start in the first row */
|
|
145
|
+ for (C = range.s.c; C <= range.e.c; ++C) { /* walk every column in the range */
|
|
146
|
+ var cell = sheet[XLSX.utils.encode_cell({ c: C, r: R })] /* find the cell in the first row */
|
|
147
|
+ var hdr = 'UNKNOWN ' + C // <-- replace with your desired default
|
|
148
|
+ if (cell && cell.t) hdr = XLSX.utils.format_cell(cell)
|
|
149
|
+ headers.push(hdr)
|
|
150
|
+ }
|
|
151
|
+ return headers
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+ },
|
|
156
|
+ isExcel(file) {
|
|
157
|
+
|
|
158
|
+ return /\.(xlsx|xls|csv)$/.test(file.name)
|
|
159
|
+ }
|
|
160
|
+ }
|
|
161
|
+ }
|
|
162
|
+</script>
|
|
163
|
+
|
|
164
|
+<style scoped>
|
|
165
|
+ #excel-upload-input {
|
|
166
|
+ display: none;
|
|
167
|
+ z-index: -9999;
|
|
168
|
+ }
|
|
169
|
+
|
|
170
|
+ #drop {
|
|
171
|
+ border: 2px dashed #bbb;
|
|
172
|
+ width: 600px;
|
|
173
|
+ height: 160px;
|
|
174
|
+ line-height: 160px;
|
|
175
|
+ margin: 0 auto;
|
|
176
|
+ font-size: 24px;
|
|
177
|
+ border-radius: 5px;
|
|
178
|
+ text-align: center;
|
|
179
|
+ color: #bbb;
|
|
180
|
+ position: relative;
|
|
181
|
+ }
|
|
182
|
+</style>
|