Commit c9bc327f4f8aec50fc9a8203c4a0e7ac11368c71
1 parent
3b0c279a
修改前端
Showing
47 changed files
with
2270 additions
and
824 deletions
泰额版/Food Labeling Management Code/Yi.Vben5.Vue3/apps/web-antd/src/locales/langs/en-US/foodLabeling.json
| ... | ... | @@ -47,6 +47,7 @@ |
| 47 | 47 | "selectLocation": "Select location", |
| 48 | 48 | "selectCategory": "Select label category", |
| 49 | 49 | "selectType": "Select label type", |
| 50 | + "selectTypeOptional": "Optional — select label type", | |
| 50 | 51 | "selectTemplate": "Select template", |
| 51 | 52 | "action": "Actions", |
| 52 | 53 | "labelCodeOptional": "Leave empty to auto-generate", | ... | ... |
泰额版/Food Labeling Management Code/Yi.Vben5.Vue3/apps/web-antd/src/locales/langs/zh-CN/foodLabeling.json
泰额版/Food Labeling Management Code/Yi.Vben5.Vue3/apps/web-antd/src/views/food-labeling/labeling/labels/data.ts
| ... | ... | @@ -198,12 +198,15 @@ export function modalSchema(options: { |
| 198 | 198 | component: 'Select', |
| 199 | 199 | fieldName: 'labelTypeId', |
| 200 | 200 | label: $t('foodLabeling.labels.labelType'), |
| 201 | - rules: 'required', | |
| 201 | + ...(options.isUpdate ? { rules: 'required' as const } : {}), | |
| 202 | 202 | componentProps: { |
| 203 | 203 | options: options.typeOptions, |
| 204 | 204 | showSearch: true, |
| 205 | 205 | optionFilterProp: 'label', |
| 206 | - placeholder: $t('foodLabeling.labels.selectType'), | |
| 206 | + allowClear: !options.isUpdate, | |
| 207 | + placeholder: options.isUpdate | |
| 208 | + ? $t('foodLabeling.labels.selectType') | |
| 209 | + : $t('foodLabeling.labels.selectTypeOptional'), | |
| 207 | 210 | onChange: (v: string) => options.onLabelTypeIdChange?.(v), |
| 208 | 211 | }, |
| 209 | 212 | }, | ... | ... |
泰额版/Food Labeling Management Code/Yi.Vben5.Vue3/apps/web-antd/src/views/food-labeling/labeling/labels/label-modal.vue
| ... | ... | @@ -274,7 +274,7 @@ function formToPayload(values: Record<string, unknown>) { |
| 274 | 274 | templateCode: String(values.templateCode ?? '').trim(), |
| 275 | 275 | locationId: String(values.locationId ?? '').trim(), |
| 276 | 276 | labelCategoryId: String(values.labelCategoryId ?? '').trim(), |
| 277 | - labelTypeId: String(values.labelTypeId ?? '').trim(), | |
| 277 | + labelTypeId: String(values.labelTypeId ?? '').trim() || null, | |
| 278 | 278 | productIds: productId ? [productId] : [], |
| 279 | 279 | labelInfoJson: null, |
| 280 | 280 | state: values.state !== false, | ... | ... |
美国版/Food Labeling Management App UniApp/.env.development
美国版/Food Labeling Management App UniApp/.env.example
美国版/Food Labeling Management App UniApp/src/pages/labels/bluetooth.vue
| ... | ... | @@ -28,7 +28,7 @@ |
| 28 | 28 | <text class="tab-text">Bluetooth</text> |
| 29 | 29 | </view> |
| 30 | 30 | <view |
| 31 | - v-if="isBuiltinModeAvailable" | |
| 31 | + v-if="isBuiltinModeAvailable && SHOW_BUILTIN_PRINTER_UI" | |
| 32 | 32 | class="tab" |
| 33 | 33 | :class="{ active: printerType === 'builtin' }" |
| 34 | 34 | @click="switchType('builtin')" |
| ... | ... | @@ -64,8 +64,8 @@ |
| 64 | 64 | </view> |
| 65 | 65 | </view> |
| 66 | 66 | |
| 67 | - <!-- Print route debug: always visible when connected (no need to expand) --> | |
| 68 | - <view v-if="connectedDevice" class="print-route-card"> | |
| 67 | + <!-- Print route debug: hidden in production UI --> | |
| 68 | + <view v-if="connectedDevice && SHOW_PRINT_ROUTE_DEBUG" class="print-route-card"> | |
| 69 | 69 | <view class="print-route-header"> |
| 70 | 70 | <text class="print-route-title">Print Route (Debug)</text> |
| 71 | 71 | <view class="btn-refresh-debug" @click="refreshPrintRouteDebug"> |
| ... | ... | @@ -92,7 +92,7 @@ |
| 92 | 92 | </view> |
| 93 | 93 | |
| 94 | 94 | <!-- Bluetooth: scan & list --> |
| 95 | - <template v-if="printerType === 'bluetooth'"> | |
| 95 | + <template v-if="printerType === 'bluetooth' || !SHOW_BUILTIN_PRINTER_UI"> | |
| 96 | 96 | <view class="section-header"> |
| 97 | 97 | <text class="section-title">Available Printers</text> |
| 98 | 98 | <view class="btn-scan" :class="{ scanning: isScanning }" @click="handleScan"> |
| ... | ... | @@ -100,7 +100,7 @@ |
| 100 | 100 | <text class="btn-scan-text">{{ isScanning ? 'Scanning...' : 'Scan' }}</text> |
| 101 | 101 | </view> |
| 102 | 102 | </view> |
| 103 | - <view v-if="isBuiltinModeAvailable" class="switch-to-builtin-row"> | |
| 103 | + <view v-if="isBuiltinModeAvailable && SHOW_BUILTIN_PRINTER_UI" class="switch-to-builtin-row"> | |
| 104 | 104 | <view class="btn-switch-to-builtin" @click="switchToBuiltinMode"> |
| 105 | 105 | <text class="btn-switch-to-builtin-text">Built-in Printing</text> |
| 106 | 106 | </view> |
| ... | ... | @@ -148,8 +148,8 @@ |
| 148 | 148 | </view> |
| 149 | 149 | </template> |
| 150 | 150 | |
| 151 | - <!-- Built-in: single option --> | |
| 152 | - <template v-else> | |
| 151 | + <!-- Built-in / UPOS:仅开发调试时展示 --> | |
| 152 | + <template v-else-if="SHOW_BUILTIN_PRINTER_UI"> | |
| 153 | 153 | <view class="section-header"> |
| 154 | 154 | <text class="section-title">Built-in Printer</text> |
| 155 | 155 | </view> |
| ... | ... | @@ -165,14 +165,14 @@ |
| 165 | 165 | </view> |
| 166 | 166 | </view> |
| 167 | 167 | |
| 168 | - <view class="builtin-advanced-entry"> | |
| 168 | + <view v-if="SHOW_BUILTIN_UPOS_ADVANCED" class="builtin-advanced-entry"> | |
| 169 | 169 | <view class="btn-advanced" @click="showBuiltinAdvanced = !showBuiltinAdvanced"> |
| 170 | 170 | <text class="btn-advanced-text">{{ showBuiltinAdvanced ? 'Hide advanced options' : 'Show advanced options' }}</text> |
| 171 | 171 | </view> |
| 172 | 172 | </view> |
| 173 | 173 | |
| 174 | 174 | <!-- UPOS options: builtin / serial --> |
| 175 | - <view v-if="showBuiltinAdvanced" class="upos-card"> | |
| 175 | + <view v-if="SHOW_BUILTIN_UPOS_ADVANCED && showBuiltinAdvanced" class="upos-card"> | |
| 176 | 176 | <text class="upos-title">UPOS options (for AIO devices)</text> |
| 177 | 177 | <text class="upos-desc"> |
| 178 | 178 | If printing does not feed paper, try switching to Serial and set a correct port/baudrate. |
| ... | ... | @@ -254,15 +254,12 @@ |
| 254 | 254 | <text class="tips-item">3. Place the printer within 10 m and tap Scan again</text> |
| 255 | 255 | <text class="tips-item">4. Scan list only shows: GP-D320FX-spp_A7FO and Virtual BT Printer</text> |
| 256 | 256 | <text class="tips-item">5. Pair the printer in system Bluetooth first if it does not appear after Scan</text> |
| 257 | - <text class="tips-item">6. Restart the printer or app if not visible</text> | |
| 258 | - <text class="tips-item tips-item-last"> | |
| 259 | - 7. Built-in TSC: defaults to TSPL label commands via UPOS; use Advanced → ESC/POS only if your device is receipt-only. | |
| 260 | - </text> | |
| 257 | + <text class="tips-item tips-item-last">6. Restart the printer or app if not visible</text> | |
| 261 | 258 | </view> |
| 262 | 259 | </view> |
| 263 | 260 | </view> |
| 264 | 261 | |
| 265 | - <view class="collapse-section collapse-section--debug"> | |
| 262 | + <view v-if="SHOW_PRINTER_DEBUG_UI" class="collapse-section collapse-section--debug"> | |
| 266 | 263 | <view class="collapse-header collapse-header--debug" @click="debugExpanded = !debugExpanded"> |
| 267 | 264 | <text class="collapse-title">Debug Status</text> |
| 268 | 265 | <view class="collapse-action-wrap"> |
| ... | ... | @@ -324,6 +321,7 @@ import { |
| 324 | 321 | restoreD320faxVirtualBtBluetoothMode, |
| 325 | 322 | resolvePrintSendChannelHint, |
| 326 | 323 | setBuiltinPrinter, |
| 324 | + isAoaAioDevice, | |
| 327 | 325 | } from '../../utils/print/printerConnection' |
| 328 | 326 | import { ensureBluetoothPermissions } from '../../utils/print/bluetoothPermissions' |
| 329 | 327 | import { isAllowedBluetoothPrinterName } from '../../utils/print/bluetoothPrinterAllowlist' |
| ... | ... | @@ -361,11 +359,22 @@ const btAdapterReady = ref(false) |
| 361 | 359 | const availablePrinterTypes = getAvailablePrinterTypes() |
| 362 | 360 | const availablePrinterTypesLabel = availablePrinterTypes.map(item => item === 'builtin' ? 'Built-in' : 'Bluetooth').join(' / ') |
| 363 | 361 | const deviceIdentity = getDeviceIdentity() |
| 362 | +const isAoaDevice = isAoaAioDevice() | |
| 364 | 363 | const isBluetoothModeAvailable = availablePrinterTypes.includes('bluetooth') |
| 365 | 364 | const isBuiltinModeAvailable = availablePrinterTypes.includes('builtin') |
| 366 | -/** 打印机设置页优先展示 Bluetooth 扫描(便于配对);实际出纸仍读 storage 中的 builtin/bluetooth */ | |
| 365 | +/** 打印机页「Print Route (Debug)」卡片;正式版对用户隐藏 */ | |
| 366 | +const SHOW_PRINT_ROUTE_DEBUG = false | |
| 367 | +/** AOA 一体机显示 Built-in 入口;普通手机隐藏 */ | |
| 368 | +const SHOW_BUILTIN_PRINTER_UI = isAoaDevice | |
| 369 | +/** UPOS 高级选项仅开发调试 */ | |
| 370 | +const SHOW_BUILTIN_UPOS_ADVANCED = false | |
| 371 | +/** Troubleshooting 下方 Debug Status;正式版对用户隐藏 */ | |
| 372 | +const SHOW_PRINTER_DEBUG_UI = false | |
| 373 | +/** AOA 默认内置打印;其它设备默认蓝牙 */ | |
| 367 | 374 | const printerType = ref<PrinterType>( |
| 368 | - isBluetoothModeAvailable ? 'bluetooth' : (isBuiltinModeAvailable ? 'builtin' : 'bluetooth'), | |
| 375 | + isAoaDevice && isBuiltinModeAvailable | |
| 376 | + ? 'builtin' | |
| 377 | + : (isBluetoothModeAvailable ? 'bluetooth' : (isBuiltinModeAvailable ? 'builtin' : 'bluetooth')), | |
| 369 | 378 | ) |
| 370 | 379 | const currentPrinter = ref(getCurrentPrinterSummary()) |
| 371 | 380 | const showBuiltinAdvanced = ref(false) |
| ... | ... | @@ -548,6 +557,7 @@ const connectedDevice = computed(() => { |
| 548 | 557 | }) |
| 549 | 558 | |
| 550 | 559 | function switchType (type: 'bluetooth' | 'builtin') { |
| 560 | + if (!SHOW_BUILTIN_PRINTER_UI && type === 'builtin') return | |
| 551 | 561 | if (!availablePrinterTypes.includes(type)) return |
| 552 | 562 | printerType.value = type |
| 553 | 563 | showBuiltinAdvanced.value = false |
| ... | ... | @@ -558,7 +568,7 @@ function switchType (type: 'bluetooth' | 'builtin') { |
| 558 | 568 | } |
| 559 | 569 | |
| 560 | 570 | function switchToBuiltinMode () { |
| 561 | - if (!isBuiltinModeAvailable) return | |
| 571 | + if (!SHOW_BUILTIN_PRINTER_UI || !isBuiltinModeAvailable) return | |
| 562 | 572 | switchType('builtin') |
| 563 | 573 | } |
| 564 | 574 | |
| ... | ... | @@ -927,20 +937,38 @@ const handleDisconnect = async () => { |
| 927 | 937 | uni.showToast({ title: 'Disconnected', icon: 'none' }) |
| 928 | 938 | } |
| 929 | 939 | |
| 940 | +function ensureAoaBuiltinPrinterReady () { | |
| 941 | + if (!isAoaDevice || !isBuiltinModeAvailable) return | |
| 942 | + printerType.value = 'builtin' | |
| 943 | + if (getPrinterType() !== 'builtin') { | |
| 944 | + useBuiltinPrinter(builtinDriverKey.value) | |
| 945 | + logBuiltinTscCapability('aoa_auto_builtin', { | |
| 946 | + screen: 'bluetooth_builtin', | |
| 947 | + driverKey: builtinDriverKey.value, | |
| 948 | + model: deviceIdentity.model || '', | |
| 949 | + product: deviceIdentity.product || '', | |
| 950 | + }) | |
| 951 | + refreshCurrentPrinter() | |
| 952 | + } | |
| 953 | +} | |
| 954 | + | |
| 930 | 955 | onMounted(() => { |
| 931 | 956 | debugInfo.value.classicModuleReady = !!classicBluetooth |
| 932 | 957 | refreshNativeDebug() |
| 933 | - uni.onBluetoothDeviceFound(onDeviceFound) | |
| 934 | - uni.onBluetoothAdapterStateChange((res: any) => { | |
| 935 | - if (!res.available) { | |
| 936 | - btAdapterReady.value = false | |
| 937 | - isScanning.value = false | |
| 938 | - errorMsg.value = 'Bluetooth has been turned off.' | |
| 939 | - } else { | |
| 940 | - btAdapterReady.value = true | |
| 941 | - errorMsg.value = '' | |
| 942 | - } | |
| 943 | - }) | |
| 958 | + ensureAoaBuiltinPrinterReady() | |
| 959 | + if (!isAoaDevice) { | |
| 960 | + uni.onBluetoothDeviceFound(onDeviceFound) | |
| 961 | + uni.onBluetoothAdapterStateChange((res: any) => { | |
| 962 | + if (!res.available) { | |
| 963 | + btAdapterReady.value = false | |
| 964 | + isScanning.value = false | |
| 965 | + errorMsg.value = 'Bluetooth has been turned off.' | |
| 966 | + } else { | |
| 967 | + btAdapterReady.value = true | |
| 968 | + errorMsg.value = '' | |
| 969 | + } | |
| 970 | + }) | |
| 971 | + } | |
| 944 | 972 | refreshCurrentPrinter() |
| 945 | 973 | }) |
| 946 | 974 | |
| ... | ... | @@ -968,12 +996,15 @@ async function refreshBluetoothDeviceList () { |
| 968 | 996 | |
| 969 | 997 | onShow(() => { |
| 970 | 998 | restoreD320faxVirtualBtBluetoothMode() |
| 999 | + ensureAoaBuiltinPrinterReady() | |
| 971 | 1000 | refreshCurrentPrinter() |
| 972 | 1001 | if (isVirtualBtBluetoothConnection()) { |
| 973 | 1002 | debugExpanded.value = true |
| 974 | 1003 | } |
| 975 | 1004 | void refreshNativeDebug() |
| 976 | - void refreshBluetoothDeviceList() | |
| 1005 | + if (!isAoaDevice) { | |
| 1006 | + void refreshBluetoothDeviceList() | |
| 1007 | + } | |
| 977 | 1008 | }) |
| 978 | 1009 | |
| 979 | 1010 | onUnmounted(() => { | ... | ... |
美国版/Food Labeling Management App UniApp/src/pages/labels/labels.vue
| ... | ... | @@ -40,38 +40,47 @@ |
| 40 | 40 | @click="selectedCategoryId = cat.id" |
| 41 | 41 | > |
| 42 | 42 | <view v-if="selectedCategoryId === cat.id" class="active-bar" /> |
| 43 | - <view | |
| 44 | - class="cat-icon" | |
| 45 | - :class="[ | |
| 46 | - labelCategoryVisual(cat).mode === 'image' ? 'cat-icon--photo' : 'cat-icon--fallback', | |
| 47 | - 'cat-icon--fixed', | |
| 48 | - ]" | |
| 49 | - :style="labelCategoryIconBoxStyle(cat)" | |
| 50 | - > | |
| 51 | - <image | |
| 52 | - v-if="labelCategoryVisual(cat).mode === 'image'" | |
| 53 | - :src="resolveMediaUrlForApp(labelCategoryVisual(cat).imageUrl) || ''" | |
| 54 | - class="cat-photo" | |
| 55 | - mode="aspectFill" | |
| 56 | - /> | |
| 43 | + <view class="cat-card-frame"> | |
| 44 | + <view class="cat-card-inner"> | |
| 57 | 45 | <view |
| 58 | - v-else-if="labelCategoryVisual(cat).mode === 'colorText'" | |
| 59 | - class="cat-icon-text-inner" | |
| 46 | + class="cat-visual" | |
| 47 | + :class="[ | |
| 48 | + labelCategoryVisual(cat).mode === 'image' ? 'cat-visual--photo' : 'cat-visual--fallback', | |
| 49 | + ]" | |
| 50 | + :style="labelCategoryVisualZoneStyle(cat)" | |
| 60 | 51 | > |
| 61 | - <text | |
| 62 | - class="cat-icon-text cat-icon-text--on-color cat-icon-text--wrap" | |
| 63 | - :style="{ color: labelCategoryVisual(cat).textColor || '#ffffff' }" | |
| 52 | + <image | |
| 53 | + v-if="labelCategoryVisual(cat).mode === 'image'" | |
| 54 | + :src="resolveMediaUrlForApp(labelCategoryVisual(cat).imageUrl) || ''" | |
| 55 | + class="cat-photo-fill" | |
| 56 | + mode="aspectFill" | |
| 57 | + /> | |
| 58 | + <view | |
| 59 | + v-else-if="labelCategoryVisual(cat).mode === 'colorText'" | |
| 60 | + class="cat-visual-text-inner" | |
| 64 | 61 | > |
| 65 | - {{ labelCategoryVisual(cat).text }} | |
| 66 | - </text> | |
| 62 | + <text | |
| 63 | + class="cat-visual-text cat-visual-text--on-color" | |
| 64 | + :style="{ color: labelCategoryVisual(cat).textColor || '#ffffff' }" | |
| 65 | + > | |
| 66 | + {{ labelCategoryVisual(cat).text }} | |
| 67 | + </text> | |
| 68 | + </view> | |
| 69 | + <view v-else-if="labelCategoryVisual(cat).mode === 'color'" class="cat-visual-color-fill" /> | |
| 70 | + <view v-else-if="labelCategoryVisual(cat).mode === 'text'" class="cat-visual-text-inner"> | |
| 71 | + <text class="cat-visual-text">{{ labelCategoryVisual(cat).text }}</text> | |
| 72 | + </view> | |
| 73 | + <view v-else class="cat-visual-fallback" :class="colorClassForName(cat.categoryName)"> | |
| 74 | + <text class="cat-visual-text cat-visual-text--on-color"> | |
| 75 | + {{ (cat.categoryName || '').trim().slice(0, 12) }} | |
| 76 | + </text> | |
| 77 | + </view> | |
| 78 | + </view> | |
| 79 | + <view class="cat-name-band"> | |
| 80 | + <text class="cat-name">{{ cat.categoryName }}</text> | |
| 67 | 81 | </view> |
| 68 | - <view v-else-if="labelCategoryVisual(cat).mode === 'text'" class="cat-icon-text-inner"> | |
| 69 | - <text class="cat-icon-text cat-icon-text--wrap"> | |
| 70 | - {{ labelCategoryVisual(cat).text }} | |
| 71 | - </text> | |
| 72 | 82 | </view> |
| 73 | 83 | </view> |
| 74 | - <text class="cat-name">{{ cat.categoryName }}</text> | |
| 75 | 84 | </view> |
| 76 | 85 | </view> |
| 77 | 86 | |
| ... | ... | @@ -194,58 +203,60 @@ |
| 194 | 203 | class="food-card" |
| 195 | 204 | @click="handleProductClick(product, pCat.name)" |
| 196 | 205 | > |
| 197 | - <view | |
| 198 | - class="food-img-wrap" | |
| 199 | - :class=" | |
| 200 | - productVisual(product, pCat).mode === 'image' | |
| 201 | - ? 'food-img-wrap--photo' | |
| 202 | - : 'food-img-wrap--fallback' | |
| 203 | - " | |
| 204 | - :style="productThumbWrapStyle(product, pCat)" | |
| 205 | - > | |
| 206 | - <image | |
| 207 | - v-if="productVisual(product, pCat).mode === 'image'" | |
| 208 | - :src="resolveMediaUrlForApp(productVisual(product, pCat).imageUrl) || ''" | |
| 209 | - class="food-img" | |
| 210 | - mode="aspectFill" | |
| 211 | - /> | |
| 206 | + <view class="food-card-frame"> | |
| 207 | + <view class="food-card-inner"> | |
| 212 | 208 | <view |
| 213 | - v-else-if="productVisual(product, pCat).mode === 'colorText'" | |
| 214 | - class="food-thumb-text-wrap" | |
| 209 | + class="food-visual" | |
| 210 | + :class=" | |
| 211 | + productVisual(product, pCat).mode === 'image' | |
| 212 | + ? 'food-visual--photo' | |
| 213 | + : 'food-visual--fallback' | |
| 214 | + " | |
| 215 | + :style="productVisualZoneStyle(product, pCat)" | |
| 215 | 216 | > |
| 216 | - <text | |
| 217 | - class="food-thumb-text food-thumb-text--on-color" | |
| 218 | - :style="{ color: productVisual(product, pCat).textColor || '#ffffff' }" | |
| 217 | + <image | |
| 218 | + v-if="productVisual(product, pCat).mode === 'image'" | |
| 219 | + :src="resolveMediaUrlForApp(productVisual(product, pCat).imageUrl) || ''" | |
| 220 | + class="food-img-fill" | |
| 221 | + mode="aspectFill" | |
| 222 | + /> | |
| 223 | + <view | |
| 224 | + v-else-if="productVisual(product, pCat).mode === 'colorText'" | |
| 225 | + class="food-visual-text-wrap" | |
| 219 | 226 | > |
| 220 | - {{ productThumbDisplayText(product, pCat) }} | |
| 221 | - </text> | |
| 222 | - </view> | |
| 223 | - <view | |
| 224 | - v-else-if="productVisual(product, pCat).mode === 'text'" | |
| 225 | - class="food-thumb-text-wrap" | |
| 226 | - > | |
| 227 | - <text class="food-thumb-text"> | |
| 228 | - {{ productThumbDisplayText(product, pCat) }} | |
| 229 | - </text> | |
| 227 | + <text | |
| 228 | + class="food-visual-text food-visual-text--on-color" | |
| 229 | + :style="{ color: productVisual(product, pCat).textColor || '#ffffff' }" | |
| 230 | + > | |
| 231 | + {{ productThumbDisplayText(product, pCat) }} | |
| 232 | + </text> | |
| 233 | + </view> | |
| 234 | + <view | |
| 235 | + v-else-if="productVisual(product, pCat).mode === 'text'" | |
| 236 | + class="food-visual-text-wrap" | |
| 237 | + > | |
| 238 | + <text class="food-visual-text"> | |
| 239 | + {{ productThumbDisplayText(product, pCat) }} | |
| 240 | + </text> | |
| 241 | + </view> | |
| 242 | + <view | |
| 243 | + v-else-if="productVisual(product, pCat).mode === 'color'" | |
| 244 | + class="food-visual-color-fill" | |
| 245 | + /> | |
| 246 | + <view | |
| 247 | + v-else | |
| 248 | + class="food-visual-fallback" | |
| 249 | + :class="colorClassForName(product.productName)" | |
| 250 | + > | |
| 251 | + <text class="food-visual-text food-visual-text--on-color"> | |
| 252 | + {{ productThumbFallbackText(product) }} | |
| 253 | + </text> | |
| 254 | + </view> | |
| 230 | 255 | </view> |
| 231 | - <view | |
| 232 | - v-else-if="productVisual(product, pCat).mode === 'color'" | |
| 233 | - class="food-thumb-color-fill" | |
| 234 | - :style="{ backgroundColor: productVisual(product, pCat).bg }" | |
| 235 | - /> | |
| 236 | - <view | |
| 237 | - v-else | |
| 238 | - class="food-thumb-fallback" | |
| 239 | - :class="colorClassForName(product.productName)" | |
| 240 | - > | |
| 241 | - <text class="food-thumb-text food-thumb-text--on-color food-thumb-text--wrap"> | |
| 242 | - {{ productThumbFallbackText(product) }} | |
| 243 | - </text> | |
| 256 | + <view class="food-name-band"> | |
| 257 | + <text class="food-name">{{ product.productName }}</text> | |
| 244 | 258 | </view> |
| 245 | - </view> | |
| 246 | - <view class="food-info-row"> | |
| 247 | - <text class="food-name">{{ product.productName }}</text> | |
| 248 | - <view class="food-meta-badges"> | |
| 259 | + <view class="food-meta-band"> | |
| 249 | 260 | <text class="food-meta-size">{{ primaryLabelSizeText(product) }}</text> |
| 250 | 261 | <text |
| 251 | 262 | v-if="effectiveLabelTypeCount(product) > 0" |
| ... | ... | @@ -254,8 +265,8 @@ |
| 254 | 265 | {{ effectiveLabelTypeCount(product) }} Types |
| 255 | 266 | </text> |
| 256 | 267 | </view> |
| 268 | + </view> | |
| 257 | 269 | </view> |
| 258 | - <text v-if="showFoodSubtitle(product)" class="food-desc">{{ product.subtitle }}</text> | |
| 259 | 270 | </view> |
| 260 | 271 | </view> |
| 261 | 272 | </view> |
| ... | ... | @@ -452,23 +463,23 @@ function labelCategoryVisual(cat: UsAppLabelCategoryTreeNodeDto): CategoryVisual |
| 452 | 463 | }) |
| 453 | 464 | } |
| 454 | 465 | |
| 455 | -function labelCategoryIconBoxStyle(cat: UsAppLabelCategoryTreeNodeDto): Record<string, string> { | |
| 466 | +function labelCategoryVisualZoneStyle(cat: UsAppLabelCategoryTreeNodeDto): Record<string, string> { | |
| 456 | 467 | const v = labelCategoryVisual(cat) |
| 457 | - const scale = layoutScale.value | |
| 458 | - if (v.mode === 'image') { | |
| 459 | - return categoryButtonDisplayWidthStyle(scale) | |
| 460 | - } | |
| 461 | - if (v.mode === 'color') { | |
| 462 | - return { | |
| 463 | - ...categoryButtonColorOnlyBoxStyle(scale), | |
| 464 | - backgroundColor: v.bg, | |
| 465 | - } | |
| 468 | + if (v.mode === 'color' || v.mode === 'colorText') { | |
| 469 | + return { backgroundColor: v.bg } | |
| 466 | 470 | } |
| 467 | - const style = categoryButtonDisplayTextShellStyle(scale) | |
| 468 | - if (v.mode === 'colorText') { | |
| 469 | - style.backgroundColor = v.bg | |
| 471 | + return {} | |
| 472 | +} | |
| 473 | + | |
| 474 | +function productVisualZoneStyle( | |
| 475 | + p: UsAppLabelingProductNodeDto, | |
| 476 | + pCat?: UsAppProductCategoryNodeDto, | |
| 477 | +): Record<string, string> { | |
| 478 | + const v = productVisual(p, pCat) | |
| 479 | + if (v.mode === 'color' || v.mode === 'colorText') { | |
| 480 | + return { backgroundColor: v.bg } | |
| 470 | 481 | } |
| 471 | - return style | |
| 482 | + return {} | |
| 472 | 483 | } |
| 473 | 484 | |
| 474 | 485 | function productCategoryVisual(p: UsAppProductCategoryNodeDto): CategoryVisualRender { |
| ... | ... | @@ -596,12 +607,6 @@ function productThumbDisplayText( |
| 596 | 607 | return t || name || '?' |
| 597 | 608 | } |
| 598 | 609 | |
| 599 | -function showFoodSubtitle(p: UsAppLabelingProductNodeDto): boolean { | |
| 600 | - const sub = (p.subtitle ?? '').trim() | |
| 601 | - if (!sub) return false | |
| 602 | - return sub.toLowerCase() !== (p.productName ?? '').trim().toLowerCase() | |
| 603 | -} | |
| 604 | - | |
| 605 | 610 | function productCategoryRowKey(p: UsAppProductCategoryNodeDto, index: number): string { |
| 606 | 611 | const id = p.categoryId != null ? String(p.categoryId).trim() : '' |
| 607 | 612 | if (id) return `id:${id}` |
| ... | ... | @@ -647,17 +652,6 @@ function productVisual( |
| 647 | 652 | return { mode: 'none' } |
| 648 | 653 | } |
| 649 | 654 | |
| 650 | -function productThumbWrapStyle( | |
| 651 | - p: UsAppLabelingProductNodeDto, | |
| 652 | - pCat?: UsAppProductCategoryNodeDto, | |
| 653 | -): Record<string, string> { | |
| 654 | - const v = productVisual(p, pCat) | |
| 655 | - if (v.mode === 'colorText' || v.mode === 'color') { | |
| 656 | - return { backgroundColor: v.bg } | |
| 657 | - } | |
| 658 | - return {} | |
| 659 | -} | |
| 660 | - | |
| 661 | 655 | function productThumbFallbackText(p: UsAppLabelingProductNodeDto): string { |
| 662 | 656 | const name = (p.productName ?? '').trim() |
| 663 | 657 | if (!name) return '?' |
| ... | ... | @@ -883,7 +877,7 @@ const goBluetoothPage = () => { |
| 883 | 877 | display: flex; |
| 884 | 878 | flex-direction: column; |
| 885 | 879 | align-items: center; |
| 886 | - padding: 20rpx 8rpx; | |
| 880 | + padding: 12rpx 10rpx; | |
| 887 | 881 | position: relative; |
| 888 | 882 | } |
| 889 | 883 | |
| ... | ... | @@ -901,82 +895,134 @@ const goBluetoothPage = () => { |
| 901 | 895 | border-radius: 0 6rpx 6rpx 0; |
| 902 | 896 | } |
| 903 | 897 | |
| 904 | -.cat-icon { | |
| 898 | +/* Label Category:外框 3:2,上 70% 视觉区 + 下 30% 名称 */ | |
| 899 | +.cat-card-frame { | |
| 900 | + width: 100%; | |
| 901 | + max-width: 200rpx; | |
| 905 | 902 | position: relative; |
| 906 | - display: flex; | |
| 907 | - align-items: center; | |
| 908 | - justify-content: center; | |
| 909 | - margin-bottom: 8rpx; | |
| 910 | - background: transparent; | |
| 903 | + height: 0; | |
| 904 | + padding-top: 66.6667%; | |
| 905 | + border-radius: 12rpx; | |
| 906 | + overflow: hidden; | |
| 907 | + background: #ffffff; | |
| 908 | + box-shadow: 0 2rpx 6rpx rgba(0, 0, 0, 0.06); | |
| 911 | 909 | box-sizing: border-box; |
| 912 | 910 | } |
| 913 | 911 | |
| 914 | -.cat-icon--fixed { | |
| 915 | - overflow: hidden; | |
| 912 | +.cat-card-inner { | |
| 913 | + position: absolute; | |
| 914 | + left: 0; | |
| 915 | + top: 0; | |
| 916 | + right: 0; | |
| 917 | + bottom: 0; | |
| 918 | + display: flex; | |
| 919 | + flex-direction: column; | |
| 916 | 920 | } |
| 917 | 921 | |
| 918 | -.cat-icon-text-inner { | |
| 919 | - position: relative; | |
| 922 | +.cat-visual { | |
| 923 | + flex: 7 1 0; | |
| 924 | + min-height: 0; | |
| 920 | 925 | width: 100%; |
| 921 | 926 | display: flex; |
| 922 | 927 | align-items: center; |
| 923 | 928 | justify-content: center; |
| 924 | 929 | overflow: hidden; |
| 925 | - padding: 4rpx 6rpx; | |
| 930 | + background: #f3f4f6; | |
| 926 | 931 | box-sizing: border-box; |
| 927 | 932 | } |
| 928 | 933 | |
| 929 | -.cat-icon--photo { | |
| 930 | - overflow: hidden; | |
| 931 | - border-radius: 14rpx; | |
| 932 | -} | |
| 933 | - | |
| 934 | -.cat-icon--fallback { | |
| 935 | - overflow: hidden; | |
| 936 | - border-radius: 14rpx; | |
| 934 | +.cat-visual--photo, | |
| 935 | +.cat-visual--fallback { | |
| 936 | + position: relative; | |
| 937 | 937 | } |
| 938 | 938 | |
| 939 | -.cat-photo { | |
| 939 | +.cat-photo-fill { | |
| 940 | 940 | width: 100%; |
| 941 | - height: auto; | |
| 942 | - border-radius: 14rpx; | |
| 941 | + height: 100%; | |
| 943 | 942 | display: block; |
| 944 | - vertical-align: top; | |
| 945 | 943 | } |
| 946 | 944 | |
| 947 | -.cat-icon-text { | |
| 948 | - max-width: 100%; | |
| 949 | - padding: 0; | |
| 950 | - font-size: 20rpx; | |
| 951 | - font-weight: 700; | |
| 952 | - color: #111827; | |
| 953 | - line-height: 1.15; | |
| 954 | - text-align: center; | |
| 955 | - box-sizing: border-box; | |
| 945 | +.cat-visual-color-fill { | |
| 946 | + width: 100%; | |
| 947 | + height: 100%; | |
| 956 | 948 | } |
| 957 | 949 | |
| 958 | -.cat-icon-text--wrap, | |
| 959 | -.cat-icon-text--on-color { | |
| 960 | - white-space: pre-wrap; | |
| 961 | - word-break: break-word; | |
| 950 | +.cat-visual-text-inner { | |
| 951 | + width: 100%; | |
| 952 | + height: 100%; | |
| 953 | + display: flex; | |
| 954 | + align-items: center; | |
| 955 | + justify-content: center; | |
| 956 | + padding: 6rpx 8rpx; | |
| 957 | + box-sizing: border-box; | |
| 962 | 958 | overflow: hidden; |
| 963 | 959 | } |
| 964 | 960 | |
| 965 | -.cat-icon-text--on-color { | |
| 966 | - max-width: 100%; | |
| 961 | +.cat-visual-text { | |
| 962 | + width: 100%; | |
| 967 | 963 | max-height: 100%; |
| 968 | - padding: 0; | |
| 969 | 964 | font-size: 18rpx; |
| 970 | 965 | font-weight: 700; |
| 966 | + color: #111827; | |
| 971 | 967 | line-height: 1.15; |
| 972 | 968 | text-align: center; |
| 969 | + white-space: pre-wrap; | |
| 970 | + word-break: break-word; | |
| 971 | + overflow: hidden; | |
| 972 | +} | |
| 973 | + | |
| 974 | +.cat-visual-text--on-color { | |
| 975 | + color: #ffffff; | |
| 976 | +} | |
| 977 | + | |
| 978 | +.cat-visual-fallback { | |
| 979 | + width: 100%; | |
| 980 | + height: 100%; | |
| 981 | + display: flex; | |
| 982 | + align-items: center; | |
| 983 | + justify-content: center; | |
| 984 | + padding: 6rpx; | |
| 985 | + box-sizing: border-box; | |
| 986 | +} | |
| 987 | + | |
| 988 | +.cat-visual-fallback.bg-red { | |
| 989 | + background: #ef4444; | |
| 990 | +} | |
| 991 | +.cat-visual-fallback.bg-blue { | |
| 992 | + background: #3b82f6; | |
| 993 | +} | |
| 994 | +.cat-visual-fallback.bg-green { | |
| 995 | + background: #22c55e; | |
| 996 | +} | |
| 997 | +.cat-visual-fallback.bg-orange { | |
| 998 | + background: #f97316; | |
| 999 | +} | |
| 1000 | +.cat-visual-fallback.bg-purple { | |
| 1001 | + background: #a855f7; | |
| 1002 | +} | |
| 1003 | + | |
| 1004 | +.cat-name-band { | |
| 1005 | + flex: 3 1 0; | |
| 1006 | + min-height: 0; | |
| 1007 | + width: 100%; | |
| 1008 | + display: flex; | |
| 1009 | + align-items: center; | |
| 1010 | + justify-content: center; | |
| 1011 | + padding: 4rpx 6rpx; | |
| 1012 | + background: #ffffff; | |
| 1013 | + box-sizing: border-box; | |
| 973 | 1014 | } |
| 974 | 1015 | |
| 975 | 1016 | .cat-name { |
| 976 | - font-size: 20rpx; | |
| 977 | - color: #4b5563; | |
| 1017 | + font-size: 18rpx; | |
| 1018 | + color: #374151; | |
| 978 | 1019 | text-align: center; |
| 1020 | + line-height: 1.2; | |
| 979 | 1021 | word-break: break-word; |
| 1022 | + overflow: hidden; | |
| 1023 | + display: -webkit-box; | |
| 1024 | + -webkit-box-orient: vertical; | |
| 1025 | + -webkit-line-clamp: 2; | |
| 980 | 1026 | } |
| 981 | 1027 | |
| 982 | 1028 | .cat-item.active .cat-name { |
| ... | ... | @@ -1171,185 +1217,180 @@ const goBluetoothPage = () => { |
| 1171 | 1217 | flex-wrap: wrap; |
| 1172 | 1218 | align-content: flex-start; |
| 1173 | 1219 | padding-top: 8rpx; |
| 1174 | - margin-left: -5rpx; | |
| 1175 | - margin-right: -5rpx; | |
| 1220 | + margin-left: -6rpx; | |
| 1221 | + margin-right: -6rpx; | |
| 1176 | 1222 | } |
| 1177 | 1223 | |
| 1224 | +/* Product:外框 3:2,上 70% 视觉 + 中 20% 名称 + 下 10% 尺寸/类型 */ | |
| 1178 | 1225 | .food-card { |
| 1179 | 1226 | width: 47%; |
| 1180 | - max-width: 240rpx; | |
| 1227 | + max-width: 260rpx; | |
| 1181 | 1228 | flex: 0 0 auto; |
| 1182 | 1229 | box-sizing: border-box; |
| 1183 | - margin: 0 5rpx 12rpx 5rpx; | |
| 1184 | - background: #f9fafb; | |
| 1185 | - padding: 8rpx; | |
| 1186 | - border-radius: 12rpx; | |
| 1230 | + margin: 0 6rpx 14rpx 6rpx; | |
| 1187 | 1231 | min-width: 0; |
| 1188 | 1232 | } |
| 1189 | 1233 | |
| 1190 | -.food-card:active { | |
| 1191 | - background: #f3f4f6; | |
| 1234 | +.food-card:active .food-card-frame { | |
| 1235 | + opacity: 0.92; | |
| 1192 | 1236 | } |
| 1193 | 1237 | |
| 1194 | -.food-img-wrap { | |
| 1238 | +.food-card-frame { | |
| 1195 | 1239 | width: 100%; |
| 1196 | 1240 | position: relative; |
| 1197 | 1241 | height: 0; |
| 1198 | - padding-top: 58%; | |
| 1199 | - border-radius: 8rpx; | |
| 1200 | - background: #e5e7eb; | |
| 1201 | - margin-bottom: 8rpx; | |
| 1242 | + padding-top: 66.6667%; | |
| 1243 | + border-radius: 12rpx; | |
| 1202 | 1244 | overflow: hidden; |
| 1245 | + background: #ffffff; | |
| 1246 | + border: 1rpx solid #e5e7eb; | |
| 1247 | + box-sizing: border-box; | |
| 1203 | 1248 | } |
| 1204 | 1249 | |
| 1205 | -.food-img-wrap--fallback { | |
| 1206 | - display: block; | |
| 1207 | -} | |
| 1208 | - | |
| 1209 | -.food-img { | |
| 1250 | +.food-card-inner { | |
| 1210 | 1251 | position: absolute; |
| 1211 | 1252 | left: 0; |
| 1212 | 1253 | top: 0; |
| 1254 | + right: 0; | |
| 1255 | + bottom: 0; | |
| 1256 | + display: flex; | |
| 1257 | + flex-direction: column; | |
| 1258 | +} | |
| 1259 | + | |
| 1260 | +.food-visual { | |
| 1261 | + flex: 7 1 0; | |
| 1262 | + min-height: 0; | |
| 1263 | + width: 100%; | |
| 1264 | + position: relative; | |
| 1265 | + overflow: hidden; | |
| 1266 | + background: #f3f4f6; | |
| 1267 | + box-sizing: border-box; | |
| 1268 | +} | |
| 1269 | + | |
| 1270 | +.food-img-fill { | |
| 1213 | 1271 | width: 100%; |
| 1214 | 1272 | height: 100%; |
| 1273 | + display: block; | |
| 1215 | 1274 | } |
| 1216 | 1275 | |
| 1217 | -.food-thumb-text-wrap { | |
| 1218 | - position: absolute; | |
| 1219 | - left: 0; | |
| 1220 | - top: 0; | |
| 1221 | - right: 0; | |
| 1222 | - bottom: 0; | |
| 1276 | +.food-visual-color-fill { | |
| 1277 | + width: 100%; | |
| 1278 | + height: 100%; | |
| 1279 | +} | |
| 1280 | + | |
| 1281 | +.food-visual-text-wrap { | |
| 1223 | 1282 | width: 100%; |
| 1224 | 1283 | height: 100%; |
| 1225 | 1284 | display: flex; |
| 1226 | 1285 | align-items: center; |
| 1227 | 1286 | justify-content: center; |
| 1228 | - padding: 10rpx 12rpx; | |
| 1287 | + padding: 8rpx 10rpx; | |
| 1229 | 1288 | box-sizing: border-box; |
| 1230 | 1289 | overflow: hidden; |
| 1231 | 1290 | } |
| 1232 | 1291 | |
| 1233 | -.food-thumb-text { | |
| 1292 | +.food-visual-text { | |
| 1234 | 1293 | width: 100%; |
| 1235 | - max-width: 100%; | |
| 1236 | 1294 | max-height: 100%; |
| 1237 | - font-size: 22rpx; | |
| 1295 | + font-size: 20rpx; | |
| 1238 | 1296 | font-weight: 700; |
| 1239 | 1297 | color: #111827; |
| 1240 | - line-height: 1.25; | |
| 1298 | + line-height: 1.2; | |
| 1241 | 1299 | text-align: center; |
| 1242 | 1300 | white-space: pre-wrap; |
| 1243 | 1301 | word-break: break-word; |
| 1244 | 1302 | overflow: hidden; |
| 1245 | - box-sizing: border-box; | |
| 1246 | -} | |
| 1247 | - | |
| 1248 | -.food-thumb-text--wrap { | |
| 1249 | - white-space: pre-wrap; | |
| 1250 | - word-break: break-word; | |
| 1251 | 1303 | } |
| 1252 | 1304 | |
| 1253 | -.food-thumb-text--on-color { | |
| 1254 | - font-size: 22rpx; | |
| 1305 | +.food-visual-text--on-color { | |
| 1255 | 1306 | color: #ffffff; |
| 1256 | 1307 | } |
| 1257 | 1308 | |
| 1258 | -.food-thumb-color-fill { | |
| 1259 | - position: absolute; | |
| 1260 | - left: 0; | |
| 1261 | - top: 0; | |
| 1262 | - width: 100%; | |
| 1263 | - height: 100%; | |
| 1264 | -} | |
| 1265 | - | |
| 1266 | -.food-thumb-fallback { | |
| 1267 | - position: absolute; | |
| 1268 | - left: 0; | |
| 1269 | - top: 0; | |
| 1309 | +.food-visual-fallback { | |
| 1270 | 1310 | width: 100%; |
| 1271 | 1311 | height: 100%; |
| 1272 | 1312 | display: flex; |
| 1273 | 1313 | align-items: center; |
| 1274 | 1314 | justify-content: center; |
| 1275 | - padding: 12rpx; | |
| 1315 | + padding: 8rpx; | |
| 1276 | 1316 | box-sizing: border-box; |
| 1277 | 1317 | } |
| 1278 | 1318 | |
| 1279 | -.food-thumb-fallback.bg-red { | |
| 1319 | +.food-visual-fallback.bg-red { | |
| 1280 | 1320 | background: #ef4444; |
| 1281 | 1321 | } |
| 1282 | -.food-thumb-fallback.bg-blue { | |
| 1322 | +.food-visual-fallback.bg-blue { | |
| 1283 | 1323 | background: #3b82f6; |
| 1284 | 1324 | } |
| 1285 | -.food-thumb-fallback.bg-green { | |
| 1325 | +.food-visual-fallback.bg-green { | |
| 1286 | 1326 | background: #22c55e; |
| 1287 | 1327 | } |
| 1288 | -.food-thumb-fallback.bg-orange { | |
| 1328 | +.food-visual-fallback.bg-orange { | |
| 1289 | 1329 | background: #f97316; |
| 1290 | 1330 | } |
| 1291 | -.food-thumb-fallback.bg-purple { | |
| 1331 | +.food-visual-fallback.bg-purple { | |
| 1292 | 1332 | background: #a855f7; |
| 1293 | 1333 | } |
| 1294 | 1334 | |
| 1295 | -.food-thumb-text--on-color { | |
| 1335 | +.food-name-band { | |
| 1336 | + flex: 2 1 0; | |
| 1337 | + min-height: 0; | |
| 1338 | + width: 100%; | |
| 1339 | + display: flex; | |
| 1340 | + align-items: center; | |
| 1341 | + justify-content: center; | |
| 1342 | + padding: 2rpx 8rpx; | |
| 1343 | + background: #ffffff; | |
| 1344 | + box-sizing: border-box; | |
| 1345 | +} | |
| 1346 | + | |
| 1347 | +.food-name { | |
| 1348 | + width: 100%; | |
| 1296 | 1349 | font-size: 20rpx; |
| 1297 | - color: #ffffff; | |
| 1350 | + font-weight: 600; | |
| 1351 | + color: #111827; | |
| 1352 | + line-height: 1.2; | |
| 1353 | + text-align: center; | |
| 1354 | + word-break: break-word; | |
| 1355 | + overflow: hidden; | |
| 1356 | + display: -webkit-box; | |
| 1357 | + -webkit-box-orient: vertical; | |
| 1358 | + -webkit-line-clamp: 2; | |
| 1298 | 1359 | } |
| 1299 | 1360 | |
| 1300 | -.food-info-row { | |
| 1361 | +.food-meta-band { | |
| 1362 | + flex: 1 1 0; | |
| 1363 | + min-height: 0; | |
| 1364 | + width: 100%; | |
| 1301 | 1365 | display: flex; |
| 1302 | 1366 | flex-direction: row; |
| 1303 | 1367 | align-items: center; |
| 1304 | 1368 | justify-content: space-between; |
| 1305 | - gap: 8rpx; | |
| 1306 | - margin-top: 4rpx; | |
| 1307 | - margin-bottom: 2rpx; | |
| 1308 | - min-width: 0; | |
| 1309 | -} | |
| 1310 | - | |
| 1311 | -.food-meta-badges { | |
| 1312 | - display: flex; | |
| 1313 | - flex-direction: column; | |
| 1314 | - align-items: flex-end; | |
| 1315 | - justify-content: center; | |
| 1316 | - padding: 5px; | |
| 1317 | - flex-shrink: 0; | |
| 1318 | - max-width: 58%; | |
| 1369 | + gap: 4rpx; | |
| 1370 | + padding: 0 8rpx 4rpx; | |
| 1371 | + background: #ffffff; | |
| 1319 | 1372 | box-sizing: border-box; |
| 1320 | - background: #e5e7eb; | |
| 1321 | - border-radius: 8rpx; | |
| 1322 | - gap: 2rpx; | |
| 1323 | 1373 | } |
| 1324 | 1374 | |
| 1325 | 1375 | .food-meta-size, |
| 1326 | 1376 | .food-meta-types { |
| 1327 | - font-size: 16rpx; | |
| 1328 | - color: #4b5563; | |
| 1329 | - font-weight: 500; | |
| 1330 | - line-height: 1.2; | |
| 1331 | - white-space: nowrap; | |
| 1332 | - text-align: right; | |
| 1333 | -} | |
| 1334 | - | |
| 1335 | -.food-name { | |
| 1336 | - font-size: 22rpx; | |
| 1337 | - font-weight: 600; | |
| 1338 | - color: #111827; | |
| 1339 | 1377 | flex: 1; |
| 1340 | 1378 | min-width: 0; |
| 1379 | + font-size: 14rpx; | |
| 1380 | + color: #6b7280; | |
| 1381 | + font-weight: 500; | |
| 1382 | + line-height: 1.15; | |
| 1341 | 1383 | overflow: hidden; |
| 1342 | 1384 | text-overflow: ellipsis; |
| 1343 | 1385 | white-space: nowrap; |
| 1344 | 1386 | } |
| 1345 | 1387 | |
| 1346 | -.food-desc { | |
| 1347 | - font-size: 18rpx; | |
| 1348 | - color: #6b7280; | |
| 1349 | - display: block; | |
| 1350 | - overflow: hidden; | |
| 1351 | - text-overflow: ellipsis; | |
| 1352 | - white-space: nowrap; | |
| 1388 | +.food-meta-size { | |
| 1389 | + text-align: left; | |
| 1390 | +} | |
| 1391 | + | |
| 1392 | +.food-meta-types { | |
| 1393 | + text-align: right; | |
| 1353 | 1394 | } |
| 1354 | 1395 | |
| 1355 | 1396 | .modal-mask { |
| ... | ... | @@ -1441,21 +1482,27 @@ const goBluetoothPage = () => { |
| 1441 | 1482 | padding: 20rpx 24rpx; |
| 1442 | 1483 | } |
| 1443 | 1484 | |
| 1444 | - /* 平板:内联样式在运行时由 rpx 换算,此处仅作 H5 预览补充 */ | |
| 1445 | - | |
| 1446 | 1485 | .food-card { |
| 1447 | 1486 | width: 31%; |
| 1448 | - max-width: 280rpx; | |
| 1449 | - padding: 10rpx; | |
| 1450 | - margin: 0 7rpx 14rpx 7rpx; | |
| 1487 | + max-width: 300rpx; | |
| 1488 | + margin: 0 8rpx 16rpx 8rpx; | |
| 1451 | 1489 | } |
| 1452 | 1490 | |
| 1453 | - .food-img-wrap { | |
| 1454 | - padding-top: 62%; | |
| 1491 | + .food-name { | |
| 1492 | + font-size: 22rpx; | |
| 1493 | + } | |
| 1494 | + | |
| 1495 | + .food-meta-size, | |
| 1496 | + .food-meta-types { | |
| 1497 | + font-size: 16rpx; | |
| 1455 | 1498 | } |
| 1456 | 1499 | |
| 1457 | 1500 | .sidebar { |
| 1458 | 1501 | width: 260rpx; |
| 1459 | 1502 | } |
| 1503 | + | |
| 1504 | + .cat-card-frame { | |
| 1505 | + max-width: 220rpx; | |
| 1506 | + } | |
| 1460 | 1507 | } |
| 1461 | 1508 | </style> | ... | ... |
美国版/Food Labeling Management App UniApp/src/pages/labels/preview.vue
| ... | ... | @@ -57,10 +57,7 @@ |
| 57 | 57 | </view> |
| 58 | 58 | |
| 59 | 59 | <view v-if="printFreeFieldList.length" class="print-options-card"> |
| 60 | - <text class="print-options-title">Print input</text> | |
| 61 | - <text class="print-options-hint"> | |
| 62 | - with a box below. If the template has a unit, it is appended on the label after what you type. | |
| 63 | - </text> | |
| 60 | + <text class="print-input-prompt">Please enter below value(s) to print the label:</text> | |
| 64 | 61 | <view v-for="el in printFreeFieldList" :key="'free-' + el.id" class="print-option-block"> |
| 65 | 62 | <text class="print-option-label">{{ freeFieldNameLabel(el) }}</text> |
| 66 | 63 | <template v-if="isWeightPrintField(el)"> |
| ... | ... | @@ -72,6 +69,15 @@ |
| 72 | 69 | @input="onFreeFieldInput(el.id, $event)" |
| 73 | 70 | /> |
| 74 | 71 | </template> |
| 72 | + <template v-else-if="freeFieldInputKind(el) === 'number'"> | |
| 73 | + <input | |
| 74 | + class="free-field-input" | |
| 75 | + type="digit" | |
| 76 | + :value="printFreeFieldValues[el.id] ?? ''" | |
| 77 | + placeholder="0" | |
| 78 | + @input="onFreeFieldInput(el.id, $event)" | |
| 79 | + /> | |
| 80 | + </template> | |
| 75 | 81 | <template v-else-if="freeFieldInputKind(el) === 'text'"> |
| 76 | 82 | <input |
| 77 | 83 | class="free-field-input" |
| ... | ... | @@ -144,11 +150,6 @@ |
| 144 | 150 | </view> |
| 145 | 151 | </view> |
| 146 | 152 | </scroll-view> |
| 147 | - | |
| 148 | - <view class="note-card"> | |
| 149 | - <AppIcon name="alert" size="sm" color="blue" /> | |
| 150 | - <text class="note-text">This is a preview of the label. Actual printed labels may vary slightly in appearance.</text> | |
| 151 | - </view> | |
| 152 | 153 | </template> |
| 153 | 154 | </view> |
| 154 | 155 | |
| ... | ... | @@ -271,6 +272,8 @@ import { |
| 271 | 272 | isPrintInputOptionsElement, |
| 272 | 273 | mergePrintInputFreeFields, |
| 273 | 274 | mergePrintOptionSelections, |
| 275 | + printInputFieldLabelFromElement, | |
| 276 | + printInputPreText, | |
| 274 | 277 | readFreeFieldValuesFromTemplate, |
| 275 | 278 | readSelectionsFromTemplate, |
| 276 | 279 | validatePrintInputFreeFieldsBeforePrint, |
| ... | ... | @@ -298,7 +301,9 @@ import { |
| 298 | 301 | import { |
| 299 | 302 | applyTemplateProductDefaultValuesToTemplate, |
| 300 | 303 | applyLabelBindingsToPreviewTemplate, |
| 304 | + applyLabelSizeTextToTemplate, | |
| 301 | 305 | applyProductCodeValueToTemplateScanElements, |
| 306 | + expandNutritionElementsToFitContent, | |
| 302 | 307 | extractProductCodeValueFromPreviewPayload, |
| 303 | 308 | extractTemplateProductDefaultValuesFromPreviewPayload, |
| 304 | 309 | normalizeLabelTemplateFromPreviewApi, |
| ... | ... | @@ -910,6 +915,8 @@ function dictValuesForElement(elementId: string): string[] { |
| 910 | 915 | } |
| 911 | 916 | |
| 912 | 917 | function optionFieldTitle(el: SystemTemplateElementBase): string { |
| 918 | + const preText = printInputPreText(el) | |
| 919 | + if (preText) return preText | |
| 913 | 920 | return ( |
| 914 | 921 | dictLabelsByElementId.value[el.id] || |
| 915 | 922 | el.elementName || |
| ... | ... | @@ -935,8 +942,7 @@ function togglePrintOption(elementId: string, value: string) { |
| 935 | 942 | } |
| 936 | 943 | |
| 937 | 944 | function freeFieldNameLabel(el: SystemTemplateElementBase): string { |
| 938 | - const n = el.elementName || el.inputKey || 'Field' | |
| 939 | - return `${n}:` | |
| 945 | + return printInputFieldLabelFromElement(el) | |
| 940 | 946 | } |
| 941 | 947 | |
| 942 | 948 | function isWeightPrintField(el: SystemTemplateElementBase): boolean { |
| ... | ... | @@ -960,10 +966,11 @@ function freeFieldDateFormat(el: SystemTemplateElementBase): string { |
| 960 | 966 | return String(c.format ?? c.Format ?? '').trim() |
| 961 | 967 | } |
| 962 | 968 | |
| 963 | -function freeFieldInputKind(el: SystemTemplateElementBase): 'text' | 'date' | 'time' | 'datetime' { | |
| 969 | +function freeFieldInputKind(el: SystemTemplateElementBase): 'text' | 'number' | 'date' | 'time' | 'datetime' { | |
| 964 | 970 | const type = String(el.type || '').toUpperCase() |
| 965 | 971 | const c = el.config || {} |
| 966 | 972 | const inputType = String(c.inputType ?? c.InputType ?? '').toLowerCase() |
| 973 | + if (inputType === 'number') return 'number' | |
| 967 | 974 | if (type === 'TIME') return 'time' |
| 968 | 975 | if (type === 'DATE' && inputType === 'datetime') return 'datetime' |
| 969 | 976 | if (type === 'DATE') return 'date' |
| ... | ... | @@ -1234,6 +1241,7 @@ function computeMergedPreviewTemplate(at?: Date): SystemLabelTemplate | null { |
| 1234 | 1241 | labelTypeName: labelTypeName.value, |
| 1235 | 1242 | productName: displayProductName.value, |
| 1236 | 1243 | }) |
| 1244 | + m = expandNutritionElementsToFitContent(m) | |
| 1237 | 1245 | return ensureTemplateHeightCoversElements(m) |
| 1238 | 1246 | } |
| 1239 | 1247 | |
| ... | ... | @@ -1469,6 +1477,10 @@ async function loadPreview() { |
| 1469 | 1477 | if (labelSizeTextFromApi && labelSizeTextFromApi.trim()) { |
| 1470 | 1478 | templateSize.value = labelSizeTextFromApi.trim() |
| 1471 | 1479 | } |
| 1480 | + let tmplBase = tmplRaw | |
| 1481 | + if (labelSizeTextFromApi && labelSizeTextFromApi.trim()) { | |
| 1482 | + tmplBase = applyLabelSizeTextToTemplate(tmplBase, labelSizeTextFromApi) | |
| 1483 | + } | |
| 1472 | 1484 | const productDefaults = extractTemplateProductDefaultValuesFromPreviewPayload(raw) |
| 1473 | 1485 | previewProductDefaults.value = productDefaults |
| 1474 | 1486 | const readLabelName = (L: Record<string, unknown> | null): string => { |
| ... | ... | @@ -1480,7 +1492,6 @@ async function loadPreview() { |
| 1480 | 1492 | readLabelName(root) || |
| 1481 | 1493 | readLabelName(inner) || |
| 1482 | 1494 | '' |
| 1483 | - let tmplBase = tmplRaw | |
| 1484 | 1495 | const productCodeValue = extractProductCodeValueFromPreviewPayload(raw) |
| 1485 | 1496 | if (productCodeValue) { |
| 1486 | 1497 | tmplBase = applyProductCodeValueToTemplateScanElements(tmplBase, productCodeValue) |
| ... | ... | @@ -2523,6 +2534,15 @@ const handlePrint = async () => { |
| 2523 | 2534 | margin-bottom: 24rpx; |
| 2524 | 2535 | } |
| 2525 | 2536 | |
| 2537 | +.print-input-prompt { | |
| 2538 | + font-size: 28rpx; | |
| 2539 | + font-weight: 600; | |
| 2540 | + color: var(--theme-primary, #1f3a8a); | |
| 2541 | + line-height: 1.45; | |
| 2542 | + display: block; | |
| 2543 | + margin-bottom: 24rpx; | |
| 2544 | +} | |
| 2545 | + | |
| 2526 | 2546 | .print-option-block { |
| 2527 | 2547 | margin-bottom: 28rpx; |
| 2528 | 2548 | } |
| ... | ... | @@ -2769,23 +2789,6 @@ const handlePrint = async () => { |
| 2769 | 2789 | display: block; |
| 2770 | 2790 | } |
| 2771 | 2791 | |
| 2772 | -.note-card { | |
| 2773 | - display: flex; | |
| 2774 | - align-items: flex-start; | |
| 2775 | - gap: 16rpx; | |
| 2776 | - padding: 24rpx 28rpx; | |
| 2777 | - background: #eff6ff; | |
| 2778 | - border: 1rpx solid #bfdbfe; | |
| 2779 | - border-radius: 16rpx; | |
| 2780 | -} | |
| 2781 | - | |
| 2782 | -.note-text { | |
| 2783 | - flex: 1; | |
| 2784 | - font-size: 26rpx; | |
| 2785 | - color: #1e40af; | |
| 2786 | - line-height: 1.5; | |
| 2787 | -} | |
| 2788 | - | |
| 2789 | 2792 | .bottom-bar { |
| 2790 | 2793 | position: fixed; |
| 2791 | 2794 | bottom: 0; | ... | ... |
美国版/Food Labeling Management App UniApp/src/utils/apiBase.ts
| ... | ... | @@ -3,7 +3,7 @@ |
| 3 | 3 | * 调试本地时请与 .env.development 保持一致;仅作 fallback,优先使用环境变量。 |
| 4 | 4 | */ |
| 5 | 5 | // export const US_BACKEND_ORIGIN_FALLBACK = 'http://flus-test.3ffoodsafety.com' |
| 6 | -export const US_BACKEND_ORIGIN_FALLBACK = 'http://192.168.31.89:19001' | |
| 6 | +export const US_BACKEND_ORIGIN_FALLBACK = 'http://192.168.31.88:19001' | |
| 7 | 7 | |
| 8 | 8 | /** |
| 9 | 9 | * 解析美国版后端根地址(不含末尾 /)。 | ... | ... |
美国版/Food Labeling Management App UniApp/src/utils/deviceInfo.ts
| ... | ... | @@ -61,3 +61,24 @@ export function getDeviceFingerprint (): string { |
| 61 | 61 | .join(' | ') |
| 62 | 62 | .toLowerCase() |
| 63 | 63 | } |
| 64 | + | |
| 65 | +/** Gprinter AOA 一体机(rk3568 / 型号含 AOA):走内置 UPOS/TCP,勿依赖外接蓝牙扫描 */ | |
| 66 | +export function isAoaAioDevice (): boolean { | |
| 67 | + const identity = getDeviceIdentity() | |
| 68 | + const fingerprint = getDeviceFingerprint() | |
| 69 | + const blob = [ | |
| 70 | + identity.model, | |
| 71 | + identity.product, | |
| 72 | + identity.device, | |
| 73 | + identity.brand, | |
| 74 | + fingerprint, | |
| 75 | + ] | |
| 76 | + .map((s) => String(s || '').toLowerCase()) | |
| 77 | + .join(' ') | |
| 78 | + if (!blob.trim()) return false | |
| 79 | + return ( | |
| 80 | + blob.includes('aoa_rk3568') | |
| 81 | + || blob.includes('rk3568') | |
| 82 | + || /\baoa\b/.test(blob) | |
| 83 | + ) | |
| 84 | +} | ... | ... |
美国版/Food Labeling Management App UniApp/src/utils/labelPreview/normalizePreviewTemplate.ts
| ... | ... | @@ -8,13 +8,61 @@ import { |
| 8 | 8 | sanitizeDateElementConfig, |
| 9 | 9 | tryParsePrintInputOffsetStored, |
| 10 | 10 | } from './printInputOffset' |
| 11 | -import { applyNutritionDefaultJsonToConfig } from './nutritionDefaultsMerge' | |
| 11 | +import { | |
| 12 | + applyNutritionDefaultJsonToConfig, | |
| 13 | + collectNutritionManualFromDefaults, | |
| 14 | + hydrateNutritionElementsInTemplate, | |
| 15 | + expandNutritionElementsToFitContent, | |
| 16 | +} from './nutritionDefaultsMerge' | |
| 17 | + | |
| 18 | +export { expandNutritionElementsToFitContent, reflowElementsBelowNutritionPanel } | |
| 12 | 19 | |
| 13 | 20 | function asRecord(v: unknown): Record<string, unknown> { |
| 14 | 21 | if (v != null && typeof v === 'object' && !Array.isArray(v)) return v as Record<string, unknown> |
| 15 | 22 | return {} |
| 16 | 23 | } |
| 17 | 24 | |
| 25 | +const KNOWN_ELEMENT_TYPES = new Set([ | |
| 26 | + 'TEXT_STATIC', | |
| 27 | + 'TEXT', | |
| 28 | + 'TEXT_PRODUCT', | |
| 29 | + 'TEXT_PRICE', | |
| 30 | + 'TEXT_CATEGORY', | |
| 31 | + 'TEXT_LABEL_ID', | |
| 32 | + 'BARCODE', | |
| 33 | + 'QRCODE', | |
| 34 | + 'IMAGE', | |
| 35 | + 'LOGO', | |
| 36 | + 'DATE', | |
| 37 | + 'TIME', | |
| 38 | + 'DURATION', | |
| 39 | + 'WEIGHT', | |
| 40 | + 'NUTRITION', | |
| 41 | +]) | |
| 42 | + | |
| 43 | +/** 与 Web canonicalElementType 对齐:print|Number → TEXT_STATIC 等 */ | |
| 44 | +function canonicalElementType (raw: string): string { | |
| 45 | + const type = String(raw ?? '').trim() | |
| 46 | + if (!type) return 'TEXT_STATIC' | |
| 47 | + const upper = type.toUpperCase() | |
| 48 | + if (KNOWN_ELEMENT_TYPES.has(upper)) return upper | |
| 49 | + const m = type.match(/^([^|]+)\|(.+)$/i) | |
| 50 | + if (!m) return 'TEXT_STATIC' | |
| 51 | + const group = m[1].trim().toLowerCase() | |
| 52 | + const label = m[2].trim().toLowerCase() | |
| 53 | + if (group === 'print') { | |
| 54 | + if (label === 'number' || label === 'text' || label === 'multiple options') return 'TEXT_STATIC' | |
| 55 | + if (label === 'weight') return 'WEIGHT' | |
| 56 | + if (label === 'date & time' || label === 'date and time') return 'DATE' | |
| 57 | + } | |
| 58 | + if (group === 'auto') { | |
| 59 | + if (label.includes('current date')) return 'DATE' | |
| 60 | + if (label.includes('current time')) return 'TIME' | |
| 61 | + } | |
| 62 | + if (group === 'label' && label.includes('duration') && !label.includes('date')) return 'DURATION' | |
| 63 | + return 'TEXT_STATIC' | |
| 64 | +} | |
| 65 | + | |
| 18 | 66 | function normalizeConfig(raw: unknown): Record<string, unknown> { |
| 19 | 67 | if (raw == null) return {} |
| 20 | 68 | if (typeof raw === 'string') { |
| ... | ... | @@ -538,21 +586,27 @@ export function applyTemplateProductDefaultValuesToTemplate( |
| 538 | 586 | } |
| 539 | 587 | |
| 540 | 588 | if (type === 'NUTRITION') { |
| 589 | + const manual = collectNutritionManualFromDefaults(el, defaults) | |
| 590 | + if (Object.keys(manual).some((k) => String(manual[k] ?? '').trim() !== '')) { | |
| 591 | + const merged = applyNutritionDefaultJsonToConfig(cfg, JSON.stringify(manual)) | |
| 592 | + return { ...el, config: merged } | |
| 593 | + } | |
| 541 | 594 | const s = String(v).trim() |
| 542 | 595 | if (s.startsWith('{')) { |
| 543 | 596 | const merged = applyNutritionDefaultJsonToConfig(cfg, s) |
| 544 | 597 | return { ...el, config: merged } |
| 545 | 598 | } |
| 546 | - cfg.text = s | |
| 547 | - cfg.Text = s | |
| 548 | - return { ...el, config: cfg } | |
| 599 | + return el | |
| 549 | 600 | } |
| 550 | 601 | |
| 551 | 602 | cfg.text = v |
| 552 | 603 | cfg.Text = v |
| 553 | 604 | return { ...el, config: cfg } |
| 554 | 605 | }) |
| 555 | - return applyLiveDateTimeFieldsToTemplate({ ...template, elements }, base) | |
| 606 | + return hydrateNutritionElementsInTemplate( | |
| 607 | + applyLiveDateTimeFieldsToTemplate({ ...template, elements }, base), | |
| 608 | + defaults, | |
| 609 | + ) | |
| 556 | 610 | } |
| 557 | 611 | |
| 558 | 612 | function elementArrayLength (o: Record<string, unknown>): number { |
| ... | ... | @@ -625,7 +679,7 @@ export function normalizeLabelTemplateFromPreviewApi(payload: unknown): SystemLa |
| 625 | 679 | ) |
| 626 | 680 | cfg = mergeFlatElementFieldsIntoConfig(e, cfg) |
| 627 | 681 | captureDateDisplayFormatInConfig(cfg) |
| 628 | - const type = String(e.type ?? e.elementType ?? e.ElementType ?? 'TEXT_STATIC') | |
| 682 | + const type = canonicalElementType(String(e.type ?? e.elementType ?? e.ElementType ?? 'TEXT_STATIC')) | |
| 629 | 683 | const vst = e.valueSourceType ?? e.ValueSourceType |
| 630 | 684 | const ik = e.inputKey ?? e.InputKey |
| 631 | 685 | const en = e.elementName ?? e.ElementName |
| ... | ... | @@ -660,7 +714,7 @@ export function normalizeLabelTemplateFromPreviewApi(payload: unknown): SystemLa |
| 660 | 714 | | 'cm' |
| 661 | 715 | | 'px' |
| 662 | 716 | |
| 663 | - return { | |
| 717 | + const tmpl: SystemLabelTemplate = { | |
| 664 | 718 | id: String(t.id ?? t.Id ?? 'preview'), |
| 665 | 719 | name: String(t.name ?? t.Name ?? 'Label'), |
| 666 | 720 | labelType: String(t.labelType ?? t.LabelType ?? ''), |
| ... | ... | @@ -676,6 +730,7 @@ export function normalizeLabelTemplateFromPreviewApi(payload: unknown): SystemLa |
| 676 | 730 | ), |
| 677 | 731 | elements, |
| 678 | 732 | } |
| 733 | + return hydrateNutritionElementsInTemplate(tmpl, {}) | |
| 679 | 734 | } |
| 680 | 735 | |
| 681 | 736 | export function sortElementsForPreview( | ... | ... |
美国版/Food Labeling Management App UniApp/src/utils/labelPreview/nutritionDefaultsMerge.ts
| ... | ... | @@ -2,7 +2,162 @@ |
| 2 | 2 | * 将管理端保存的营养成分默认值 JSON 合并进 NUTRITION 元素 config(与 Web nutritionManualEntry 字段一致)。 |
| 3 | 3 | * `<` 前缀由模板 config 决定,录入 JSON 不包含 LessThan 字段。 |
| 4 | 4 | */ |
| 5 | -import { NUTRITION_FACTS_LAYOUT_ROWS } from '../nutritionFactsLayout' | |
| 5 | +import type { SystemLabelTemplate, SystemTemplateElementBase } from '../print/types/printer' | |
| 6 | +import { NUTRITION_FACTS_LAYOUT_ROWS, buildNutritionFactsViewModel, NUTRITION_BODY_FONT_SIZE } from '../nutritionFactsLayout' | |
| 7 | + | |
| 8 | +/** 与 Web nutritionManualEntry.NUTRITION_FIELD_COMPOSITE_SEP 一致 */ | |
| 9 | +export const NUTRITION_FIELD_COMPOSITE_SEP = '###nut###' | |
| 10 | + | |
| 11 | +export function nutritionCompositeFieldKey(nutritionElementId: string, subKey: string): string { | |
| 12 | + return `${nutritionElementId}${NUTRITION_FIELD_COMPOSITE_SEP}${subKey}` | |
| 13 | +} | |
| 14 | + | |
| 15 | +function defaultLookupCandidates(el: SystemTemplateElementBase): string[] { | |
| 16 | + return [ | |
| 17 | + String(el.id ?? '').trim(), | |
| 18 | + String(el.inputKey ?? '').trim(), | |
| 19 | + String(el.elementName ?? '').trim(), | |
| 20 | + ].filter(Boolean) | |
| 21 | +} | |
| 22 | + | |
| 23 | +/** 从 templateProductDefaultValues 解析营养成分手动录入(JSON 或 composite 列键) */ | |
| 24 | +export function collectNutritionManualFromDefaults( | |
| 25 | + el: SystemTemplateElementBase, | |
| 26 | + defaults: Record<string, string>, | |
| 27 | +): Record<string, string> { | |
| 28 | + if (!defaults || !Object.keys(defaults).length) return {} | |
| 29 | + const candidates = defaultLookupCandidates(el) | |
| 30 | + for (const k of candidates) { | |
| 31 | + if (Object.prototype.hasOwnProperty.call(defaults, k)) { | |
| 32 | + const raw = String(defaults[k] ?? '').trim() | |
| 33 | + if (raw.startsWith('{')) { | |
| 34 | + try { | |
| 35 | + const p = JSON.parse(raw) as Record<string, string> | |
| 36 | + if (p && typeof p === 'object' && !Array.isArray(p)) return p | |
| 37 | + } catch { | |
| 38 | + /* ignore */ | |
| 39 | + } | |
| 40 | + } | |
| 41 | + } | |
| 42 | + } | |
| 43 | + const lower = Object.entries(defaults).map(([k, v]) => [k.toLowerCase(), v] as const) | |
| 44 | + for (const k of candidates) { | |
| 45 | + const hit = lower.find(([dk]) => dk === k.toLowerCase()) | |
| 46 | + if (hit) { | |
| 47 | + const raw = String(hit[1] ?? '').trim() | |
| 48 | + if (raw.startsWith('{')) { | |
| 49 | + try { | |
| 50 | + return JSON.parse(raw) as Record<string, string> | |
| 51 | + } catch { | |
| 52 | + /* ignore */ | |
| 53 | + } | |
| 54 | + } | |
| 55 | + } | |
| 56 | + } | |
| 57 | + const id = String(el.id ?? '').trim() | |
| 58 | + if (!id) return {} | |
| 59 | + const prefix = `${id}${NUTRITION_FIELD_COMPOSITE_SEP}` | |
| 60 | + const manual: Record<string, string> = {} | |
| 61 | + for (const [k, v] of Object.entries(defaults)) { | |
| 62 | + if (k.startsWith(prefix)) manual[k.slice(prefix.length)] = String(v ?? '') | |
| 63 | + } | |
| 64 | + return manual | |
| 65 | +} | |
| 66 | + | |
| 67 | +function parseNutritionJsonFromConfigText(cfg: Record<string, unknown>): string | null { | |
| 68 | + const text = String(cfg.text ?? cfg.Text ?? '').trim() | |
| 69 | + return text.startsWith('{') ? text : null | |
| 70 | +} | |
| 71 | + | |
| 72 | +/** 接口 8.2 会把营养成分 JSON 误写入 config.text;解析进 fixedNutrients / calories 等字段 */ | |
| 73 | +export function hydrateNutritionElementsInTemplate( | |
| 74 | + template: SystemLabelTemplate, | |
| 75 | + defaults: Record<string, string> = {}, | |
| 76 | +): SystemLabelTemplate { | |
| 77 | + const elements = (template.elements || []).map((el) => { | |
| 78 | + if (String(el.type || '').toUpperCase() !== 'NUTRITION') return el | |
| 79 | + let cfg = { ...(el.config || {}) } as Record<string, unknown> | |
| 80 | + const fromText = parseNutritionJsonFromConfigText(cfg) | |
| 81 | + if (fromText) { | |
| 82 | + cfg = applyNutritionDefaultJsonToConfig(cfg, fromText) | |
| 83 | + } | |
| 84 | + const manual = collectNutritionManualFromDefaults(el, defaults) | |
| 85 | + if (Object.keys(manual).some((k) => String(manual[k] ?? '').trim() !== '')) { | |
| 86 | + cfg = applyNutritionDefaultJsonToConfig(cfg, JSON.stringify(manual)) | |
| 87 | + } | |
| 88 | + if (fromText) { | |
| 89 | + delete cfg.text | |
| 90 | + delete cfg.Text | |
| 91 | + } | |
| 92 | + return { ...el, config: cfg } | |
| 93 | + }) | |
| 94 | + return { ...template, elements } | |
| 95 | +} | |
| 96 | + | |
| 97 | +/** 按实际行数估算营养表所需高度,避免矮框裁掉 Vitamin D / 页脚等行 */ | |
| 98 | +export function estimateNutritionPanelHeightPx(cfg: Record<string, unknown>): number { | |
| 99 | + const model = buildNutritionFactsViewModel(cfg) | |
| 100 | + const bodySize = NUTRITION_BODY_FONT_SIZE | |
| 101 | + const titleSize = Math.max(11, Math.min(18, model.titleFontSize)) | |
| 102 | + const lh = (fs: number) => fs + 4 | |
| 103 | + let y = 12 | |
| 104 | + y += titleSize + 6 | |
| 105 | + y += lh(bodySize) + 3 + lh(bodySize) + 5 | |
| 106 | + y += bodySize + 8 | |
| 107 | + for (const row of model.rows) { | |
| 108 | + y += lh(bodySize) | |
| 109 | + if (row.dividerAfter === 'double') y += 5 | |
| 110 | + else if (row.dividerAfter === 'thin') y += 3 | |
| 111 | + } | |
| 112 | + y += 24 | |
| 113 | + return Math.ceil(y) | |
| 114 | +} | |
| 115 | + | |
| 116 | +/** | |
| 117 | + * 仅当营养表增高时,把「原底边以下」的元素整体下移相同 delta,保持同行左右布局不变。 | |
| 118 | + * 禁止按 cursor 重排,避免 Consume By 标签/日期、ALLERGENS 等被拆散叠在一起。 | |
| 119 | + */ | |
| 120 | +export function reflowElementsBelowNutritionPanel( | |
| 121 | + template: SystemLabelTemplate, | |
| 122 | + previousNutritionBottom?: number, | |
| 123 | +): SystemLabelTemplate { | |
| 124 | + const elements = template.elements || [] | |
| 125 | + const nutrition = elements.find((el) => String(el.type || '').toUpperCase() === 'NUTRITION') | |
| 126 | + if (!nutrition) return template | |
| 127 | + | |
| 128 | + const nutY = Number(nutrition.y) || 0 | |
| 129 | + const nutBottom = nutY + (Number(nutrition.height) || 0) | |
| 130 | + const oldBottom = previousNutritionBottom ?? nutBottom | |
| 131 | + const delta = nutBottom - oldBottom | |
| 132 | + if (delta <= 1) return template | |
| 133 | + | |
| 134 | + const threshold = oldBottom - 2 | |
| 135 | + const next = elements.map((el) => { | |
| 136 | + if (el.id === nutrition.id) return el | |
| 137 | + const y = Number(el.y) || 0 | |
| 138 | + if (y >= threshold) return { ...el, y: y + delta } | |
| 139 | + return el | |
| 140 | + }) | |
| 141 | + return { ...template, elements: next } | |
| 142 | +} | |
| 143 | + | |
| 144 | +/** 仅在内容确实超出模板框时微增高度,并整体下移其下元素(不重排同行) */ | |
| 145 | +export function expandNutritionElementsToFitContent(template: SystemLabelTemplate): SystemLabelTemplate { | |
| 146 | + const elements = [...(template.elements || [])] | |
| 147 | + const nutIdx = elements.findIndex((el) => String(el.type || '').toUpperCase() === 'NUTRITION') | |
| 148 | + if (nutIdx < 0) return template | |
| 149 | + | |
| 150 | + const nutrition = elements[nutIdx] | |
| 151 | + const nutY = Number(nutrition.y) || 0 | |
| 152 | + const oldH = Number(nutrition.height) || 0 | |
| 153 | + const oldBottom = nutY + oldH | |
| 154 | + const need = estimateNutritionPanelHeightPx((nutrition.config || {}) as Record<string, unknown>) | |
| 155 | + const maxH = Math.max(oldH, need) | |
| 156 | + if (maxH <= oldH + 2) return template | |
| 157 | + | |
| 158 | + elements[nutIdx] = { ...nutrition, height: maxH } | |
| 159 | + return reflowElementsBelowNutritionPanel({ ...template, elements }, oldBottom) | |
| 160 | +} | |
| 6 | 161 | |
| 7 | 162 | function fixedLabelForKey(key: string): string { |
| 8 | 163 | const hit = NUTRITION_FACTS_LAYOUT_ROWS.find((x) => x.key === key) | ... | ... |
美国版/Food Labeling Management App UniApp/src/utils/labelPreview/printInputOffset.ts
| ... | ... | @@ -3,7 +3,6 @@ |
| 3 | 3 | * `{"unit":"Days","value":"2"}`,预览与打印时按 BaseTime 解析为 config.text。 |
| 4 | 4 | */ |
| 5 | 5 | import type { SystemLabelTemplate, SystemTemplateElementBase } from '../print/types/printer' |
| 6 | -import { readInvertColors } from '../invertColorsConfig' | |
| 7 | 6 | |
| 8 | 7 | const OFFSET_UNITS = new Set([ |
| 9 | 8 | 'Minutes', |
| ... | ... | @@ -111,15 +110,82 @@ function frozenKnownDateFormat (cfg: Record<string, unknown>): string { |
| 111 | 110 | return '' |
| 112 | 111 | } |
| 113 | 112 | |
| 113 | +/** 打印时输入的字面量字段(Number/Text/Weight),不得按日期偏移重算 */ | |
| 114 | +export function isPrintInputLiteralField (el: SystemTemplateElementBase): boolean { | |
| 115 | + const vst = String(el.valueSourceType || '').toUpperCase() | |
| 116 | + if (vst !== 'PRINT_INPUT') return false | |
| 117 | + const rawType = String(el.type || '').trim() | |
| 118 | + const typeUpper = rawType.toUpperCase() | |
| 119 | + if (typeUpper === 'WEIGHT' || typeUpper.includes('|WEIGHT')) return true | |
| 120 | + const cfg = (el.config || {}) as Record<string, unknown> | |
| 121 | + const it = String(cfg.inputType ?? cfg.InputType ?? '').toLowerCase() | |
| 122 | + if (it === 'number' || it === 'text') return true | |
| 123 | + const ta = resolveElementTypeAdd(el).toLowerCase() | |
| 124 | + if (/\|\s*number\b/.test(ta) || ta.endsWith('number')) return true | |
| 125 | + if (/\|\s*weight\b/.test(ta) || ta.endsWith('weight')) return true | |
| 126 | + if (/\|\s*text\b/.test(ta) || ta.endsWith('|text')) return true | |
| 127 | + if (/print[\s|]*number/i.test(rawType)) return true | |
| 128 | + if (/print[\s|]*text/i.test(rawType)) return true | |
| 129 | + if (/print[\s|]*weight/i.test(rawType)) return true | |
| 130 | + return false | |
| 131 | +} | |
| 132 | + | |
| 133 | +/** 打印时输入 Number/Text/Weight 的展示文案(与 Web 画布 print|number 一致) */ | |
| 134 | +export function resolvePrintInputLiteralDisplay (el: SystemTemplateElementBase): string { | |
| 135 | + const cfg = (el.config || {}) as Record<string, unknown> | |
| 136 | + const it = String(cfg.inputType ?? cfg.InputType ?? '').toLowerCase() | |
| 137 | + const type = String(el.type || '').toUpperCase() | |
| 138 | + let body = '' | |
| 139 | + if (type === 'WEIGHT') { | |
| 140 | + body = String(cfg.value ?? cfg.Value ?? '').trim() | |
| 141 | + if (!body) body = String(cfg.text ?? cfg.Text ?? '').trim() | |
| 142 | + } else { | |
| 143 | + body = String(cfg.text ?? cfg.Text ?? '').trim() | |
| 144 | + } | |
| 145 | + if (!body && it === 'number') body = '0' | |
| 146 | + /** 字面量 Number/Text 的纯数字不能走偏移 JSON 解析(否则会误当成 Days 偏移而清空) */ | |
| 147 | + if (isCorruptedDateDisplayFormat(body)) return '' | |
| 148 | + const unit = String(cfg.unit ?? cfg.Unit ?? '').trim() | |
| 149 | + if (unit && body && !body.endsWith(unit)) body = `${body}${unit}` | |
| 150 | + return applyElementPrefix(cfg, body) | |
| 151 | +} | |
| 152 | + | |
| 153 | +/** Text (For Template) 等固定文案:invertColors 仅表示黑底白字,不是星期条 */ | |
| 154 | +export function isFixedStaticTextElement (el: SystemTemplateElementBase): boolean { | |
| 155 | + if (isPrintInputLiteralField(el)) return true | |
| 156 | + const type = String(el.type || '').toUpperCase() | |
| 157 | + if (type !== 'TEXT_STATIC' && type !== 'TEXT' && type !== 'TEXT_PRODUCT' && type !== 'TEXT_PRICE') { | |
| 158 | + return false | |
| 159 | + } | |
| 160 | + const vst = String(el.valueSourceType || '').toUpperCase() | |
| 161 | + if (vst && vst !== 'FIXED' && vst !== 'STATIC') return false | |
| 162 | + const cfg = (el.config || {}) as Record<string, unknown> | |
| 163 | + const inputType = String(cfg.inputType ?? cfg.InputType ?? '').toLowerCase() | |
| 164 | + if ( | |
| 165 | + inputType === 'date' || | |
| 166 | + inputType === 'datetime' || | |
| 167 | + inputType === 'number' || | |
| 168 | + inputType === 'options' | |
| 169 | + ) { | |
| 170 | + return false | |
| 171 | + } | |
| 172 | + if (isUniAppDateTimeOffsetField(el)) return false | |
| 173 | + if (isWeekdayBarNameHint(elementDateNameHint(el))) return false | |
| 174 | + return true | |
| 175 | +} | |
| 176 | + | |
| 114 | 177 | /** |
| 115 | - * 底部黑条星期元素。 | |
| 116 | - * 模板约定:**invertColors 黑底 = 星期条**;Use By(durationdate)无 invertColors。 | |
| 178 | + * 底部黑条星期元素(durationdate2 / FULLY DAY 格式)。 | |
| 179 | + * invertColors 只负责黑底白字样式,不能单独判定为星期条。 | |
| 117 | 180 | */ |
| 118 | 181 | export function isWeekdayBarElement (el: SystemTemplateElementBase): boolean { |
| 182 | + const type = String(el.type || '').toUpperCase() | |
| 183 | + /** 重打已焙成 TEXT_STATIC 的星期条不再走 live 偏移,直接画 config.text */ | |
| 184 | + if (type === 'TEXT_STATIC' || type === 'TEXT') return false | |
| 185 | + if (isFixedStaticTextElement(el)) return false | |
| 119 | 186 | const cfg = (el.config || {}) as Record<string, unknown> |
| 120 | 187 | const hint = elementDateNameHint(el) |
| 121 | 188 | if (isWeekdayBarNameHint(hint)) return true |
| 122 | - if (readInvertColors(cfg)) return true | |
| 123 | 189 | const fmt = frozenKnownDateFormat(cfg) |
| 124 | 190 | if (fmt === 'FULLY DAY(WEDNESDAY)' || fmt === 'DAY (WED)') return true |
| 125 | 191 | return false |
| ... | ... | @@ -147,6 +213,7 @@ export function sanitizeDateElementConfig ( |
| 147 | 213 | el: SystemTemplateElementBase, |
| 148 | 214 | cfg: Record<string, unknown>, |
| 149 | 215 | ): void { |
| 216 | + if (isFixedStaticTextElement(el)) return | |
| 150 | 217 | captureDateDisplayFormatInConfig(cfg) |
| 151 | 218 | const preserved = String(cfg.__dateDisplayFormat ?? cfg.__DateDisplayFormat ?? '').trim() |
| 152 | 219 | let fmt = String(cfg.format ?? cfg.Format ?? '').trim() |
| ... | ... | @@ -175,10 +242,10 @@ export function sanitizeDateElementConfig ( |
| 175 | 242 | } |
| 176 | 243 | fmt = String(cfg.format ?? cfg.Format ?? '').trim() |
| 177 | 244 | } else if (fmt && (isCorruptedDateDisplayFormat(fmt) || looksLikeCustomDateTimeFormatTemplate(fmt))) { |
| 245 | + if (isFixedStaticTextElement(el)) return | |
| 178 | 246 | const hint = elementDateNameHint(el) |
| 179 | 247 | const next = |
| 180 | 248 | preserved || |
| 181 | - (readInvertColors(cfg) ? 'FULLY DAY(WEDNESDAY)' : '') || | |
| 182 | 249 | (isWeekdayBarNameHint(hint) ? 'FULLY DAY(WEDNESDAY)' : '') || |
| 183 | 250 | (isUseByDateNameHint(hint) ? 'MM/DD/YYYY' : '') |
| 184 | 251 | if (next) { |
| ... | ... | @@ -247,6 +314,30 @@ const WEEKDAY_LONG_EN = [ |
| 247 | 314 | |
| 248 | 315 | const WEEKDAY_SHORT_EN = ['SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT'] as const |
| 249 | 316 | |
| 317 | +/** 已是展开后的星期展示文案(重打快照 / 落库 __previewFormatted) */ | |
| 318 | +export function isResolvedWeekdayDisplayLiteral (raw: string | null | undefined): boolean { | |
| 319 | + const t = String(raw ?? '').trim().toUpperCase() | |
| 320 | + if (!t) return false | |
| 321 | + if ((WEEKDAY_LONG_EN as readonly string[]).includes(t)) return true | |
| 322 | + if ((WEEKDAY_SHORT_EN as readonly string[]).includes(t)) return true | |
| 323 | + if (/^DAY\s*\([A-Z]{3}\)$/i.test(String(raw ?? '').trim())) return true | |
| 324 | + return false | |
| 325 | +} | |
| 326 | + | |
| 327 | +/** 重打/画布:优先读快照里已冻结的星期文案,禁止再按 today 重算 */ | |
| 328 | +export function readFrozenWeekdayDisplayFromConfig (cfg: Record<string, unknown>): string { | |
| 329 | + for (const key of ['__previewFormatted', '__PreviewFormatted', 'text', 'Text'] as const) { | |
| 330 | + const raw = String(cfg[key] ?? '').trim() | |
| 331 | + if (!raw) continue | |
| 332 | + if (parseStoredPrintInputOffset(raw)) continue | |
| 333 | + if (isCorruptedDateDisplayFormat(raw)) continue | |
| 334 | + if (isResolvedWeekdayDisplayLiteral(raw)) { | |
| 335 | + return raw.toUpperCase().startsWith('DAY (') ? raw : raw.toUpperCase() | |
| 336 | + } | |
| 337 | + } | |
| 338 | + return '' | |
| 339 | +} | |
| 340 | + | |
| 250 | 341 | const MONTH_LONG_EN = [ |
| 251 | 342 | 'JANUARY', |
| 252 | 343 | 'FEBRUARY', |
| ... | ... | @@ -487,6 +578,10 @@ export function resolveElementDateTimeDisplay ( |
| 487 | 578 | |
| 488 | 579 | const cfg = { ...(el.config || {}) } as Record<string, unknown> |
| 489 | 580 | sanitizeDateElementConfig(el, cfg) |
| 581 | + if (isPrintInputLiteralField({ ...el, config: cfg })) { | |
| 582 | + const line = resolvePrintInputLiteralDisplay({ ...el, config: cfg }) | |
| 583 | + return line || null | |
| 584 | + } | |
| 490 | 585 | const vst = String(el.valueSourceType ?? '').toUpperCase() |
| 491 | 586 | const inputType = String(cfg.inputType ?? cfg.InputType ?? '').toLowerCase() |
| 492 | 587 | const rawText = String(cfg.text ?? cfg.Text ?? '').trim() |
| ... | ... | @@ -572,6 +667,8 @@ export function resolveWeekdayDisplayForElement ( |
| 572 | 667 | ): string { |
| 573 | 668 | const cfg = { ...(el.config || {}) } as Record<string, unknown> |
| 574 | 669 | sanitizeDateElementConfig(el, cfg) |
| 670 | + const frozen = readFrozenWeekdayDisplayFromConfig(cfg) | |
| 671 | + if (frozen) return applyElementPrefix(cfg, frozen) | |
| 575 | 672 | const off = readOffsetFromElementConfig({ ...el, config: cfg }) |
| 576 | 673 | const amount = off?.amount ?? 0 |
| 577 | 674 | const unit = off?.unit ?? 'Days' |
| ... | ... | @@ -612,8 +709,9 @@ export function parseStoredPrintInputOffset ( |
| 612 | 709 | return tryParsePrintInputOffsetStored(raw) |
| 613 | 710 | } |
| 614 | 711 | |
| 615 | -/** 与 Web isDateTimeLiveResolveField 对齐(含 config.typeAdd、duration date 命名兜底) */ | |
| 712 | +/** 与 Web isDateTimeLiveResolveField 对齐:含 auto current date/time 等相对基准解析字段 */ | |
| 616 | 713 | export function isUniAppDateTimeOffsetField (el: SystemTemplateElementBase): boolean { |
| 714 | + if (isPrintInputLiteralField(el)) return false | |
| 617 | 715 | const type = String(el.type || '').toUpperCase() |
| 618 | 716 | const cfg = (el.config || {}) as Record<string, unknown> |
| 619 | 717 | if (type === 'TIME' || type === 'DURATION') return true |
| ... | ... | @@ -625,7 +723,7 @@ export function isUniAppDateTimeOffsetField (el: SystemTemplateElementBase): boo |
| 625 | 723 | if (ta === 'auto_current date' || ta.includes('current date')) return true |
| 626 | 724 | if (ta === 'auto_current time' || ta.includes('current time')) return true |
| 627 | 725 | const nameHint = `${el.elementName ?? ''} ${el.inputKey ?? ''}`.toLowerCase() |
| 628 | - if (/(durationdate|duration_date|duration\s*date|use\s*by|prepped|prep\s*date|expir|must\s*use)/.test(nameHint)) return true | |
| 726 | + if (/(durationdate|duration_date|duration\s*date)/.test(nameHint)) return true | |
| 629 | 727 | return false |
| 630 | 728 | } |
| 631 | 729 | |
| ... | ... | @@ -644,6 +742,7 @@ export function applyLiveDateTimeFieldsToTemplate ( |
| 644 | 742 | const type = String(el.type || '').toUpperCase() |
| 645 | 743 | const cfg = { ...(el.config || {}) } as Record<string, unknown> |
| 646 | 744 | const weekdayEl = isWeekdayBarElement(el) |
| 745 | + if (isPrintInputLiteralField(el)) return el | |
| 647 | 746 | const needsLive = |
| 648 | 747 | type === 'DATE' || |
| 649 | 748 | type === 'TIME' || | ... | ... |
美国版/Food Labeling Management App UniApp/src/utils/labelPreview/printInputOptions.ts
| ... | ... | @@ -3,9 +3,29 @@ import type { |
| 3 | 3 | SystemLabelTemplate, |
| 4 | 4 | SystemTemplateElementBase, |
| 5 | 5 | } from '../print/types/printer' |
| 6 | -import { isUniAppDateTimeOffsetField } from './printInputOffset' | |
| 6 | +import { isUniAppDateTimeOffsetField, isPrintInputLiteralField } from './printInputOffset' | |
| 7 | 7 | import { formatWeightDisplay } from '../weightElement' |
| 8 | 8 | |
| 9 | +/** Entered When Printing:config.text 为 Pre-text,仅作 App 打印页提示,不出现在标签上 */ | |
| 10 | +export function printInputPreText(el: SystemTemplateElementBase): string { | |
| 11 | + const c = el.config || {} | |
| 12 | + return String(c.text ?? c.Text ?? '').trim() | |
| 13 | +} | |
| 14 | + | |
| 15 | +export function printInputFieldLabelFromElement(el: SystemTemplateElementBase): string { | |
| 16 | + const preText = printInputPreText(el) | |
| 17 | + if (preText) return preText | |
| 18 | + const type = String(el.type || '').toUpperCase() | |
| 19 | + const c = el.config || {} | |
| 20 | + const it = String(c.inputType ?? c.InputType ?? '').toLowerCase() | |
| 21 | + if (it === 'number') return 'Number' | |
| 22 | + if (it === 'text') return 'Text' | |
| 23 | + if (it === 'options') return 'Multiple Options' | |
| 24 | + if (it === 'datetime' || it === 'date') return 'Date & Time' | |
| 25 | + if (type === 'WEIGHT') return 'Weight' | |
| 26 | + return type.replace(/_/g, ' ') | |
| 27 | +} | |
| 28 | + | |
| 9 | 29 | /** |
| 10 | 30 | * 打印/预览 printInputJson 的 key:与接口文档一致优先 inputKey,否则 elementName,最后兜底元素 id。 |
| 11 | 31 | * 兼容根字段与 config 内 InputKey(部分模板把键写在 config)。 |
| ... | ... | @@ -65,18 +85,31 @@ export function readFreeFieldValuesFromTemplate(template: SystemLabelTemplate): |
| 65 | 85 | for (const el of template.elements || []) { |
| 66 | 86 | if (!isPrintInputFreeFieldElement(el)) continue |
| 67 | 87 | if (isUniAppDateTimeOffsetField(el)) continue |
| 88 | + if (isPrintInputLiteralField(el)) { | |
| 89 | + const c = el.config || {} | |
| 90 | + const it = String(c.inputType ?? c.InputType ?? '').toLowerCase() | |
| 91 | + const type = String(el.type || '').toUpperCase() | |
| 92 | + if (type === 'WEIGHT') { | |
| 93 | + const v = c.value ?? c.Value | |
| 94 | + out[el.id] = v != null && String(v).trim() !== '' ? String(v).trim() : '' | |
| 95 | + continue | |
| 96 | + } | |
| 97 | + if (it === 'number') { | |
| 98 | + out[el.id] = '0' | |
| 99 | + continue | |
| 100 | + } | |
| 101 | + out[el.id] = '' | |
| 102 | + continue | |
| 103 | + } | |
| 68 | 104 | const c = el.config || {} |
| 69 | 105 | const type = String(el.type || '').toUpperCase() |
| 70 | - let initial = '' | |
| 71 | 106 | if (type === 'WEIGHT') { |
| 72 | 107 | const v = c.value ?? c.Value |
| 73 | - if (v != null && String(v).trim() !== '') initial = String(v).trim() | |
| 74 | - } else { | |
| 75 | - const fmt = String(c.format ?? c.Format ?? '').trim() | |
| 76 | - const t = String(c.text ?? c.Text ?? '').trim() | |
| 77 | - if (t && (!fmt || t !== fmt)) initial = t | |
| 108 | + out[el.id] = v != null && String(v).trim() !== '' ? String(v).trim() : '' | |
| 109 | + continue | |
| 78 | 110 | } |
| 79 | - out[el.id] = initial | |
| 111 | + // DATE/TIME 等:config.text 为 Pre-text,初始录入为空 | |
| 112 | + out[el.id] = '' | |
| 80 | 113 | } |
| 81 | 114 | return out |
| 82 | 115 | } |
| ... | ... | @@ -111,6 +144,16 @@ export function mergePrintInputFreeFields( |
| 111 | 144 | const type = String(el.type || '').toUpperCase() |
| 112 | 145 | |
| 113 | 146 | if (!raw) { |
| 147 | + if (isPrintInputLiteralField(el)) { | |
| 148 | + const it = String(cfg.inputType ?? cfg.InputType ?? '').toLowerCase() | |
| 149 | + if (type === 'WEIGHT') { | |
| 150 | + return { ...el, config: { ...cfg, text: '', value: '' } } | |
| 151 | + } | |
| 152 | + if (it === 'number') { | |
| 153 | + return { ...el, config: { ...cfg, text: '' } } | |
| 154 | + } | |
| 155 | + return { ...el, config: { ...cfg, text: '' } } | |
| 156 | + } | |
| 114 | 157 | if (type === 'DATE' || type === 'TIME') { |
| 115 | 158 | const fmt = String(cfg.format ?? cfg.Format ?? '') |
| 116 | 159 | return { ...el, config: { ...cfg, text: fmt } } | ... | ... |
美国版/Food Labeling Management App UniApp/src/utils/labelPreview/renderLabelPreviewCanvas.ts
| ... | ... | @@ -11,6 +11,8 @@ import { |
| 11 | 11 | resolveWeekdayDisplayForElement, |
| 12 | 12 | isUniAppDateTimeOffsetField, |
| 13 | 13 | isWeekdayBarElement, |
| 14 | + isPrintInputLiteralField, | |
| 15 | + resolvePrintInputLiteralDisplay, | |
| 14 | 16 | } from './printInputOffset' |
| 15 | 17 | import { getLoggedInEmployeeDisplayName, isEmployeeTemplateElement } from './employeeElement' |
| 16 | 18 | import QRCode from 'qrcode' |
| ... | ... | @@ -20,7 +22,17 @@ import { |
| 20 | 22 | isLabelEditorFontItalicLoaded, |
| 21 | 23 | resolveLabelEditorFontFamily, |
| 22 | 24 | } from '../labelEditorFonts' |
| 23 | -import { computeVerticalTextBlockOffset, readVerticalAlign, readElementBorder, readFontStyle, readFontWeight, readTextDecoration, readElementRotation, type TextVerticalAlign } from '../textElementLayout' | |
| 25 | +import { | |
| 26 | + computeVerticalTextBlockOffset, | |
| 27 | + readVerticalAlign, | |
| 28 | + readElementBorder, | |
| 29 | + readFontStyle, | |
| 30 | + readFontWeight, | |
| 31 | + readTextDecoration, | |
| 32 | + readElementRotation, | |
| 33 | + isShortSingleLineTextBox, | |
| 34 | + type TextVerticalAlign, | |
| 35 | +} from '../textElementLayout' | |
| 24 | 36 | |
| 25 | 37 | import { computeImageDrawRect, readImageScaleMode } from '../imageScaleMode' |
| 26 | 38 | import { |
| ... | ... | @@ -124,10 +136,52 @@ function layoutInvertedWeekdayStrip( |
| 124 | 136 | return { stripTop, stripHeight, textY } |
| 125 | 137 | } |
| 126 | 138 | |
| 127 | -/** 矮框 TIME(如 h=16):文字贴上沿,避免下沉侵占黑条上边距 */ | |
| 128 | -function textYForShortTimeBox(by: number, bh: number, printFontSize: number): number { | |
| 129 | - const lineTop = by + Math.max(0, Math.floor((bh - printFontSize) * 0.08)) | |
| 130 | - return lineTop + Math.round(printFontSize * 0.72) | |
| 139 | +/** 单行在元素框内垂直对齐(与 Web flex + verticalAlign 一致);多行仍用 alphabetic baseline */ | |
| 140 | +function computeLineDrawY( | |
| 141 | + by: number, | |
| 142 | + bh: number, | |
| 143 | + li: number, | |
| 144 | + fittedLineHeight: number, | |
| 145 | + visibleLineCount: number, | |
| 146 | + printFontSize: number, | |
| 147 | + verticalAlign: TextVerticalAlign, | |
| 148 | +): { y: number; baseline: 'middle' | 'alphabetic' } { | |
| 149 | + const blockHeight = visibleLineCount * fittedLineHeight | |
| 150 | + const verticalOffset = computeVerticalTextBlockOffset(bh, blockHeight, verticalAlign) | |
| 151 | + const lineTop = by + verticalOffset + li * fittedLineHeight | |
| 152 | + if (visibleLineCount === 1 && verticalAlign === 'center') { | |
| 153 | + return { y: by + bh / 2, baseline: 'middle' } | |
| 154 | + } | |
| 155 | + if (visibleLineCount === 1 && verticalAlign === 'bottom') { | |
| 156 | + return { | |
| 157 | + y: by + bh - Math.max(1, Math.round(printFontSize * 0.12)), | |
| 158 | + baseline: 'alphabetic', | |
| 159 | + } | |
| 160 | + } | |
| 161 | + if (visibleLineCount > 1) { | |
| 162 | + return { y: lineTop + fittedLineHeight / 2, baseline: 'middle' } | |
| 163 | + } | |
| 164 | + return { y: lineTop + Math.round(printFontSize * 0.82), baseline: 'alphabetic' } | |
| 165 | +} | |
| 166 | + | |
| 167 | +/** middle 基线时 y 在字高中心,下划线贴在数字底部略下方 */ | |
| 168 | +function computeUnderlineDrawY( | |
| 169 | + y: number, | |
| 170 | + fontSize: number, | |
| 171 | + baseline: 'middle' | 'alphabetic', | |
| 172 | +): number { | |
| 173 | + if (baseline === 'middle') { | |
| 174 | + /** 数字 cap-height 约 0.72em,自 center 到字底约 0.36em */ | |
| 175 | + return y + Math.round(fontSize * 0.36) + Math.max(1, Math.round(fontSize * 0.04)) | |
| 176 | + } | |
| 177 | + return y + Math.max(1, Math.round(fontSize * 0.1)) | |
| 178 | +} | |
| 179 | + | |
| 180 | +function readCanvasFontWeight(config: Record<string, any>): 'normal' | 'bold' { | |
| 181 | + if (readFontWeight(config) === 'bold') return 'bold' | |
| 182 | + const family = resolveLabelEditorFontFamily(config).toLowerCase() | |
| 183 | + if (family.includes('bold')) return 'bold' | |
| 184 | + return 'normal' | |
| 131 | 185 | } |
| 132 | 186 | |
| 133 | 187 | function applyCanvasFontFromConfig( |
| ... | ... | @@ -137,7 +191,7 @@ function applyCanvasFontFromConfig( |
| 137 | 191 | ): void { |
| 138 | 192 | const fontFamily = resolveLabelEditorFontFamily(config) |
| 139 | 193 | const anyCtx = ctx as any |
| 140 | - const weight = readFontWeight(config) === 'bold' ? 'bold' : 'normal' | |
| 194 | + const weight = readCanvasFontWeight(config) | |
| 141 | 195 | const style = readFontStyle(config) === 'italic' ? 'italic' : 'normal' |
| 142 | 196 | if (typeof anyCtx.setFontFamily === 'function') { |
| 143 | 197 | anyCtx.setFontFamily(fontFamily) |
| ... | ... | @@ -154,6 +208,216 @@ function approxTextWidth(text: string, fontSize: number): number { |
| 154 | 208 | return Math.max(4, String(text).length * fontSize * 0.55) |
| 155 | 209 | } |
| 156 | 210 | |
| 211 | +function canvasSupportsMeasureText(ctx: UniApp.CanvasContext): boolean { | |
| 212 | + return typeof (ctx as any).measureText === 'function' | |
| 213 | +} | |
| 214 | + | |
| 215 | +function measureCanvasLineWidth( | |
| 216 | + ctx: UniApp.CanvasContext, | |
| 217 | + text: string, | |
| 218 | + fontSize: number, | |
| 219 | + config: Record<string, any>, | |
| 220 | +): number { | |
| 221 | + if (canvasSupportsMeasureText(ctx)) { | |
| 222 | + try { | |
| 223 | + applyCanvasFontFromConfig(ctx, config, fontSize) | |
| 224 | + const w = Number((ctx as any).measureText(String(text)).width) | |
| 225 | + if (Number.isFinite(w) && w > 0) return w | |
| 226 | + } catch { | |
| 227 | + /* fall through */ | |
| 228 | + } | |
| 229 | + } | |
| 230 | + return approxTextWidth(text, fontSize) | |
| 231 | +} | |
| 232 | + | |
| 233 | +function pushLongWordByMeasuredWidth( | |
| 234 | + ctx: UniApp.CanvasContext, | |
| 235 | + word: string, | |
| 236 | + maxWidth: number, | |
| 237 | + fontSize: number, | |
| 238 | + config: Record<string, any>, | |
| 239 | + out: string[], | |
| 240 | +): string { | |
| 241 | + let buf = '' | |
| 242 | + for (let i = 0; i < word.length; i++) { | |
| 243 | + const ch = word.charAt(i) | |
| 244 | + const trial = buf + ch | |
| 245 | + if (buf && measureCanvasLineWidth(ctx, trial, fontSize, config) > maxWidth) { | |
| 246 | + out.push(buf) | |
| 247 | + buf = ch | |
| 248 | + } else { | |
| 249 | + buf = trial | |
| 250 | + } | |
| 251 | + } | |
| 252 | + return buf | |
| 253 | +} | |
| 254 | + | |
| 255 | +function flushMeasuredRemainder( | |
| 256 | + ctx: UniApp.CanvasContext, | |
| 257 | + remainder: string, | |
| 258 | + maxWidth: number, | |
| 259 | + fontSize: number, | |
| 260 | + config: Record<string, any>, | |
| 261 | + out: string[], | |
| 262 | +): void { | |
| 263 | + let rest = String(remainder ?? '') | |
| 264 | + let guard = 0 | |
| 265 | + const guardMax = Math.max(64, rest.length * 4) | |
| 266 | + while (rest.length > 0 && guard++ < guardMax) { | |
| 267 | + const before = out.length | |
| 268 | + rest = pushLongWordByMeasuredWidth(ctx, rest, maxWidth, fontSize, config, out) | |
| 269 | + if (!rest) break | |
| 270 | + if (out.length === before) { | |
| 271 | + /** 单字宽于行宽或 push 未产出新行:强制推进 1 字符,避免 while(rest) 死循环 */ | |
| 272 | + out.push(rest.slice(0, 1)) | |
| 273 | + rest = rest.slice(1) | |
| 274 | + continue | |
| 275 | + } | |
| 276 | + if (measureCanvasLineWidth(ctx, rest, fontSize, config) <= maxWidth) { | |
| 277 | + out.push(rest) | |
| 278 | + break | |
| 279 | + } | |
| 280 | + } | |
| 281 | + if (rest.length > 0 && guard >= guardMax) { | |
| 282 | + out.push(rest) | |
| 283 | + } | |
| 284 | +} | |
| 285 | + | |
| 286 | +/** 优先 measureText 按真实宽度断行;breakAll 与 Web `break-all` 一致 */ | |
| 287 | +function wrapTextToCanvasWidth( | |
| 288 | + ctx: UniApp.CanvasContext, | |
| 289 | + text: string, | |
| 290 | + innerWidthPx: number, | |
| 291 | + fontSize: number, | |
| 292 | + config: Record<string, any>, | |
| 293 | + breakAll = false, | |
| 294 | +): string[] { | |
| 295 | + const maxW = Math.max(4, innerWidthPx) | |
| 296 | + const rawLines = String(text ?? '').split(/\r?\n/) | |
| 297 | + const out: string[] = [] | |
| 298 | + | |
| 299 | + for (const segment of rawLines) { | |
| 300 | + const s = String(segment ?? '') | |
| 301 | + if (!s.trim()) { | |
| 302 | + out.push(s) | |
| 303 | + continue | |
| 304 | + } | |
| 305 | + if (measureCanvasLineWidth(ctx, s, fontSize, config) <= maxW) { | |
| 306 | + out.push(s) | |
| 307 | + continue | |
| 308 | + } | |
| 309 | + if (!canvasSupportsMeasureText(ctx)) { | |
| 310 | + out.push(...wrapTextToWidth(s, maxCharsPerLine(maxW, fontSize))) | |
| 311 | + continue | |
| 312 | + } | |
| 313 | + if (breakAll) { | |
| 314 | + flushMeasuredRemainder(ctx, s, maxW, fontSize, config, out) | |
| 315 | + continue | |
| 316 | + } | |
| 317 | + const words = s.trim().split(/\s+/).filter(Boolean) | |
| 318 | + let cur = '' | |
| 319 | + for (const word of words) { | |
| 320 | + const trial = cur ? `${cur} ${word}` : word | |
| 321 | + if (measureCanvasLineWidth(ctx, trial, fontSize, config) <= maxW) { | |
| 322 | + cur = trial | |
| 323 | + } else { | |
| 324 | + if (cur) out.push(cur) | |
| 325 | + if (measureCanvasLineWidth(ctx, word, fontSize, config) <= maxW) { | |
| 326 | + cur = word | |
| 327 | + } else { | |
| 328 | + cur = pushLongWordByMeasuredWidth(ctx, word, maxW, fontSize, config, out) | |
| 329 | + } | |
| 330 | + } | |
| 331 | + } | |
| 332 | + if (cur) out.push(cur) | |
| 333 | + } | |
| 334 | + return out.length ? out : [''] | |
| 335 | +} | |
| 336 | + | |
| 337 | +/** 黑底/静态文案行高:与 Web leading-tight(≈1.25)一致,避免矮框多行末行被 clip */ | |
| 338 | +function invertedStaticLineHeight(fontSize: number): number { | |
| 339 | + return lineHeightForTextElement(fontSize, false) | |
| 340 | +} | |
| 341 | + | |
| 342 | +function shouldBreakAllTextWrap(_type: string, invertedStatic: boolean): boolean { | |
| 343 | + /** 仅黑底窄框标签需 break-all;长地址等普通 TEXT_STATIC 用词边界换行,避免逐字 measureText 卡死 */ | |
| 344 | + return invertedStatic | |
| 345 | +} | |
| 346 | + | |
| 347 | +function maxLinesForBox( | |
| 348 | + layoutBoxH: number, | |
| 349 | + fittedLineHeight: number, | |
| 350 | + printFontSize: number, | |
| 351 | +): number { | |
| 352 | + if (layoutBoxH < printFontSize) return 1 | |
| 353 | + const descentPad = Math.max(2, Math.round(printFontSize * 0.14)) | |
| 354 | + return Math.max(1, Math.floor((layoutBoxH + descentPad) / fittedLineHeight)) | |
| 355 | +} | |
| 356 | + | |
| 357 | +function fitFontSizeToInnerWidth( | |
| 358 | + ctx: UniApp.CanvasContext, | |
| 359 | + line: string, | |
| 360 | + innerW: number, | |
| 361 | + fontSize: number, | |
| 362 | + config: Record<string, any>, | |
| 363 | +): number { | |
| 364 | + if (!line || innerW <= 0) return fontSize | |
| 365 | + if (!canvasSupportsMeasureText(ctx)) { | |
| 366 | + const w = approxTextWidth(line, fontSize) | |
| 367 | + if (w <= innerW) return fontSize | |
| 368 | + return Math.max(8, Math.floor(fontSize * (innerW / w) * 0.98)) | |
| 369 | + } | |
| 370 | + let size = fontSize | |
| 371 | + for (let i = 0; i < 5; i++) { | |
| 372 | + const w = measureCanvasLineWidth(ctx, line, size, config) | |
| 373 | + if (w <= innerW || w <= 0) return size | |
| 374 | + size = Math.max(8, Math.floor(size * (innerW / w) * 0.98)) | |
| 375 | + } | |
| 376 | + return Math.max(8, size) | |
| 377 | +} | |
| 378 | + | |
| 379 | +function resolveTextLinesForDraw( | |
| 380 | + ctx: UniApp.CanvasContext, | |
| 381 | + text: string, | |
| 382 | + innerW: number, | |
| 383 | + layoutBoxH: number, | |
| 384 | + printFontSize: number, | |
| 385 | + fittedLineHeight: number, | |
| 386 | + config: Record<string, any>, | |
| 387 | + opts: { breakAll: boolean; isDateTimeType: boolean }, | |
| 388 | +): { lines: string[]; drawFontSize: number } { | |
| 389 | + const trimmed = String(text ?? '').trim() | |
| 390 | + if (!trimmed) return { lines: [''], drawFontSize: printFontSize } | |
| 391 | + if (opts.isDateTimeType) { | |
| 392 | + return { lines: [trimmed.replace(/\s+/g, ' ')], drawFontSize: printFontSize } | |
| 393 | + } | |
| 394 | + const preferSingleLine = | |
| 395 | + !opts.breakAll && | |
| 396 | + !trimmed.includes('\n') && | |
| 397 | + isShortSingleLineTextBox(layoutBoxH, printFontSize, trimmed, { breakAll: opts.breakAll }) | |
| 398 | + if (preferSingleLine) { | |
| 399 | + const drawFontSize = fitFontSizeToInnerWidth(ctx, trimmed, innerW, printFontSize, config) | |
| 400 | + return { lines: [trimmed], drawFontSize } | |
| 401 | + } | |
| 402 | + return { | |
| 403 | + lines: wrapTextToCanvasWidth(ctx, trimmed, innerW, printFontSize, config, opts.breakAll), | |
| 404 | + drawFontSize: printFontSize, | |
| 405 | + } | |
| 406 | +} | |
| 407 | + | |
| 408 | +function computeVisibleTextLines( | |
| 409 | + lines: string[], | |
| 410 | + layoutBoxH: number, | |
| 411 | + fittedLineHeight: number, | |
| 412 | + printFontSize: number, | |
| 413 | + isDateTimeType: boolean, | |
| 414 | +): string[] { | |
| 415 | + if (isDateTimeType) return lines.slice(0, 1) | |
| 416 | + if (layoutBoxH < printFontSize) return lines.slice(0, 1) | |
| 417 | + const maxLines = Math.min(lines.length, maxLinesForBox(layoutBoxH, fittedLineHeight, printFontSize)) | |
| 418 | + return lines.slice(0, maxLines) | |
| 419 | +} | |
| 420 | + | |
| 157 | 421 | function drawStyledTextLine( |
| 158 | 422 | ctx: UniApp.CanvasContext, |
| 159 | 423 | line: string, |
| ... | ... | @@ -164,6 +428,7 @@ function drawStyledTextLine( |
| 164 | 428 | config: Record<string, any>, |
| 165 | 429 | fillColor: string, |
| 166 | 430 | emphasizeForPrint = false, |
| 431 | + baseline: 'middle' | 'alphabetic' = 'alphabetic', | |
| 167 | 432 | ): void { |
| 168 | 433 | const family = resolveLabelEditorFontFamily(config) |
| 169 | 434 | const italic = readFontStyle(config) === 'italic' |
| ... | ... | @@ -171,6 +436,9 @@ function drawStyledTextLine( |
| 171 | 436 | const useSkewFallback = italic && !isLabelEditorFontItalicLoaded(family) |
| 172 | 437 | const anyCtx = ctx as any |
| 173 | 438 | const lineWidth = approxTextWidth(line, fontSize) |
| 439 | + if (typeof anyCtx.setTextBaseline === 'function') { | |
| 440 | + anyCtx.setTextBaseline(baseline) | |
| 441 | + } | |
| 174 | 442 | |
| 175 | 443 | const drawFill = () => { |
| 176 | 444 | if (emphasizeForPrint) { |
| ... | ... | @@ -198,11 +466,12 @@ function drawStyledTextLine( |
| 198 | 466 | x1 = tx - lineWidth |
| 199 | 467 | x2 = tx |
| 200 | 468 | } |
| 469 | + const underlineY = computeUnderlineDrawY(y, fontSize, baseline) | |
| 201 | 470 | ctx.setStrokeStyle(fillColor) |
| 202 | 471 | ctx.setLineWidth(Math.max(1, Math.round(fontSize * 0.06))) |
| 203 | 472 | ctx.beginPath() |
| 204 | - ctx.moveTo(x1, y + Math.max(1, Math.round(fontSize * 0.12))) | |
| 205 | - ctx.lineTo(x2, y + Math.max(1, Math.round(fontSize * 0.12))) | |
| 473 | + ctx.moveTo(x1, underlineY) | |
| 474 | + ctx.lineTo(x2, underlineY) | |
| 206 | 475 | ctx.stroke() |
| 207 | 476 | } |
| 208 | 477 | if (useSkewFallback && typeof anyCtx.restore === 'function') { |
| ... | ... | @@ -275,6 +544,9 @@ function resolveDateTimePreviewText ( |
| 275 | 544 | element: SystemTemplateElementBase, |
| 276 | 545 | baseTime: Date = new Date(), |
| 277 | 546 | ): string { |
| 547 | + if (isPrintInputLiteralField(element)) { | |
| 548 | + return resolvePrintInputLiteralDisplay(element) | |
| 549 | + } | |
| 278 | 550 | if (isWeekdayBarElement(element)) { |
| 279 | 551 | return applyConfigPrefix( |
| 280 | 552 | element.config || {}, |
| ... | ... | @@ -306,6 +578,9 @@ function resolveDateTimePreviewText ( |
| 306 | 578 | function previewTextForElement(element: SystemTemplateElementBase, baseTime: Date = new Date()): string { |
| 307 | 579 | const type = String(element.type || '').toUpperCase() |
| 308 | 580 | const config = element.config || {} |
| 581 | + if (isPrintInputLiteralField(element)) { | |
| 582 | + return resolvePrintInputLiteralDisplay(element) | |
| 583 | + } | |
| 309 | 584 | if (isWeekdayBarElement(element)) { |
| 310 | 585 | return applyConfigPrefix(config, resolveWeekdayDisplayForElement(element, baseTime)) |
| 311 | 586 | } |
| ... | ... | @@ -331,13 +606,12 @@ function previewTextForElement(element: SystemTemplateElementBase, baseTime: Dat |
| 331 | 606 | const rawSel = config.selectedOptionValues ?? config.SelectedOptionValues |
| 332 | 607 | const arr = Array.isArray(rawSel) ? rawSel.map((x: unknown) => String(x)) : [] |
| 333 | 608 | const txt = cfgStr(config, ['text', 'Text'], '') |
| 334 | - if (arr.length > 0 && txt.trim()) return txt | |
| 335 | 609 | if (arr.length > 0) { |
| 336 | 610 | const joined = arr.join(', ') |
| 611 | + if (txt.trim() && arr.some((v) => txt.includes(v))) return txt | |
| 337 | 612 | return applyConfigPrefix(config, joined) |
| 338 | 613 | } |
| 339 | - const hint = txt.trim() || 'Select below' | |
| 340 | - return applyConfigPrefix(config, hint) | |
| 614 | + return '' | |
| 341 | 615 | } |
| 342 | 616 | if (vst === 'PRINT_INPUT' && !(inputType === 'options' || hasDict)) { |
| 343 | 617 | if ( |
| ... | ... | @@ -350,7 +624,12 @@ function previewTextForElement(element: SystemTemplateElementBase, baseTime: Dat |
| 350 | 624 | let body = cfgStr(config, ['text', 'Text'], '') |
| 351 | 625 | if (!body.trim()) body = cfgStr(config, ['value', 'Value'], '') |
| 352 | 626 | if (!body.trim()) body = cfgStr(config, ['format', 'Format', 'placeholder', 'Placeholder'], '') |
| 353 | - if (isStoredPrintInputOffsetPayload(body) || isCorruptedDateDisplayFormat(body)) return '' | |
| 627 | + const literalNumberOrText = | |
| 628 | + inputType === 'number' || | |
| 629 | + inputType === 'text' || | |
| 630 | + isPrintInputLiteralField(element) | |
| 631 | + if (!literalNumberOrText && isStoredPrintInputOffsetPayload(body)) return '' | |
| 632 | + if (isCorruptedDateDisplayFormat(body)) return '' | |
| 354 | 633 | const unit = String(config.unit ?? config.Unit ?? '').trim() |
| 355 | 634 | if (unit && body.trim() && !body.endsWith(unit)) body = `${body}${unit}` |
| 356 | 635 | return applyConfigPrefix(config, body) |
| ... | ... | @@ -411,11 +690,13 @@ function drawBarcodeLikePreview( |
| 411 | 690 | w: number, |
| 412 | 691 | h: number, |
| 413 | 692 | value: string, |
| 414 | - options?: { orientation?: string; showText?: boolean } | |
| 693 | + options?: { orientation?: string; showText?: boolean; fontSize?: number }, | |
| 415 | 694 | ): void { |
| 416 | - const bw = Math.max(40, w || 140) | |
| 417 | - const bh = Math.max(28, h || 56) | |
| 695 | + const bw = Math.max(8, w || 140) | |
| 696 | + const bh = Math.max(8, h || 56) | |
| 418 | 697 | const showText = options?.showText !== false |
| 698 | + const fontSize = Math.max(8, Math.round(Number(options?.fontSize ?? 14) || 14)) | |
| 699 | + const labelReserve = showText ? Math.max(12, fontSize + 4) : 4 | |
| 419 | 700 | const pad = 2 |
| 420 | 701 | const modules = barcodeModulesFromValue(value) |
| 421 | 702 | const orientation = String(options?.orientation || 'horizontal').toLowerCase() |
| ... | ... | @@ -429,42 +710,42 @@ function drawBarcodeLikePreview( |
| 429 | 710 | ctx.setFillStyle('#111827') |
| 430 | 711 | |
| 431 | 712 | if (!isVertical) { |
| 432 | - const textH = showText && txt ? Math.max(10, Math.round(bh * 0.2)) : 0 | |
| 433 | - const barH = Math.max(10, bh - textH - pad * 2) | |
| 713 | + const textH = showText && txt ? labelReserve : 0 | |
| 714 | + const barH = Math.max(20, bh - textH - pad) | |
| 434 | 715 | const innerW = Math.max(8, bw - pad * 2) |
| 435 | 716 | const moduleW = innerW / modules.length |
| 436 | 717 | let cursor = x + pad |
| 437 | 718 | for (let i = 0; i < modules.length; i++) { |
| 438 | 719 | if (modules[i] === 1) { |
| 439 | - const rw = Math.max(0.7, moduleW * 0.72) | |
| 720 | + const rw = Math.max(0.7, moduleW * 0.85) | |
| 440 | 721 | ctx.fillRect(cursor, y + pad, rw, barH) |
| 441 | 722 | } |
| 442 | 723 | cursor += moduleW |
| 443 | 724 | } |
| 444 | 725 | if (showText && txt) { |
| 445 | - ctx.setFontSize(Math.max(9, Math.min(12, Math.round(textH * 0.8)))) | |
| 726 | + ctx.setFontSize(fontSize) | |
| 446 | 727 | ctx.setTextAlign('center') |
| 447 | - ctx.fillText(txt, x + bw / 2, y + bh - 2) | |
| 728 | + ctx.fillText(txt, x + bw / 2, y + bh - Math.max(2, Math.round(fontSize * 0.2))) | |
| 448 | 729 | ctx.setTextAlign('left') |
| 449 | 730 | } |
| 450 | 731 | return |
| 451 | 732 | } |
| 452 | 733 | |
| 453 | 734 | // vertical:条码在左,data 竖排在右 |
| 454 | - const textBandW = showText && txt ? Math.max(10, Math.round(bw * 0.18)) : 0 | |
| 455 | - const barW = Math.max(10, bw - textBandW - pad * 2) | |
| 456 | - const innerH = Math.max(10, bh - pad * 2) | |
| 735 | + const textBandW = showText && txt ? Math.max(14, Math.round(fontSize + 6)) : 0 | |
| 736 | + const barW = Math.max(12, bw - textBandW - pad * 2) | |
| 737 | + const innerH = Math.max(12, bh - pad * 2) | |
| 457 | 738 | const moduleH = innerH / modules.length |
| 458 | 739 | let cursorY = y + pad |
| 459 | 740 | for (let i = 0; i < modules.length; i++) { |
| 460 | 741 | if (modules[i] === 1) { |
| 461 | - const rh = Math.max(0.7, moduleH * 0.72) | |
| 742 | + const rh = Math.max(0.7, moduleH * 0.85) | |
| 462 | 743 | ctx.fillRect(x + pad, cursorY, barW, rh) |
| 463 | 744 | } |
| 464 | 745 | cursorY += moduleH |
| 465 | 746 | } |
| 466 | 747 | if (showText && txt) { |
| 467 | - const font = Math.max(9, Math.min(11, Math.floor(textBandW * 0.75))) | |
| 748 | + const font = fontSize | |
| 468 | 749 | const cx = x + bw - textBandW / 2 |
| 469 | 750 | const cy = y + bh / 2 |
| 470 | 751 | const anyCtx = ctx as any |
| ... | ... | @@ -597,10 +878,7 @@ function drawNutritionFactsOnCanvas( |
| 597 | 878 | /** 正文用 Roboto,避免 FreightSans Bold 导致整表加粗(与 Web 一致) */ |
| 598 | 879 | const bodyCfg = { ...config, fontFamily: 'Roboto', FontFamily: 'Roboto' } |
| 599 | 880 | |
| 600 | - ctx.setFillStyle('#ffffff') | |
| 601 | - ctx.fillRect(boxX, boxY, boxW, boxH) | |
| 602 | - | |
| 603 | - const rowStep = (fs: number) => fs + 4 | |
| 881 | + const rowStep = (fs: number) => fs + 3 | |
| 604 | 882 | |
| 605 | 883 | const drawLine = (label: string, value: string, fs: number, labelBold = false) => { |
| 606 | 884 | const f = Math.max(8, Math.round(fs)) |
| ... | ... | @@ -887,7 +1165,12 @@ function runLabelPreviewCanvasDraw( |
| 887 | 1165 | const configOrientation = cfgStr(config, ['orientation', 'Orientation'], 'horizontal').toLowerCase() |
| 888 | 1166 | const orientation = elementRotation === 'vertical' ? 'vertical' : configOrientation |
| 889 | 1167 | const showText = String(config.showText ?? config.ShowText ?? 'true').toLowerCase() !== 'false' |
| 890 | - drawBarcodeLikePreview(ctx, x, y, w || 140, h || 56, d, { orientation, showText }) | |
| 1168 | + const barcodeFontSize = Number(config.fontSize ?? config.FontSize ?? 14) || 14 | |
| 1169 | + drawBarcodeLikePreview(ctx, x, y, w || 140, h || 56, d, { | |
| 1170 | + orientation, | |
| 1171 | + showText, | |
| 1172 | + fontSize: barcodeFontSize, | |
| 1173 | + }) | |
| 891 | 1174 | finishElement() |
| 892 | 1175 | return |
| 893 | 1176 | } |
| ... | ... | @@ -963,12 +1246,6 @@ function runLabelPreviewCanvasDraw( |
| 963 | 1246 | const fontSize = readFontSize(config) |
| 964 | 1247 | const invertedBar = readInvertColors(config) |
| 965 | 1248 | const printFontSize = invertedBar && forPrint ? fontSize + Math.max(1, Math.round(fontSize * 0.08)) : fontSize |
| 966 | - ctx.setFontSize(printFontSize) | |
| 967 | - applyCanvasFontFromConfig(ctx, config, printFontSize) | |
| 968 | - const fontWeight = invertedBar && forPrint ? 'bold' : readFontWeight(config) | |
| 969 | - if (typeof anyCtx.setFontWeight === 'function') { | |
| 970 | - anyCtx.setFontWeight(fontWeight === 'bold' ? 'bold' : 'normal') | |
| 971 | - } | |
| 972 | 1249 | const align = readTextAlign(config) |
| 973 | 1250 | const fillColor = invertedBar ? '#ffffff' : readFillColor(config) |
| 974 | 1251 | /** 与 Web AlignedTextBox 的 Tailwind px-1 一致:仅水平 4px,垂直无 padding */ |
| ... | ... | @@ -978,28 +1255,49 @@ function runLabelPreviewCanvasDraw( |
| 978 | 1255 | let tx = bx + hPad |
| 979 | 1256 | if (align === 'center') tx = bx + bw / 2 |
| 980 | 1257 | else if (align === 'right') tx = bx + bw - hPad |
| 981 | - ctx.setTextAlign(align === 'center' ? 'center' : align === 'right' ? 'right' : 'left') | |
| 982 | 1258 | const isDateTimeType = type === 'DATE' || type === 'TIME' || type === 'DURATION' || weekdayBar |
| 983 | - const lineHeight = lineHeightForTextElement(printFontSize, isDateTimeType) | |
| 984 | - const lines = isDateTimeType | |
| 985 | - ? [String(finalText).replace(/\s+/g, ' ').trim()] | |
| 986 | - : wrapTextToWidth(finalText, maxCharsPerLine(innerW, printFontSize)) | |
| 1259 | + const invertedStatic = invertedBar && !weekdayBar | |
| 1260 | + const breakAll = shouldBreakAllTextWrap(type, invertedStatic) | |
| 987 | 1261 | const layoutBoxH = invertedBar ? bh : innerH |
| 1262 | + const baseLineHeight = invertedStatic | |
| 1263 | + ? invertedStaticLineHeight(printFontSize) | |
| 1264 | + : lineHeightForTextElement(printFontSize, isDateTimeType) | |
| 1265 | + const { lines, drawFontSize } = resolveTextLinesForDraw( | |
| 1266 | + ctx, | |
| 1267 | + finalText, | |
| 1268 | + innerW, | |
| 1269 | + layoutBoxH, | |
| 1270 | + printFontSize, | |
| 1271 | + baseLineHeight, | |
| 1272 | + config, | |
| 1273 | + { breakAll, isDateTimeType }, | |
| 1274 | + ) | |
| 1275 | + ctx.setFontSize(drawFontSize) | |
| 1276 | + applyCanvasFontFromConfig(ctx, config, drawFontSize) | |
| 1277 | + const fontWeight = invertedBar && forPrint ? 'bold' : readCanvasFontWeight(config) | |
| 1278 | + if (typeof anyCtx.setFontWeight === 'function') { | |
| 1279 | + anyCtx.setFontWeight(fontWeight === 'bold' ? 'bold' : 'normal') | |
| 1280 | + } | |
| 1281 | + ctx.setTextAlign(align === 'center' ? 'center' : align === 'right' ? 'right' : 'left') | |
| 988 | 1282 | const fittedLineHeight = |
| 989 | - isDateTimeType && !invertedBar && bh > 0 ? Math.min(lineHeight, bh) : lineHeight | |
| 990 | - const maxLines = isDateTimeType | |
| 991 | - ? 1 | |
| 992 | - : layoutBoxH >= printFontSize | |
| 993 | - ? Math.max(1, Math.floor(layoutBoxH / fittedLineHeight)) | |
| 994 | - : lines.length | |
| 995 | - const visibleLines = lines.slice(0, maxLines) | |
| 996 | - const blockHeight = visibleLines.length * fittedLineHeight | |
| 1283 | + isDateTimeType && !invertedBar && bh > 0 | |
| 1284 | + ? Math.min( | |
| 1285 | + invertedStatic | |
| 1286 | + ? invertedStaticLineHeight(drawFontSize) | |
| 1287 | + : lineHeightForTextElement(drawFontSize, isDateTimeType), | |
| 1288 | + bh, | |
| 1289 | + ) | |
| 1290 | + : invertedStatic | |
| 1291 | + ? invertedStaticLineHeight(drawFontSize) | |
| 1292 | + : lineHeightForTextElement(drawFontSize, isDateTimeType) | |
| 1293 | + const visibleLines = computeVisibleTextLines( | |
| 1294 | + lines, | |
| 1295 | + layoutBoxH, | |
| 1296 | + fittedLineHeight, | |
| 1297 | + drawFontSize, | |
| 1298 | + isDateTimeType, | |
| 1299 | + ) | |
| 997 | 1300 | const verticalAlign = resolveDrawVerticalAlign(config, el, { invertedBar, weekdayBar }) |
| 998 | - const verticalOffset = computeVerticalTextBlockOffset(layoutBoxH, blockHeight, verticalAlign) | |
| 999 | - const lineTop = by + verticalOffset | |
| 1000 | - const baselineFromLineTop = Math.round(printFontSize * 0.82) | |
| 1001 | - const defaultStartY = lineTop + baselineFromLineTop | |
| 1002 | - const shortTimeBox = type === 'TIME' && !invertedBar && bh > 0 && bh <= 20 | |
| 1003 | 1301 | const weekdayStrip = invertedBar && weekdayBar |
| 1004 | 1302 | ? layoutInvertedWeekdayStrip(by, bh, printFontSize, verticalAlign) |
| 1005 | 1303 | : null |
| ... | ... | @@ -1010,30 +1308,47 @@ function runLabelPreviewCanvasDraw( |
| 1010 | 1308 | ctx.setFillStyle('#ffffff') |
| 1011 | 1309 | } else if (invertedBar) { |
| 1012 | 1310 | ctx.setFillStyle('#000000') |
| 1013 | - ctx.fillRect(bx + hPad, lineTop, innerW, blockHeight) | |
| 1311 | + /** 黑底铺满元素框(与 Web invert 控件视觉尺寸一致),避免只画文字行高导致「控件不完整」 */ | |
| 1312 | + ctx.fillRect(bx + hPad, by, innerW, bh) | |
| 1014 | 1313 | ctx.setFillStyle('#ffffff') |
| 1015 | 1314 | } else { |
| 1016 | 1315 | ctx.setFillStyle(fillColor) |
| 1017 | 1316 | } |
| 1018 | 1317 | |
| 1019 | 1318 | visibleLines.forEach((ln, li) => { |
| 1020 | - const textY = weekdayStrip | |
| 1021 | - ? weekdayStrip.textY + li * weekdayStrip.stripHeight | |
| 1022 | - : shortTimeBox | |
| 1023 | - ? textYForShortTimeBox(by, bh, printFontSize) + li * fittedLineHeight | |
| 1024 | - : defaultStartY + li * fittedLineHeight | |
| 1319 | + let textY: number | |
| 1320 | + let baseline: 'middle' | 'alphabetic' = 'alphabetic' | |
| 1321 | + if (weekdayStrip) { | |
| 1322 | + textY = weekdayStrip.textY + li * weekdayStrip.stripHeight | |
| 1323 | + } else { | |
| 1324 | + const pos = computeLineDrawY( | |
| 1325 | + by, | |
| 1326 | + layoutBoxH, | |
| 1327 | + li, | |
| 1328 | + fittedLineHeight, | |
| 1329 | + visibleLines.length, | |
| 1330 | + drawFontSize, | |
| 1331 | + verticalAlign, | |
| 1332 | + ) | |
| 1333 | + textY = pos.y | |
| 1334 | + baseline = pos.baseline | |
| 1335 | + } | |
| 1025 | 1336 | drawStyledTextLine( |
| 1026 | 1337 | ctx, |
| 1027 | 1338 | ln, |
| 1028 | 1339 | tx, |
| 1029 | 1340 | textY, |
| 1030 | - printFontSize, | |
| 1341 | + drawFontSize, | |
| 1031 | 1342 | align, |
| 1032 | 1343 | config, |
| 1033 | 1344 | fillColor, |
| 1034 | 1345 | invertedBar && forPrint, |
| 1346 | + baseline, | |
| 1035 | 1347 | ) |
| 1036 | 1348 | }) |
| 1349 | + if (typeof (ctx as any).setTextBaseline === 'function') { | |
| 1350 | + ;(ctx as any).setTextBaseline('alphabetic') | |
| 1351 | + } | |
| 1037 | 1352 | ctx.setTextAlign('left') |
| 1038 | 1353 | if (typeof anyCtx.restore === 'function') anyCtx.restore() |
| 1039 | 1354 | } | ... | ... |
美国版/Food Labeling Management App UniApp/src/utils/nutritionFactsLayout.ts
| ... | ... | @@ -14,12 +14,12 @@ export type NutritionLayoutRowDef = { |
| 14 | 14 | |
| 15 | 15 | export const NUTRITION_FACTS_LAYOUT_ROWS: readonly NutritionLayoutRowDef[] = [ |
| 16 | 16 | { key: 'fat', label: 'Total Fat', defaultUnit: 'g', labelBold: true, dividerAfter: 'double' }, |
| 17 | - { key: 'transFat', label: 'Trans Fat', defaultUnit: 'g', indent: true, dividerAfter: 'thin' }, | |
| 17 | + { key: 'transFat', label: 'Trans Fat', defaultUnit: 'g', dividerAfter: 'thin' }, | |
| 18 | 18 | { key: 'cholesterol', label: 'Cholesterol', defaultUnit: 'mg', labelBold: true, dividerAfter: 'double' }, |
| 19 | 19 | { key: 'sodium', label: 'Sodium', defaultUnit: 'mg', labelBold: true, dividerAfter: 'double' }, |
| 20 | 20 | { key: 'carbs', label: 'Total Carbo.', defaultUnit: 'g', labelBold: true, dividerAfter: 'double' }, |
| 21 | - { key: 'totalSugar', label: 'Sugars', defaultUnit: 'g', indent: true, dividerAfter: 'thin' }, | |
| 22 | - { key: 'dietaryFiber', label: 'Dietary Fiber', defaultUnit: 'g', indent: true, dividerAfter: 'thin' }, | |
| 21 | + { key: 'totalSugar', label: 'Sugars', defaultUnit: 'g', dividerAfter: 'thin' }, | |
| 22 | + { key: 'dietaryFiber', label: 'Dietary Fiber', defaultUnit: 'g', dividerAfter: 'thin' }, | |
| 23 | 23 | { key: 'protein', label: 'Protein', defaultUnit: 'g', labelBold: true, dividerAfter: 'double' }, |
| 24 | 24 | { key: 'calcium', label: 'Calcium', defaultUnit: 'mg', dividerAfter: 'thin' }, |
| 25 | 25 | { key: 'potassium', label: 'Potassium', defaultUnit: 'mg', dividerAfter: 'thin' }, | ... | ... |
美国版/Food Labeling Management App UniApp/src/utils/print/bleWriteModeRules.ts
| 1 | 1 | /** |
| 2 | - * 部分标签机 BLE 串口:GATT 同时声明 write / writeNoResponse,但数据口实际只接受 Write Command(无响应)。 | |
| 3 | - * 用默认「带响应写」时常见首包过、第二包起 writeBLECharacteristicValue:fail property not support (10007)。 | |
| 4 | - * printerManager 在「同时声明两种属性」时对本白名单 UUID 优先选 writeNoResponse;若系统只暴露 write,则绝不强行 Command 写(否则 10007)。 | |
| 5 | - * sendViaBle 在已选无响应写仍 10007 时,对白名单禁止翻到带响应写(避免佳博长任务第二包必挂)。 | |
| 2 | + * 佳博 GP-D320FX 等 Nordic UART 数据口:实机必须 writeNoResponse 才能稳定长任务下发。 | |
| 3 | + * 安卓 GATT 常只声明 write,若按 GATT 走带响应写,可能首包或跑十几包后 10007 property not support。 | |
| 4 | + * 白名单 UUID 连接/打印时一律优先 writeNoResponse;首包仍 10007 时才允许试一次带响应写。 | |
| 5 | + * 已用 writeNoResponse 跑通后禁止切回带响应写。 | |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | export function normalizeBleUuid (uuid: string): string { |
| ... | ... | @@ -11,16 +11,25 @@ export function normalizeBleUuid (uuid: string): string { |
| 11 | 11 | |
| 12 | 12 | /** serviceUuid + characteristicUuid(无横线小写) */ |
| 13 | 13 | const FORCE_WRITE_NO_RESPONSE: Array<{ service: string; characteristic: string }> = [ |
| 14 | - /** 佳博 GP-D320FX 等常见 Nordic UART 风格串口(与你机子日志一致) */ | |
| 14 | + /** 佳博 GP-D320FX 等 Silicon Labs / 旧 Nordic UART */ | |
| 15 | 15 | { |
| 16 | 16 | service: '49535343fe7d4ae58fa99fafd205e455', |
| 17 | 17 | characteristic: '49535343884143f4a8d4ecbe34729bb3', |
| 18 | 18 | }, |
| 19 | + /** 标准 Nordic UART (NUS) */ | |
| 20 | + { | |
| 21 | + service: '6e400001b5a3f393e0a9e50e24dcca9e', | |
| 22 | + characteristic: '6e400002b5a3f393e0a9e50e24dcca9e', | |
| 23 | + }, | |
| 19 | 24 | ] |
| 20 | 25 | |
| 26 | +const NORDIC_UART_SERVICE_IDS = new Set( | |
| 27 | + FORCE_WRITE_NO_RESPONSE.map((p) => p.service), | |
| 28 | +) | |
| 29 | + | |
| 21 | 30 | export function blePairRequiresWriteNoResponse ( |
| 22 | 31 | serviceId: string, |
| 23 | - characteristicId: string | |
| 32 | + characteristicId: string, | |
| 24 | 33 | ): boolean { |
| 25 | 34 | const s = normalizeBleUuid(serviceId) |
| 26 | 35 | const c = normalizeBleUuid(characteristicId) |
| ... | ... | @@ -29,6 +38,5 @@ export function blePairRequiresWriteNoResponse ( |
| 29 | 38 | |
| 30 | 39 | /** 是否为本仓库维护的 Nordic UART 风格串口服务(需写前开 notify、分包间留间隔等) */ |
| 31 | 40 | export function isNordicUartStyleBleService (serviceId: string): boolean { |
| 32 | - const s = normalizeBleUuid(serviceId) | |
| 33 | - return FORCE_WRITE_NO_RESPONSE.some((p) => p.service === s) | |
| 41 | + return NORDIC_UART_SERVICE_IDS.has(normalizeBleUuid(serviceId)) | |
| 34 | 42 | } | ... | ... |
美国版/Food Labeling Management App UniApp/src/utils/print/manager/printerManager.ts
| 1 | -import { blePairRequiresWriteNoResponse } from '../bleWriteModeRules' | |
| 2 | 1 | import { |
| 3 | 2 | clearPrinter, |
| 3 | + discoverBleWriteCharacteristic, | |
| 4 | 4 | ensureBleUartNotifyIfNeeded, |
| 5 | 5 | getBluetoothConnection, |
| 6 | 6 | getCurrentPrinterDriverKey, |
| ... | ... | @@ -175,93 +175,6 @@ function connectClassicBluetooth (device: PrinterCandidate, driver: PrinterDrive |
| 175 | 175 | }) |
| 176 | 176 | } |
| 177 | 177 | |
| 178 | -/** | |
| 179 | - * 优先带响应 write(与 uni 默认写入方式一致);仅当没有 write 再用 writeNoResponse(需在下发时传 writeType)。 | |
| 180 | - * 若系统 GATT 只声明 write、未声明 writeNoResponse,却强行 writeNoResponse,会报 property not support (10007)。 | |
| 181 | - */ | |
| 182 | -function hasBleWriteProperty (item: any): boolean { | |
| 183 | - const w = item.properties?.write | |
| 184 | - return w === true || w === 'true' | |
| 185 | -} | |
| 186 | - | |
| 187 | -function hasBleWriteNoResponseProperty (item: any): boolean { | |
| 188 | - const p = item.properties || {} | |
| 189 | - return ( | |
| 190 | - p.writeNoResponse === true || | |
| 191 | - p.writeNoResponse === 'true' || | |
| 192 | - p.writeWithoutResponse === true || | |
| 193 | - p.writeWithoutResponse === 'true' | |
| 194 | - ) | |
| 195 | -} | |
| 196 | - | |
| 197 | -/** | |
| 198 | - * 无 writeNoResponse 属性则绝不走 Command 写。 | |
| 199 | - * Nordic 白名单在「同时声明 write + writeNoResponse」时优先无响应写(佳博等实测);否则有 write 时优先带响应。 | |
| 200 | - */ | |
| 201 | -function pickBleWriteUsesNoResponse (serviceId: string, item: any): boolean { | |
| 202 | - const hw = hasBleWriteProperty(item) | |
| 203 | - const hn = hasBleWriteNoResponseProperty(item) | |
| 204 | - if (!hn) return false | |
| 205 | - if (!hw) return true | |
| 206 | - return blePairRequiresWriteNoResponse(serviceId, String(item.uuid || '')) | |
| 207 | -} | |
| 208 | - | |
| 209 | -function findBleWriteCharacteristic (deviceId: string): Promise<{ | |
| 210 | - serviceId: string | |
| 211 | - characteristicId: string | |
| 212 | - bleWriteUsesNoResponse: boolean | |
| 213 | -} | null> { | |
| 214 | - return new Promise((resolve) => { | |
| 215 | - uni.getBLEDeviceServices({ | |
| 216 | - deviceId, | |
| 217 | - success: (serviceRes) => { | |
| 218 | - const services = serviceRes.services || [] | |
| 219 | - const next = (index: number) => { | |
| 220 | - if (index >= services.length) { | |
| 221 | - resolve(null) | |
| 222 | - return | |
| 223 | - } | |
| 224 | - const serviceId = services[index].uuid | |
| 225 | - uni.getBLEDeviceCharacteristics({ | |
| 226 | - deviceId, | |
| 227 | - serviceId, | |
| 228 | - success: (charRes) => { | |
| 229 | - const chars = charRes.characteristics || [] | |
| 230 | - const writable = (item: any) => hasBleWriteProperty(item) || hasBleWriteNoResponseProperty(item) | |
| 231 | - for (const item of chars) { | |
| 232 | - const cid = String(item.uuid || '') | |
| 233 | - if (blePairRequiresWriteNoResponse(serviceId, cid) && writable(item)) { | |
| 234 | - resolve({ | |
| 235 | - serviceId, | |
| 236 | - characteristicId: cid, | |
| 237 | - bleWriteUsesNoResponse: pickBleWriteUsesNoResponse(serviceId, item), | |
| 238 | - }) | |
| 239 | - return | |
| 240 | - } | |
| 241 | - } | |
| 242 | - const withResp = chars.find(hasBleWriteProperty) | |
| 243 | - const noResp = chars.find(hasBleWriteNoResponseProperty) | |
| 244 | - const target = withResp || noResp | |
| 245 | - if (target) { | |
| 246 | - resolve({ | |
| 247 | - serviceId, | |
| 248 | - characteristicId: String(target.uuid || ''), | |
| 249 | - bleWriteUsesNoResponse: pickBleWriteUsesNoResponse(serviceId, target), | |
| 250 | - }) | |
| 251 | - return | |
| 252 | - } | |
| 253 | - next(index + 1) | |
| 254 | - }, | |
| 255 | - fail: () => next(index + 1), | |
| 256 | - }) | |
| 257 | - } | |
| 258 | - next(0) | |
| 259 | - }, | |
| 260 | - fail: () => resolve(null), | |
| 261 | - }) | |
| 262 | - }) | |
| 263 | -} | |
| 264 | - | |
| 265 | 178 | function requestBleMtu (deviceId: string, preferredMtu: number): Promise<number> { |
| 266 | 179 | return new Promise((resolve) => { |
| 267 | 180 | const targetMtu = Math.max(20, Math.min(512, Math.round(preferredMtu || 20))) |
| ... | ... | @@ -288,7 +201,7 @@ function requestBleMtu (deviceId: string, preferredMtu: number): Promise<number> |
| 288 | 201 | |
| 289 | 202 | function connectBlePrinter (device: PrinterCandidate, driver: PrinterDriver): Promise<void> { |
| 290 | 203 | const finalizeExistingBleConnection = async () => { |
| 291 | - const write = await findBleWriteCharacteristic(device.deviceId) | |
| 204 | + const write = await discoverBleWriteCharacteristic(device.deviceId) | |
| 292 | 205 | if (!write) { |
| 293 | 206 | throw new Error('No writable characteristic found. This device may not support printing.') |
| 294 | 207 | } | ... | ... |
美国版/Food Labeling Management App UniApp/src/utils/print/nativeTemplateElementSupport.ts
| ... | ... | @@ -9,7 +9,11 @@ import type { |
| 9 | 9 | } from './types/printer' |
| 10 | 10 | import { formatBarcodeValueForTsc, normalizeBarcodeType } from '../barcodeFormat' |
| 11 | 11 | import { applyTemplateData } from './templateRenderer' |
| 12 | -import { resolveElementDateTimeDisplay } from '../labelPreview/printInputOffset' | |
| 12 | +import { | |
| 13 | + isCorruptedDateDisplayFormat, | |
| 14 | + parseStoredPrintInputOffset, | |
| 15 | + resolveElementDateTimeDisplay, | |
| 16 | +} from '../labelPreview/printInputOffset' | |
| 13 | 17 | import { readInvertColors } from '../invertColorsConfig' |
| 14 | 18 | import { normalizeTemplatePrintOrientation } from '../labelPreview/normalizePreviewTemplate' |
| 15 | 19 | import { |
| ... | ... | @@ -100,6 +104,17 @@ function prepareBarcodeElementForNativePrint (el: SystemTemplateElementBase): Sy |
| 100 | 104 | return { ...el, config: cfg } |
| 101 | 105 | } |
| 102 | 106 | |
| 107 | +function readNativeSnapshotText (config: Record<string, any>): string { | |
| 108 | + for (const key of ['__previewFormatted', '__PreviewFormatted', 'text', 'Text'] as const) { | |
| 109 | + const raw = String(config[key] ?? '').trim() | |
| 110 | + if (!raw) continue | |
| 111 | + if (parseStoredPrintInputOffset(raw)) continue | |
| 112 | + if (isCorruptedDateDisplayFormat(raw)) continue | |
| 113 | + return raw | |
| 114 | + } | |
| 115 | + return '' | |
| 116 | +} | |
| 117 | + | |
| 103 | 118 | /** |
| 104 | 119 | * 将 WEIGHT / DATE / TIME / DURATION 转为 TEXT_STATIC(展示文案与合并后的 config.text 一致), |
| 105 | 120 | * LOGO → IMAGE,使同一套模板可走 native printTemplate,避免仅因元素类型名而整页光栅(进度长期停在 ~12–14%)。 |
| ... | ... | @@ -121,9 +136,14 @@ export function normalizeTemplateForNativeFastJob ( |
| 121 | 136 | if (type === 'WEIGHT' || type === 'DATE' || type === 'TIME' || type === 'DURATION') { |
| 122 | 137 | let text = '' |
| 123 | 138 | if (type === 'DATE' || type === 'TIME' || type === 'DURATION') { |
| 124 | - const live = resolveElementDateTimeDisplay(el, now) | |
| 125 | - if (live != null && live.trim()) { | |
| 126 | - text = applyTemplateData(live, data) | |
| 139 | + const snap = readNativeSnapshotText(config) | |
| 140 | + if (snap) { | |
| 141 | + text = applyTemplateData(snap, data) | |
| 142 | + } else { | |
| 143 | + const live = resolveElementDateTimeDisplay(el, now) | |
| 144 | + if (live != null && live.trim()) { | |
| 145 | + text = applyTemplateData(live, data) | |
| 146 | + } | |
| 127 | 147 | } |
| 128 | 148 | } else if (type === 'WEIGHT') { |
| 129 | 149 | let raw = String(config.text ?? config.Text ?? '').trim() | ... | ... |
美国版/Food Labeling Management App UniApp/src/utils/print/printerConnection.ts
| ... | ... | @@ -3,7 +3,7 @@ |
| 3 | 3 | */ |
| 4 | 4 | import type { ActiveBtDeviceType, PrinterType } from './types/printer' |
| 5 | 5 | import classicBluetooth from './bluetoothTool.js' |
| 6 | -import { getDeviceFingerprint } from '../deviceInfo' | |
| 6 | +import { getDeviceFingerprint, isAoaAioDevice } from '../deviceInfo' | |
| 7 | 7 | import { |
| 8 | 8 | blePairRequiresWriteNoResponse, |
| 9 | 9 | isNordicUartStyleBleService, |
| ... | ... | @@ -44,9 +44,9 @@ const STORAGE_PRINTER_DEBUG_LOG = 'printerDebugLog' // '1' | '0' |
| 44 | 44 | const BUILTIN_PROBE_PORTS = [9100, 4000, 9000, 6000] |
| 45 | 45 | const BUILTIN_PRINTER_DEVICE_KEYWORDS: string[] = [ |
| 46 | 46 | // 在这里补充需要走 Built-in 的设备型号关键字(小写匹配) |
| 47 | - // 例如:'desktop-aio', 'pos-terminal-x1' | |
| 48 | 47 | 'rk3568', |
| 49 | 48 | 'aoa_rk3568', |
| 49 | + 'aoa', | |
| 50 | 50 | ] |
| 51 | 51 | export type BtDeviceType = ActiveBtDeviceType |
| 52 | 52 | |
| ... | ... | @@ -277,6 +277,153 @@ export async function logPrinterDebug (options?: { |
| 277 | 277 | |
| 278 | 278 | const BLE_MTU_DEFAULT = 20 |
| 279 | 279 | |
| 280 | +function hasBleWriteProperty (item: any): boolean { | |
| 281 | + const w = item.properties?.write | |
| 282 | + return w === true || w === 'true' | |
| 283 | +} | |
| 284 | + | |
| 285 | +function hasBleWriteNoResponseProperty (item: any): boolean { | |
| 286 | + const p = item.properties || {} | |
| 287 | + return ( | |
| 288 | + p.writeNoResponse === true || | |
| 289 | + p.writeNoResponse === 'true' || | |
| 290 | + p.writeWithoutResponse === true || | |
| 291 | + p.writeWithoutResponse === 'true' | |
| 292 | + ) | |
| 293 | +} | |
| 294 | + | |
| 295 | +/** | |
| 296 | + * Nordic 白名单:GATT 可能只报 write,但机芯数据口仍须 writeNoResponse(长任务带响应写易 mid-job 10007)。 | |
| 297 | + * 非白名单:按 GATT 属性选择。 | |
| 298 | + */ | |
| 299 | +function pickBleWriteUsesNoResponse (serviceId: string, item: any): boolean { | |
| 300 | + const cid = String(item.uuid || '') | |
| 301 | + const hw = hasBleWriteProperty(item) | |
| 302 | + const hn = hasBleWriteNoResponseProperty(item) | |
| 303 | + if (blePairRequiresWriteNoResponse(serviceId, cid)) { | |
| 304 | + return hw || hn | |
| 305 | + } | |
| 306 | + if (!hn) return false | |
| 307 | + if (!hw) return true | |
| 308 | + return false | |
| 309 | +} | |
| 310 | + | |
| 311 | +function listBleCharacteristics ( | |
| 312 | + deviceId: string, | |
| 313 | + serviceId: string, | |
| 314 | +): Promise<Array<{ uuid?: string; properties?: Record<string, unknown> }>> { | |
| 315 | + return new Promise((resolve) => { | |
| 316 | + uni.getBLEDeviceCharacteristics({ | |
| 317 | + deviceId, | |
| 318 | + serviceId, | |
| 319 | + success: (charRes) => resolve((charRes.characteristics || []) as Array<{ uuid?: string; properties?: Record<string, unknown> }>), | |
| 320 | + fail: () => resolve([]), | |
| 321 | + }) | |
| 322 | + }) | |
| 323 | +} | |
| 324 | + | |
| 325 | +function resolveBleWriteTargetFromChars ( | |
| 326 | + serviceId: string, | |
| 327 | + chars: Array<{ uuid?: string; properties?: Record<string, unknown> }>, | |
| 328 | +): { serviceId: string; characteristicId: string; bleWriteUsesNoResponse: boolean } | null { | |
| 329 | + const writable = (item: { properties?: Record<string, unknown> }) => | |
| 330 | + hasBleWriteProperty(item) || hasBleWriteNoResponseProperty(item) | |
| 331 | + for (const item of chars) { | |
| 332 | + const cid = String(item.uuid || '') | |
| 333 | + if (blePairRequiresWriteNoResponse(serviceId, cid) && writable(item)) { | |
| 334 | + return { | |
| 335 | + serviceId, | |
| 336 | + characteristicId: cid, | |
| 337 | + bleWriteUsesNoResponse: pickBleWriteUsesNoResponse(serviceId, item), | |
| 338 | + } | |
| 339 | + } | |
| 340 | + } | |
| 341 | + return null | |
| 342 | +} | |
| 343 | + | |
| 344 | +function resolveBleGenericWriteFromChars ( | |
| 345 | + serviceId: string, | |
| 346 | + chars: Array<{ uuid?: string; properties?: Record<string, unknown> }>, | |
| 347 | +): { serviceId: string; characteristicId: string; bleWriteUsesNoResponse: boolean } | null { | |
| 348 | + const withResp = chars.find(hasBleWriteProperty) | |
| 349 | + const noResp = chars.find(hasBleWriteNoResponseProperty) | |
| 350 | + const target = withResp || noResp | |
| 351 | + if (!target) return null | |
| 352 | + return { | |
| 353 | + serviceId, | |
| 354 | + characteristicId: String(target.uuid || ''), | |
| 355 | + bleWriteUsesNoResponse: pickBleWriteUsesNoResponse(serviceId, target), | |
| 356 | + } | |
| 357 | +} | |
| 358 | + | |
| 359 | +/** 连接/打印前重新发现可写特征,避免 storage 里仍是错误写入方式 */ | |
| 360 | +export function discoverBleWriteCharacteristic (deviceId: string): Promise<{ | |
| 361 | + serviceId: string | |
| 362 | + characteristicId: string | |
| 363 | + bleWriteUsesNoResponse: boolean | |
| 364 | +} | null> { | |
| 365 | + return new Promise((resolve) => { | |
| 366 | + uni.getBLEDeviceServices({ | |
| 367 | + deviceId, | |
| 368 | + success: async (serviceRes) => { | |
| 369 | + const services = serviceRes.services || [] | |
| 370 | + try { | |
| 371 | + /** 第一轮:只认 Nordic 打印串口,避免误选其它服务的可写特征导致 property not support */ | |
| 372 | + for (const svc of services) { | |
| 373 | + const serviceId = String(svc.uuid || '') | |
| 374 | + const chars = await listBleCharacteristics(deviceId, serviceId) | |
| 375 | + const nordic = resolveBleWriteTargetFromChars(serviceId, chars) | |
| 376 | + if (nordic) { | |
| 377 | + console.log('[BLE] discover: nordic uart', nordic) | |
| 378 | + resolve(nordic) | |
| 379 | + return | |
| 380 | + } | |
| 381 | + } | |
| 382 | + for (const svc of services) { | |
| 383 | + const serviceId = String(svc.uuid || '') | |
| 384 | + const chars = await listBleCharacteristics(deviceId, serviceId) | |
| 385 | + const generic = resolveBleGenericWriteFromChars(serviceId, chars) | |
| 386 | + if (generic) { | |
| 387 | + console.log('[BLE] discover: generic writable', generic) | |
| 388 | + resolve(generic) | |
| 389 | + return | |
| 390 | + } | |
| 391 | + } | |
| 392 | + resolve(null) | |
| 393 | + } catch { | |
| 394 | + resolve(null) | |
| 395 | + } | |
| 396 | + }, | |
| 397 | + fail: () => resolve(null), | |
| 398 | + }) | |
| 399 | + }) | |
| 400 | +} | |
| 401 | + | |
| 402 | +async function refreshBlePrintTarget (deviceId: string, driverKey: string): Promise<{ | |
| 403 | + serviceId: string | |
| 404 | + characteristicId: string | |
| 405 | + bleWriteUsesNoResponse: boolean | |
| 406 | +} | null> { | |
| 407 | + const write = await discoverBleWriteCharacteristic(deviceId) | |
| 408 | + if (!write) return null | |
| 409 | + const driver = getPrinterDriverByKey(driverKey) | |
| 410 | + await ensureBleUartNotifyIfNeeded(deviceId, write.serviceId, write.characteristicId) | |
| 411 | + const negotiatedMtu = await requestBleMtuNegotiation(deviceId, driver.preferredBleMtu || BLE_MTU_DEFAULT) | |
| 412 | + setBluetoothConnection({ | |
| 413 | + deviceId, | |
| 414 | + deviceName: uni.getStorageSync(STORAGE_BT_DEVICE_NAME) || 'Bluetooth Printer', | |
| 415 | + serviceId: write.serviceId, | |
| 416 | + characteristicId: write.characteristicId, | |
| 417 | + deviceType: 'ble', | |
| 418 | + mtu: negotiatedMtu, | |
| 419 | + driverKey, | |
| 420 | + bleWriteUsesNoResponse: blePairRequiresWriteNoResponse(write.serviceId, write.characteristicId) | |
| 421 | + ? true | |
| 422 | + : write.bleWriteUsesNoResponse, | |
| 423 | + }) | |
| 424 | + return write | |
| 425 | +} | |
| 426 | + | |
| 280 | 427 | export function getPrinterType (): PrinterType | '' { |
| 281 | 428 | const type = (uni.getStorageSync(STORAGE_PRINTER_TYPE) as PrinterType) || '' |
| 282 | 429 | if (!type) return '' |
| ... | ... | @@ -403,6 +550,8 @@ export function isBuiltinPrinterAvailable (): boolean { |
| 403 | 550 | // #endif |
| 404 | 551 | } |
| 405 | 552 | |
| 553 | +export { isAoaAioDevice } from '../deviceInfo' | |
| 554 | + | |
| 406 | 555 | export function isBuiltinPrinterEnabledByDeviceModel (): boolean { |
| 407 | 556 | const fingerprint = getDeviceFingerprint() |
| 408 | 557 | if (!fingerprint) return false |
| ... | ... | @@ -746,7 +895,10 @@ function sendViaBle ( |
| 746 | 895 | if (!conn) { |
| 747 | 896 | return Promise.reject(new Error('Bluetooth printer not connected.')) |
| 748 | 897 | } |
| 749 | - const { deviceId, serviceId, characteristicId, bleWriteUsesNoResponse } = conn | |
| 898 | + const deviceId = conn.deviceId | |
| 899 | + let serviceId = conn.serviceId | |
| 900 | + let characteristicId = conn.characteristicId | |
| 901 | + let bleWriteUsesNoResponse = conn.bleWriteUsesNoResponse | |
| 750 | 902 | |
| 751 | 903 | const runWritesWithPayloadSize = (payloadSize: number): Promise<void> => { |
| 752 | 904 | const chunks: number[][] = [] |
| ... | ... | @@ -768,11 +920,10 @@ function sendViaBle ( |
| 768 | 920 | ? 2 |
| 769 | 921 | : 10 |
| 770 | 922 | |
| 771 | - /** Nordic 白名单:仅用于失败时是否禁止翻到带响应写(佳博等);写入方式以连接时写入 storage 的 bleWriteUsesNoResponse 为准 */ | |
| 923 | + /** 佳博 Nordic 白名单:无视 storage 里旧的 false,长任务必须 writeNoResponse */ | |
| 772 | 924 | const blePairForceNoResponse = blePairRequiresWriteNoResponse(serviceId, characteristicId) |
| 773 | 925 | |
| 774 | - /** 本 job 内若因 property not support 翻过模式,后续包统一用 effectiveUseNoResp */ | |
| 775 | - let effectiveUseNoResp = bleWriteUsesNoResponse | |
| 926 | + let effectiveUseNoResp = blePairForceNoResponse ? true : bleWriteUsesNoResponse | |
| 776 | 927 | let hasFlippedWriteModeThisJob = false |
| 777 | 928 | let pendingPersistUseNoResp: boolean | null = null |
| 778 | 929 | |
| ... | ... | @@ -798,7 +949,7 @@ function sendViaBle ( |
| 798 | 949 | errCode: err?.errCode, |
| 799 | 950 | errno: err?.errno, |
| 800 | 951 | code: err?.code, |
| 801 | - writeTypeUsed: useNoResp ? 'writeNoResponse' : '(omitted, default write)', | |
| 952 | + writeTypeUsed: useNoResp ? 'writeNoResponse' : 'write', | |
| 802 | 953 | effectiveUseNoResp, |
| 803 | 954 | bleWriteUsesNoResponseSaved: bleWriteUsesNoResponse, |
| 804 | 955 | bufferLen, |
| ... | ... | @@ -822,10 +973,8 @@ function sendViaBle ( |
| 822 | 973 | characteristicId, |
| 823 | 974 | value: buffer, |
| 824 | 975 | } |
| 825 | - /** 仅无响应写显式声明;带响应写不传 writeType,与 uni 历史默认一致,避免部分机型报 property not support */ | |
| 826 | - if (useNoResp) { | |
| 827 | - opts.writeType = 'writeNoResponse' | |
| 828 | - } | |
| 976 | + /** 无响应写 / 带响应写均显式声明 writeType,兼容新版 Android BLE 栈 */ | |
| 977 | + opts.writeType = useNoResp ? 'writeNoResponse' : 'write' | |
| 829 | 978 | uni.writeBLECharacteristicValue({ |
| 830 | 979 | ...opts, |
| 831 | 980 | success: () => { |
| ... | ... | @@ -847,27 +996,37 @@ function sendViaBle ( |
| 847 | 996 | msg.includes('property not support') || |
| 848 | 997 | msg.includes('not support') || |
| 849 | 998 | String(err?.errCode) === '10007' |
| 850 | - if (allowFlip && !hasFlippedWriteModeThisJob && notSupport) { | |
| 999 | + if (notSupport) { | |
| 851 | 1000 | const nextUseNoResp = !useNoResp |
| 852 | - /** | |
| 853 | - * 佳博等 Nordic 串口:GATT 虽声明 write,实测只接受 writeNoResponse。 | |
| 854 | - * 若 writeNoResponse 偶发失败后翻到「默认写」,首包可能仍 success,从第二包起必现 10007(打印量变长更易触发)。 | |
| 855 | - */ | |
| 856 | - if (blePairForceNoResponse && !nextUseNoResp) { | |
| 1001 | + /** 白名单 mid-job:带响应写跑若干包后 10007 → 立即改 writeNoResponse 重试本包 */ | |
| 1002 | + if (blePairForceNoResponse && !useNoResp && nextUseNoResp) { | |
| 1003 | + effectiveUseNoResp = true | |
| 1004 | + pendingPersistUseNoResp = true | |
| 1005 | + console.warn('[sendViaBle] 白名单串口 mid-job write→writeNoResponse 重试', { | |
| 1006 | + sentIndex: sent, | |
| 1007 | + totalChunks: total, | |
| 1008 | + }) | |
| 1009 | + tryWrite(true, false) | |
| 1010 | + return | |
| 1011 | + } | |
| 1012 | + /** 白名单已用 writeNoResponse 跑通后,禁止切到带响应写 */ | |
| 1013 | + if (blePairForceNoResponse && !nextUseNoResp && sent > 0) { | |
| 857 | 1014 | console.warn( |
| 858 | - '[sendViaBle] 白名单串口禁止切到带响应写;请保持 writeNoResponse 或检查连接/MTU' | |
| 1015 | + '[sendViaBle] 白名单串口已发包后禁止切到带响应写;请保持 writeNoResponse' | |
| 859 | 1016 | ) |
| 860 | 1017 | reject(new Error(msg || 'BLE write failed')) |
| 861 | 1018 | return |
| 862 | 1019 | } |
| 863 | - hasFlippedWriteModeThisJob = true | |
| 864 | - effectiveUseNoResp = nextUseNoResp | |
| 865 | - pendingPersistUseNoResp = effectiveUseNoResp | |
| 866 | - console.warn('[sendViaBle] property not support → 切换写入方式重试本包', { | |
| 867 | - nextMode: effectiveUseNoResp ? 'writeNoResponse' : 'defaultWrite(no writeType)', | |
| 868 | - }) | |
| 869 | - tryWrite(effectiveUseNoResp, false) | |
| 870 | - return | |
| 1020 | + if (allowFlip && !hasFlippedWriteModeThisJob && sent === 0) { | |
| 1021 | + hasFlippedWriteModeThisJob = true | |
| 1022 | + effectiveUseNoResp = nextUseNoResp | |
| 1023 | + pendingPersistUseNoResp = effectiveUseNoResp | |
| 1024 | + console.warn('[sendViaBle] property not support → 首包切换写入方式重试', { | |
| 1025 | + nextMode: effectiveUseNoResp ? 'writeNoResponse' : 'write', | |
| 1026 | + }) | |
| 1027 | + tryWrite(effectiveUseNoResp, false) | |
| 1028 | + return | |
| 1029 | + } | |
| 871 | 1030 | } |
| 872 | 1031 | reject(new Error(msg || 'BLE write failed')) |
| 873 | 1032 | }, |
| ... | ... | @@ -937,14 +1096,23 @@ function sendViaBle ( |
| 937 | 1096 | |
| 938 | 1097 | // #ifdef APP-PLUS |
| 939 | 1098 | if (conn.deviceType === 'ble') { |
| 940 | - const driver = getPrinterDriverByKey(getCurrentPrinterDriverKey()) | |
| 941 | - const preferred = driver.preferredBleMtu || BLE_MTU_DEFAULT | |
| 1099 | + const driverKey = getCurrentPrinterDriverKey() | |
| 1100 | + const driver = getPrinterDriverByKey(driverKey) | |
| 1101 | + const nordicLikely = isNordicUartStyleBleService(serviceId) | |
| 942 | 1102 | return bleOpenAdapter() |
| 943 | 1103 | .then(() => bleEnsureDeviceConnected(deviceId)) |
| 944 | - .then(() => new Promise<void>((r) => setTimeout(r, 100))) | |
| 945 | - .then(() => ensureBleUartNotifyIfNeeded(deviceId, serviceId, characteristicId)) | |
| 946 | - .then(() => requestBleMtuNegotiation(deviceId, preferred)) | |
| 947 | - .then((negotiated) => runWritesWithPayloadSize(mtuToPayloadSize(negotiated))) | |
| 1104 | + .then(() => new Promise<void>((r) => setTimeout(r, nordicLikely ? 200 : 100))) | |
| 1105 | + .then(() => refreshBlePrintTarget(deviceId, driverKey || driver.key)) | |
| 1106 | + .then(() => { | |
| 1107 | + const latest = getBluetoothConnection() | |
| 1108 | + if (latest) { | |
| 1109 | + serviceId = latest.serviceId | |
| 1110 | + characteristicId = latest.characteristicId | |
| 1111 | + bleWriteUsesNoResponse = latest.bleWriteUsesNoResponse | |
| 1112 | + } | |
| 1113 | + const mtu = latest?.mtu || driver.preferredBleMtu || BLE_MTU_DEFAULT | |
| 1114 | + return runWritesWithPayloadSize(mtuToPayloadSize(mtu)) | |
| 1115 | + }) | |
| 948 | 1116 | } |
| 949 | 1117 | // #endif |
| 950 | 1118 | return runWritesWithPayloadSize(mtuToPayloadSize(conn.mtu || BLE_MTU_DEFAULT)) | ... | ... |
美国版/Food Labeling Management App UniApp/src/utils/print/systemTemplateAdapter.ts
| ... | ... | @@ -6,6 +6,10 @@ import { |
| 6 | 6 | shouldRasterizeTextElement, |
| 7 | 7 | } from './nativeBitmapPatch' |
| 8 | 8 | import { applyTemplateData } from './templateRenderer' |
| 9 | +import { | |
| 10 | + isPrintInputLiteralField, | |
| 11 | + resolvePrintInputLiteralDisplay, | |
| 12 | +} from '../labelPreview/printInputOffset' | |
| 9 | 13 | import { readElementBorder } from '../textElementLayout' |
| 10 | 14 | import type { |
| 11 | 15 | EscTemplateItem, |
| ... | ... | @@ -172,6 +176,9 @@ function resolveElementText ( |
| 172 | 176 | element: SystemTemplateElementBase, |
| 173 | 177 | data: LabelTemplateData |
| 174 | 178 | ): string { |
| 179 | + if (isPrintInputLiteralField(element)) { | |
| 180 | + return resolvePrintInputLiteralDisplay(element) | |
| 181 | + } | |
| 175 | 182 | const config = element.config || {} |
| 176 | 183 | const type = String(element.type || '').toUpperCase() |
| 177 | 184 | const hasText = config.text != null && config.text !== '' | ... | ... |
美国版/Food Labeling Management App UniApp/src/utils/printFromPrintDataList.ts
| ... | ... | @@ -4,6 +4,12 @@ import { |
| 4 | 4 | sortElementsForPreview, |
| 5 | 5 | } from './labelPreview/normalizePreviewTemplate' |
| 6 | 6 | import { |
| 7 | + isCorruptedDateDisplayFormat, | |
| 8 | + isWeekdayBarElement, | |
| 9 | + parseStoredPrintInputOffset, | |
| 10 | + resolveWeekdayDisplayForElement, | |
| 11 | +} from './labelPreview/printInputOffset' | |
| 12 | +import { | |
| 7 | 13 | canPrintCurrentLabelViaNativeFastJob, |
| 8 | 14 | getCurrentPrinterSummary, |
| 9 | 15 | printLabelPrintJobPayloadForCurrentPrinter, |
| ... | ... | @@ -64,11 +70,35 @@ function formatPriceLineForBake (config: Record<string, any>, rawText: string): |
| 64 | 70 | return `${prefix}${value}${suffix}` |
| 65 | 71 | } |
| 66 | 72 | |
| 73 | +function parseReprintBaseTime (row: PrintLogItemDto): Date { | |
| 74 | + const raw = String(row.printedAt ?? '').trim() | |
| 75 | + if (raw) { | |
| 76 | + const d = new Date(raw) | |
| 77 | + if (!Number.isNaN(d.getTime())) return d | |
| 78 | + } | |
| 79 | + return new Date() | |
| 80 | +} | |
| 81 | + | |
| 82 | +/** 重打快照里已算好的展示文案(优先 __previewFormatted,其次 config.text) */ | |
| 83 | +function readReprintSnapshotLine (cfg: Record<string, any>): string { | |
| 84 | + for (const key of ['__previewFormatted', '__PreviewFormatted', 'text', 'Text'] as const) { | |
| 85 | + const raw = String(cfg[key] ?? '').trim() | |
| 86 | + if (!raw) continue | |
| 87 | + if (parseStoredPrintInputOffset(raw)) continue | |
| 88 | + if (isCorruptedDateDisplayFormat(raw)) continue | |
| 89 | + return raw | |
| 90 | + } | |
| 91 | + return '' | |
| 92 | +} | |
| 93 | + | |
| 67 | 94 | /** |
| 68 | 95 | * 原生快打插件对 TEXT_PRODUCT / TEXT_PRICE / DATE 等仍会按类型做绑定;重打时 dataJson 为空, |
| 69 | 96 | * 必须把接口/落库快照里已填好的展示值焙成 TEXT_STATIC,否则会出现第二行商品名、价格 0、日期成 format 等问题。 |
| 70 | 97 | */ |
| 71 | -export function bakeReprintTemplateSnapshot (tmpl: SystemLabelTemplate): SystemLabelTemplate { | |
| 98 | +export function bakeReprintTemplateSnapshot ( | |
| 99 | + tmpl: SystemLabelTemplate, | |
| 100 | + reprintBaseTime: Date = new Date(), | |
| 101 | +): SystemLabelTemplate { | |
| 72 | 102 | const elements = (tmpl.elements || []).map((el) => { |
| 73 | 103 | const vst = String(el.valueSourceType || '').toUpperCase() |
| 74 | 104 | const type = String(el.type || '').toUpperCase() |
| ... | ... | @@ -78,9 +108,47 @@ export function bakeReprintTemplateSnapshot (tmpl: SystemLabelTemplate): SystemL |
| 78 | 108 | ...el, |
| 79 | 109 | type: 'TEXT_STATIC', |
| 80 | 110 | valueSourceType: 'FIXED', |
| 81 | - config: { ...cfg, text: line, Text: line }, | |
| 111 | + config: { | |
| 112 | + ...cfg, | |
| 113 | + text: line, | |
| 114 | + Text: line, | |
| 115 | + __previewFormatted: line, | |
| 116 | + __PreviewFormatted: line, | |
| 117 | + }, | |
| 82 | 118 | }) |
| 83 | 119 | |
| 120 | + /** 黑条星期:快照有 FRIDAY 等则冻结;仅有偏移 JSON 时按原打印时刻 + 偏移还原 */ | |
| 121 | + if (isWeekdayBarElement({ ...el, config: cfg })) { | |
| 122 | + let line = readReprintSnapshotLine(cfg) | |
| 123 | + if (!line) { | |
| 124 | + try { | |
| 125 | + const live = resolveWeekdayDisplayForElement({ ...el, config: cfg }, reprintBaseTime) | |
| 126 | + if (live && !isCorruptedDateDisplayFormat(live)) line = live | |
| 127 | + } catch { | |
| 128 | + /* ignore */ | |
| 129 | + } | |
| 130 | + } | |
| 131 | + if (line) return toStatic(line) | |
| 132 | + } | |
| 133 | + | |
| 134 | + /** 快照里 DATE/TIME/DURATION 的 config.text 已是当时出纸值;重打禁止再按 now 重算 */ | |
| 135 | + if (type === 'DATE' || type === 'TIME' || type === 'DURATION') { | |
| 136 | + const line = readReprintSnapshotLine(cfg) | |
| 137 | + if (line) return toStatic(line) | |
| 138 | + } | |
| 139 | + | |
| 140 | + if (type === 'WEIGHT') { | |
| 141 | + const line = | |
| 142 | + readReprintSnapshotLine(cfg) | |
| 143 | + || (() => { | |
| 144 | + const v = String(cfg.value ?? cfg.Value ?? '').trim() | |
| 145 | + const u = String(cfg.unit ?? cfg.Unit ?? '').trim() | |
| 146 | + if (v && u) return v.endsWith(u) ? v : `${v}${u}` | |
| 147 | + return v || u | |
| 148 | + })() | |
| 149 | + if (line) return toStatic(line) | |
| 150 | + } | |
| 151 | + | |
| 84 | 152 | if (vst === 'FIXED') { |
| 85 | 153 | if (type === 'TEXT_PRODUCT' || type === 'TEXT_CATEGORY' || type === 'TEXT_LABEL_ID') { |
| 86 | 154 | const t = String(cfg.text ?? cfg.Text ?? '').trim() |
| ... | ... | @@ -101,10 +169,6 @@ export function bakeReprintTemplateSnapshot (tmpl: SystemLabelTemplate): SystemL |
| 101 | 169 | } |
| 102 | 170 | |
| 103 | 171 | if (vst === 'PRINT_INPUT') { |
| 104 | - if (type === 'DATE' || type === 'TIME' || type === 'DURATION') { | |
| 105 | - const textVal = String(cfg.text ?? cfg.Text ?? '').trim() | |
| 106 | - if (textVal) return toStatic(textVal) | |
| 107 | - } | |
| 108 | 172 | if (type === 'WEIGHT') { |
| 109 | 173 | const textVal = String(cfg.text ?? cfg.Text ?? '').trim() |
| 110 | 174 | const v = String(cfg.value ?? cfg.Value ?? '').trim() |
| ... | ... | @@ -153,7 +217,8 @@ export function bakeReprintTemplateSnapshot (tmpl: SystemLabelTemplate): SystemL |
| 153 | 217 | * normalizeLabelTemplateFromPreviewApi 只读 e.config,会得到空 config,打印全空。 |
| 154 | 218 | */ |
| 155 | 219 | function inferElementTypeFromPrintSnapshotConfig (cfg: Record<string, unknown>): string { |
| 156 | - const inputType = String(cfg.inputType ?? '').toLowerCase() | |
| 220 | + const inputType = String(cfg.inputType ?? cfg.InputType ?? '').toLowerCase() | |
| 221 | + if (inputType === 'number' || inputType === 'text') return 'TEXT_STATIC' | |
| 157 | 222 | const hasUnit = cfg.unit != null || cfg.Unit != null |
| 158 | 223 | const hasValue = cfg.value != null || cfg.Value != null |
| 159 | 224 | if (hasUnit && hasValue) return 'WEIGHT' |
| ... | ... | @@ -241,6 +306,25 @@ function elementFromPrintDataItem ( |
| 241 | 306 | base.id = item.elementId |
| 242 | 307 | base.Id = item.elementId |
| 243 | 308 | } |
| 309 | + const cfgRaw = base.config ?? base.Config ?? base.ConfigJson ?? base.configJson | |
| 310 | + let cfg: Record<string, unknown> | |
| 311 | + if (cfgRaw != null && typeof cfgRaw === 'object' && !Array.isArray(cfgRaw)) { | |
| 312 | + cfg = { ...(cfgRaw as Record<string, unknown>) } | |
| 313 | + } else if (typeof cfgRaw === 'string' && cfgRaw.trim()) { | |
| 314 | + try { | |
| 315 | + cfg = JSON.parse(cfgRaw) as Record<string, unknown> | |
| 316 | + } catch { | |
| 317 | + cfg = {} | |
| 318 | + } | |
| 319 | + } else { | |
| 320 | + cfg = {} | |
| 321 | + } | |
| 322 | + const elType = String( | |
| 323 | + base.type ?? base.Type ?? base.elementType ?? base.ElementType ?? inferElementTypeFromPrintSnapshotConfig(cfg), | |
| 324 | + ) | |
| 325 | + applyRenderValueToSnapshotConfig(cfg, elType, item.renderValue) | |
| 326 | + base.config = cfg | |
| 327 | + base.Config = cfg | |
| 244 | 328 | return base |
| 245 | 329 | } |
| 246 | 330 | |
| ... | ... | @@ -452,8 +536,9 @@ export async function printFromPrintDataListRow ( |
| 452 | 536 | } |
| 453 | 537 | |
| 454 | 538 | const templateData = labelTemplateDataForSnapshotReprint() |
| 539 | + const reprintBase = parseReprintBaseTime(row) | |
| 455 | 540 | tmpl = overlayReprintResolvedFields(tmpl, row, templateData) |
| 456 | - tmpl = bakeReprintTemplateSnapshot(tmpl) | |
| 541 | + tmpl = bakeReprintTemplateSnapshot(tmpl, reprintBase) | |
| 457 | 542 | |
| 458 | 543 | tmpl = { |
| 459 | 544 | ...tmpl, |
| ... | ... | @@ -584,8 +669,9 @@ export async function printFromMergedTemplateJsonString ( |
| 584 | 669 | throw new Error('Cannot parse merged template') |
| 585 | 670 | } |
| 586 | 671 | const templateData = labelTemplateDataForSnapshotReprint() |
| 672 | + const reprintBase = parseReprintBaseTime(row) | |
| 587 | 673 | tmpl = overlayReprintResolvedFields(tmpl, row, templateData) |
| 588 | - tmpl = bakeReprintTemplateSnapshot(tmpl) | |
| 674 | + tmpl = bakeReprintTemplateSnapshot(tmpl, reprintBase) | |
| 589 | 675 | tmpl = { |
| 590 | 676 | ...tmpl, |
| 591 | 677 | elements: sortElementsForPreview(tmpl.elements || []), | ... | ... |
美国版/Food Labeling Management App UniApp/src/utils/textElementLayout.ts
| ... | ... | @@ -65,3 +65,28 @@ export function readElementRotation( |
| 65 | 65 | const v = String(el.rotation ?? el.Rotation ?? 'horizontal').trim().toLowerCase() |
| 66 | 66 | return v === 'vertical' ? 'vertical' : 'horizontal' |
| 67 | 67 | } |
| 68 | + | |
| 69 | +/** 与 renderLabelPreviewCanvas lineHeightForTextElement 一致 */ | |
| 70 | +export function textElementLineHeightPx(fontSize: number, isDateTimeType = false): number { | |
| 71 | + return isDateTimeType | |
| 72 | + ? Math.max(fontSize + 2, Math.round(fontSize * 1.2)) | |
| 73 | + : Math.max(fontSize + 2, Math.round(fontSize * 1.25)) | |
| 74 | +} | |
| 75 | + | |
| 76 | +/** 矮框内固定文案:强制单行,避免折行后仅显示第一行 */ | |
| 77 | +export function isShortSingleLineTextBox( | |
| 78 | + boxHeightPx: number, | |
| 79 | + fontSize: number, | |
| 80 | + text: string, | |
| 81 | + opts?: { breakAll?: boolean }, | |
| 82 | +): boolean { | |
| 83 | + if (opts?.breakAll) return false | |
| 84 | + if (String(text ?? '').includes('\n')) return false | |
| 85 | + const lineH = textElementLineHeightPx(fontSize, false) | |
| 86 | + const descentPad = Math.max(2, Math.round(fontSize * 0.14)) | |
| 87 | + const maxLines = Math.max( | |
| 88 | + 1, | |
| 89 | + Math.floor((Math.max(0, boxHeightPx) + descentPad) / lineH), | |
| 90 | + ) | |
| 91 | + return maxLines <= 1 | |
| 92 | +} | ... | ... |
美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application.Contracts/Dtos/Label/LabelCreateInputVo.cs
| ... | ... | @@ -45,7 +45,8 @@ public class LabelCreateInputVo |
| 45 | 45 | |
| 46 | 46 | public string LabelCategoryId { get; set; } = string.Empty; |
| 47 | 47 | |
| 48 | - public string LabelTypeId { get; set; } = string.Empty; | |
| 48 | + /// <summary>标签类型 Id(可选)</summary> | |
| 49 | + public string? LabelTypeId { get; set; } | |
| 49 | 50 | |
| 50 | 51 | public List<string> ProductIds { get; set; } = new(); |
| 51 | 52 | ... | ... |
美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application.Contracts/Dtos/Label/LabelUpdateInputVo.cs
| ... | ... | @@ -31,7 +31,8 @@ public class LabelUpdateInputVo |
| 31 | 31 | |
| 32 | 32 | public string LabelCategoryId { get; set; } = string.Empty; |
| 33 | 33 | |
| 34 | - public string LabelTypeId { get; set; } = string.Empty; | |
| 34 | + /// <summary>标签类型 Id(可选)</summary> | |
| 35 | + public string? LabelTypeId { get; set; } | |
| 35 | 36 | |
| 36 | 37 | public List<string> ProductIds { get; set; } = new(); |
| 37 | 38 | ... | ... |
美国版/Food Labeling Management Code/Yi.Abp.Net8/module/food-labeling-us/FoodLabeling.Application.Contracts/IServices/ILabelAppService.cs
| ... | ... | @@ -21,7 +21,7 @@ public interface ILabelAppService : IApplicationService |
| 21 | 21 | Task<LabelGetOutputDto> GetAsync(string id); |
| 22 | 22 | |
| 23 | 23 | /// <summary> |
| 24 | - /// 新增标签。Body 支持 <c>appliedRegionType</c>(ALL/SPECIFIED)、<c>regionIds</c> / <c>groupIds</c>(落库 <c>fl_label_region</c>)、<c>locationIds</c>(主字段,落库 <c>fl_label_location</c>);<c>locationId</c> 与 <c>locationIds</c> 合并。 | |
| 24 | + /// 新增标签。Body 支持 <c>appliedRegionType</c>(ALL/SPECIFIED)、<c>regionIds</c> / <c>groupIds</c>(落库 <c>fl_label_region</c>)、<c>locationIds</c>(主字段,落库 <c>fl_label_location</c>);<c>locationId</c> 与 <c>locationIds</c> 合并;<c>labelTypeId</c> 可选。 | |
| 25 | 25 | /// </summary> |
| 26 | 26 | Task<LabelGetOutputDto> CreateAsync(LabelCreateInputVo input); |
| 27 | 27 | ... | ... |
美国版/Food Labeling Management Platform/.env.local
美国版/Food Labeling Management Platform/src/components/labels/LabelCategoriesView.tsx
| ... | ... | @@ -68,10 +68,9 @@ import { getLocations } from "../../services/locationService"; |
| 68 | 68 | import { getGroups } from "../../services/groupService"; |
| 69 | 69 | import { getPartners } from "../../services/partnerService"; |
| 70 | 70 | import { |
| 71 | - buildSpecifiedLocationPayload, | |
| 72 | - effectiveScopePartnerId, | |
| 71 | + buildLabelingScopeSaveBody, | |
| 73 | 72 | hydrateCategoryScopeFromLocationIds, |
| 74 | - scopePartnerValidationMessage, | |
| 73 | + hydrateLabelingScopeFromLocationIds, | |
| 75 | 74 | } from "../../lib/categoryScopeForm"; |
| 76 | 75 | import { CategoryScopeFields } from "../shared/category-scope-fields"; |
| 77 | 76 | import { useCategoryScopeAuth } from "../../hooks/useCategoryScopeAuth"; |
| ... | ... | @@ -841,7 +840,9 @@ function CreateLabelCategoryDialog({ |
| 841 | 840 | const [displayTextForPhoto, setDisplayTextForPhoto] = useState(""); |
| 842 | 841 | const [buttonBgColor, setButtonBgColor] = useState("#3B82F6"); |
| 843 | 842 | const [selectedPartnerId, setSelectedPartnerId] = useState(""); |
| 843 | + const [selectedPartnerIds, setSelectedPartnerIds] = useState<string[]>([]); | |
| 844 | 844 | const [selectedRegionNames, setSelectedRegionNames] = useState<string[]>([]); |
| 845 | + const [selectedRegionIds, setSelectedRegionIds] = useState<string[]>([]); | |
| 845 | 846 | const [selectedLocationIds, setSelectedLocationIds] = useState<string[]>([]); |
| 846 | 847 | const [form, setForm] = useState<LabelCategoryCreateInput>({ |
| 847 | 848 | categoryName: "", |
| ... | ... | @@ -862,7 +863,9 @@ function CreateLabelCategoryDialog({ |
| 862 | 863 | setDisplayTextForPhoto(""); |
| 863 | 864 | setButtonBgColor("#3B82F6"); |
| 864 | 865 | setSelectedPartnerId(""); |
| 866 | + setSelectedPartnerIds([]); | |
| 865 | 867 | setSelectedRegionNames([]); |
| 868 | + setSelectedRegionIds([]); | |
| 866 | 869 | setSelectedLocationIds([]); |
| 867 | 870 | setForm({ |
| 868 | 871 | categoryName: "", |
| ... | ... | @@ -920,27 +923,20 @@ function CreateLabelCategoryDialog({ |
| 920 | 923 | } |
| 921 | 924 | } |
| 922 | 925 | |
| 923 | - const scopePartnerId = effectiveScopePartnerId( | |
| 926 | + const scopePayload = buildLabelingScopeSaveBody({ | |
| 924 | 927 | requireCompanySelection, |
| 925 | 928 | selectedPartnerId, |
| 926 | 929 | fixedPartnerId, |
| 927 | - ); | |
| 928 | - const partnerErr = scopePartnerValidationMessage(requireCompanySelection, scopePartnerId); | |
| 929 | - if (partnerErr) { | |
| 930 | - toast.error("Validation failed", { description: partnerErr }); | |
| 931 | - return; | |
| 932 | - } | |
| 933 | - | |
| 934 | - const locPayload = buildSpecifiedLocationPayload( | |
| 935 | - locations, | |
| 936 | - partners, | |
| 937 | - groups, | |
| 938 | - scopePartnerId, | |
| 930 | + selectedPartnerIds, | |
| 939 | 931 | selectedRegionNames, |
| 932 | + selectedRegionIds, | |
| 940 | 933 | selectedLocationIds, |
| 941 | - ); | |
| 942 | - if (!locPayload.ok) { | |
| 943 | - toast.error("Validation failed", { description: locPayload.message }); | |
| 934 | + partners, | |
| 935 | + groups, | |
| 936 | + locations, | |
| 937 | + }); | |
| 938 | + if (!scopePayload.ok) { | |
| 939 | + toast.error("Validation failed", { description: scopePayload.message }); | |
| 944 | 940 | return; |
| 945 | 941 | } |
| 946 | 942 | |
| ... | ... | @@ -971,8 +967,13 @@ function CreateLabelCategoryDialog({ |
| 971 | 967 | buttonImageUrl: apSel.image ? String(form.categoryPhotoUrl ?? "").trim() || null : null, |
| 972 | 968 | buttonAppearance: tokens, |
| 973 | 969 | buttonStyleJson, |
| 974 | - availabilityType: "SPECIFIED", | |
| 975 | - locationIds: locPayload.locationIds, | |
| 970 | + availabilityType: scopePayload.body.availabilityType, | |
| 971 | + appliedPartnerType: scopePayload.body.appliedPartnerType, | |
| 972 | + partnerIds: scopePayload.body.partnerIds, | |
| 973 | + companyIds: scopePayload.body.companyIds, | |
| 974 | + regionIds: scopePayload.body.regionIds, | |
| 975 | + groupIds: scopePayload.body.groupIds, | |
| 976 | + locationIds: scopePayload.body.locationIds, | |
| 976 | 977 | }); |
| 977 | 978 | toast.success("Label category created.", { |
| 978 | 979 | description: "The label category has been created successfully.", |
| ... | ... | @@ -1016,12 +1017,17 @@ function CreateLabelCategoryDialog({ |
| 1016 | 1017 | locations={locations} |
| 1017 | 1018 | selectedPartnerId={selectedPartnerId} |
| 1018 | 1019 | onPartnerChange={setSelectedPartnerId} |
| 1020 | + selectedPartnerIds={selectedPartnerIds} | |
| 1021 | + onPartnerIdsChange={setSelectedPartnerIds} | |
| 1019 | 1022 | selectedRegionNames={selectedRegionNames} |
| 1020 | 1023 | onRegionChange={setSelectedRegionNames} |
| 1024 | + selectedRegionIds={selectedRegionIds} | |
| 1025 | + onRegionIdsChange={setSelectedRegionIds} | |
| 1021 | 1026 | selectedLocationIds={selectedLocationIds} |
| 1022 | 1027 | onLocationChange={setSelectedLocationIds} |
| 1023 | 1028 | requireCompanySelection={requireCompanySelection} |
| 1024 | 1029 | fixedPartnerId={fixedPartnerId} |
| 1030 | + templateScopeMode={requireCompanySelection} | |
| 1025 | 1031 | /> |
| 1026 | 1032 | |
| 1027 | 1033 | <div className="space-y-2"> |
| ... | ... | @@ -1232,7 +1238,9 @@ function EditLabelCategoryDialog({ |
| 1232 | 1238 | const [displayTextForPhoto, setDisplayTextForPhoto] = useState(""); |
| 1233 | 1239 | const [buttonBgColor, setButtonBgColor] = useState("#3B82F6"); |
| 1234 | 1240 | const [selectedPartnerId, setSelectedPartnerId] = useState(""); |
| 1241 | + const [selectedPartnerIds, setSelectedPartnerIds] = useState<string[]>([]); | |
| 1235 | 1242 | const [selectedRegionNames, setSelectedRegionNames] = useState<string[]>([]); |
| 1243 | + const [selectedRegionIds, setSelectedRegionIds] = useState<string[]>([]); | |
| 1236 | 1244 | const [selectedLocationIds, setSelectedLocationIds] = useState<string[]>([]); |
| 1237 | 1245 | const [form, setForm] = useState<LabelCategoryUpdateInput>({ |
| 1238 | 1246 | categoryName: "", |
| ... | ... | @@ -1255,15 +1263,28 @@ function EditLabelCategoryDialog({ |
| 1255 | 1263 | const rawAt = String(category.availabilityType ?? "ALL").trim().toUpperCase(); |
| 1256 | 1264 | if (rawAt !== "SPECIFIED") { |
| 1257 | 1265 | setSelectedPartnerId(""); |
| 1266 | + setSelectedPartnerIds([]); | |
| 1258 | 1267 | setSelectedRegionNames([]); |
| 1268 | + setSelectedRegionIds([]); | |
| 1259 | 1269 | setSelectedLocationIds([]); |
| 1260 | 1270 | return; |
| 1261 | 1271 | } |
| 1262 | 1272 | const lids = (category.locationIds ?? []).map((x) => String(x).trim()).filter(Boolean); |
| 1273 | + if (requireCompanySelection) { | |
| 1274 | + const scope = hydrateLabelingScopeFromLocationIds(lids, locations, partners, groups); | |
| 1275 | + setSelectedPartnerIds(scope.partnerIds); | |
| 1276 | + setSelectedRegionIds(scope.regionIds); | |
| 1277 | + setSelectedLocationIds(scope.locationIds); | |
| 1278 | + setSelectedPartnerId(""); | |
| 1279 | + setSelectedRegionNames([]); | |
| 1280 | + return; | |
| 1281 | + } | |
| 1263 | 1282 | const scope = hydrateCategoryScopeFromLocationIds(lids, locations, partners, groups); |
| 1264 | 1283 | setSelectedPartnerId(scope.partnerId); |
| 1265 | 1284 | setSelectedRegionNames(scope.regionNames); |
| 1266 | 1285 | setSelectedLocationIds(scope.locationIds); |
| 1286 | + setSelectedPartnerIds([]); | |
| 1287 | + setSelectedRegionIds([]); | |
| 1267 | 1288 | }; |
| 1268 | 1289 | |
| 1269 | 1290 | const styleParsed = parseCategoryButtonStyleV1( |
| ... | ... | @@ -1391,27 +1412,20 @@ function EditLabelCategoryDialog({ |
| 1391 | 1412 | } |
| 1392 | 1413 | } |
| 1393 | 1414 | |
| 1394 | - const scopePartnerId = effectiveScopePartnerId( | |
| 1415 | + const scopePayload = buildLabelingScopeSaveBody({ | |
| 1395 | 1416 | requireCompanySelection, |
| 1396 | 1417 | selectedPartnerId, |
| 1397 | 1418 | fixedPartnerId, |
| 1398 | - ); | |
| 1399 | - const partnerErr = scopePartnerValidationMessage(requireCompanySelection, scopePartnerId); | |
| 1400 | - if (partnerErr) { | |
| 1401 | - toast.error("Validation failed", { description: partnerErr }); | |
| 1402 | - return; | |
| 1403 | - } | |
| 1404 | - | |
| 1405 | - const locPayload = buildSpecifiedLocationPayload( | |
| 1406 | - locations, | |
| 1407 | - partners, | |
| 1408 | - groups, | |
| 1409 | - scopePartnerId, | |
| 1419 | + selectedPartnerIds, | |
| 1410 | 1420 | selectedRegionNames, |
| 1421 | + selectedRegionIds, | |
| 1411 | 1422 | selectedLocationIds, |
| 1412 | - ); | |
| 1413 | - if (!locPayload.ok) { | |
| 1414 | - toast.error("Validation failed", { description: locPayload.message }); | |
| 1423 | + partners, | |
| 1424 | + groups, | |
| 1425 | + locations, | |
| 1426 | + }); | |
| 1427 | + if (!scopePayload.ok) { | |
| 1428 | + toast.error("Validation failed", { description: scopePayload.message }); | |
| 1415 | 1429 | return; |
| 1416 | 1430 | } |
| 1417 | 1431 | |
| ... | ... | @@ -1442,8 +1456,13 @@ function EditLabelCategoryDialog({ |
| 1442 | 1456 | buttonImageUrl: apSel.image ? String(form.categoryPhotoUrl ?? "").trim() || null : null, |
| 1443 | 1457 | buttonAppearance: tokens, |
| 1444 | 1458 | buttonStyleJson, |
| 1445 | - availabilityType: "SPECIFIED", | |
| 1446 | - locationIds: locPayload.locationIds, | |
| 1459 | + availabilityType: scopePayload.body.availabilityType, | |
| 1460 | + appliedPartnerType: scopePayload.body.appliedPartnerType, | |
| 1461 | + partnerIds: scopePayload.body.partnerIds, | |
| 1462 | + companyIds: scopePayload.body.companyIds, | |
| 1463 | + regionIds: scopePayload.body.regionIds, | |
| 1464 | + groupIds: scopePayload.body.groupIds, | |
| 1465 | + locationIds: scopePayload.body.locationIds, | |
| 1447 | 1466 | }); |
| 1448 | 1467 | toast.success("Label category updated.", { |
| 1449 | 1468 | description: "The label category has been updated successfully.", |
| ... | ... | @@ -1487,12 +1506,17 @@ function EditLabelCategoryDialog({ |
| 1487 | 1506 | locations={locations} |
| 1488 | 1507 | selectedPartnerId={selectedPartnerId} |
| 1489 | 1508 | onPartnerChange={setSelectedPartnerId} |
| 1509 | + selectedPartnerIds={selectedPartnerIds} | |
| 1510 | + onPartnerIdsChange={setSelectedPartnerIds} | |
| 1490 | 1511 | selectedRegionNames={selectedRegionNames} |
| 1491 | 1512 | onRegionChange={setSelectedRegionNames} |
| 1513 | + selectedRegionIds={selectedRegionIds} | |
| 1514 | + onRegionIdsChange={setSelectedRegionIds} | |
| 1492 | 1515 | selectedLocationIds={selectedLocationIds} |
| 1493 | 1516 | onLocationChange={setSelectedLocationIds} |
| 1494 | 1517 | requireCompanySelection={requireCompanySelection} |
| 1495 | 1518 | fixedPartnerId={fixedPartnerId} |
| 1519 | + templateScopeMode={requireCompanySelection} | |
| 1496 | 1520 | /> |
| 1497 | 1521 | |
| 1498 | 1522 | <div className="space-y-2"> | ... | ... |
美国版/Food Labeling Management Platform/src/components/labels/LabelTemplateEditor/LabelCanvas.tsx
| ... | ... | @@ -57,6 +57,8 @@ import { |
| 57 | 57 | readFontWeight, |
| 58 | 58 | readFontStyle, |
| 59 | 59 | readTextDecoration, |
| 60 | + isShortSingleLineTextBox, | |
| 61 | + fitFontSizeToInnerWidthEstimate, | |
| 60 | 62 | } from '../../../utils/textElementLayout'; |
| 61 | 63 | |
| 62 | 64 | export type { PrintOrientation } from '../../../types/labelTemplate'; |
| ... | ... | @@ -1292,6 +1294,30 @@ function AlignedTextBox({ |
| 1292 | 1294 | ); |
| 1293 | 1295 | } |
| 1294 | 1296 | |
| 1297 | +/** 矮框固定文案:单行 + 必要时微缩字号,与 App 预览/打印一致 */ | |
| 1298 | +function staticTextBoxLayout( | |
| 1299 | + el: { width: number; height: number }, | |
| 1300 | + cfg: Record<string, unknown>, | |
| 1301 | + text: string, | |
| 1302 | + commonStyle: React.CSSProperties, | |
| 1303 | + inverted: boolean, | |
| 1304 | +): { commonStyle: React.CSSProperties; className: string } { | |
| 1305 | + const fontSize = (cfg?.fontSize as number) ?? 14; | |
| 1306 | + const family = String(commonStyle.fontFamily ?? cfg?.fontFamily ?? '').toLowerCase(); | |
| 1307 | + const bold = readFontWeight(cfg) === 'bold' || family.includes('bold'); | |
| 1308 | + const singleLineShort = isShortSingleLineTextBox(Number(el.height) || 0, fontSize, text, { | |
| 1309 | + breakAll: inverted, | |
| 1310 | + }); | |
| 1311 | + const innerW = Math.max(4, (Number(el.width) || 0) - 8); | |
| 1312 | + const fittedSize = singleLineShort | |
| 1313 | + ? fitFontSizeToInnerWidthEstimate(text, innerW, fontSize, bold) | |
| 1314 | + : fontSize; | |
| 1315 | + return { | |
| 1316 | + commonStyle: { ...commonStyle, fontSize: fittedSize }, | |
| 1317 | + className: singleLineShort ? 'whitespace-nowrap leading-tight' : 'break-all leading-tight', | |
| 1318 | + }; | |
| 1319 | +} | |
| 1320 | + | |
| 1295 | 1321 | /** 根据元素类型与 config 渲染画布上的默认内容 */ |
| 1296 | 1322 | function ElementContent({ |
| 1297 | 1323 | el, |
| ... | ... | @@ -1360,12 +1386,14 @@ function ElementContent({ |
| 1360 | 1386 | } |
| 1361 | 1387 | if (type === 'TEXT_STATIC') { |
| 1362 | 1388 | const text = (cfg?.text as string) ?? 'Text'; |
| 1389 | + const printFieldPlaceholder = | |
| 1390 | + inputType === 'number' ? '0' : inputType === 'text' ? '' : '…'; | |
| 1363 | 1391 | if (isAppPrintField && previewAsPrinted) { |
| 1364 | 1392 | if (inputType === 'options') { |
| 1365 | 1393 | const selected = Array.isArray(cfg?.selectedOptionValues) |
| 1366 | 1394 | ? (cfg.selectedOptionValues as string[]) |
| 1367 | 1395 | : []; |
| 1368 | - const line = formatMultipleOptionsCanvasLine(cfg, text, selected); | |
| 1396 | + const line = formatMultipleOptionsCanvasLine(cfg, '…', selected); | |
| 1369 | 1397 | const muted = selected.length === 0; |
| 1370 | 1398 | return wrapVertical( |
| 1371 | 1399 | <AlignedTextBox |
| ... | ... | @@ -1383,13 +1411,18 @@ function ElementContent({ |
| 1383 | 1411 | ); |
| 1384 | 1412 | } |
| 1385 | 1413 | const display = |
| 1386 | - inputType === 'number' ? ((cfg?.text as string) ?? '0') : text; | |
| 1414 | + inputType === 'number' | |
| 1415 | + ? ((cfg?.text as string) ?? '0') | |
| 1416 | + : inputType === 'text' | |
| 1417 | + ? ((cfg?.text as string) ?? '') | |
| 1418 | + : text; | |
| 1419 | + const printedLayout = staticTextBoxLayout(el, cfg, display, commonStyle, inverted); | |
| 1387 | 1420 | return wrapVertical( |
| 1388 | 1421 | <AlignedTextBox |
| 1389 | 1422 | cfg={cfg} |
| 1390 | - commonStyle={commonStyle} | |
| 1423 | + commonStyle={printedLayout.commonStyle} | |
| 1391 | 1424 | allowOverflow={textOverflow} |
| 1392 | - className="whitespace-pre-wrap break-words leading-tight" | |
| 1425 | + className={printedLayout.className} | |
| 1393 | 1426 | > |
| 1394 | 1427 | {display} |
| 1395 | 1428 | </AlignedTextBox>, |
| ... | ... | @@ -1400,7 +1433,7 @@ function ElementContent({ |
| 1400 | 1433 | const selected = Array.isArray(cfg?.selectedOptionValues) |
| 1401 | 1434 | ? (cfg.selectedOptionValues as string[]) |
| 1402 | 1435 | : []; |
| 1403 | - const line = formatMultipleOptionsCanvasLine(cfg, text, selected); | |
| 1436 | + const line = formatMultipleOptionsCanvasLine(cfg, '…', selected); | |
| 1404 | 1437 | return wrapVertical( |
| 1405 | 1438 | <div |
| 1406 | 1439 | className="w-full h-full px-1 flex flex-col justify-center overflow-hidden pointer-events-none italic text-[11px] leading-tight break-all" |
| ... | ... | @@ -1411,8 +1444,7 @@ function ElementContent({ |
| 1411 | 1444 | </div>, |
| 1412 | 1445 | ); |
| 1413 | 1446 | } |
| 1414 | - const display = | |
| 1415 | - inputType === 'number' ? ((cfg?.text as string) ?? '0') : text; | |
| 1447 | + const display = printFieldPlaceholder; | |
| 1416 | 1448 | return wrapVertical( |
| 1417 | 1449 | <div |
| 1418 | 1450 | className={cn( |
| ... | ... | @@ -1476,8 +1508,14 @@ function ElementContent({ |
| 1476 | 1508 | />, |
| 1477 | 1509 | ); |
| 1478 | 1510 | } |
| 1511 | + const defaultLayout = staticTextBoxLayout(el, cfg, text, commonStyle, inverted); | |
| 1479 | 1512 | return wrapVertical( |
| 1480 | - <AlignedTextBox cfg={cfg} commonStyle={commonStyle} allowOverflow={textOverflow} className="break-all leading-tight"> | |
| 1513 | + <AlignedTextBox | |
| 1514 | + cfg={cfg} | |
| 1515 | + commonStyle={defaultLayout.commonStyle} | |
| 1516 | + allowOverflow={textOverflow} | |
| 1517 | + className={defaultLayout.className} | |
| 1518 | + > | |
| 1481 | 1519 | {text} |
| 1482 | 1520 | </AlignedTextBox>, |
| 1483 | 1521 | ); | ... | ... |
美国版/Food Labeling Management Platform/src/components/labels/LabelTemplateEditor/PropertiesPanel.tsx
| ... | ... | @@ -750,6 +750,8 @@ function BiuStyleFields({ |
| 750 | 750 | } |
| 751 | 751 | |
| 752 | 752 | const DEMO_VALUE_HINT = 'Value entered here for demo only.'; |
| 753 | +const PRINT_PRETEXT_HINT = | |
| 754 | + 'Shown on the app print screen as a prompt only; not printed on the label.'; | |
| 753 | 755 | |
| 754 | 756 | function TextStaticStyleFields({ |
| 755 | 757 | cfg, |
| ... | ... | @@ -762,8 +764,8 @@ function TextStaticStyleFields({ |
| 762 | 764 | cfg: Record<string, unknown>; |
| 763 | 765 | update: (key: string, value: unknown) => void; |
| 764 | 766 | textAlignDefault: string; |
| 765 | - /** Template 面板静态文案在属性里称 Value,其它分组仍用 Text */ | |
| 766 | - primaryTextLabel?: 'Text' | 'Value'; | |
| 767 | + /** Template 面板静态文案在属性里称 Value;Entered When Printing 称 Pre-text */ | |
| 768 | + primaryTextLabel?: 'Text' | 'Value' | 'Pre-text'; | |
| 767 | 769 | /** Value/Text 输入框下方提示(如 Price 演示值说明) */ |
| 768 | 770 | primaryTextHint?: string; |
| 769 | 771 | /** Auto-generated 控件:文案由系统自动填充,隐藏 Text/Value 输入 */ |
| ... | ... | @@ -817,6 +819,27 @@ function TextStaticStyleFields({ |
| 817 | 819 | ); |
| 818 | 820 | } |
| 819 | 821 | |
| 822 | +function PreTextField({ | |
| 823 | + cfg, | |
| 824 | + update, | |
| 825 | +}: { | |
| 826 | + cfg: Record<string, unknown>; | |
| 827 | + update: (key: string, value: unknown) => void; | |
| 828 | +}) { | |
| 829 | + return ( | |
| 830 | + <div> | |
| 831 | + <Label className="text-xs">Pre-text</Label> | |
| 832 | + <Input | |
| 833 | + value={(cfg.text as string) ?? ''} | |
| 834 | + onChange={(e) => update('text', e.target.value)} | |
| 835 | + className="h-8 text-sm mt-1" | |
| 836 | + placeholder="e.g. Ingredients:" | |
| 837 | + /> | |
| 838 | + <p className="text-[10px] text-gray-400 mt-1">{PRINT_PRETEXT_HINT}</p> | |
| 839 | + </div> | |
| 840 | + ); | |
| 841 | +} | |
| 842 | + | |
| 820 | 843 | /** 读 config(兼容后端 PascalCase、数字以字符串下发) */ |
| 821 | 844 | function cfgPickStr(cfg: Record<string, unknown>, keys: string[], fallback: string): string { |
| 822 | 845 | for (const k of keys) { |
| ... | ... | @@ -898,10 +921,19 @@ function ElementConfigFields({ |
| 898 | 921 | const fromTemplatePalette = isTemplateSectionPersistedType(element); |
| 899 | 922 | const fromLabelPalette = isLabelSectionPersistedType(element); |
| 900 | 923 | const fromPrintPalette = isPrintInputSectionPersistedType(element); |
| 901 | - const staticTextLabel = fromTemplatePalette ? ('Value' as const) : ('Text' as const); | |
| 924 | + const staticTextLabel = fromTemplatePalette | |
| 925 | + ? ('Value' as const) | |
| 926 | + : fromPrintPalette | |
| 927 | + ? ('Pre-text' as const) | |
| 928 | + : ('Text' as const); | |
| 902 | 929 | const hidePrimaryText = isAutoGeneratedElement(element); |
| 903 | - const demoValueHint = | |
| 904 | - !hidePrimaryText && (fromLabelPalette || fromPrintPalette) ? DEMO_VALUE_HINT : undefined; | |
| 930 | + const demoValueHint = !hidePrimaryText | |
| 931 | + ? fromPrintPalette | |
| 932 | + ? PRINT_PRETEXT_HINT | |
| 933 | + : fromLabelPalette | |
| 934 | + ? DEMO_VALUE_HINT | |
| 935 | + : undefined | |
| 936 | + : undefined; | |
| 905 | 937 | |
| 906 | 938 | switch (elementType) { |
| 907 | 939 | case 'TEXT_STATIC': |
| ... | ... | @@ -1129,6 +1161,7 @@ function ElementConfigFields({ |
| 1129 | 1161 | </p> |
| 1130 | 1162 | ) : null} |
| 1131 | 1163 | </div> |
| 1164 | + {isPrintDate && fromPrintPalette ? <PreTextField cfg={cfg} update={update} /> : null} | |
| 1132 | 1165 | <FontFamilyField cfg={cfg} update={update} /> |
| 1133 | 1166 | <div> |
| 1134 | 1167 | <Label className="text-xs">Font Size</Label> |
| ... | ... | @@ -1257,6 +1290,7 @@ function ElementConfigFields({ |
| 1257 | 1290 | const editorWeightInputMode = weightInputMode === 'tare' ? 'net' : weightInputMode; |
| 1258 | 1291 | return ( |
| 1259 | 1292 | <> |
| 1293 | + {fromPrintPalette ? <PreTextField cfg={cfg} update={update} /> : null} | |
| 1260 | 1294 | <div> |
| 1261 | 1295 | <Label className="text-xs">Weight input mode</Label> |
| 1262 | 1296 | <Select | ... | ... |
美国版/Food Labeling Management Platform/src/components/labels/LabelTemplateEditor/index.tsx
| ... | ... | @@ -19,6 +19,7 @@ import type { ElementLibraryCategory, LabelTemplate, LabelElement, PrintOrientat |
| 19 | 19 | import { |
| 20 | 20 | buildLabelTemplateScopePayload, |
| 21 | 21 | buildTemplateContentsFromElements, |
| 22 | + allocateElementDisplayName, | |
| 22 | 23 | allocateElementName, |
| 23 | 24 | canonicalElementType, |
| 24 | 25 | composeElementTypeForPersist, | ... | ... |
美国版/Food Labeling Management Platform/src/components/labels/LabelTypesView.tsx
| ... | ... | @@ -51,11 +51,9 @@ import { getLocations } from "../../services/locationService"; |
| 51 | 51 | import { getGroups } from "../../services/groupService"; |
| 52 | 52 | import { getPartners } from "../../services/partnerService"; |
| 53 | 53 | import { |
| 54 | - buildSpecifiedLocationPayload, | |
| 55 | - effectiveScopePartnerId, | |
| 54 | + buildLabelingScopeSaveBody, | |
| 56 | 55 | hydrateCategoryScopeFromLocationIds, |
| 57 | - regionNamesToGroupIds, | |
| 58 | - scopePartnerValidationMessage, | |
| 56 | + hydrateLabelingScopeFromLocationIds, | |
| 59 | 57 | } from "../../lib/categoryScopeForm"; |
| 60 | 58 | import { CategoryScopeFields } from "../shared/category-scope-fields"; |
| 61 | 59 | import { useCategoryScopeAuth } from "../../hooks/useCategoryScopeAuth"; |
| ... | ... | @@ -822,7 +820,9 @@ function CreateLabelTypeDialog({ |
| 822 | 820 | }) { |
| 823 | 821 | const [submitting, setSubmitting] = useState(false); |
| 824 | 822 | const [selectedPartnerId, setSelectedPartnerId] = useState(""); |
| 823 | + const [selectedPartnerIds, setSelectedPartnerIds] = useState<string[]>([]); | |
| 825 | 824 | const [selectedRegionNames, setSelectedRegionNames] = useState<string[]>([]); |
| 825 | + const [selectedRegionIds, setSelectedRegionIds] = useState<string[]>([]); | |
| 826 | 826 | const [selectedLocationIds, setSelectedLocationIds] = useState<string[]>([]); |
| 827 | 827 | const [form, setForm] = useState<LabelTypeCreateInput>({ |
| 828 | 828 | typeName: "", |
| ... | ... | @@ -859,40 +859,35 @@ function CreateLabelTypeDialog({ |
| 859 | 859 | return; |
| 860 | 860 | } |
| 861 | 861 | |
| 862 | - const scopePartnerId = effectiveScopePartnerId( | |
| 862 | + const scopePayload = buildLabelingScopeSaveBody({ | |
| 863 | 863 | requireCompanySelection, |
| 864 | 864 | selectedPartnerId, |
| 865 | 865 | fixedPartnerId, |
| 866 | - ); | |
| 867 | - const partnerErr = scopePartnerValidationMessage(requireCompanySelection, scopePartnerId); | |
| 868 | - if (partnerErr) { | |
| 869 | - toast.error("Validation failed", { description: partnerErr }); | |
| 870 | - return; | |
| 871 | - } | |
| 872 | - | |
| 873 | - const locPayload = buildSpecifiedLocationPayload( | |
| 874 | - locations, | |
| 875 | - partners, | |
| 876 | - groups, | |
| 877 | - scopePartnerId, | |
| 866 | + selectedPartnerIds, | |
| 878 | 867 | selectedRegionNames, |
| 868 | + selectedRegionIds, | |
| 879 | 869 | selectedLocationIds, |
| 880 | - ); | |
| 881 | - if (!locPayload.ok) { | |
| 882 | - toast.error("Validation failed", { description: locPayload.message }); | |
| 870 | + partners, | |
| 871 | + groups, | |
| 872 | + locations, | |
| 873 | + }); | |
| 874 | + if (!scopePayload.ok) { | |
| 875 | + toast.error("Validation failed", { description: scopePayload.message }); | |
| 883 | 876 | return; |
| 884 | 877 | } |
| 885 | 878 | |
| 886 | - const groupIds = regionNamesToGroupIds(selectedRegionNames, groups, scopePartnerId); | |
| 887 | - | |
| 888 | 879 | setSubmitting(true); |
| 889 | 880 | try { |
| 890 | 881 | await createLabelType({ |
| 891 | 882 | ...form, |
| 892 | 883 | typeCode: typeCodeFromName(form.typeName), |
| 893 | - groupIds, | |
| 894 | - regionIds: groupIds, | |
| 895 | - locationIds: locPayload.locationIds, | |
| 884 | + availabilityType: scopePayload.body.availabilityType, | |
| 885 | + appliedPartnerType: scopePayload.body.appliedPartnerType, | |
| 886 | + partnerIds: scopePayload.body.partnerIds, | |
| 887 | + companyIds: scopePayload.body.companyIds, | |
| 888 | + groupIds: scopePayload.body.groupIds, | |
| 889 | + regionIds: scopePayload.body.regionIds, | |
| 890 | + locationIds: scopePayload.body.locationIds, | |
| 896 | 891 | }); |
| 897 | 892 | toast.success("Label type created.", { |
| 898 | 893 | description: "The label type has been created successfully.", |
| ... | ... | @@ -950,12 +945,17 @@ function CreateLabelTypeDialog({ |
| 950 | 945 | locations={locations} |
| 951 | 946 | selectedPartnerId={selectedPartnerId} |
| 952 | 947 | onPartnerChange={setSelectedPartnerId} |
| 948 | + selectedPartnerIds={selectedPartnerIds} | |
| 949 | + onPartnerIdsChange={setSelectedPartnerIds} | |
| 953 | 950 | selectedRegionNames={selectedRegionNames} |
| 954 | 951 | onRegionChange={setSelectedRegionNames} |
| 952 | + selectedRegionIds={selectedRegionIds} | |
| 953 | + onRegionIdsChange={setSelectedRegionIds} | |
| 955 | 954 | selectedLocationIds={selectedLocationIds} |
| 956 | 955 | onLocationChange={setSelectedLocationIds} |
| 957 | 956 | requireCompanySelection={requireCompanySelection} |
| 958 | 957 | fixedPartnerId={fixedPartnerId} |
| 958 | + templateScopeMode={requireCompanySelection} | |
| 959 | 959 | /> |
| 960 | 960 | </div> |
| 961 | 961 | |
| ... | ... | @@ -996,7 +996,9 @@ function EditLabelTypeDialog({ |
| 996 | 996 | const [submitting, setSubmitting] = useState(false); |
| 997 | 997 | const [loadingDetail, setLoadingDetail] = useState(false); |
| 998 | 998 | const [selectedPartnerId, setSelectedPartnerId] = useState(""); |
| 999 | + const [selectedPartnerIds, setSelectedPartnerIds] = useState<string[]>([]); | |
| 999 | 1000 | const [selectedRegionNames, setSelectedRegionNames] = useState<string[]>([]); |
| 1001 | + const [selectedRegionIds, setSelectedRegionIds] = useState<string[]>([]); | |
| 1000 | 1002 | const [selectedLocationIds, setSelectedLocationIds] = useState<string[]>([]); |
| 1001 | 1003 | const [form, setForm] = useState<LabelTypeUpdateInput>({ |
| 1002 | 1004 | typeName: "", |
| ... | ... | @@ -1023,10 +1025,21 @@ function EditLabelTypeDialog({ |
| 1023 | 1025 | |
| 1024 | 1026 | const lids = (detail.locationIds ?? []).map((x) => String(x).trim()).filter(Boolean); |
| 1025 | 1027 | if (lids.length > 0) { |
| 1026 | - const scope = hydrateCategoryScopeFromLocationIds(lids, locations, partners, groups); | |
| 1027 | - setSelectedPartnerId(scope.partnerId); | |
| 1028 | - setSelectedRegionNames(scope.regionNames); | |
| 1029 | - setSelectedLocationIds(scope.locationIds); | |
| 1028 | + if (requireCompanySelection) { | |
| 1029 | + const scope = hydrateLabelingScopeFromLocationIds(lids, locations, partners, groups); | |
| 1030 | + setSelectedPartnerIds(scope.partnerIds); | |
| 1031 | + setSelectedRegionIds(scope.regionIds); | |
| 1032 | + setSelectedLocationIds(scope.locationIds); | |
| 1033 | + setSelectedPartnerId(""); | |
| 1034 | + setSelectedRegionNames([]); | |
| 1035 | + } else { | |
| 1036 | + const scope = hydrateCategoryScopeFromLocationIds(lids, locations, partners, groups); | |
| 1037 | + setSelectedPartnerId(scope.partnerId); | |
| 1038 | + setSelectedRegionNames(scope.regionNames); | |
| 1039 | + setSelectedLocationIds(scope.locationIds); | |
| 1040 | + setSelectedPartnerIds([]); | |
| 1041 | + setSelectedRegionIds([]); | |
| 1042 | + } | |
| 1030 | 1043 | return; |
| 1031 | 1044 | } |
| 1032 | 1045 | |
| ... | ... | @@ -1035,26 +1048,52 @@ function EditLabelTypeDialog({ |
| 1035 | 1048 | .filter(Boolean); |
| 1036 | 1049 | if (gids.length > 0) { |
| 1037 | 1050 | const matchedGroups = groups.filter((g) => gids.includes(g.id)); |
| 1038 | - const pid = matchedGroups[0]?.partnerId ?? ""; | |
| 1039 | - const names = [ | |
| 1040 | - ...new Set(matchedGroups.map((g) => (g.groupName ?? "").trim()).filter(Boolean)), | |
| 1041 | - ]; | |
| 1042 | - setSelectedPartnerId(pid); | |
| 1043 | - setSelectedRegionNames(names); | |
| 1044 | - setSelectedLocationIds([]); | |
| 1051 | + if (requireCompanySelection) { | |
| 1052 | + const partnerIds = [ | |
| 1053 | + ...new Set(matchedGroups.map((g) => String(g.partnerId ?? "").trim()).filter(Boolean)), | |
| 1054 | + ]; | |
| 1055 | + setSelectedPartnerIds(partnerIds); | |
| 1056 | + setSelectedRegionIds(gids); | |
| 1057 | + setSelectedLocationIds([]); | |
| 1058 | + setSelectedPartnerId(""); | |
| 1059 | + setSelectedRegionNames([]); | |
| 1060 | + } else { | |
| 1061 | + const pid = matchedGroups[0]?.partnerId ?? ""; | |
| 1062 | + const names = [ | |
| 1063 | + ...new Set(matchedGroups.map((g) => (g.groupName ?? "").trim()).filter(Boolean)), | |
| 1064 | + ]; | |
| 1065 | + setSelectedPartnerId(pid); | |
| 1066 | + setSelectedRegionNames(names); | |
| 1067 | + setSelectedLocationIds([]); | |
| 1068 | + setSelectedPartnerIds([]); | |
| 1069 | + setSelectedRegionIds([]); | |
| 1070 | + } | |
| 1045 | 1071 | return; |
| 1046 | 1072 | } |
| 1047 | 1073 | |
| 1048 | 1074 | const fromLabels = await fetchLocationIdsForLabelType(type.id, ac.signal); |
| 1049 | 1075 | if (ac.signal.aborted) return; |
| 1050 | 1076 | if (fromLabels.length > 0) { |
| 1051 | - const scope = hydrateCategoryScopeFromLocationIds(fromLabels, locations, partners, groups); | |
| 1052 | - setSelectedPartnerId(scope.partnerId); | |
| 1053 | - setSelectedRegionNames(scope.regionNames); | |
| 1054 | - setSelectedLocationIds(scope.locationIds); | |
| 1077 | + if (requireCompanySelection) { | |
| 1078 | + const scope = hydrateLabelingScopeFromLocationIds(fromLabels, locations, partners, groups); | |
| 1079 | + setSelectedPartnerIds(scope.partnerIds); | |
| 1080 | + setSelectedRegionIds(scope.regionIds); | |
| 1081 | + setSelectedLocationIds(scope.locationIds); | |
| 1082 | + setSelectedPartnerId(""); | |
| 1083 | + setSelectedRegionNames([]); | |
| 1084 | + } else { | |
| 1085 | + const scope = hydrateCategoryScopeFromLocationIds(fromLabels, locations, partners, groups); | |
| 1086 | + setSelectedPartnerId(scope.partnerId); | |
| 1087 | + setSelectedRegionNames(scope.regionNames); | |
| 1088 | + setSelectedLocationIds(scope.locationIds); | |
| 1089 | + setSelectedPartnerIds([]); | |
| 1090 | + setSelectedRegionIds([]); | |
| 1091 | + } | |
| 1055 | 1092 | } else { |
| 1056 | 1093 | setSelectedPartnerId(""); |
| 1094 | + setSelectedPartnerIds([]); | |
| 1057 | 1095 | setSelectedRegionNames([]); |
| 1096 | + setSelectedRegionIds([]); | |
| 1058 | 1097 | setSelectedLocationIds([]); |
| 1059 | 1098 | } |
| 1060 | 1099 | } catch { |
| ... | ... | @@ -1073,7 +1112,7 @@ function EditLabelTypeDialog({ |
| 1073 | 1112 | })(); |
| 1074 | 1113 | |
| 1075 | 1114 | return () => ac.abort(); |
| 1076 | - }, [open, type, locations, partners, groups]); | |
| 1115 | + }, [open, type, locations, partners, groups, requireCompanySelection]); | |
| 1077 | 1116 | |
| 1078 | 1117 | const submit = async () => { |
| 1079 | 1118 | if (!type?.id) return; |
| ... | ... | @@ -1088,40 +1127,35 @@ function EditLabelTypeDialog({ |
| 1088 | 1127 | return; |
| 1089 | 1128 | } |
| 1090 | 1129 | |
| 1091 | - const scopePartnerId = effectiveScopePartnerId( | |
| 1130 | + const scopePayload = buildLabelingScopeSaveBody({ | |
| 1092 | 1131 | requireCompanySelection, |
| 1093 | 1132 | selectedPartnerId, |
| 1094 | 1133 | fixedPartnerId, |
| 1095 | - ); | |
| 1096 | - const partnerErr = scopePartnerValidationMessage(requireCompanySelection, scopePartnerId); | |
| 1097 | - if (partnerErr) { | |
| 1098 | - toast.error("Validation failed", { description: partnerErr }); | |
| 1099 | - return; | |
| 1100 | - } | |
| 1101 | - | |
| 1102 | - const locPayload = buildSpecifiedLocationPayload( | |
| 1103 | - locations, | |
| 1104 | - partners, | |
| 1105 | - groups, | |
| 1106 | - scopePartnerId, | |
| 1134 | + selectedPartnerIds, | |
| 1107 | 1135 | selectedRegionNames, |
| 1136 | + selectedRegionIds, | |
| 1108 | 1137 | selectedLocationIds, |
| 1109 | - ); | |
| 1110 | - if (!locPayload.ok) { | |
| 1111 | - toast.error("Validation failed", { description: locPayload.message }); | |
| 1138 | + partners, | |
| 1139 | + groups, | |
| 1140 | + locations, | |
| 1141 | + }); | |
| 1142 | + if (!scopePayload.ok) { | |
| 1143 | + toast.error("Validation failed", { description: scopePayload.message }); | |
| 1112 | 1144 | return; |
| 1113 | 1145 | } |
| 1114 | 1146 | |
| 1115 | - const groupIds = regionNamesToGroupIds(selectedRegionNames, groups, scopePartnerId); | |
| 1116 | - | |
| 1117 | 1147 | setSubmitting(true); |
| 1118 | 1148 | try { |
| 1119 | 1149 | await updateLabelType(type.id, { |
| 1120 | 1150 | ...form, |
| 1121 | 1151 | typeCode: type.typeCode ?? "", |
| 1122 | - groupIds, | |
| 1123 | - regionIds: groupIds, | |
| 1124 | - locationIds: locPayload.locationIds, | |
| 1152 | + availabilityType: scopePayload.body.availabilityType, | |
| 1153 | + appliedPartnerType: scopePayload.body.appliedPartnerType, | |
| 1154 | + partnerIds: scopePayload.body.partnerIds, | |
| 1155 | + companyIds: scopePayload.body.companyIds, | |
| 1156 | + groupIds: scopePayload.body.groupIds, | |
| 1157 | + regionIds: scopePayload.body.regionIds, | |
| 1158 | + locationIds: scopePayload.body.locationIds, | |
| 1125 | 1159 | }); |
| 1126 | 1160 | toast.success("Label type updated.", { |
| 1127 | 1161 | description: "The label type has been updated successfully.", |
| ... | ... | @@ -1182,12 +1216,17 @@ function EditLabelTypeDialog({ |
| 1182 | 1216 | locations={locations} |
| 1183 | 1217 | selectedPartnerId={selectedPartnerId} |
| 1184 | 1218 | onPartnerChange={setSelectedPartnerId} |
| 1219 | + selectedPartnerIds={selectedPartnerIds} | |
| 1220 | + onPartnerIdsChange={setSelectedPartnerIds} | |
| 1185 | 1221 | selectedRegionNames={selectedRegionNames} |
| 1186 | 1222 | onRegionChange={setSelectedRegionNames} |
| 1223 | + selectedRegionIds={selectedRegionIds} | |
| 1224 | + onRegionIdsChange={setSelectedRegionIds} | |
| 1187 | 1225 | selectedLocationIds={selectedLocationIds} |
| 1188 | 1226 | onLocationChange={setSelectedLocationIds} |
| 1189 | 1227 | requireCompanySelection={requireCompanySelection} |
| 1190 | 1228 | fixedPartnerId={fixedPartnerId} |
| 1229 | + templateScopeMode={requireCompanySelection} | |
| 1191 | 1230 | /> |
| 1192 | 1231 | )} |
| 1193 | 1232 | </div> | ... | ... |
美国版/Food Labeling Management Platform/src/components/labels/LabelsList.tsx
| ... | ... | @@ -2091,9 +2091,9 @@ function CreateLabelDialog({ |
| 2091 | 2091 | toast.error("Validation failed", { description: locPayload.message }); |
| 2092 | 2092 | return; |
| 2093 | 2093 | } |
| 2094 | - if (!form.labelName.trim() || !form.templateCode.trim() || !form.labelCategoryId.trim() || !form.labelTypeId.trim()) { | |
| 2094 | + if (!form.labelName.trim() || !form.templateCode.trim() || !form.labelCategoryId.trim()) { | |
| 2095 | 2095 | toast.error("Validation failed", { |
| 2096 | - description: "Fill all required fields and select template, location(s), category, and type.", | |
| 2096 | + description: "Fill all required fields and select template, location(s), and category.", | |
| 2097 | 2097 | }); |
| 2098 | 2098 | return; |
| 2099 | 2099 | } |
| ... | ... | @@ -2175,6 +2175,7 @@ function CreateLabelDialog({ |
| 2175 | 2175 | try { |
| 2176 | 2176 | await createLabel({ |
| 2177 | 2177 | ...form, |
| 2178 | + labelTypeId: form.labelTypeId.trim() || null, | |
| 2178 | 2179 | locationIds: locPayload.locationIds, |
| 2179 | 2180 | appliedRegionType: regionPayload.appliedRegionType, |
| 2180 | 2181 | regionIds: regionPayload.regionIds, |
| ... | ... | @@ -2506,13 +2507,13 @@ function CreateLabelDialog({ |
| 2506 | 2507 | /> |
| 2507 | 2508 | </div> |
| 2508 | 2509 | <div className="space-y-2"> |
| 2509 | - <Label>Label Type *</Label> | |
| 2510 | + <Label>Label Type</Label> | |
| 2510 | 2511 | <SearchableSelect |
| 2511 | 2512 | value={form.labelTypeId} |
| 2512 | 2513 | onValueChange={(v) => setForm((p) => ({ ...p, labelTypeId: v }))} |
| 2513 | 2514 | options={typeOptions} |
| 2514 | 2515 | placeholder={ |
| 2515 | - scopedOptionsLoading ? "Loading types…" : regionScopeReady ? "Select type" : scopeGateHint | |
| 2516 | + scopedOptionsLoading ? "Loading types…" : regionScopeReady ? "Optional" : scopeGateHint | |
| 2516 | 2517 | } |
| 2517 | 2518 | searchPlaceholder="Search type…" |
| 2518 | 2519 | emptyText={ | ... | ... |
美国版/Food Labeling Management Platform/src/components/labels/MultipleOptionsView.tsx
| ... | ... | @@ -51,11 +51,9 @@ import { getLocations } from "../../services/locationService"; |
| 51 | 51 | import { getGroups } from "../../services/groupService"; |
| 52 | 52 | import { getPartners } from "../../services/partnerService"; |
| 53 | 53 | import { |
| 54 | - buildSpecifiedLocationPayload, | |
| 55 | - effectiveScopePartnerId, | |
| 54 | + buildLabelingScopeSaveBody, | |
| 56 | 55 | hydrateCategoryScopeFromLocationIds, |
| 57 | - regionNamesToGroupIds, | |
| 58 | - scopePartnerValidationMessage, | |
| 56 | + hydrateLabelingScopeFromLocationIds, | |
| 59 | 57 | } from "../../lib/categoryScopeForm"; |
| 60 | 58 | import { CategoryScopeFields } from "../shared/category-scope-fields"; |
| 61 | 59 | import { useCategoryScopeAuth } from "../../hooks/useCategoryScopeAuth"; |
| ... | ... | @@ -806,7 +804,9 @@ function CreateMultipleOptionDialog({ |
| 806 | 804 | }) { |
| 807 | 805 | const [submitting, setSubmitting] = useState(false); |
| 808 | 806 | const [selectedPartnerId, setSelectedPartnerId] = useState(""); |
| 807 | + const [selectedPartnerIds, setSelectedPartnerIds] = useState<string[]>([]); | |
| 809 | 808 | const [selectedRegionNames, setSelectedRegionNames] = useState<string[]>([]); |
| 809 | + const [selectedRegionIds, setSelectedRegionIds] = useState<string[]>([]); | |
| 810 | 810 | const [selectedLocationIds, setSelectedLocationIds] = useState<string[]>([]); |
| 811 | 811 | const [form, setForm] = useState<LabelMultipleOptionCreateInput>({ |
| 812 | 812 | optionName: "", |
| ... | ... | @@ -876,40 +876,35 @@ function CreateMultipleOptionDialog({ |
| 876 | 876 | return; |
| 877 | 877 | } |
| 878 | 878 | |
| 879 | - const scopePartnerId = effectiveScopePartnerId( | |
| 879 | + const scopePayload = buildLabelingScopeSaveBody({ | |
| 880 | 880 | requireCompanySelection, |
| 881 | 881 | selectedPartnerId, |
| 882 | 882 | fixedPartnerId, |
| 883 | - ); | |
| 884 | - const partnerErr = scopePartnerValidationMessage(requireCompanySelection, scopePartnerId); | |
| 885 | - if (partnerErr) { | |
| 886 | - toast.error("Validation failed", { description: partnerErr }); | |
| 887 | - return; | |
| 888 | - } | |
| 889 | - | |
| 890 | - const locPayload = buildSpecifiedLocationPayload( | |
| 891 | - locations, | |
| 892 | - partners, | |
| 893 | - groups, | |
| 894 | - scopePartnerId, | |
| 883 | + selectedPartnerIds, | |
| 895 | 884 | selectedRegionNames, |
| 885 | + selectedRegionIds, | |
| 896 | 886 | selectedLocationIds, |
| 897 | - ); | |
| 898 | - if (!locPayload.ok) { | |
| 899 | - toast.error("Validation failed", { description: locPayload.message }); | |
| 887 | + partners, | |
| 888 | + groups, | |
| 889 | + locations, | |
| 890 | + }); | |
| 891 | + if (!scopePayload.ok) { | |
| 892 | + toast.error("Validation failed", { description: scopePayload.message }); | |
| 900 | 893 | return; |
| 901 | 894 | } |
| 902 | 895 | |
| 903 | - const groupIds = regionNamesToGroupIds(selectedRegionNames, groups, scopePartnerId); | |
| 904 | - | |
| 905 | 896 | setSubmitting(true); |
| 906 | 897 | try { |
| 907 | 898 | await createLabelMultipleOption({ |
| 908 | 899 | ...form, |
| 909 | 900 | optionCode: optionCodeFromName(form.optionName), |
| 910 | - groupIds, | |
| 911 | - regionIds: groupIds, | |
| 912 | - locationIds: locPayload.locationIds, | |
| 901 | + availabilityType: scopePayload.body.availabilityType, | |
| 902 | + appliedPartnerType: scopePayload.body.appliedPartnerType, | |
| 903 | + partnerIds: scopePayload.body.partnerIds, | |
| 904 | + companyIds: scopePayload.body.companyIds, | |
| 905 | + groupIds: scopePayload.body.groupIds, | |
| 906 | + regionIds: scopePayload.body.regionIds, | |
| 907 | + locationIds: scopePayload.body.locationIds, | |
| 913 | 908 | }); |
| 914 | 909 | toast.success("Multiple option created.", { |
| 915 | 910 | description: "The multiple option has been created successfully.", |
| ... | ... | @@ -1003,12 +998,17 @@ function CreateMultipleOptionDialog({ |
| 1003 | 998 | locations={locations} |
| 1004 | 999 | selectedPartnerId={selectedPartnerId} |
| 1005 | 1000 | onPartnerChange={setSelectedPartnerId} |
| 1001 | + selectedPartnerIds={selectedPartnerIds} | |
| 1002 | + onPartnerIdsChange={setSelectedPartnerIds} | |
| 1006 | 1003 | selectedRegionNames={selectedRegionNames} |
| 1007 | 1004 | onRegionChange={setSelectedRegionNames} |
| 1005 | + selectedRegionIds={selectedRegionIds} | |
| 1006 | + onRegionIdsChange={setSelectedRegionIds} | |
| 1008 | 1007 | selectedLocationIds={selectedLocationIds} |
| 1009 | 1008 | onLocationChange={setSelectedLocationIds} |
| 1010 | 1009 | requireCompanySelection={requireCompanySelection} |
| 1011 | 1010 | fixedPartnerId={fixedPartnerId} |
| 1011 | + templateScopeMode={requireCompanySelection} | |
| 1012 | 1012 | /> |
| 1013 | 1013 | </div> |
| 1014 | 1014 | |
| ... | ... | @@ -1049,7 +1049,9 @@ function EditMultipleOptionDialog({ |
| 1049 | 1049 | const [submitting, setSubmitting] = useState(false); |
| 1050 | 1050 | const [loadingDetail, setLoadingDetail] = useState(false); |
| 1051 | 1051 | const [selectedPartnerId, setSelectedPartnerId] = useState(""); |
| 1052 | + const [selectedPartnerIds, setSelectedPartnerIds] = useState<string[]>([]); | |
| 1052 | 1053 | const [selectedRegionNames, setSelectedRegionNames] = useState<string[]>([]); |
| 1054 | + const [selectedRegionIds, setSelectedRegionIds] = useState<string[]>([]); | |
| 1053 | 1055 | const [selectedLocationIds, setSelectedLocationIds] = useState<string[]>([]); |
| 1054 | 1056 | const [form, setForm] = useState<LabelMultipleOptionUpdateInput>({ |
| 1055 | 1057 | optionName: "", |
| ... | ... | @@ -1080,10 +1082,21 @@ function EditMultipleOptionDialog({ |
| 1080 | 1082 | |
| 1081 | 1083 | const lids = (detail.locationIds ?? []).map((x) => String(x).trim()).filter(Boolean); |
| 1082 | 1084 | if (lids.length > 0) { |
| 1083 | - const scope = hydrateCategoryScopeFromLocationIds(lids, locations, partners, groups); | |
| 1084 | - setSelectedPartnerId(scope.partnerId); | |
| 1085 | - setSelectedRegionNames(scope.regionNames); | |
| 1086 | - setSelectedLocationIds(scope.locationIds); | |
| 1085 | + if (requireCompanySelection) { | |
| 1086 | + const scope = hydrateLabelingScopeFromLocationIds(lids, locations, partners, groups); | |
| 1087 | + setSelectedPartnerIds(scope.partnerIds); | |
| 1088 | + setSelectedRegionIds(scope.regionIds); | |
| 1089 | + setSelectedLocationIds(scope.locationIds); | |
| 1090 | + setSelectedPartnerId(""); | |
| 1091 | + setSelectedRegionNames([]); | |
| 1092 | + } else { | |
| 1093 | + const scope = hydrateCategoryScopeFromLocationIds(lids, locations, partners, groups); | |
| 1094 | + setSelectedPartnerId(scope.partnerId); | |
| 1095 | + setSelectedRegionNames(scope.regionNames); | |
| 1096 | + setSelectedLocationIds(scope.locationIds); | |
| 1097 | + setSelectedPartnerIds([]); | |
| 1098 | + setSelectedRegionIds([]); | |
| 1099 | + } | |
| 1087 | 1100 | return; |
| 1088 | 1101 | } |
| 1089 | 1102 | |
| ... | ... | @@ -1092,26 +1105,52 @@ function EditMultipleOptionDialog({ |
| 1092 | 1105 | .filter(Boolean); |
| 1093 | 1106 | if (gids.length > 0) { |
| 1094 | 1107 | const matchedGroups = groups.filter((g) => gids.includes(g.id)); |
| 1095 | - const pid = matchedGroups[0]?.partnerId ?? ""; | |
| 1096 | - const names = [ | |
| 1097 | - ...new Set(matchedGroups.map((g) => (g.groupName ?? "").trim()).filter(Boolean)), | |
| 1098 | - ]; | |
| 1099 | - setSelectedPartnerId(pid); | |
| 1100 | - setSelectedRegionNames(names); | |
| 1101 | - setSelectedLocationIds([]); | |
| 1108 | + if (requireCompanySelection) { | |
| 1109 | + const partnerIds = [ | |
| 1110 | + ...new Set(matchedGroups.map((g) => String(g.partnerId ?? "").trim()).filter(Boolean)), | |
| 1111 | + ]; | |
| 1112 | + setSelectedPartnerIds(partnerIds); | |
| 1113 | + setSelectedRegionIds(gids); | |
| 1114 | + setSelectedLocationIds([]); | |
| 1115 | + setSelectedPartnerId(""); | |
| 1116 | + setSelectedRegionNames([]); | |
| 1117 | + } else { | |
| 1118 | + const pid = matchedGroups[0]?.partnerId ?? ""; | |
| 1119 | + const names = [ | |
| 1120 | + ...new Set(matchedGroups.map((g) => (g.groupName ?? "").trim()).filter(Boolean)), | |
| 1121 | + ]; | |
| 1122 | + setSelectedPartnerId(pid); | |
| 1123 | + setSelectedRegionNames(names); | |
| 1124 | + setSelectedLocationIds([]); | |
| 1125 | + setSelectedPartnerIds([]); | |
| 1126 | + setSelectedRegionIds([]); | |
| 1127 | + } | |
| 1102 | 1128 | return; |
| 1103 | 1129 | } |
| 1104 | 1130 | |
| 1105 | 1131 | const fromLabels = await fetchLocationIdsForMultipleOption(option.id, ac.signal); |
| 1106 | 1132 | if (ac.signal.aborted) return; |
| 1107 | 1133 | if (fromLabels.length > 0) { |
| 1108 | - const scope = hydrateCategoryScopeFromLocationIds(fromLabels, locations, partners, groups); | |
| 1109 | - setSelectedPartnerId(scope.partnerId); | |
| 1110 | - setSelectedRegionNames(scope.regionNames); | |
| 1111 | - setSelectedLocationIds(scope.locationIds); | |
| 1134 | + if (requireCompanySelection) { | |
| 1135 | + const scope = hydrateLabelingScopeFromLocationIds(fromLabels, locations, partners, groups); | |
| 1136 | + setSelectedPartnerIds(scope.partnerIds); | |
| 1137 | + setSelectedRegionIds(scope.regionIds); | |
| 1138 | + setSelectedLocationIds(scope.locationIds); | |
| 1139 | + setSelectedPartnerId(""); | |
| 1140 | + setSelectedRegionNames([]); | |
| 1141 | + } else { | |
| 1142 | + const scope = hydrateCategoryScopeFromLocationIds(fromLabels, locations, partners, groups); | |
| 1143 | + setSelectedPartnerId(scope.partnerId); | |
| 1144 | + setSelectedRegionNames(scope.regionNames); | |
| 1145 | + setSelectedLocationIds(scope.locationIds); | |
| 1146 | + setSelectedPartnerIds([]); | |
| 1147 | + setSelectedRegionIds([]); | |
| 1148 | + } | |
| 1112 | 1149 | } else { |
| 1113 | 1150 | setSelectedPartnerId(""); |
| 1151 | + setSelectedPartnerIds([]); | |
| 1114 | 1152 | setSelectedRegionNames([]); |
| 1153 | + setSelectedRegionIds([]); | |
| 1115 | 1154 | setSelectedLocationIds([]); |
| 1116 | 1155 | } |
| 1117 | 1156 | } catch { |
| ... | ... | @@ -1132,7 +1171,7 @@ function EditMultipleOptionDialog({ |
| 1132 | 1171 | })(); |
| 1133 | 1172 | |
| 1134 | 1173 | return () => ac.abort(); |
| 1135 | - }, [open, option, locations, partners, groups]); | |
| 1174 | + }, [open, option, locations, partners, groups, requireCompanySelection]); | |
| 1136 | 1175 | |
| 1137 | 1176 | const addValue = () => { |
| 1138 | 1177 | const trimmed = newValue.trim(); |
| ... | ... | @@ -1176,40 +1215,35 @@ function EditMultipleOptionDialog({ |
| 1176 | 1215 | return; |
| 1177 | 1216 | } |
| 1178 | 1217 | |
| 1179 | - const scopePartnerId = effectiveScopePartnerId( | |
| 1218 | + const scopePayload = buildLabelingScopeSaveBody({ | |
| 1180 | 1219 | requireCompanySelection, |
| 1181 | 1220 | selectedPartnerId, |
| 1182 | 1221 | fixedPartnerId, |
| 1183 | - ); | |
| 1184 | - const partnerErr = scopePartnerValidationMessage(requireCompanySelection, scopePartnerId); | |
| 1185 | - if (partnerErr) { | |
| 1186 | - toast.error("Validation failed", { description: partnerErr }); | |
| 1187 | - return; | |
| 1188 | - } | |
| 1189 | - | |
| 1190 | - const locPayload = buildSpecifiedLocationPayload( | |
| 1191 | - locations, | |
| 1192 | - partners, | |
| 1193 | - groups, | |
| 1194 | - scopePartnerId, | |
| 1222 | + selectedPartnerIds, | |
| 1195 | 1223 | selectedRegionNames, |
| 1224 | + selectedRegionIds, | |
| 1196 | 1225 | selectedLocationIds, |
| 1197 | - ); | |
| 1198 | - if (!locPayload.ok) { | |
| 1199 | - toast.error("Validation failed", { description: locPayload.message }); | |
| 1226 | + partners, | |
| 1227 | + groups, | |
| 1228 | + locations, | |
| 1229 | + }); | |
| 1230 | + if (!scopePayload.ok) { | |
| 1231 | + toast.error("Validation failed", { description: scopePayload.message }); | |
| 1200 | 1232 | return; |
| 1201 | 1233 | } |
| 1202 | 1234 | |
| 1203 | - const groupIds = regionNamesToGroupIds(selectedRegionNames, groups, scopePartnerId); | |
| 1204 | - | |
| 1205 | 1235 | setSubmitting(true); |
| 1206 | 1236 | try { |
| 1207 | 1237 | await updateLabelMultipleOption(option.id, { |
| 1208 | 1238 | ...form, |
| 1209 | 1239 | optionCode: option.optionCode ?? "", |
| 1210 | - groupIds, | |
| 1211 | - regionIds: groupIds, | |
| 1212 | - locationIds: locPayload.locationIds, | |
| 1240 | + availabilityType: scopePayload.body.availabilityType, | |
| 1241 | + appliedPartnerType: scopePayload.body.appliedPartnerType, | |
| 1242 | + partnerIds: scopePayload.body.partnerIds, | |
| 1243 | + companyIds: scopePayload.body.companyIds, | |
| 1244 | + groupIds: scopePayload.body.groupIds, | |
| 1245 | + regionIds: scopePayload.body.regionIds, | |
| 1246 | + locationIds: scopePayload.body.locationIds, | |
| 1213 | 1247 | }); |
| 1214 | 1248 | toast.success("Multiple option updated.", { |
| 1215 | 1249 | description: "The multiple option has been updated successfully.", |
| ... | ... | @@ -1306,12 +1340,17 @@ function EditMultipleOptionDialog({ |
| 1306 | 1340 | locations={locations} |
| 1307 | 1341 | selectedPartnerId={selectedPartnerId} |
| 1308 | 1342 | onPartnerChange={setSelectedPartnerId} |
| 1343 | + selectedPartnerIds={selectedPartnerIds} | |
| 1344 | + onPartnerIdsChange={setSelectedPartnerIds} | |
| 1309 | 1345 | selectedRegionNames={selectedRegionNames} |
| 1310 | 1346 | onRegionChange={setSelectedRegionNames} |
| 1347 | + selectedRegionIds={selectedRegionIds} | |
| 1348 | + onRegionIdsChange={setSelectedRegionIds} | |
| 1311 | 1349 | selectedLocationIds={selectedLocationIds} |
| 1312 | 1350 | onLocationChange={setSelectedLocationIds} |
| 1313 | 1351 | requireCompanySelection={requireCompanySelection} |
| 1314 | 1352 | fixedPartnerId={fixedPartnerId} |
| 1353 | + templateScopeMode={requireCompanySelection} | |
| 1315 | 1354 | /> |
| 1316 | 1355 | )} |
| 1317 | 1356 | </div> | ... | ... |
美国版/Food Labeling Management Platform/src/components/shared/category-scope-fields.tsx
| ... | ... | @@ -390,15 +390,30 @@ export function CategoryScopeFields({ |
| 390 | 390 | {requireCompanySelection ? ( |
| 391 | 391 | <div className="space-y-2"> |
| 392 | 392 | <Label>Company *</Label> |
| 393 | - <SearchableSelect | |
| 394 | - value={selectedPartnerId} | |
| 395 | - onValueChange={onCompanyChange} | |
| 396 | - options={companyOptions} | |
| 397 | - placeholder="Select company" | |
| 398 | - searchPlaceholder="Search company…" | |
| 399 | - emptyText="No companies found." | |
| 400 | - /> | |
| 401 | - <p className="text-xs text-gray-500">Required. Select company before region and location.</p> | |
| 393 | + {templateScopeMode ? ( | |
| 394 | + <SearchableMultiSelect | |
| 395 | + values={selectedPartnerIds} | |
| 396 | + onValuesChange={onCompaniesChange} | |
| 397 | + options={companyOptions} | |
| 398 | + placeholder="All Companies" | |
| 399 | + searchPlaceholder="Search companies…" | |
| 400 | + selectAllRowLabel="Select All" | |
| 401 | + /> | |
| 402 | + ) : ( | |
| 403 | + <SearchableSelect | |
| 404 | + value={selectedPartnerId} | |
| 405 | + onValueChange={onCompanyChange} | |
| 406 | + options={companyOptions} | |
| 407 | + placeholder="Select company" | |
| 408 | + searchPlaceholder="Search company…" | |
| 409 | + emptyText="No companies found." | |
| 410 | + /> | |
| 411 | + )} | |
| 412 | + <p className="text-xs text-gray-500"> | |
| 413 | + {templateScopeMode | |
| 414 | + ? "Required. Select one or more companies, or Select All." | |
| 415 | + : "Required. Select company before region and location."} | |
| 416 | + </p> | |
| 402 | 417 | </div> |
| 403 | 418 | ) : null} |
| 404 | 419 | |
| ... | ... | @@ -406,8 +421,8 @@ export function CategoryScopeFields({ |
| 406 | 421 | <div className="space-y-2"> |
| 407 | 422 | <Label>Region *</Label> |
| 408 | 423 | <SearchableMultiSelect |
| 409 | - values={selectedRegionNames} | |
| 410 | - onValuesChange={onRegionMultiChange} | |
| 424 | + values={templateScopeMode ? selectedRegionIds : selectedRegionNames} | |
| 425 | + onValuesChange={templateScopeMode ? onRegionIdsMultiChange : onRegionMultiChange} | |
| 411 | 426 | options={regionOptions} |
| 412 | 427 | placeholder={regionPlaceholder} |
| 413 | 428 | searchPlaceholder="Search regions…" | ... | ... |
美国版/Food Labeling Management Platform/src/components/ui/searchable-multi-select.tsx
| ... | ... | @@ -37,6 +37,8 @@ export function SearchableMultiSelect({ |
| 37 | 37 | compact = false, |
| 38 | 38 | /** 在列表顶部增加一行「全选」:针对当前搜索结果对应的选项值全选 / 清除 */ |
| 39 | 39 | selectAllRowLabel, |
| 40 | + /** 勾选/取消后收起下拉;默认 false,点选多项时保持展开,点击外部再关闭 */ | |
| 41 | + closeOnSelect, | |
| 40 | 42 | }: { |
| 41 | 43 | values: string[]; |
| 42 | 44 | onValuesChange: (next: string[]) => void; |
| ... | ... | @@ -48,10 +50,17 @@ export function SearchableMultiSelect({ |
| 48 | 50 | className?: string; |
| 49 | 51 | compact?: boolean; |
| 50 | 52 | selectAllRowLabel?: string; |
| 53 | + closeOnSelect?: boolean; | |
| 51 | 54 | }) { |
| 52 | 55 | const [open, setOpen] = useState(false); |
| 53 | 56 | const [q, setQ] = useState(""); |
| 54 | 57 | const normalized = useMemo(() => dedupeIds(values), [values]); |
| 58 | + const shouldCloseOnSelect = closeOnSelect ?? false; | |
| 59 | + | |
| 60 | + const closePanel = () => { | |
| 61 | + setOpen(false); | |
| 62 | + setQ(""); | |
| 63 | + }; | |
| 55 | 64 | |
| 56 | 65 | const filtered = useMemo(() => { |
| 57 | 66 | const s = q.trim().toLowerCase(); |
| ... | ... | @@ -93,6 +102,7 @@ export function SearchableMultiSelect({ |
| 93 | 102 | } else { |
| 94 | 103 | onValuesChange([...normalized, t]); |
| 95 | 104 | } |
| 105 | + if (shouldCloseOnSelect) closePanel(); | |
| 96 | 106 | }; |
| 97 | 107 | |
| 98 | 108 | const toggleSelectAllFiltered = () => { |
| ... | ... | @@ -102,10 +112,12 @@ export function SearchableMultiSelect({ |
| 102 | 112 | } else { |
| 103 | 113 | onValuesChange(dedupeIds([...normalized, ...filteredValues])); |
| 104 | 114 | } |
| 115 | + if (shouldCloseOnSelect) closePanel(); | |
| 105 | 116 | }; |
| 106 | 117 | |
| 107 | 118 | return ( |
| 108 | 119 | <Popover |
| 120 | + modal | |
| 109 | 121 | open={open} |
| 110 | 122 | onOpenChange={(o) => { |
| 111 | 123 | setOpen(o); |
| ... | ... | @@ -197,7 +209,7 @@ export function SearchableMultiSelect({ |
| 197 | 209 | className="text-xs text-gray-500 underline-offset-2 hover:text-gray-900 hover:underline" |
| 198 | 210 | onClick={() => { |
| 199 | 211 | onValuesChange([]); |
| 200 | - setQ(""); | |
| 212 | + closePanel(); | |
| 201 | 213 | }} |
| 202 | 214 | > |
| 203 | 215 | Clear all ({normalized.length}) | ... | ... |
美国版/Food Labeling Management Platform/src/components/ui/searchable-select.tsx
| 1 | 1 | "use client"; |
| 2 | 2 | |
| 3 | 3 | import * as React from "react"; |
| 4 | -import { useState } from "react"; | |
| 5 | -import { ChevronsUpDown } from "lucide-react"; | |
| 4 | +import { useMemo, useState } from "react"; | |
| 5 | +import { ChevronsUpDown, Search } from "lucide-react"; | |
| 6 | 6 | import { cn } from "./utils"; |
| 7 | 7 | import { Button } from "./button"; |
| 8 | -import { | |
| 9 | - Command, | |
| 10 | - CommandEmpty, | |
| 11 | - CommandGroup, | |
| 12 | - CommandInput, | |
| 13 | - CommandItem, | |
| 14 | - CommandList, | |
| 15 | -} from "./command"; | |
| 8 | +import { Input } from "./input"; | |
| 16 | 9 | import { Popover, PopoverContent, PopoverTrigger } from "./popover"; |
| 17 | 10 | |
| 18 | 11 | export type SearchableSelectOption = { |
| ... | ... | @@ -20,6 +13,11 @@ export type SearchableSelectOption = { |
| 20 | 13 | label: string; |
| 21 | 14 | }; |
| 22 | 15 | |
| 16 | +/** 下拉默认可见行数,超出部分滚动 */ | |
| 17 | +const DEFAULT_VISIBLE_LIST_ROWS = 5; | |
| 18 | +/** 单行高度:py-2 + text-sm 行高 */ | |
| 19 | +const LIST_ROW_PX = 36; | |
| 20 | + | |
| 23 | 21 | export function SearchableSelect({ |
| 24 | 22 | value, |
| 25 | 23 | onValueChange, |
| ... | ... | @@ -31,6 +29,8 @@ export function SearchableSelect({ |
| 31 | 29 | className, |
| 32 | 30 | /** 顶栏/模板编辑器:与 SelectTrigger !h-7、text-xs 对齐 */ |
| 33 | 31 | compact = false, |
| 32 | + /** 下拉列表可见行数(默认 5 行,其余滚动) */ | |
| 33 | + visibleListRows = DEFAULT_VISIBLE_LIST_ROWS, | |
| 34 | 34 | }: { |
| 35 | 35 | /** 空字符串表示未选择 */ |
| 36 | 36 | value: string; |
| ... | ... | @@ -42,13 +42,36 @@ export function SearchableSelect({ |
| 42 | 42 | disabled?: boolean; |
| 43 | 43 | className?: string; |
| 44 | 44 | compact?: boolean; |
| 45 | + visibleListRows?: number; | |
| 45 | 46 | }) { |
| 46 | 47 | const [open, setOpen] = useState(false); |
| 48 | + const [q, setQ] = useState(""); | |
| 47 | 49 | const hit = value ? options.find((o) => o.value === value) : undefined; |
| 48 | 50 | const selectedLabel = value ? hit?.label ?? value : null; |
| 51 | + const listMaxHeightPx = Math.max(1, visibleListRows) * LIST_ROW_PX; | |
| 52 | + | |
| 53 | + const filtered = useMemo(() => { | |
| 54 | + const s = q.trim().toLowerCase(); | |
| 55 | + if (!s) return options; | |
| 56 | + return options.filter( | |
| 57 | + (o) => o.label.toLowerCase().includes(s) || o.value.toLowerCase().includes(s), | |
| 58 | + ); | |
| 59 | + }, [options, q]); | |
| 60 | + | |
| 61 | + const closePanel = () => { | |
| 62 | + setOpen(false); | |
| 63 | + setQ(""); | |
| 64 | + }; | |
| 49 | 65 | |
| 50 | 66 | return ( |
| 51 | - <Popover open={open} onOpenChange={setOpen}> | |
| 67 | + <Popover | |
| 68 | + modal | |
| 69 | + open={open} | |
| 70 | + onOpenChange={(next) => { | |
| 71 | + setOpen(next); | |
| 72 | + if (!next) setQ(""); | |
| 73 | + }} | |
| 74 | + > | |
| 52 | 75 | <PopoverTrigger asChild> |
| 53 | 76 | <Button |
| 54 | 77 | type="button" |
| ... | ... | @@ -82,36 +105,44 @@ export function SearchableSelect({ |
| 82 | 105 | </Button> |
| 83 | 106 | </PopoverTrigger> |
| 84 | 107 | <PopoverContent |
| 85 | - className="p-0 w-[var(--radix-popover-trigger-width)] max-w-[min(100vw-2rem,400px)]" | |
| 108 | + className="w-[var(--radix-popover-trigger-width)] max-w-[min(100vw-2rem,400px)] p-0 overflow-hidden" | |
| 86 | 109 | align="start" |
| 87 | 110 | > |
| 88 | - <Command> | |
| 89 | - <CommandInput placeholder={searchPlaceholder} /> | |
| 90 | - <CommandList> | |
| 91 | - <CommandEmpty>{emptyText}</CommandEmpty> | |
| 92 | - <CommandGroup> | |
| 93 | - {options.map((opt) => ( | |
| 94 | - <CommandItem | |
| 95 | - key={opt.value} | |
| 96 | - value={`${opt.label} ${opt.value}`} | |
| 97 | - onSelect={() => { | |
| 98 | - onValueChange(opt.value); | |
| 99 | - setOpen(false); | |
| 100 | - }} | |
| 101 | - className={cn( | |
| 102 | - "cursor-pointer rounded-md px-2 py-2 transition-colors", | |
| 103 | - "hover:bg-gray-100 hover:text-gray-900", | |
| 104 | - "data-[selected=true]:bg-gray-100", | |
| 105 | - value === opt.value && | |
| 106 | - "bg-blue-50 text-gray-900 font-medium data-[selected=true]:bg-blue-100", | |
| 107 | - )} | |
| 108 | - > | |
| 109 | - <span className="truncate">{opt.label}</span> | |
| 110 | - </CommandItem> | |
| 111 | - ))} | |
| 112 | - </CommandGroup> | |
| 113 | - </CommandList> | |
| 114 | - </Command> | |
| 111 | + <div className="flex items-center gap-2 border-b border-gray-100 px-2 py-2"> | |
| 112 | + <Search className="h-4 w-4 shrink-0 text-gray-400" /> | |
| 113 | + <Input | |
| 114 | + value={q} | |
| 115 | + onChange={(e) => setQ(e.target.value)} | |
| 116 | + placeholder={searchPlaceholder} | |
| 117 | + className="h-8 border-0 bg-transparent px-0 text-sm shadow-none focus-visible:ring-0" | |
| 118 | + /> | |
| 119 | + </div> | |
| 120 | + <div | |
| 121 | + className="overflow-y-auto overscroll-contain p-1 [scrollbar-gutter:stable]" | |
| 122 | + style={{ maxHeight: listMaxHeightPx }} | |
| 123 | + > | |
| 124 | + {filtered.length === 0 ? ( | |
| 125 | + <div className="px-2 py-6 text-center text-sm text-gray-500">{emptyText}</div> | |
| 126 | + ) : ( | |
| 127 | + filtered.map((opt) => ( | |
| 128 | + <button | |
| 129 | + key={opt.value} | |
| 130 | + type="button" | |
| 131 | + className={cn( | |
| 132 | + "flex w-full min-h-9 cursor-pointer items-center rounded-md px-2 py-2 text-left text-sm transition-colors", | |
| 133 | + "hover:bg-gray-100 hover:text-gray-900", | |
| 134 | + value === opt.value && "bg-blue-50 font-medium text-gray-900 hover:bg-blue-100", | |
| 135 | + )} | |
| 136 | + onClick={() => { | |
| 137 | + onValueChange(opt.value); | |
| 138 | + closePanel(); | |
| 139 | + }} | |
| 140 | + > | |
| 141 | + <span className="min-w-0 flex-1 truncate">{opt.label}</span> | |
| 142 | + </button> | |
| 143 | + )) | |
| 144 | + )} | |
| 145 | + </div> | |
| 115 | 146 | {value ? ( |
| 116 | 147 | <div className="border-t border-gray-100 px-2 py-1.5"> |
| 117 | 148 | <button |
| ... | ... | @@ -119,7 +150,7 @@ export function SearchableSelect({ |
| 119 | 150 | className="text-xs text-gray-500 hover:text-gray-900 underline-offset-2 hover:underline" |
| 120 | 151 | onClick={() => { |
| 121 | 152 | onValueChange(""); |
| 122 | - setOpen(false); | |
| 153 | + closePanel(); | |
| 123 | 154 | }} |
| 124 | 155 | > |
| 125 | 156 | Clear selection | ... | ... |
美国版/Food Labeling Management Platform/src/lib/categoryScopeForm.ts
| 1 | 1 | import type { GroupListItem } from "../types/group"; |
| 2 | 2 | import type { LocationDto } from "../types/location"; |
| 3 | 3 | import type { PartnerListItem } from "../types/partner"; |
| 4 | -import { parseAppliedScopeType } from "../types/labelTemplate"; | |
| 4 | +import { | |
| 5 | + buildLabelTemplateScopePayload, | |
| 6 | + type AppliedScopeType, | |
| 7 | + parseAppliedScopeType, | |
| 8 | +} from "../types/labelTemplate"; | |
| 5 | 9 | |
| 6 | 10 | /** 表单提交用的公司 Id(非管理员取账号绑定公司) */ |
| 7 | 11 | export function effectiveScopePartnerId( |
| ... | ... | @@ -24,6 +28,194 @@ export function scopePartnerValidationMessage( |
| 24 | 28 | : "Your account is not assigned to a company. Please contact an administrator."; |
| 25 | 29 | } |
| 26 | 30 | |
| 31 | +/** 系统管理员多选 Company 时的校验 */ | |
| 32 | +export function scopePartnersValidationMessage( | |
| 33 | + requireCompanySelection: boolean, | |
| 34 | + partnerIds: string[], | |
| 35 | +): string | null { | |
| 36 | + if (!requireCompanySelection) return null; | |
| 37 | + if (partnerIds.some((id) => id.trim())) return null; | |
| 38 | + return "Please select at least one company."; | |
| 39 | +} | |
| 40 | + | |
| 41 | +export function effectiveScopePartnerIds( | |
| 42 | + requireCompanySelection: boolean, | |
| 43 | + selectedPartnerIds: string[], | |
| 44 | + fixedPartnerId: string, | |
| 45 | +): string[] { | |
| 46 | + if (requireCompanySelection) { | |
| 47 | + return [...new Set(selectedPartnerIds.map((x) => x.trim()).filter(Boolean))]; | |
| 48 | + } | |
| 49 | + const pid = fixedPartnerId.trim(); | |
| 50 | + return pid ? [pid] : []; | |
| 51 | +} | |
| 52 | + | |
| 53 | +export type LabelingScopeSaveBody = { | |
| 54 | + availabilityType: "ALL" | "SPECIFIED"; | |
| 55 | + appliedPartnerType?: AppliedScopeType; | |
| 56 | + partnerIds?: string[]; | |
| 57 | + companyIds?: string[]; | |
| 58 | + appliedRegionType?: AppliedScopeType; | |
| 59 | + regionIds?: string[]; | |
| 60 | + groupIds?: string[]; | |
| 61 | + appliedLocation?: AppliedScopeType; | |
| 62 | + locationIds?: string[]; | |
| 63 | + appliedLocationIds?: string[]; | |
| 64 | +}; | |
| 65 | + | |
| 66 | +/** 分类/类型/多选项保存:系统管理员传 partnerIds 数组 + Select All;公司管理员单公司 */ | |
| 67 | +export function buildLabelingScopeSaveBody(input: { | |
| 68 | + requireCompanySelection: boolean; | |
| 69 | + selectedPartnerId: string; | |
| 70 | + fixedPartnerId: string; | |
| 71 | + selectedPartnerIds: string[]; | |
| 72 | + selectedRegionNames: string[]; | |
| 73 | + selectedRegionIds: string[]; | |
| 74 | + selectedLocationIds: string[]; | |
| 75 | + partners: PartnerListItem[]; | |
| 76 | + groups: GroupListItem[]; | |
| 77 | + locations: LocationDto[]; | |
| 78 | +}): { ok: true; body: LabelingScopeSaveBody } | { ok: false; message: string } { | |
| 79 | + if (input.requireCompanySelection) { | |
| 80 | + const availablePartnerIds = input.partners | |
| 81 | + .map((p) => String(p.id ?? "").trim()) | |
| 82 | + .filter(Boolean); | |
| 83 | + const effectivePartnerIds = effectiveScopePartnerIds( | |
| 84 | + true, | |
| 85 | + input.selectedPartnerIds, | |
| 86 | + "", | |
| 87 | + ); | |
| 88 | + const partnerErr = scopePartnersValidationMessage(true, effectivePartnerIds); | |
| 89 | + if (partnerErr) return { ok: false, message: partnerErr }; | |
| 90 | + | |
| 91 | + const partnerIdsForFilter = | |
| 92 | + effectivePartnerIds.length > 0 ? effectivePartnerIds : availablePartnerIds; | |
| 93 | + const availableRegionIds = regionOptionsForPartners(input.groups, partnerIdsForFilter).map( | |
| 94 | + (r) => r.value, | |
| 95 | + ); | |
| 96 | + const scopedLocs = locationsScopedForTemplateScope( | |
| 97 | + input.locations, | |
| 98 | + input.partners, | |
| 99 | + input.groups, | |
| 100 | + partnerIdsForFilter, | |
| 101 | + input.selectedRegionIds, | |
| 102 | + ); | |
| 103 | + const availableLocationIds = scopedLocs.map((l) => l.id); | |
| 104 | + | |
| 105 | + const scope = buildLabelTemplateScopePayload({ | |
| 106 | + selectedPartnerIds: effectivePartnerIds, | |
| 107 | + selectedRegionIds: input.selectedRegionIds, | |
| 108 | + selectedLocationIds: input.selectedLocationIds, | |
| 109 | + availablePartnerIds, | |
| 110 | + availableRegionIds, | |
| 111 | + availableLocationIds, | |
| 112 | + }); | |
| 113 | + if (!scope.ok) return scope; | |
| 114 | + | |
| 115 | + const { body } = scope; | |
| 116 | + const isFullAll = | |
| 117 | + body.appliedPartnerType === "ALL" | |
| 118 | + && body.appliedRegionType === "ALL" | |
| 119 | + && body.appliedLocation === "ALL"; | |
| 120 | + | |
| 121 | + return { | |
| 122 | + ok: true, | |
| 123 | + body: { | |
| 124 | + availabilityType: isFullAll ? "ALL" : "SPECIFIED", | |
| 125 | + appliedPartnerType: body.appliedPartnerType, | |
| 126 | + partnerIds: body.partnerIds, | |
| 127 | + companyIds: body.companyIds, | |
| 128 | + appliedRegionType: body.appliedRegionType, | |
| 129 | + regionIds: body.regionIds, | |
| 130 | + groupIds: body.groupIds, | |
| 131 | + appliedLocation: body.appliedLocation, | |
| 132 | + locationIds: body.locationIds, | |
| 133 | + appliedLocationIds: body.appliedLocationIds, | |
| 134 | + }, | |
| 135 | + }; | |
| 136 | + } | |
| 137 | + | |
| 138 | + const scopePartnerId = effectiveScopePartnerId( | |
| 139 | + false, | |
| 140 | + input.selectedPartnerId, | |
| 141 | + input.fixedPartnerId, | |
| 142 | + ); | |
| 143 | + const partnerErr = scopePartnerValidationMessage(false, scopePartnerId); | |
| 144 | + if (partnerErr) return { ok: false, message: partnerErr }; | |
| 145 | + | |
| 146 | + const locPayload = buildSpecifiedLocationPayload( | |
| 147 | + input.locations, | |
| 148 | + input.partners, | |
| 149 | + input.groups, | |
| 150 | + scopePartnerId, | |
| 151 | + input.selectedRegionNames, | |
| 152 | + input.selectedLocationIds, | |
| 153 | + ); | |
| 154 | + if (!locPayload.ok) return locPayload; | |
| 155 | + | |
| 156 | + const groupIds = regionNamesToGroupIds(input.selectedRegionNames, input.groups, scopePartnerId); | |
| 157 | + | |
| 158 | + return { | |
| 159 | + ok: true, | |
| 160 | + body: { | |
| 161 | + availabilityType: "SPECIFIED", | |
| 162 | + partnerIds: [scopePartnerId], | |
| 163 | + companyIds: [scopePartnerId], | |
| 164 | + appliedPartnerType: "SPECIFIED", | |
| 165 | + regionIds: groupIds, | |
| 166 | + groupIds, | |
| 167 | + appliedLocation: "SPECIFIED", | |
| 168 | + locationIds: locPayload.locationIds, | |
| 169 | + appliedLocationIds: locPayload.locationIds, | |
| 170 | + }, | |
| 171 | + }; | |
| 172 | +} | |
| 173 | + | |
| 174 | +export function inferPartnerIdsFromLocationIds( | |
| 175 | + locationIds: string[], | |
| 176 | + locations: LocationDto[], | |
| 177 | + partners: PartnerListItem[], | |
| 178 | + groups: GroupListItem[], | |
| 179 | +): string[] { | |
| 180 | + const lids = [...new Set(locationIds.map((x) => String(x).trim()).filter(Boolean))]; | |
| 181 | + const out = new Set<string>(); | |
| 182 | + for (const lid of lids) { | |
| 183 | + const pid = inferPartnerIdFromLocationIds([lid], locations, partners, groups); | |
| 184 | + if (pid) out.add(pid); | |
| 185 | + } | |
| 186 | + return [...out]; | |
| 187 | +} | |
| 188 | + | |
| 189 | +/** 编辑回显:由门店反推多公司 + Region Id */ | |
| 190 | +export function hydrateLabelingScopeFromLocationIds( | |
| 191 | + locationIds: string[], | |
| 192 | + locations: LocationDto[], | |
| 193 | + partners: PartnerListItem[], | |
| 194 | + groups: GroupListItem[], | |
| 195 | +): { partnerIds: string[]; regionIds: string[]; locationIds: string[] } { | |
| 196 | + const lids = [...new Set(locationIds.map((x) => String(x).trim()).filter(Boolean))]; | |
| 197 | + const partnerIds = inferPartnerIdsFromLocationIds(lids, locations, partners, groups); | |
| 198 | + const regionIds = [ | |
| 199 | + ...new Set( | |
| 200 | + lids | |
| 201 | + .map((id) => { | |
| 202 | + const loc = locations.find((l) => l.id === id); | |
| 203 | + if (!loc) return ""; | |
| 204 | + const gn = (loc.groupName ?? "").trim(); | |
| 205 | + const pn = (loc.partner ?? "").trim(); | |
| 206 | + const g = groups.find( | |
| 207 | + (x) => | |
| 208 | + (x.groupName ?? "").trim() === gn | |
| 209 | + && (!pn || (x.partnerName ?? "").trim() === pn), | |
| 210 | + ); | |
| 211 | + return g?.id ?? ""; | |
| 212 | + }) | |
| 213 | + .filter(Boolean), | |
| 214 | + ), | |
| 215 | + ]; | |
| 216 | + return { partnerIds, regionIds, locationIds: lids }; | |
| 217 | +} | |
| 218 | + | |
| 27 | 219 | export function partnerNameById(partners: PartnerListItem[], partnerId: string): string { |
| 28 | 220 | return (partners.find((p) => p.id === partnerId)?.partnerName ?? "").trim(); |
| 29 | 221 | } | ... | ... |
美国版/Food Labeling Management Platform/src/lib/nutritionFactsLayout.ts
| 1 | 1 | /** |
| 2 | - * US Nutrition Facts 面板布局(与图2一致):三列(名称 / 含量 / %DV)、粗细分隔线、可选 "<" 前缀。 | |
| 2 | + * US Nutrition Facts 面板布局:三列(名称 / 含量 / %DV)、粗细分隔线、可选 "<" 前缀。 | |
| 3 | 3 | */ |
| 4 | 4 | export type NutritionDivider = 'none' | 'thin' | 'double'; |
| 5 | 5 | |
| ... | ... | @@ -15,12 +15,12 @@ export type NutritionLayoutRowDef = { |
| 15 | 15 | /** 模板固定营养成分行(图2顺序与文案;大类 dividerAfter=double,其他=thin) */ |
| 16 | 16 | export const NUTRITION_FACTS_LAYOUT_ROWS: readonly NutritionLayoutRowDef[] = [ |
| 17 | 17 | { key: 'fat', label: 'Total Fat', defaultUnit: 'g', labelBold: true, dividerAfter: 'double' }, |
| 18 | - { key: 'transFat', label: 'Trans Fat', defaultUnit: 'g', indent: true, dividerAfter: 'thin' }, | |
| 18 | + { key: 'transFat', label: 'Trans Fat', defaultUnit: 'g', dividerAfter: 'thin' }, | |
| 19 | 19 | { key: 'cholesterol', label: 'Cholesterol', defaultUnit: 'mg', labelBold: true, dividerAfter: 'double' }, |
| 20 | 20 | { key: 'sodium', label: 'Sodium', defaultUnit: 'mg', labelBold: true, dividerAfter: 'double' }, |
| 21 | 21 | { key: 'carbs', label: 'Total Carbo.', defaultUnit: 'g', labelBold: true, dividerAfter: 'double' }, |
| 22 | - { key: 'totalSugar', label: 'Sugars', defaultUnit: 'g', indent: true, dividerAfter: 'thin' }, | |
| 23 | - { key: 'dietaryFiber', label: 'Dietary Fiber', defaultUnit: 'g', indent: true, dividerAfter: 'thin' }, | |
| 22 | + { key: 'totalSugar', label: 'Sugars', defaultUnit: 'g', dividerAfter: 'thin' }, | |
| 23 | + { key: 'dietaryFiber', label: 'Dietary Fiber', defaultUnit: 'g', dividerAfter: 'thin' }, | |
| 24 | 24 | { key: 'protein', label: 'Protein', defaultUnit: 'g', labelBold: true, dividerAfter: 'double' }, |
| 25 | 25 | { key: 'calcium', label: 'Calcium', defaultUnit: 'mg', dividerAfter: 'thin' }, |
| 26 | 26 | { key: 'potassium', label: 'Potassium', defaultUnit: 'mg', dividerAfter: 'thin' }, | ... | ... |
美国版/Food Labeling Management Platform/src/services/labelService.ts
| ... | ... | @@ -146,7 +146,7 @@ export async function createLabel(input: LabelCreateInput): Promise<LabelDto> { |
| 146 | 146 | templateCode: input.templateCode, |
| 147 | 147 | ...locationBodyFromInput(input), |
| 148 | 148 | labelCategoryId: input.labelCategoryId, |
| 149 | - labelTypeId: input.labelTypeId, | |
| 149 | + labelTypeId: String(input.labelTypeId ?? "").trim() || null, | |
| 150 | 150 | productIds: input.productIds, |
| 151 | 151 | labelInfoJson: input.labelInfoJson, |
| 152 | 152 | state: input.state ?? true, | ... | ... |
美国版/Food Labeling Management Platform/src/types/label.ts
| ... | ... | @@ -66,7 +66,7 @@ export type LabelCreateInput = { |
| 66 | 66 | /** 适用门店(至少 1 个;Select All 时为当前 Region 下全部门店 Id) */ |
| 67 | 67 | locationIds: string[]; |
| 68 | 68 | labelCategoryId: string; |
| 69 | - labelTypeId: string; | |
| 69 | + labelTypeId?: string | null; | |
| 70 | 70 | productIds: string[]; // 至少 1 个 |
| 71 | 71 | labelInfoJson?: Record<string, unknown> | null; |
| 72 | 72 | state?: boolean; | ... | ... |
美国版/Food Labeling Management Platform/src/types/labelCategory.ts
| ... | ... | @@ -57,6 +57,11 @@ export type LabelCategoryCreateInput = { |
| 57 | 57 | state?: boolean; |
| 58 | 58 | orderNum?: number | null; |
| 59 | 59 | availabilityType?: "ALL" | "SPECIFIED" | string | null; |
| 60 | + appliedPartnerType?: "ALL" | "SPECIFIED" | string | null; | |
| 61 | + partnerIds?: string[] | null; | |
| 62 | + companyIds?: string[] | null; | |
| 63 | + regionIds?: string[] | null; | |
| 64 | + groupIds?: string[] | null; | |
| 60 | 65 | locationIds?: string[] | null; |
| 61 | 66 | }; |
| 62 | 67 | ... | ... |
美国版/Food Labeling Management Platform/src/types/labelMultipleOption.ts
| ... | ... | @@ -39,6 +39,10 @@ export type LabelMultipleOptionCreateInput = { |
| 39 | 39 | optionValuesJson: string[]; |
| 40 | 40 | state?: boolean; |
| 41 | 41 | orderNum?: number | null; |
| 42 | + availabilityType?: "ALL" | "SPECIFIED" | string | null; | |
| 43 | + appliedPartnerType?: "ALL" | "SPECIFIED" | string | null; | |
| 44 | + partnerIds?: string[] | null; | |
| 45 | + companyIds?: string[] | null; | |
| 42 | 46 | groupIds?: string[] | null; |
| 43 | 47 | regionIds?: string[] | null; |
| 44 | 48 | locationIds?: string[] | null; | ... | ... |
美国版/Food Labeling Management Platform/src/types/labelTemplate.ts
| ... | ... | @@ -989,43 +989,6 @@ export function deriveContentsLabelFromTypeAdd(el: LabelElement): string { |
| 989 | 989 | return fallback[type] ?? inferPaletteEnglishLabel(el); |
| 990 | 990 | } |
| 991 | 991 | |
| 992 | -function isLegacySlugElementName(name: string, el: LabelElement): boolean { | |
| 993 | - if (name.includes(" ") || name.includes("(")) return false; | |
| 994 | - if (!/^[a-z0-9]+$/i.test(name)) return false; | |
| 995 | - const field = inferPaletteEnglishLabel(el); | |
| 996 | - const slugField = slugPaletteItemLabel(field); | |
| 997 | - const lower = name.toLowerCase(); | |
| 998 | - if (lower === slugField) return true; | |
| 999 | - return lower.startsWith(slugField) && /^\d+$/.test(lower.slice(slugField.length)); | |
| 1000 | -} | |
| 1001 | - | |
| 1002 | -/** Contents / 列表展示:优先 Element name;旧 slug 则按 typeAdd 推导 */ | |
| 1003 | -export function resolveElementContentsLabel(el: LabelElement): string { | |
| 1004 | - const name = (el.elementName ?? "").trim(); | |
| 1005 | - const derived = deriveContentsLabelFromTypeAdd(el); | |
| 1006 | - if (!name) return derived; | |
| 1007 | - if (isLegacySlugElementName(name, el)) return derived || name; | |
| 1008 | - return name; | |
| 1009 | -} | |
| 1010 | - | |
| 1011 | -/** 保存模板时 contents:各控件展示名按 orderNum 去重后逗号拼接 */ | |
| 1012 | -export function buildTemplateContentsFromElements(elements: LabelElement[]): string { | |
| 1013 | - const seen = new Set<string>(); | |
| 1014 | - const names: string[] = []; | |
| 1015 | - const sorted = [...elements].sort( | |
| 1016 | - (a, b) => (a.orderNum ?? 0) - (b.orderNum ?? 0) || String(a.id).localeCompare(String(b.id)), | |
| 1017 | - ); | |
| 1018 | - for (const el of sorted) { | |
| 1019 | - const name = resolveElementContentsLabel(el).trim(); | |
| 1020 | - if (!name) continue; | |
| 1021 | - const key = name.toLowerCase(); | |
| 1022 | - if (seen.has(key)) continue; | |
| 1023 | - seen.add(key); | |
| 1024 | - names.push(name); | |
| 1025 | - } | |
| 1026 | - return names.join(", "); | |
| 1027 | -} | |
| 1028 | - | |
| 1029 | 992 | function dedupeIdList(ids: unknown[] | null | undefined): string[] { |
| 1030 | 993 | if (!Array.isArray(ids)) return []; |
| 1031 | 994 | return [...new Set(ids.map((x) => String(x).trim()).filter(Boolean))]; |
| ... | ... | @@ -1159,7 +1122,7 @@ export function isPrintInputElement(el: LabelElement): boolean { |
| 1159 | 1122 | return false; |
| 1160 | 1123 | } |
| 1161 | 1124 | |
| 1162 | -/** 录入数据表列标题 */ | |
| 1125 | +/** 录入数据表列标题 / App 打印页字段提示(Pre-text,config.text) */ | |
| 1163 | 1126 | export function printInputFieldLabel(el: LabelElement): string { |
| 1164 | 1127 | const cfg = (el.config ?? {}) as Record<string, unknown>; |
| 1165 | 1128 | const type = canonicalElementType(el.type); | ... | ... |
美国版/Food Labeling Management Platform/src/types/labelType.ts
| ... | ... | @@ -44,6 +44,10 @@ export type LabelTypeCreateInput = { |
| 44 | 44 | typeName: string; |
| 45 | 45 | state?: boolean; |
| 46 | 46 | orderNum?: number | null; |
| 47 | + availabilityType?: "ALL" | "SPECIFIED" | string | null; | |
| 48 | + appliedPartnerType?: "ALL" | "SPECIFIED" | string | null; | |
| 49 | + partnerIds?: string[] | null; | |
| 50 | + companyIds?: string[] | null; | |
| 47 | 51 | /** Region 多选(`fl_group.Id`) */ |
| 48 | 52 | groupIds?: string[] | null; |
| 49 | 53 | regionIds?: string[] | null; | ... | ... |
美国版/Food Labeling Management Platform/src/utils/textElementLayout.ts
| ... | ... | @@ -80,6 +80,47 @@ export function readElementRotation( |
| 80 | 80 | return v === 'vertical' ? 'vertical' : 'horizontal'; |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | +/** 与 App lineHeightForTextElement 一致:leading-tight ≈ 1.25 */ | |
| 84 | +export function textElementLineHeightPx(fontSize: number): number { | |
| 85 | + return Math.max(fontSize + 2, Math.round(fontSize * 1.25)); | |
| 86 | +} | |
| 87 | + | |
| 88 | +/** 矮框(约 1 行高)内的固定文案:预览/打印应单行展示,避免 break-all 折行后第二行被裁切 */ | |
| 89 | +export function isShortSingleLineTextBox( | |
| 90 | + boxHeightPx: number, | |
| 91 | + fontSize: number, | |
| 92 | + text: string, | |
| 93 | + opts?: { breakAll?: boolean }, | |
| 94 | +): boolean { | |
| 95 | + if (opts?.breakAll) return false; | |
| 96 | + if (String(text ?? '').includes('\n')) return false; | |
| 97 | + const lineH = textElementLineHeightPx(fontSize); | |
| 98 | + const descentPad = Math.max(2, Math.round(fontSize * 0.14)); | |
| 99 | + const maxLines = Math.max( | |
| 100 | + 1, | |
| 101 | + Math.floor((Math.max(0, boxHeightPx) + descentPad) / lineH), | |
| 102 | + ); | |
| 103 | + return maxLines <= 1; | |
| 104 | +} | |
| 105 | + | |
| 106 | +/** 单行文案在 inner 宽度内微缩字号(与 App canvas fit 逻辑一致,供 Web 预览) */ | |
| 107 | +export function fitFontSizeToInnerWidthEstimate( | |
| 108 | + text: string, | |
| 109 | + innerWidthPx: number, | |
| 110 | + fontSize: number, | |
| 111 | + bold = false, | |
| 112 | +): number { | |
| 113 | + if (!text || innerWidthPx <= 0) return fontSize; | |
| 114 | + const factor = bold ? 0.62 : 0.55; | |
| 115 | + let size = fontSize; | |
| 116 | + for (let i = 0; i < 5; i++) { | |
| 117 | + const w = Math.max(4, String(text).length * size * factor); | |
| 118 | + if (w <= innerWidthPx) return size; | |
| 119 | + size = Math.max(8, Math.floor(size * (innerWidthPx / w) * 0.98)); | |
| 120 | + } | |
| 121 | + return Math.max(8, size); | |
| 122 | +} | |
| 123 | + | |
| 83 | 124 | /** 切换 horizontal / vertical 时交换宽高并保持中心点不变(与参考图 2 一致) */ |
| 84 | 125 | export function patchElementRotationWithLayout( |
| 85 | 126 | el: { x: number; y: number; width: number; height: number; rotation?: string | null }, | ... | ... |