From f24ec48c98a485af26321f7f4b74fe1611162f5a Mon Sep 17 00:00:00 2001
From: spring <2396852758@qq.com>
Date: 星期三, 25 三月 2026 18:05:32 +0800
Subject: [PATCH] fix: 吨/公斤换算

---
 src/pages/inventoryManagement/stockManagement/subtract.vue |   73 ++++++++++++++++++++++++++++++++++--
 1 files changed, 68 insertions(+), 5 deletions(-)

diff --git a/src/pages/inventoryManagement/stockManagement/subtract.vue b/src/pages/inventoryManagement/stockManagement/subtract.vue
index 7fb85e1..6da92d4 100644
--- a/src/pages/inventoryManagement/stockManagement/subtract.vue
+++ b/src/pages/inventoryManagement/stockManagement/subtract.vue
@@ -14,7 +14,14 @@
         </view>
         <view class="form-row">
           <text class="form-label">鍗曚綅</text>
-          <up-input v-model="form.unit" disabled />
+          <up-radio-group v-model="form.unit" class="unit-radio-group">
+            <up-radio
+              v-for="opt in unitOptions"
+              :key="opt.value"
+              :label="opt.label"
+              :name="opt.value"
+            ></up-radio>
+          </up-radio-group>
         </view>
       </view>
 
@@ -22,7 +29,11 @@
         <view class="section-title">鍑哄簱淇℃伅</view>
         <view class="form-row">
           <text class="form-label">鍑哄簱鏁伴噺</text>
-          <up-input v-model="form.stockOutNum" type="number" :placeholder="'鏈�澶�' + (form.unLockedQuantity ?? 0)" />
+          <up-input
+            v-model="form.stockOutNum"
+            type="number"
+            :placeholder="'鏈�澶�' + maxOutQuantity"
+          />
         </view>
         <view class="form-row">
           <text class="form-label">澶囨敞</text>
@@ -52,7 +63,7 @@
   parentName: '',
   productName: '',
   model: '',
-  unit: '',
+  unit: '鍚�',
   qualitity: undefined,
   lockedQuantity: undefined,
   unLockedQuantity: undefined,
@@ -69,6 +80,45 @@
 })
 
 const type = ref('0') // 鍥哄畾鍚堟牸搴撳瓨
+const unitOptions = [
+  { label: '鍚�', value: '鍚�' },
+  { label: '鍏枻', value: '鍏枻' }
+]
+
+// 璁板綍鐨勫師濮嬪崟浣嶏紝鐢ㄤ簬鍚�/鍏枻鎹㈢畻鏍¢獙鈥滄渶澶у彲鍑哄簱鏁伴噺鈥�
+const recordUnit = ref('')
+
+const normalizeUnit = (u) => {
+  if (!u) return ''
+  const s = String(u).trim()
+  if (s === '鍚�' || s === 't' || s === 'ton' || s === 'tonne') return '鍚�'
+  if (
+    s === '鍏枻' ||
+    s === 'kg' ||
+    s === 'kilogram' ||
+    s === '鍗冨厠' ||
+    s === 'kilograms'
+  )
+    return '鍏枻'
+  return s
+}
+
+const convertByUnit = (value, fromUnit, toUnit) => {
+  if (value === '' || value === undefined || value === null) return 0
+  const num = Number(value)
+  if (Number.isNaN(num)) return 0
+  if (fromUnit === toUnit) return num
+  // 榛樿鍙鐞嗗惃<->鍏枻锛涘叾浠栧崟浣嶇洿鎺ュ師鏍疯繑鍥�
+  if (fromUnit === '鍚�' && toUnit === '鍏枻') return num * 1000
+  if (fromUnit === '鍏枻' && toUnit === '鍚�') return num / 1000
+  return num
+}
+
+const maxOutQuantity = computed(() => {
+  const baseMax = Number(form.unLockedQuantity ?? 0)
+  if (!recordUnit.value || !form.unit) return baseMax
+  return convertByUnit(baseMax, recordUnit.value, form.unit)
+})
 
 onLoad((options) => {
   type.value = '0'
@@ -79,6 +129,9 @@
       const item = payload && payload.item ? payload.item : payload
       // 灏嗗垪琛ㄨ褰曠殑瀹屾暣瀛楁鎷疯礉鍒拌〃鍗曚腑锛屼繚鎸佷笌 PC 绔� Subtract.vue 涓�鑷�
       Object.assign(form, item)
+      const normalizedUnit = normalizeUnit(form.unit)
+      form.unit = normalizedUnit === '鍚�' || normalizedUnit === '鍏枻' ? normalizedUnit : '鍚�'
+      recordUnit.value = form.unit
       uni.removeStorageSync('stockSubtractRecord')
     } catch (e) {
       uni.removeStorageSync('stockSubtractRecord')
@@ -91,8 +144,11 @@
     uni.showToast({ title: '璁板綍淇℃伅缂哄け锛屾棤娉曞嚭搴�', icon: 'none' })
     return
   }
+  const normalizedUnit = normalizeUnit(form.unit)
+  form.unit = normalizedUnit === '鍚�' || normalizedUnit === '鍏枻' ? normalizedUnit : '鍚�'
+
   const outNum = Number(form.stockOutNum)
-  const max = Number(form.unLockedQuantity ?? 0)
+  const max = Number(maxOutQuantity.value ?? 0)
   if (!outNum || outNum <= 0 || outNum > max) {
     uni.showToast({ title: `璇疯緭鍏� 1~${max} 涔嬮棿鐨勬暟閲廯, icon: 'none' })
     return
@@ -100,7 +156,8 @@
   const payload = {
     id: form.id,
     stockOutNum: outNum,
-    remark: form.remark
+    remark: form.remark,
+    unit: form.unit
   }
   subtractStockInventory(payload)
     .then(() => {
@@ -181,5 +238,11 @@
   align-items: center;
   justify-content: center;
 }
+.unit-radio-group {
+  display: flex;
+  gap: 24rpx;
+  align-items: center;
+  flex-wrap: wrap;
+}
 </style>
 

--
Gitblit v1.9.3