n20-common-lib 1.3.7 → 1.3.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "n20-common-lib",
3
- "version": "1.3.7",
3
+ "version": "1.3.8",
4
4
  "private": false,
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -0,0 +1,201 @@
1
+ <template>
2
+ <el-dropdown
3
+ trigger="click"
4
+ placement="bottom-start"
5
+ @command="handleCommand"
6
+ >
7
+ <el-button size="mini" type="primary"> 导入 </el-button>
8
+ <el-dropdown-menu slot="dropdown">
9
+ <el-dropdown-item command="a">导入数据</el-dropdown-item>
10
+ <el-dropdown-item command="b">模板下载</el-dropdown-item>
11
+ </el-dropdown-menu>
12
+ </el-dropdown>
13
+ </template>
14
+
15
+ <script>
16
+ import XLSX from 'xlsx'
17
+ import axios from '../../utils/axios'
18
+ export default {
19
+ name: 'FileImport',
20
+ props: {
21
+ title: {
22
+ type: String,
23
+ default: function () {
24
+ return '文件导入'
25
+ }
26
+ },
27
+ tips: {
28
+ type: Array,
29
+ default: () => {
30
+ return []
31
+ }
32
+ },
33
+ templateUrl: {
34
+ type: String,
35
+ default: ''
36
+ },
37
+ downConfig: {
38
+ type: Object,
39
+ default() {
40
+ return {
41
+ isFrontDown: true,
42
+ fileName: '下载.xlsx'
43
+ }
44
+ }
45
+ },
46
+ footerHandlers: {
47
+ type: Object,
48
+ default: function () {
49
+ return {
50
+ // cancel: {
51
+ // text: '取消'
52
+ // },
53
+ // confirm: {
54
+ // text: '导入'
55
+ // }
56
+ }
57
+ }
58
+ },
59
+ beforeUpload: Function, // eslint-disable-line
60
+ callback: {
61
+ type: Function,
62
+ default: () => {}
63
+ },
64
+ handleConfirm: {
65
+ type: Function,
66
+ default: () => {}
67
+ },
68
+ reset: {
69
+ type: Boolean,
70
+ default: false
71
+ }
72
+ },
73
+ data() {
74
+ return {
75
+ excelData: {
76
+ tableHeader: [],
77
+ tableData: []
78
+ },
79
+
80
+ analysisText: 'analysis',
81
+ fileName: '',
82
+ statisticsFlag: false,
83
+ columnsListTemp: [],
84
+ errorListTemp: [],
85
+ successFlag: false,
86
+ confirmDisabledFlag: true
87
+ }
88
+ },
89
+ methods: {
90
+ handleCommand(val) {
91
+ if (val == 'a') {
92
+ this.seletFile()
93
+ } else if (val == 'b') {
94
+ this.downloadTemplate()
95
+ }
96
+ },
97
+ seletFile() {
98
+ let input = document.createElement('input')
99
+ let self = this
100
+ input.type = 'file'
101
+ input.accept = '.xlsx, .xls, .csv'
102
+ input.addEventListener('change', function (event) {
103
+ const files = event.target.files
104
+ const rawFile = files[0] // only use files[0]
105
+ console.log('导入的文件:', rawFile)
106
+ self.fileName = rawFile.name
107
+ if (!rawFile) return
108
+ self.upload(rawFile)
109
+ this.value = null // fix can't select the same excel
110
+ })
111
+ input.click()
112
+ },
113
+ /* 上传文件 */
114
+ upload(rawFile) {
115
+ if (!this.beforeUpload) {
116
+ this.readerData(rawFile)
117
+ return
118
+ }
119
+ const before = this.beforeUpload(rawFile)
120
+ if (before) {
121
+ this.readerData(rawFile)
122
+ }
123
+ },
124
+ /*解析传入的表格 */
125
+ readerData(rawFile) {
126
+ return new Promise((resolve) => {
127
+ const reader = new FileReader()
128
+ reader.onload = (e) => {
129
+ const data = e.target.result
130
+ const workbook = XLSX.read(data, {
131
+ type: 'array',
132
+ cellDates: true
133
+ })
134
+ const firstSheetName = workbook.SheetNames[0]
135
+ const worksheet = workbook.Sheets[firstSheetName]
136
+ const header = this.getHeaderRow(worksheet)
137
+ const results = XLSX.utils.sheet_to_json(worksheet)
138
+
139
+ this.generateData({ header, results })
140
+ resolve()
141
+ }
142
+ reader.readAsArrayBuffer(rawFile)
143
+ })
144
+ },
145
+ generateData({ header, results }) {
146
+ this.excelData.tableHeader = header
147
+ this.excelData.tableData = results
148
+ this.callback && this.callback(this.excelData)
149
+ },
150
+ getHeaderRow(sheet) {
151
+ console.log(sheet)
152
+ const headers = []
153
+ const range = XLSX.utils.decode_range(sheet['!ref'])
154
+ let C
155
+ const R = range.s.r
156
+ /* start in the first row */
157
+ for (C = range.s.c; C <= range.e.c; ++C) {
158
+ /* walk every column in the range */
159
+ const cell = sheet[XLSX.utils.encode_cell({ c: C, r: R })]
160
+ /* find the cell in the first row */
161
+ let hdr = 'UNKNOWN ' + C // <-- replace with your desired default
162
+ if (cell && cell.t) hdr = XLSX.utils.format_cell(cell)
163
+ headers.push(hdr)
164
+ }
165
+ return headers
166
+ },
167
+ // 下载文件
168
+ downloadTemplate() {
169
+ // 前端下载
170
+ if (this.downConfig.isFrontDown) {
171
+ const aEl = document.createElement('a')
172
+ aEl.setAttribute('href', this.templateUrl)
173
+ aEl.target = '_blank'
174
+ // aEl.download = this.templateUrl
175
+ aEl.click()
176
+ } else {
177
+ axios({
178
+ url: this.templateUrl,
179
+ method: 'get',
180
+ responseType: 'blob'
181
+ }).then((res) => {
182
+ const content = res
183
+ const blob = new Blob([content])
184
+ if ('download' in document.createElement('a')) {
185
+ const link = document.createElement('a')
186
+ link.download = this.downConfig.fileName
187
+ link.style.display = 'none'
188
+ link.href = URL.createObjectURL(blob)
189
+ document.body.appendChild(link)
190
+ link.click()
191
+ URL.revokeObjectURL(link.href)
192
+ document.body.removeChild(link)
193
+ } else {
194
+ navigator.msSaveBlob(blob, this.downConfig.fileName)
195
+ }
196
+ })
197
+ }
198
+ }
199
+ }
200
+ }
201
+ </script>
@@ -1,201 +1,7 @@
1
1
  <template>
2
- <el-dropdown
3
- trigger="click"
4
- placement="bottom-start"
5
- @command="handleCommand"
6
- >
7
- <el-button size="mini" type="primary"> 导入 </el-button>
8
- <el-dropdown-menu slot="dropdown">
9
- <el-dropdown-item command="a">导入数据</el-dropdown-item>
10
- <el-dropdown-item command="b">模板下载</el-dropdown-item>
11
- </el-dropdown-menu>
12
- </el-dropdown>
2
+ <div>待完善</div>
13
3
  </template>
14
4
 
15
5
  <script>
16
- import XLSX from 'xlsx'
17
- import axios from '../../utils/axios'
18
- export default {
19
- name: 'FileImport',
20
- props: {
21
- title: {
22
- type: String,
23
- default: function () {
24
- return '文件导入'
25
- }
26
- },
27
- tips: {
28
- type: Array,
29
- default: () => {
30
- return []
31
- }
32
- },
33
- templateUrl: {
34
- type: String,
35
- default: ''
36
- },
37
- downConfig: {
38
- type: Object,
39
- default() {
40
- return {
41
- isFrontDown: true,
42
- fileName: '下载.xlsx'
43
- }
44
- }
45
- },
46
- footerHandlers: {
47
- type: Object,
48
- default: function () {
49
- return {
50
- // cancel: {
51
- // text: '取消'
52
- // },
53
- // confirm: {
54
- // text: '导入'
55
- // }
56
- }
57
- }
58
- },
59
- beforeUpload: Function, // eslint-disable-line
60
- callback: {
61
- type: Function,
62
- default: () => {}
63
- },
64
- handleConfirm: {
65
- type: Function,
66
- default: () => {}
67
- },
68
- reset: {
69
- type: Boolean,
70
- default: false
71
- }
72
- },
73
- data() {
74
- return {
75
- excelData: {
76
- tableHeader: [],
77
- tableData: []
78
- },
79
-
80
- analysisText: 'analysis',
81
- fileName: '',
82
- statisticsFlag: false,
83
- columnsListTemp: [],
84
- errorListTemp: [],
85
- successFlag: false,
86
- confirmDisabledFlag: true
87
- }
88
- },
89
- methods: {
90
- handleCommand(val) {
91
- if (val == 'a') {
92
- this.seletFile()
93
- } else if (val == 'b') {
94
- this.downloadTemplate()
95
- }
96
- },
97
- seletFile() {
98
- let input = document.createElement('input')
99
- let self = this
100
- input.type = 'file'
101
- input.accept = '.xlsx, .xls, .csv'
102
- input.addEventListener('change', function (event) {
103
- const files = event.target.files
104
- const rawFile = files[0] // only use files[0]
105
- console.log('导入的文件:', rawFile)
106
- self.fileName = rawFile.name
107
- if (!rawFile) return
108
- self.upload(rawFile)
109
- this.value = null // fix can't select the same excel
110
- })
111
- input.click()
112
- },
113
- /* 上传文件 */
114
- upload(rawFile) {
115
- if (!this.beforeUpload) {
116
- this.readerData(rawFile)
117
- return
118
- }
119
- const before = this.beforeUpload(rawFile)
120
- if (before) {
121
- this.readerData(rawFile)
122
- }
123
- },
124
- /*解析传入的表格 */
125
- readerData(rawFile) {
126
- return new Promise((resolve) => {
127
- const reader = new FileReader()
128
- reader.onload = (e) => {
129
- const data = e.target.result
130
- const workbook = XLSX.read(data, {
131
- type: 'array',
132
- cellDates: true
133
- })
134
- const firstSheetName = workbook.SheetNames[0]
135
- const worksheet = workbook.Sheets[firstSheetName]
136
- const header = this.getHeaderRow(worksheet)
137
- const results = XLSX.utils.sheet_to_json(worksheet)
138
-
139
- this.generateData({ header, results })
140
- resolve()
141
- }
142
- reader.readAsArrayBuffer(rawFile)
143
- })
144
- },
145
- generateData({ header, results }) {
146
- this.excelData.tableHeader = header
147
- this.excelData.tableData = results
148
- this.callback && this.callback(this.excelData)
149
- },
150
- getHeaderRow(sheet) {
151
- console.log(sheet)
152
- const headers = []
153
- const range = XLSX.utils.decode_range(sheet['!ref'])
154
- let C
155
- const R = range.s.r
156
- /* start in the first row */
157
- for (C = range.s.c; C <= range.e.c; ++C) {
158
- /* walk every column in the range */
159
- const cell = sheet[XLSX.utils.encode_cell({ c: C, r: R })]
160
- /* find the cell in the first row */
161
- let hdr = 'UNKNOWN ' + C // <-- replace with your desired default
162
- if (cell && cell.t) hdr = XLSX.utils.format_cell(cell)
163
- headers.push(hdr)
164
- }
165
- return headers
166
- },
167
- // 下载文件
168
- downloadTemplate() {
169
- // 前端下载
170
- if (this.downConfig.isFrontDown) {
171
- const aEl = document.createElement('a')
172
- aEl.setAttribute('href', this.templateUrl)
173
- aEl.target = '_blank'
174
- // aEl.download = this.templateUrl
175
- aEl.click()
176
- } else {
177
- axios({
178
- url: this.templateUrl,
179
- method: 'get',
180
- responseType: 'blob'
181
- }).then((res) => {
182
- const content = res
183
- const blob = new Blob([content])
184
- if ('download' in document.createElement('a')) {
185
- const link = document.createElement('a')
186
- link.download = this.downConfig.fileName
187
- link.style.display = 'none'
188
- link.href = URL.createObjectURL(blob)
189
- document.body.appendChild(link)
190
- link.click()
191
- URL.revokeObjectURL(link.href)
192
- document.body.removeChild(link)
193
- } else {
194
- navigator.msSaveBlob(blob, this.downConfig.fileName)
195
- }
196
- })
197
- }
198
- }
199
- }
200
- }
6
+ export default {}
201
7
  </script>