@duxweb/dvha-core 1.1.11 → 1.1.12

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.
@@ -1,2 +1,2 @@
1
1
  "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("json-2-csv"),c=require("vue"),u=require("./export.cjs");function l(t){const a=c.computed(()=>{const{filename:e,headers:r,csvOptions:n,...s}=t;return s}),i=c.computed(()=>{const e=t.csvOptions||{},r=e.quote;return{prependHeader:t.headers!==!1,keys:Array.isArray(t.headers)?t.headers:void 0,delimiter:{field:e.delimiter||",",wrap:typeof r=="string"?r:'"',eol:e.eol||`
2
- `},excelBOM:e.writeBOM||!1}});return u.useExport({...a.value,onSuccess:async e=>{if(!e?.data||!e?.data?.length){t.onError?.({message:"No data to export",status:400});return}try{const r=await d.json2csv(e.data,i.value),n=new Blob([r],{type:"text/csv;charset=utf-8;"}),s=URL.createObjectURL(n),o=document.createElement("a");o.href=s,o.download=t.filename||"export.csv",o.style.display="none",document.body.appendChild(o),o.click(),document.body.removeChild(o),URL.revokeObjectURL(s),t.onSuccess?.(e)}catch(r){t.onError?.({message:`CSV Export Error: ${r}`,status:500})}}})}exports.useExportCsv=l;
2
+ `},excelBOM:e.writeBOM!==!1}});return u.useExport({...a.value,onSuccess:async e=>{if(!e?.data||!e?.data?.length){t.onError?.({message:"No data to export",status:400});return}try{const r=await d.json2csv(e.data,i.value),n=new Blob([r],{type:"text/csv;charset=utf-8;"}),s=URL.createObjectURL(n),o=document.createElement("a");o.href=s,o.download=t.filename||"export.csv",o.style.display="none",document.body.appendChild(o),o.click(),document.body.removeChild(o),URL.revokeObjectURL(s),t.onSuccess?.(e)}catch(r){t.onError?.({message:`CSV Export Error: ${r}`,status:500})}}})}exports.useExportCsv=l;
@@ -1,2 +1,2 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("@vueuse/core"),p=require("json-2-csv"),i=require("vue"),v=require("./import.cjs");function g(s){const a=i.computed(()=>{const{csvOptions:e,...t}=s;return t}),c=i.computed(()=>{const e=s.csvOptions||{};return{delimiter:{field:e.delimiter||",",wrap:e.wrap||'"',eol:e.eol||`
2
- `},excelBOM:e.excelBOM||!1,headerFields:e.headerFields,keys:e.keys,trimHeaderFields:e.trimHeaderFields||!1,trimFieldValues:e.trimFieldValues||!1}}),o=v.useImport({...a.value}),n=async e=>{try{const t=await e.text(),r=await p.csv2json(t,c.value);r&&r.length>0?await o.trigger(r):s.onError?.({message:"CSV 文件为空或格式不正确",status:400})}catch(t){s.onError?.({message:`CSV 解析错误: ${t}`,status:500})}},{open:l,reset:u,onChange:m}=d.useFileDialog({accept:".csv",multiple:!1});return m(e=>{e&&e.length>0&&n(e[0])}),{...o,open:()=>{u(),l()},readFile:n}}exports.useImportCsv=g;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const C=require("@vueuse/core"),x=require("json-2-csv"),g=require("vue"),A=require("./import.cjs");function S(o){const h=g.computed(()=>{const{csvOptions:e,...n}=o;return n}),v=g.computed(()=>{const e=o.csvOptions||{};return{delimiter:{field:e.delimiter||",",wrap:e.wrap||'"',eol:e.eol||`
2
+ `},excelBOM:e.excelBOM||!1,headerFields:e.headerFields,keys:e.keys,trimHeaderFields:e.trimHeaderFields||!1,trimFieldValues:e.trimFieldValues||!1}}),f=A.useImport({...h.value}),y=async e=>{const n=await e.arrayBuffer(),t=new Uint8Array(n),M=t.length>=3&&t[0]===239&&t[1]===187&&t[2]===191,b=t.length>=2&&t[0]===255&&t[1]===254,w=t.length>=2&&t[0]===254&&t[1]===255,p=o.encoding||"auto",c=(r,s)=>{try{return new TextDecoder(r,{fatal:!1}).decode(s||t)}catch{return""}},B=r=>{if(!r)return{rep:Number.MAX_SAFE_INTEGER,ctrl:Number.MAX_SAFE_INTEGER};let s=0,i=0;for(let l=0;l<r.length;l++){const u=r.charCodeAt(l);u===65533&&s++,u<32&&u!==9&&u!==10&&u!==13&&i++}return{rep:s,ctrl:i}},a=r=>r.replace(/^\ufeff/,"");if(M)return a(c("utf-8"));if(b)return a(c("utf-16le"));if(w)return a(c("utf-16be"));if(p!=="auto")return a(c(p));const m=["utf-8","gb18030","gbk"].map(r=>{const s=c(r),i=B(s);return{enc:r,text:s,sc:i}});return m.sort((r,s)=>r.sc.rep-s.sc.rep||r.sc.ctrl-s.sc.ctrl),a(m[0].text)},d=async e=>{try{const n=await y(e),t=await x.csv2json(n,v.value);t&&t.length>0?await f.trigger(t):o.onError?.({message:"CSV 文件为空或格式不正确",status:400})}catch(n){o.onError?.({message:`CSV 解析错误: ${n}`,status:500})}},{open:E,reset:O,onChange:F}=C.useFileDialog({accept:".csv",multiple:!1});return F(e=>{e&&e.length>0&&d(e[0])}),{...f,open:()=>{O(),E()},readFile:d}}exports.useImportCsv=S;
@@ -16,7 +16,8 @@ function h(t) {
16
16
  eol: e.eol || `
17
17
  `
18
18
  },
19
- excelBOM: e.writeBOM || !1
19
+ // 默认为 Excel 加上 UTF-8 BOM,避免中文在 Excel 中出现乱码
20
+ excelBOM: e.writeBOM !== !1
20
21
  };
21
22
  });
22
23
  return l({
@@ -1,13 +1,13 @@
1
- import { useFileDialog as u } from "@vueuse/core";
2
- import { csv2json as d } from "json-2-csv";
3
- import { computed as n } from "vue";
4
- import { useImport as f } from "./import.js";
5
- function w(s) {
6
- const a = n(() => {
7
- const { csvOptions: e, ...t } = s;
8
- return t;
9
- }), l = n(() => {
10
- const e = s.csvOptions || {};
1
+ import { useFileDialog as b } from "@vueuse/core";
2
+ import { csv2json as A } from "json-2-csv";
3
+ import { computed as g } from "vue";
4
+ import { useImport as C } from "./import.js";
5
+ function V(n) {
6
+ const h = g(() => {
7
+ const { csvOptions: e, ...o } = n;
8
+ return o;
9
+ }), E = g(() => {
10
+ const e = n.csvOptions || {};
11
11
  return {
12
12
  delimiter: {
13
13
  field: e.delimiter || ",",
@@ -21,35 +21,64 @@ function w(s) {
21
21
  trimHeaderFields: e.trimHeaderFields || !1,
22
22
  trimFieldValues: e.trimFieldValues || !1
23
23
  };
24
- }), r = f({
25
- ...a.value
26
- }), i = async (e) => {
24
+ }), u = C({
25
+ ...h.value
26
+ }), y = async (e) => {
27
+ const o = await e.arrayBuffer(), t = new Uint8Array(o), w = t.length >= 3 && t[0] === 239 && t[1] === 187 && t[2] === 191, B = t.length >= 2 && t[0] === 255 && t[1] === 254, M = t.length >= 2 && t[0] === 254 && t[1] === 255, m = n.encoding || "auto", c = (r, s) => {
28
+ try {
29
+ return new TextDecoder(r, { fatal: !1 }).decode(s || t);
30
+ } catch {
31
+ return "";
32
+ }
33
+ }, x = (r) => {
34
+ if (!r) return { rep: Number.MAX_SAFE_INTEGER, ctrl: Number.MAX_SAFE_INTEGER };
35
+ let s = 0, i = 0;
36
+ for (let f = 0; f < r.length; f++) {
37
+ const l = r.charCodeAt(f);
38
+ l === 65533 && s++, l < 32 && l !== 9 && l !== 10 && l !== 13 && i++;
39
+ }
40
+ return { rep: s, ctrl: i };
41
+ }, a = (r) => r.replace(/^\ufeff/, "");
42
+ if (w)
43
+ return a(c("utf-8"));
44
+ if (B)
45
+ return a(c("utf-16le"));
46
+ if (M)
47
+ return a(c("utf-16be"));
48
+ if (m !== "auto")
49
+ return a(c(m));
50
+ const p = ["utf-8", "gb18030", "gbk"].map((r) => {
51
+ const s = c(r), i = x(s);
52
+ return { enc: r, text: s, sc: i };
53
+ });
54
+ return p.sort((r, s) => r.sc.rep - s.sc.rep || r.sc.ctrl - s.sc.ctrl), a(p[0].text);
55
+ }, d = async (e) => {
27
56
  try {
28
- const t = await e.text(), o = await d(t, l.value);
29
- o && o.length > 0 ? await r.trigger(o) : s.onError?.({
57
+ const o = await y(e), t = await A(o, E.value);
58
+ t && t.length > 0 ? await u.trigger(t) : n.onError?.({
30
59
  message: "CSV 文件为空或格式不正确",
31
60
  status: 400
32
61
  });
33
- } catch (t) {
34
- s.onError?.({
35
- message: `CSV 解析错误: ${t}`,
62
+ } catch (o) {
63
+ n.onError?.({
64
+ message: `CSV 解析错误: ${o}`,
36
65
  status: 500
37
66
  });
38
67
  }
39
- }, { open: c, reset: m, onChange: p } = u({
68
+ }, { open: O, reset: v, onChange: F } = b({
40
69
  accept: ".csv",
41
70
  multiple: !1
42
71
  });
43
- return p((e) => {
44
- e && e.length > 0 && i(e[0]);
72
+ return F((e) => {
73
+ e && e.length > 0 && d(e[0]);
45
74
  }), {
46
- ...r,
75
+ ...u,
47
76
  open: () => {
48
- m(), c();
77
+ v(), O();
49
78
  },
50
- readFile: i
79
+ readFile: d
51
80
  };
52
81
  }
53
82
  export {
54
- w as useImportCsv
83
+ V as useImportCsv
55
84
  };
@@ -1,5 +1,6 @@
1
1
  import type { IUseImportProps } from './import';
2
2
  export interface IUseImportCsvProps extends IUseImportProps {
3
+ encoding?: 'auto' | string;
3
4
  csvOptions?: {
4
5
  delimiter?: string;
5
6
  wrap?: string;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@duxweb/dvha-core",
3
3
  "type": "module",
4
- "version": "1.1.11",
4
+ "version": "1.1.12",
5
5
  "author": "DuxWeb",
6
6
  "license": "MIT",
7
7
  "repository": {