123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- import * as optionsAll from '@/components/options.js'
- let keySeed = 1
- export const genKey = (prefix = 'random-key') => {
- return `${prefix}-${keySeed ++}`
- }
- export const clone = (obj) => {
- return JSON.parse(JSON.stringify(obj))
- }
- export const str = (obj, def = '') => {
- if (obj) return obj + ''
- return def
- }
- export const strOrNull = (obj) => {
- return str(obj, null)
- }
- export const anyEq = (arr, b) => {
- for(const d of arr) {
- if(d !== b) return false
- }
- return true
- }
- export const anyNull = (arr) => {
- for(const d of arr) {
- if(d === null || d === undefined) return true
- }
- return false
- }
- export const validate = (field, message) => {
- if (!field || !field.trim()) {
- errorMsg(message)
- return false
- }
- return true
- }
- export const errorMsg = (message) => {
- uni.showToast({
- title: message,
- icon: 'error',
- })
- }
- export const successMsg = (message) => {
- uni.showToast({
- title: message,
- icon: 'success',
- })
- }
- export const staticName = (type, code, def = '') => {
- if(type in optionsAll) {
- const opts = optionsAll[type]
- const o = opts.find(d => d.value === code)
- return o ? o.text : def
- }
- return def
- }
- export const parseField2Float = (obj, field) => {
- parseField2SomeType(obj, field, parseFloat)
- }
- export const parseField2Integer = (obj, field) => {
- parseField2SomeType(obj, field, parseInt)
- }
- export const parseField2String = (obj, field) => {
- parseField2SomeType(obj, field, (d) => {
- return d + ''
- })
- }
- export const parseField2SomeType = (obj, field, convertor) => {
- if(typeof field === 'string') {
- if(obj[field])
- obj[field] = convertor(obj[field])
- }else if(Array.isArray(field)) {
- for(let f of field) {
- if(notNull(obj[f]))
- obj[f] = convertor(obj[f])
- }
- }
- }
- export const convertAll = (arrs, fun) => {
- const ret = []
- for(const d of arrs) {
- ret.push(fun(d))
- }
- return ret
- }
- export const deleteField = (obj, field) => {
- if(typeof field === 'string') {
- delete obj[field]
- }else if(Array.isArray(field)) {
- for(let f of field) {
- delete obj[f]
- }
- }
- }
- export const deleteListField = (list, field) => {
- if(list && list.length > 0) {
- list.forEach(d => {
- deleteField(d, field)
- })
- }
- }
- export const isNull = (obj) => null === obj || undefined === obj
- export const notNull = (obj) => ! isNull(obj)
|