Commit 84ebe0b796d93e234f3f8c218289cfd12e3bad67
Merge branch 'fix/import-device' into 'main_dev'
fix: 修复设备导入使用.csv格式存在问题 See merge request yunteng/thingskit-front!1081
Showing
4 changed files
with
27 additions
and
25 deletions
| ... | ... | @@ -41,6 +41,8 @@ |
| 41 | 41 | showIndexColumn: false, |
| 42 | 42 | columns: columnTypeSchema, |
| 43 | 43 | resizeHeightOffset: -300, |
| 44 | + maxHeight: 300, | |
| 45 | + canResize: false, | |
| 44 | 46 | }); |
| 45 | 47 | |
| 46 | 48 | const parseValueToTableData = () => { |
| ... | ... | @@ -54,7 +56,7 @@ |
| 54 | 56 | const dataSource = header.map((columnKey, index) => { |
| 55 | 57 | return { |
| 56 | 58 | [ColumnFileEnum.EXAMPLE_VALUE]: exampleRow[columnKey], |
| 57 | - [ColumnFileEnum.TYPE]: getColumnType(index), | |
| 59 | + [ColumnFileEnum.TYPE]: index < 2 ? getColumnType(index) : null, | |
| 58 | 60 | [ColumnFileEnum.KEY]: columnKey, |
| 59 | 61 | id: buildUUID(), |
| 60 | 62 | } as ColumnDataRecord; |
| ... | ... | @@ -136,6 +138,8 @@ |
| 136 | 138 | :disabled="getColumnNeedDisabled(index)" |
| 137 | 139 | :options="columnTypeOptions" |
| 138 | 140 | :dropdown-match-select-width="false" |
| 141 | + allow-clear | |
| 142 | + placeholder="请选择列类型" | |
| 139 | 143 | /> |
| 140 | 144 | </section> |
| 141 | 145 | </template> |
| ... | ... | @@ -165,11 +169,11 @@ |
| 165 | 169 | .ant-select, |
| 166 | 170 | .ant-input, |
| 167 | 171 | .ant-select-selector { |
| 168 | - height: 22px !important; | |
| 172 | + height: 30px !important; | |
| 169 | 173 | } |
| 170 | 174 | |
| 171 | 175 | .ant-select-selection-item { |
| 172 | - line-height: 22px !important; | |
| 176 | + line-height: 30px !important; | |
| 173 | 177 | } |
| 174 | 178 | } |
| 175 | 179 | </style> | ... | ... |
| ... | ... | @@ -54,7 +54,7 @@ |
| 54 | 54 | try { |
| 55 | 55 | const data = (event.target as FileReader).result as string; |
| 56 | 56 | |
| 57 | - const result = XLSX.read(data, { type: 'string' }); | |
| 57 | + const result = XLSX.read(data, { type: 'binary' }); | |
| 58 | 58 | |
| 59 | 59 | const sheetName = result.SheetNames.at(0); |
| 60 | 60 | const workbook = result.Sheets; |
| ... | ... | @@ -72,7 +72,7 @@ |
| 72 | 72 | resolve({ header, content }); |
| 73 | 73 | } catch (error) { |
| 74 | 74 | const { createMessage } = useMessage(); |
| 75 | - createMessage.error('请检查csv文件是否正确'); | |
| 75 | + createMessage.error('请检查xlsx文件是否正确'); | |
| 76 | 76 | throw error; |
| 77 | 77 | } |
| 78 | 78 | }; |
| ... | ... | @@ -80,7 +80,7 @@ |
| 80 | 80 | fileReader.onerror = () => { |
| 81 | 81 | reject(false); |
| 82 | 82 | }; |
| 83 | - fileReader.readAsText(file, 'GB2312'); | |
| 83 | + fileReader.readAsBinaryString(file); | |
| 84 | 84 | }); |
| 85 | 85 | }; |
| 86 | 86 | |
| ... | ... | @@ -120,7 +120,7 @@ |
| 120 | 120 | <Upload.Dragger |
| 121 | 121 | :fileList="fileList" |
| 122 | 122 | :customRequest="handleParseFile" |
| 123 | - accept=".csv" | |
| 123 | + accept=".xlsx" | |
| 124 | 124 | name="file" |
| 125 | 125 | :remove="handleRemove" |
| 126 | 126 | > | ... | ... |
| ... | ... | @@ -8,6 +8,7 @@ import { FormSchema } from '/@/components/Form'; |
| 8 | 8 | import { BasicColumn } from '/@/components/Table'; |
| 9 | 9 | import { copyTransFun } from '/@/utils/fnUtils'; |
| 10 | 10 | import { TransportTypeEnum } from '/@/views/device/profiles/components/TransportDescript/const'; |
| 11 | +import XLSX from 'xlsx'; | |
| 11 | 12 | |
| 12 | 13 | export enum FieldsEnum { |
| 13 | 14 | ORGANIZATION_ID = 'organizationId', |
| ... | ... | @@ -310,6 +311,8 @@ export const generateColumnTypeOptions = () => { |
| 310 | 311 | }; |
| 311 | 312 | options.push(credentialsOption); |
| 312 | 313 | |
| 314 | + options.push({ label: '设备名称', value: ColumTypeEnum.NAME }); | |
| 315 | + | |
| 313 | 316 | return options; |
| 314 | 317 | }; |
| 315 | 318 | |
| ... | ... | @@ -332,19 +335,13 @@ export const columnTypeSchema: BasicColumn[] = [ |
| 332 | 335 | }, |
| 333 | 336 | ]; |
| 334 | 337 | |
| 335 | -export const csvTemplate = `设备名称,凭证账号,凭证密码 | |
| 336 | -温湿度设备,admin,123456 | |
| 337 | -`; | |
| 338 | +export function exportTemplate() { | |
| 339 | + const book = XLSX.utils.book_new(); | |
| 340 | + const sheet = XLSX.utils.aoa_to_sheet([ | |
| 341 | + ['设备名称', '访问令牌'], | |
| 342 | + ['温湿度设备', 'admin'], | |
| 343 | + ]); | |
| 338 | 344 | |
| 339 | -export const downloadFile = (data: string, fileName: string, type: string, ext: string) => { | |
| 340 | - const blob = new Blob([data], { type: type }); | |
| 341 | - const objectURL = URL.createObjectURL(blob); | |
| 342 | - const element = document.createElement('a'); | |
| 343 | - element.href = objectURL; | |
| 344 | - element.download = `${fileName}.${ext}`; | |
| 345 | - element.style.display = 'none'; | |
| 346 | - document.body.appendChild(element); | |
| 347 | - element.click(); | |
| 348 | - element.remove(); | |
| 349 | - URL.revokeObjectURL(objectURL); | |
| 350 | -}; | |
| 345 | + XLSX.utils.book_append_sheet(book, sheet, '设备导入模版'); | |
| 346 | + XLSX.writeFile(book, 'template.xlsx', { bookType: 'xlsx' }); | |
| 347 | +} | ... | ... |
| ... | ... | @@ -9,7 +9,7 @@ |
| 9 | 9 | import CreateEntity from './CreateEntity.vue'; |
| 10 | 10 | import CompleteResult from './CompleteResult.vue'; |
| 11 | 11 | import { ImportDeviceResponse } from '/@/api/device/model/batchImportModel'; |
| 12 | - import { csvTemplate, downloadFile } from './config'; | |
| 12 | + import { exportTemplate } from './config'; | |
| 13 | 13 | |
| 14 | 14 | const emit = defineEmits(['importFinally']); |
| 15 | 15 | |
| ... | ... | @@ -79,7 +79,8 @@ |
| 79 | 79 | }; |
| 80 | 80 | |
| 81 | 81 | const handleTemplateDownload = () => { |
| 82 | - downloadFile(csvTemplate, 'template', 'text/csv', 'csv'); | |
| 82 | + exportTemplate(); | |
| 83 | + // downloadFile(csvTemplate, 'template', 'text/csv,chartset=UTF-8', 'csv'); | |
| 83 | 84 | }; |
| 84 | 85 | </script> |
| 85 | 86 | |
| ... | ... | @@ -142,7 +143,7 @@ |
| 142 | 143 | </Steps.Step> |
| 143 | 144 | </Steps> |
| 144 | 145 | <Tooltip |
| 145 | - title="模板表头的第一列为设备名称,且不能重复,如需预置访问凭证(ACCESS_TOKEN、MQTT client ID、MQTT user name、MQTT password)添加新的列即可。例如:凭证账号,凭证密码" | |
| 146 | + title="注意:模板表头的第一列为设备名称,第二列为访问令牌。新增列时,名称和访问令牌不能重复。下载的模板建议使用文本打开,如使用wps打开请另外为新的csv文件。" | |
| 146 | 147 | > |
| 147 | 148 | <Button |
| 148 | 149 | type="text" | ... | ... |