@uxda/appkit 4.3.2 → 4.3.4

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.
@@ -0,0 +1,218 @@
1
+ <template>
2
+ <div class="ocr-invoice" :class="[disabled ? 'disabled' : '', className]" v-track-click="'发票识别-点击'" @click="!customClick ? onUpload() : null">
3
+ <slot name="icon">
4
+ <ns-icon name="https://cdn.ddjf.com/static/images/beidouxing/ocr-icon.png"/>
5
+ </slot>
6
+ </div>
7
+
8
+ <nut-action-sheet v-model:visible="activeSheetVisible" :menu-items="actionSheetMenus" @choose="chooseImages"
9
+ cancel-txt="取消" />
10
+ </template>
11
+
12
+ <script lang="ts" setup>
13
+ import Taro, { showToast, showLoading, hideLoading, chooseMedia, chooseMessageFile, uploadFile } from '@tarojs/taro'
14
+ import { NsIcon, useNutshell } from '@uxda/nutshell/taro'
15
+ import { useAppKitOptions } from '../../Appkit'
16
+ import { ref } from 'vue';
17
+ import { useHttp } from '../../balance/api'
18
+ import { compressImage, getCompressQuality } from '../composables/useUpload'
19
+
20
+ const appKitOptions = useAppKitOptions()
21
+ const $http = useHttp(),
22
+ $n = useNutshell()
23
+
24
+ const emits = defineEmits(['complete'])
25
+
26
+ type OcrProps = {
27
+ disabled?: boolean,
28
+ side?: 'face' | 'back',
29
+ className?: string
30
+ customUpload?: Function
31
+ uploadUrl?: string
32
+ customClick?: boolean
33
+ }
34
+ type FileType = {
35
+ "downloadUrl": string,
36
+ "fileId": string,
37
+ "fileName": string,
38
+ "fileSize": number,
39
+ "fileSuffix": string,
40
+ "fileType": string,
41
+ "originalUrl": string
42
+ }
43
+
44
+ const props = withDefaults(defineProps<OcrProps>(), {
45
+ disabled: false,
46
+ side: 'face',
47
+ className: '',
48
+ uploadUrl: '/hkbase/file/uploadFile',
49
+ customClick: false
50
+ })
51
+
52
+ async function onUploadFile(csRes: any) {
53
+ try {
54
+ let { path, size, tempFilePath } = csRes.tempFiles[0]
55
+ const compressImg: any = (await compressImage(path || tempFilePath, getCompressQuality(size))) || {}
56
+ const filePath = compressImg.tempFilePath || path
57
+
58
+ if (props.customUpload) {
59
+ props.customUpload(filePath)
60
+ return
61
+ }
62
+
63
+ showLoading({ title: '发票识别中..', mask: true })
64
+
65
+ const session = appKitOptions.token()
66
+ const baseUrl = appKitOptions.baseUrl()
67
+ const upRes: any = await uploadFile({
68
+ url: `${baseUrl}${props.uploadUrl}`,
69
+ filePath,
70
+ name: 'file',
71
+ formData: {
72
+ objectNo: `min${Date.now()}`,
73
+ appCode: appKitOptions.app(),
74
+ },
75
+ header: {
76
+ token: session || '',
77
+ },
78
+ })
79
+
80
+ const res = JSON.parse(upRes.data)
81
+ if (res.code === '200') {
82
+ await getOcrInfo(res.result)
83
+ } else {
84
+ hideLoading()
85
+ showToast({
86
+ title: res.msg,
87
+ icon: 'error',
88
+ })
89
+ }
90
+ } catch (err) {
91
+ hideLoading()
92
+ console.log(err)
93
+ }
94
+ }
95
+
96
+ // 根据证件路径获取证件信息
97
+ async function getOcrInfo(file: string | FileType) {
98
+ try {
99
+ const res: any = await $http.get('/hkbase/common/vatInvoice', {
100
+ fileUrl: typeof file === 'string' ? file : file.originalUrl,
101
+ fileType: 'img'
102
+ })
103
+ hideLoading()
104
+
105
+ if (!res?.purchaserRegisterNum) {
106
+ $n.dialog({
107
+ title: '识别失败',
108
+ message: `您上传的图片可能不够清晰或与当前功能不符,请重新上传一张清晰、完整的图片。谢谢!`,
109
+ okText: '我知道了',
110
+ cancelText: '',
111
+ })
112
+ return
113
+ }
114
+
115
+ emits('complete', {
116
+ invoiceDate: res?.invoiceDate,
117
+ invoiceNum: res?.invoiceNum,
118
+ invoiceNumConfirm: res?.invoiceNumConfirm,
119
+ invoiceType: res?.invoiceType,
120
+ noteDrawer: res?.noteDrawer,
121
+ purchaserBank: res?.purchaserBank,
122
+ purchaserName: res?.purchaserName,
123
+ purchaserRegisterNum: res?.purchaserRegisterNum,
124
+ remarks: res?.remarks,
125
+ sellerName: res?.sellerName,
126
+ sellerRegisterNum: res?.sellerRegisterNum,
127
+ serviceType: res?.serviceType,
128
+ totalAmount: res?.totalAmount,
129
+ totalTax: res?.totalTax,
130
+ fileUrl: typeof file === 'string' ? file : file.originalUrl,
131
+ fileKey: typeof file === 'string' ? file : file.fileId,
132
+ })
133
+ } catch (err) {
134
+ hideLoading()
135
+ }
136
+ }
137
+
138
+ // 上传图片操作面板
139
+ const activeSheetVisible = ref(false)
140
+ const actionSheetMenus = [
141
+ {
142
+ name: '拍摄',
143
+ type: 'camera',
144
+ },
145
+ {
146
+ name: '从相册选择',
147
+ type: 'album',
148
+ },
149
+ {
150
+ name: '从聊天会话选择',
151
+ type: 'message',
152
+ },
153
+ ]
154
+ if (Taro.getEnv() === 'WEB') {
155
+ actionSheetMenus.pop()
156
+ }
157
+
158
+ // 选择图像上传
159
+ async function chooseImages(item: any) {
160
+ if (['camera', 'album'].includes(item.type)) {
161
+ const csRes = await chooseMedia({
162
+ count: 1,
163
+ sourceType: [item.type], // "camera" | "album"
164
+ maxDuration: 60, // 使用duration属性判断是图片还是视频,图片没有该属性
165
+ })
166
+
167
+ onUploadFile(csRes)
168
+ } else {
169
+ const csRes = await chooseMessageFile({
170
+ count: 1,
171
+ type: 'image',
172
+ })
173
+
174
+ onUploadFile(csRes)
175
+ }
176
+ }
177
+
178
+ async function onUpload() {
179
+ if (props.disabled) return
180
+
181
+ if (Taro.getEnv() === 'WEB') {
182
+ const csRes = await chooseMedia({
183
+ count: 1,
184
+ sourceType: ['album'], // "camera" | "album"
185
+ maxDuration: 60, // 使用duration属性判断是图片还是视频,图片没有该属性
186
+ })
187
+
188
+ onUploadFile(csRes)
189
+ return
190
+ }
191
+
192
+ activeSheetVisible.value = true
193
+ }
194
+
195
+ defineExpose({
196
+ onUpload
197
+ })
198
+ </script>
199
+
200
+ <style lang="scss">
201
+ .ocr-invoice {
202
+ width: 24px;
203
+ height: 24px;
204
+ display: inline-flex;
205
+ align-items: center;
206
+
207
+ .ns-icon {
208
+ width: 24px;
209
+ height: 24px;
210
+ }
211
+
212
+ &.disabled {
213
+ .ns-icon {
214
+ filter: brightness(1.5) grayscale(1);
215
+ }
216
+ }
217
+ }
218
+ </style>
@@ -3,6 +3,8 @@ import PageHeader from './PageHeader.vue'
3
3
  import AppVerify from './AppVerify.vue'
4
4
  import DeviceVersion from './DeviceVersion.vue'
5
5
  import OcrIcon from './OcrIcon.vue'
6
+ import OcrBank from './OcrBank.vue'
6
7
  import OcrBusinessLicense from './OcrBusinessLicense.vue'
8
+ import OcrInvoice from './OcrInvoice.vue'
7
9
 
8
- export { AppDrawer, PageHeader, DeviceVersion, AppVerify, OcrIcon, OcrBusinessLicense }
10
+ export { AppDrawer, PageHeader, DeviceVersion, AppVerify, OcrIcon, OcrBank,OcrBusinessLicense, OcrInvoice }
@@ -7,3 +7,4 @@ export * from './useUpload'
7
7
  export * from './useCrypto'
8
8
  export * from './useLogger'
9
9
  export * from './useWxAuth'
10
+ export * from './useCompress'
@@ -0,0 +1,64 @@
1
+ import Taro, { uploadFile } from '@tarojs/taro'
2
+ import { Media } from '@uxda/nutshell/taro'
3
+ import { useAppKitOptions } from '../../Appkit'
4
+
5
+ export type UploadConfig = {
6
+ baseUrl: string
7
+ name?: string
8
+ headers?: Record<string, string>
9
+ }
10
+
11
+ const mappings: { [x: string]: keyof Media } = {
12
+ downloadUrl: 'thrumb',
13
+ fileId: 'id',
14
+ fileName: 'name',
15
+ fileSize: 'size',
16
+ fileSuffix: 'ext',
17
+ fileType: 'type',
18
+ originalUrl: 'url',
19
+ }
20
+
21
+ const transformFields = (row: any) => {
22
+ return Object.fromEntries(Object.entries(row).map(([k, v]) => [mappings[k] || k, v]))
23
+ }
24
+
25
+ type UploadFunction = (url: string, file: Media) => Promise<Media>
26
+
27
+ export const useUpload = (config: UploadConfig) => {
28
+ const appkitOptions = useAppKitOptions()
29
+
30
+ // 上传文件
31
+ const upload: UploadFunction = (url: string, file: Media) => {
32
+ return new Promise<Media>((resolve, reject) => {
33
+ uploadFile({
34
+ url: config.baseUrl + url,
35
+ filePath: file.path!,
36
+ name: 'file',
37
+ formData: {
38
+ objectNo: `min${Date.now()}`,
39
+ appCode: config.headers?.appcode || appkitOptions.app(),
40
+ },
41
+ header: {
42
+ ...config.headers,
43
+ token: appkitOptions.tempToken() || appkitOptions.token(),
44
+ },
45
+ success: (rsp: any) => {
46
+ const { data } = rsp
47
+ try {
48
+ const response = JSON.parse(data)
49
+ console.log('===response', response)
50
+ resolve(transformFields(response.result))
51
+ } catch (e) {
52
+ reject({
53
+ message: '文件上传异常',
54
+ })
55
+ }
56
+ },
57
+ })
58
+ })
59
+ }
60
+
61
+ return {
62
+ upload,
63
+ }
64
+ }
@@ -1,61 +1,106 @@
1
- import Taro, { uploadFile } from '@tarojs/taro'
2
- import { Media } from '@uxda/nutshell/taro'
3
- import { useAppKitOptions } from '../../Appkit'
4
-
5
- export type UploadConfig = {
6
- baseUrl: string
7
- name?: string
8
- headers?: Record<string, string>
9
- }
1
+ import Taro from "@tarojs/taro";
10
2
 
11
- const mappings: { [x: string]: keyof Media } = {
12
- downloadUrl: 'thrumb',
13
- fileId: 'id',
14
- fileName: 'name',
15
- fileSize: 'size',
16
- fileSuffix: 'ext',
17
- fileType: 'type',
18
- originalUrl: 'url',
19
- }
3
+ /**
4
+ * 使用 canvas 压缩图片(Web 端)
5
+ */
6
+ export const compressImageWithCanvas = (
7
+ src: string,
8
+ quality: number
9
+ ): Promise<string> => {
10
+ return new Promise((resolve, reject) => {
11
+ const img = new Image();
12
+ img.crossOrigin = "anonymous";
20
13
 
21
- const transformFields = (row: any) => {
22
- return Object.fromEntries(Object.entries(row).map(([k, v]) => [mappings[k] || k, v]))
23
- }
14
+ img.onload = () => {
15
+ try {
16
+ const canvas = document.createElement("canvas");
17
+ const ctx = canvas.getContext("2d");
24
18
 
25
- type UploadFunction = (url: string, file: Media) => Promise<Media>
19
+ if (!ctx) {
20
+ reject(new Error("无法获取 canvas 上下文"));
21
+ return;
22
+ }
26
23
 
27
- export const useUpload = (config: UploadConfig) => {
28
- const appkitOptions = useAppKitOptions()
24
+ // 设置画布尺寸(可以根据需要限制最大尺寸)
25
+ const maxWidth = 900;
26
+ const maxHeight = 900;
27
+ let width = img.width;
28
+ let height = img.height;
29
29
 
30
- const upload: UploadFunction = (url: string, file: Media) => {
31
- return new Promise<Media>((resolve, reject) => {
32
- uploadFile({
33
- url: config.baseUrl + url,
34
- filePath: file.path!,
35
- name: 'file',
36
- formData: {
37
- objectNo: `min${Date.now()}`,
38
- },
39
- header: {
40
- ...config.headers,
41
- token: appkitOptions.tempToken() || appkitOptions.token(),
30
+ // 计算压缩后的尺寸
31
+ if (width > maxWidth || height > maxHeight) {
32
+ const ratio = Math.min(maxWidth / width, maxHeight / height);
33
+ width = width * ratio;
34
+ height = height * ratio;
35
+ }
36
+
37
+ canvas.width = width;
38
+ canvas.height = height;
39
+
40
+ // 绘制图片到 canvas
41
+ ctx.drawImage(img, 0, 0, width, height);
42
+
43
+ // 转换为 blob
44
+ canvas.toBlob(
45
+ (blob) => {
46
+ if (!blob) {
47
+ reject(new Error("图片压缩失败"));
48
+ return;
49
+ }
50
+ // 创建 blob URL
51
+ const compressedUrl = URL.createObjectURL(blob);
52
+ resolve(compressedUrl);
53
+ },
54
+ "image/jpeg",
55
+ quality / 100
56
+ );
57
+ } catch (error) {
58
+ reject(error);
59
+ }
60
+ };
61
+
62
+ img.onerror = () => {
63
+ reject(new Error("图片加载失败"));
64
+ };
65
+
66
+ img.src = src;
67
+ });
68
+ };
69
+
70
+ // 压缩图片
71
+ export async function compressImage(src: string, quality = 80) {
72
+ if (Taro.getEnv() === "WEB") {
73
+ try {
74
+ const compressedUrl = await compressImageWithCanvas(src, quality);
75
+ // 返回格式与小程序端一致,包含 tempFilePath 属性
76
+ return { tempFilePath: compressedUrl };
77
+ } catch (error) {
78
+ console.error("图片压缩失败:", error);
79
+ // 压缩失败时返回原图
80
+ return { tempFilePath: src };
81
+ }
82
+ } else {
83
+ return new Promise((resolve, reject) => {
84
+ Taro.compressImage({
85
+ src: src,
86
+ quality: quality,
87
+ success: (res) => {
88
+ resolve(res);
42
89
  },
43
- success: (rsp: any) => {
44
- const { data } = rsp
45
- try {
46
- const response = JSON.parse(data)
47
- console.log('===response', response)
48
- resolve(transformFields(response.result))
49
- } catch (e) {
50
- reject({
51
- message: '文件上传异常',
52
- })
53
- }
90
+ fail: (res) => {
91
+ reject(res);
54
92
  },
55
- })
56
- })
93
+ });
94
+ });
57
95
  }
58
- return {
59
- upload,
96
+ }
97
+
98
+ // 获取压缩质量
99
+ export function getCompressQuality(size: number) {
100
+ let quality = 100;
101
+ const curSize = size / (1024 * 1024);
102
+ if (curSize > 6) {
103
+ quality = quality - ((curSize - 6) / curSize) * 100;
60
104
  }
105
+ return quality;
61
106
  }
@@ -697,7 +697,6 @@ class TrackingSDK {
697
697
  * 发送埋点数据
698
698
  */
699
699
  private async flush(): Promise<void> {
700
- console.log(this.eventQueue.length, 'this.eventQueue.length')
701
700
  if (this.eventQueue.length === 0) return
702
701
 
703
702
  const events = [...this.eventQueue]
@@ -6,7 +6,7 @@
6
6
  <ns-input label="你的姓名" name="姓名" v-model="formData.姓名" placeholder="请输入或拍照识别" :maxlength="30" :disabled="已认证"
7
7
  :rules="['required']">
8
8
  <template #append>
9
- <ocr-icon v-if="!已认证" @complete="onOcrComplete" />
9
+ <ocr-icon v-if="!已认证" :has-upload-vo="false" @complete="onOcrComplete" />
10
10
  </template>
11
11
  </ns-input>
12
12
  <ns-input label="身份证号码" name="身份证号码" placeholder="请输入身份证号码" :maxlength="30" v-model="formData.身份证号码"
@@ -202,6 +202,7 @@ function requestFeedback() {
202
202
 
203
203
  showLoading({
204
204
  title: '反馈中...',
205
+ mask: true
205
206
  })
206
207
 
207
208
  const attachment = JSON.parse(
@@ -220,6 +220,7 @@ async function toUpload() {
220
220
  async function updateImage(filePath: string) {
221
221
  showLoading({
222
222
  title: "上传中...",
223
+ mask: true
223
224
  });
224
225
  const appkitOptions = useAppKitOptions();
225
226
  const $http = useHttp();
package/types/global.d.ts CHANGED
@@ -20,3 +20,5 @@ declare namespace NodeJS {
20
20
  declare module '@tarojs/components' {
21
21
  export * from '@tarojs/components/types/index.vue3'
22
22
  }
23
+
24
+ declare module '@uxda/nutshell/taro'