feat:分析值保留两位有效数字计算

This commit is contained in:
houjunxiang
2026-02-05 11:10:44 +08:00
parent e10c70e819
commit da01e1efcd
8 changed files with 123 additions and 80 deletions

View File

@@ -7,7 +7,7 @@
<span>{{ title }}</span>
</view>
<view class="x-c">
<u-icon size="150" color="#0055A2" name="scan"></u-icon>
<u-icon size="150" color="#0055A2" name="scan" @click="handleScan"></u-icon>
</view>
<!-- #ifdef H5 -->
<up-search
@@ -47,6 +47,14 @@ function iconMap(key) {
return customIconsMap.get(key)
}
function handleScan() {
uni.scanCode({
onlyFromCamera: true,
success: function (res) {
emits('scanResult', res.result)
}
})
}
function handleInputSearch(e) {
emits('deviceId', e)
}

View File

@@ -2,26 +2,26 @@ import request from '@/nx/request'
export default {
queryById: id =>
request({
url: '/lims/bus/deviceBusMaintain/getMaintainDetail',
url: '/qms/resource/device-maintain/getMaintainDetail',
method: 'GET',
params: { id }
}),
list: params =>
request({
url: '/lims/bus/deviceBusMaintain/list',
url: '/qms/resource/device-maintain/page',
method: 'GET',
params
}),
// 根据设备id或者维护和点检记录
getCheckRecord: data =>
request({
url: '/lims/bus/deviceBusMaintain/createOrGet',
url: '/qms/resource/device-maintain/createOrGet',
method: 'POST',
data
}),
submit: data =>
request({
url: '/lims/bus/deviceBusMaintain/saveMaintainVo',
url: '/qms/resource/device-maintain/saveMaintainVo',
method: 'POST',
data,
custom: {
@@ -30,13 +30,13 @@ export default {
}),
getLastDailyCheckOfToday: params =>
request({
url: '/lims/bus/deviceBusMaintain/getLastDailyCheckOfToday',
url: '/qms/resource/device-maintain/getLastDailyCheckOfToday',
method: 'GET',
params,
}),
createDailyCheck: data =>
request({
url: '/lims/bus/deviceBusMaintain/create',
url: '/qms/resource/device-maintain/create',
method: 'POST',
data
})

View File

@@ -64,11 +64,8 @@ const Get_C_KNO3 = function (weight, sValue, operator) {
//判断sValue是数字
const S = number(sValue)
const W = number(weight)
console.log(S, W)
//如果样重为0就不计算
if (W === 0) {
console.log('结束')
return ''
}
if (W === 0) return ''
@@ -130,7 +127,7 @@ export function calcAnalysisValue(group, externalFormData, taskIngredientsWay) {
v = math.evaluate(formulaVal).toString()
v = isFinite(v) ? v.toString() : 0
}
ele.value = handleRoundFiveNumber(v, ele.decimalPosition)
ele.value = handleRoundFiveNumber(v, ele.decimalPosition, ele.type === 'project')
changed = true
}
}
@@ -152,13 +149,11 @@ export function evaluateGetFromFormula(formula, parse, externalFormData, current
relFormulaVal = relFormulaVal.replace(key, value || 0)
}
}
console.log(relFormulaVal)
try {
let v = math.evaluate(relFormulaVal).toString()
v = isFinite(v) ? v : 0
if (parse) {
return handleRoundFiveNumber(v, currentColumn.decimalPosition)
return handleRoundFiveNumber(v, currentColumn.decimalPosition, currentColumn.type === 'project')
}
return v
} catch (e) {
@@ -212,7 +207,8 @@ export function calcRowAnalysisValue(row, columnObj, dynamicsColumns, externalFo
v = math.evaluate(formulaVal).toString()
v = isFinite(v) ? v : 0
}
row[curItem.fieldIndex].value = handleRoundFiveNumber(Number(v), row[curItem.fieldIndex].decimalPosition)
let rowObj = row[curItem.fieldIndex]
rowObj.value = handleRoundFiveNumber(Number(v), rowObj.decimalPosition, rowObj.type === 'project')
calcRowAnalysisValue(row, curItem, dynamicsColumns, externalFormData)
}
}
@@ -235,24 +231,56 @@ const findFieldInGroup = function (paramNo, group, p) {
* 3.四舍,六入
* 传入数值和保留位数
*/
export function handleRoundFiveNumber(number, fixed = 0) {
export function handleRoundFiveNumber(number, fixed = 0, useTwoSignificantDigits = false) {
if (number == null || number === '' || isNaN(number)) return number
if (fixed === -1) return number
number = String(number)
//可以考虑清掉末尾的0暂时限制了不会有,因为传入数值末尾0是去掉的
// 保留2位有效数字小数点后面大于0的才叫有效数字从小数点后面查找两位有效数字 ,如果末尾的有效数字的位数大于所设置的精度 以及 如果查找的有效数字没有两位的 ,就按照所设置的精度逻辑来计算,两位有效数字的最后 一位是在第几位,就用这个位数充当精度来按照原来精度的逻辑计算,
// ===== 新增:按小数点后前两个非零数字确定精度 =====
let actualFixed = fixed
if (useTwoSignificantDigits) {
const numStr = String(number)
const dotIndex = numStr.indexOf('.')
if (dotIndex !== -1) {
const decimalPart = numStr.slice(dotIndex + 1)
let nonZeroCount = 0
let secondNonZeroPos = -1
for (let i = 0; i < decimalPart.length; i++) {
if (decimalPart[i] !== '0') {
nonZeroCount++
if (nonZeroCount === 2) {
secondNonZeroPos = i + 1 // 小数点后第几位从1开始
break
}
}
}
// 只有同时满足:
// 1. 找到两个非零数字
// 2. 第二个的位置 <= 用户指定的 fixed
// 才使用该位置作为精度
if (secondNonZeroPos !== -1 && secondNonZeroPos <= fixed) {
actualFixed = secondNonZeroPos
}
}
}
console.log(actualFixed)
const index = number.indexOf('.')
if (index == -1) {
if (fixed > 0) {
if (actualFixed > 0) {
number += '.'
}
for (let i = 0; i < fixed; i++) {
for (let i = 0; i < actualFixed; i++) {
number += '0'
}
//补0返回
return number
}
//取到保留小数位的下一位5.5555取保留俩位,那就是小数点后第三位
const indexFixed = index + fixed + 1
const indexFixed = index + actualFixed + 1
if (indexFixed >= number.length) {
//如果小数位数不够, 补0直接返回
const zerolen = indexFixed - number.length
@@ -265,16 +293,16 @@ export function handleRoundFiveNumber(number, fixed = 0) {
const endNumber = number.substr(indexFixed, 1)
if (endNumber != '5') {
//如果做判断的数不是五就按正常的四舍五入即忽略了5后面补0的那些由于是数字传进来0已经去掉此处不做处理
return Number(number).toFixed(fixed)
return Number(number).toFixed(actualFixed)
}
if (indexFixed != number.length - 1) {
//由于当前判断位不是最后一位而又去除了0那么后面后的位数应该直接入位五后不为0时入,由于会有小于五的四舍五入肯定不行那么只能截取到当前保留位数然后转成数字加上10的负fixed的次方即可
number = number.substring(0, indexFixed)
if (number.indexOf('-') != -1) {
//需要考虑到负数的情况
return (Number(number) - Number(Math.pow(10, -fixed))).toFixed(fixed)
return (Number(number) - Number(Math.pow(10, -actualFixed))).toFixed(actualFixed)
}
return (Number(number) + Number(Math.pow(10, -fixed))).toFixed(fixed)
return (Number(number) + Number(Math.pow(10, -actualFixed))).toFixed(actualFixed)
//return this.accAdd(number,Math.pow(10,-fixed))
}
//接下来就时五后没有值也就是0的需要看前面的奇入偶不入了取当前位的上一位
@@ -288,9 +316,9 @@ export function handleRoundFiveNumber(number, fixed = 0) {
number = number.substring(0, indexFixed)
if (number.indexOf('-') != -1) {
//需要考虑到负数的情况
return (Number(number) - Math.pow(10, -fixed)).toFixed(fixed)
return (Number(number) - Math.pow(10, -actualFixed)).toFixed(actualFixed)
}
return (Number(number) + Math.pow(10, -fixed)).toFixed(fixed)
return (Number(number) + Math.pow(10, -actualFixed)).toFixed(actualFixed)
}
//偶不进,将取值的当前位数,直接截取字符即可
return number.substr(0, indexFixed)

View File

@@ -674,7 +674,7 @@ const saveDetail = async () => {
if (item.calcMethod === 'calculateAverageValue') {
item.value = calcAverageValue(sourceKey, currentAssayType.value.tableData)
} else {
item.value = handleRoundFiveNumber(row[sourceKey].value, row.decimalPosition)
item.value = handleRoundFiveNumber(row[sourceKey].value, row.decimalPosition, row.type === 'project')
}
// 如果处理后的值不为空,重新赋值该字段到其他样品类型下的样品上,并触发每一个样品的计算
for (const col of currentAssayType.value.columns) {
@@ -707,9 +707,10 @@ const saveDetail = async () => {
function calcAverageValue(fieldIndex, tableData) {
const rows = tableData.map(row => row[fieldIndex])
const decimalPosition = rows[0].decimalPosition
const isProject = rows[0].type === 'project'
const values = rows.map(row => Number(row.value))
if (values.some(item => item == null)) return null
return handleRoundFiveNumber(math.mean(values), decimalPosition)
return handleRoundFiveNumber(math.mean(values), decimalPosition, isProject)
}
// 表格数据更新后重新计算
@@ -1109,12 +1110,13 @@ const listenNumKeyboard = () => {
}
//自动补全小数位数
const decimalPosition = selectedField.value.decimalPosition
const isProject = selectedField.value.type === 'project'
let val = res.val
const symbol = res.symbol
if (decimalPosition == null) {
selectedField.value.value = val
} else {
selectedField.value.value = handleRoundFiveNumber(val, decimalPosition)
selectedField.value.value = handleRoundFiveNumber(val, decimalPosition, isProject)
}
if (symbol) {
selectedField.value.symbol = symbol

View File

@@ -1,14 +1,14 @@
<template>
<view>
<up-sticky>
<navbar-back title="检">
<navbar-back title="设备巡检">
<up-button
v-if="detailInfo.id"
type="primary"
:plain="true"
icon="list"
size="small"
text="设备检记录"
text="设备检记录"
@click="handleCheckRecord"
></up-button>
</navbar-back>
@@ -16,18 +16,19 @@
<view class="container">
<n-scanTemp
v-if="!detailInfo.id"
title="请扫描设备条码进行检"
title="请扫描设备条码进行检"
icon="dailyCheck"
@deviceId="id => getDailyCheckRecord(id)"
@scanResult="result => handleScanResult(result)"
/>
<view v-else class="content">
<view>
<uni-section titleFontSize="22px" type="line" title="设备检信息">
<uni-section titleFontSize="22px" type="line" title="设备检信息">
<template v-slot:right>
<up-button
v-if="detailInfo.submitFlag == '1'"
type="success"
text="新建检"
text="新建检"
@click="handleCreateDailyCheck"
></up-button>
</template>
@@ -43,7 +44,7 @@
<view>
<uni-section titleFontSize="22px" type="line" title="检查项"> </uni-section>
<up-row class="p10 font-bold" style="background-color: #f5f5f5">
<up-col span="4">检项目</up-col>
<up-col span="4">检项目</up-col>
<up-col span="3">检查标准</up-col>
<up-col span="2"> 频次</up-col>
<up-col style="text-align: center" span="3">是否正常</up-col>
@@ -75,10 +76,10 @@
<up-textarea v-model="detailInfo.content" placeholder="请输入内容"></up-textarea>
<view class="p10">附件照片</view>
<n-upload v-model="detailInfo.attachment" />
<view class="p10">检人</view>
<view class="p10">检人</view>
<up-input v-model="detailInfo.checkUserName"></up-input>
<view v-if="detailInfo.submitFlag == '1'">
<view class="p10">检日期</view>
<view class="p10">检日期</view>
<uni-datetime-picker type="datetime" v-model="detailInfo.checkDate" />
</view>
</view>
@@ -129,32 +130,37 @@ const { lockOrientation } = useScreenOrientation()
const pageLoading = ref(false)
let detailInfo = ref({})
const { scanQRInfo } = toRefs(nx.$store('biz'))
watch(scanQRInfo, newVal => {
if (newVal && nx.$router.getCurrentPage().route == 'pages/device/deviceBusDailyCheck/index') {
try {
const codeObj = JSON.parse(newVal)
if (!pageLoading.value) {
getDailyCheckRecord(codeObj.id)
}
scanQRInfo.value = ''
} catch (error) {
scanQRInfo.value = ''
uni.showToast({
title: '请扫描设备码',
icon: 'none'
})
}
function handleScanResult(result) {
const codeObj = JSON.parse(result)
if (!pageLoading.value) {
getDailyCheckRecord(codeObj.id)
}
})
}
// const { scanQRInfo } = toRefs(nx.$store('biz'))
// watch(scanQRInfo, newVal => {
// if (newVal && nx.$router.getCurrentPage().route == 'pages/device/deviceBusDailyCheck/index') {
// try {
// const codeObj = JSON.parse(newVal)
// if (!pageLoading.value) {
// getDailyCheckRecord(codeObj.id)
// }
// scanQRInfo.value = ''
// } catch (error) {
// scanQRInfo.value = ''
// uni.showToast({
// title: '请扫描设备码',
// icon: 'none'
// })
// }
// }
// })
const modalText = computed(() => {
return `确定${modalType.value == '0' ? '暂存' : '提交'}吗?${modalType.value == '0' ? '' : '提交后不能修改'} `
})
onShow(() => {
scanQRInfo.value = ''
})
let goBack = ref(false)
// onShow(() => {
// scanQRInfo.value = ''
// })
let goBack = ref(false) //设备使用前进行点检操作标记
onLoad(options => {
if (options.deviceId) {
goBack.value = true
@@ -211,7 +217,7 @@ function handleSubmit(type) {
}
if (!detailInfo.value.checkUserName) {
return uni.showToast({
title: '请输入检人',
title: '请输入检人',
icon: 'none'
})
}
@@ -246,11 +252,11 @@ function handleCheckRecord() {
nx.$store('biz').deviceInfo = deviceInfo
nx.$router.go('/pages/device/deviceBusDailyCheck/list')
}
// 新建
// 新建
function handleCreateDailyCheck() {
uni.showModal({
title: '提示',
content: '确定新建检吗?',
content: '确定新建检吗?',
success: async function (res) {
if (res.confirm) {
const res = await dailyCheckApi.createDailyCheck({

View File

@@ -156,7 +156,7 @@ function getLastDailyCheckOfToday(id) {
})
}, 100)
pageLoading.value = false
nx.$router.go('/pages/deviceBusDailyCheck/index', { deviceId: id })
nx.$router.go('/pages/device/deviceBusDailyCheck/index', { deviceId: id })
} else {
getDeviceInfo(id)
await getUseIngRecord(id)

View File

@@ -3,38 +3,37 @@
<navbar-back title="实验室管理系统【设备管理】" :autoBack="false" leftIcon="" leftText="">
<u-icon @click="popupShow = true" size="28" color="#FFF" name="setting-fill" />
</navbar-back>
<up-grid :border="false" :col="gridCol">
<up-grid-item
class="mb20 mt20"
v-for="(item, listIndex) in list"
:key="listIndex"
@click="nx.$router.go(item.url)"
>
<image style="width: 80px; height: 80px" :src="`/static/images/menus/${item.icon}.png`"></image>
<text class="grid-text">{{ item.name }}</text>
<up-grid :col="gridCol" :border="false">
<up-grid-item class="mb20 mt20" v-for="item in menuItemList" :key="item.id" @click="goTo(item.component)">
<u-icon :name="`/static/images/menus/${item.path}.png`" color="#0055A2" size="80" />
<view class="grid-text">{{ item.name }}</view>
</up-grid-item>
</up-grid>
<mePopup v-model:show="popupShow" />
</view>
</template>
<script setup>
import { reactive, ref, computed } from 'vue'
import { ref, computed, onMounted } from 'vue'
import nx from '@/nx'
import { useGridCol } from '@/nx/hooks/useGridCol'
import mePopup from '@/pages/index/me-popup.vue'
const { gridCol } = useGridCol([400, 600], [2, 3, 4])
let popupShow = ref(false)
let list = reactive([
{ url: '/pages/device/deviceBusDailyCheck/index', name: '点检', icon: 'dailyCheck' },
{ url: '/pages/device/deviceBusMaintain/index', name: '维护保养', icon: 'maintain' },
{ url: '/pages/device/deviceBusUseRecord/index', name: '使用', icon: 'useRecord' },
{ url: '/pages/device/deviceBusInfo/index', name: '设备查询', icon: 'baseInfo' },
{ url: '/pages/device/knowledge/index', name: '知识库查询', icon: 'knowledge' }
])
const menuItemList = computed(() => {
let roleMenus = nx.$store('user').roleMenus
const result = roleMenus.find(item => item.path === 'device')
if (result) {
return result.children
} else {
return []
}
})
const roleMenus = computed(() => nx.$store('user').roleMenus)
const userInfo = computed(() => nx.$store('user').userInfo)
// 方法
const goTo = url => {
nx.$router.go(url)
}
</script>
<style lang="scss" scoped>

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB