peixue-dev/peidu/uniapp/utils/params.js

50 lines
1.1 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 参数处理工具函数
*/
/**
* 解析ID参数确保返回数字类型
* @param {string|number} id - 要解析的ID
* @returns {number|null} - 返回数字ID或null
*/
export function parseId(id) {
if (id === null || id === undefined || id === '') {
return null
}
const num = Number(id)
return isNaN(num) ? null : num
}
/**
* 解析多个ID参数
* @param {object} params - 参数对象
* @param {array} keys - 需要解析的键名数组
* @returns {object} - 解析后的参数对象
*/
export function parseIds(params, keys) {
const result = { ...params }
keys.forEach(key => {
if (result[key] !== undefined) {
result[key] = parseId(result[key])
}
})
return result
}
/**
* 验证必需的ID参数
* @param {number|null} id - ID值
* @param {string} name - 参数名称(用于错误提示)
* @returns {boolean} - 是否有效
*/
export function validateId(id, name = 'ID') {
if (!id) {
uni.showToast({
title: `${name}不能为空`,
icon: 'none'
})
return false
}
return true
}