tools.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import * as optionsAll from '@/components/options.js'
  2. let keySeed = 1
  3. export const genKey = (prefix = 'random-key') => {
  4. return `${prefix}-${keySeed ++}`
  5. }
  6. export const clone = (obj) => {
  7. return JSON.parse(JSON.stringify(obj))
  8. }
  9. export const str = (obj, def = '') => {
  10. if (obj) return obj + ''
  11. return def
  12. }
  13. export const strOrNull = (obj) => {
  14. return str(obj, null)
  15. }
  16. export const anyEq = (arr, b) => {
  17. for(const d of arr) {
  18. if(d !== b) return false
  19. }
  20. return true
  21. }
  22. export const anyNull = (arr) => {
  23. for(const d of arr) {
  24. if(d === null || d === undefined) return true
  25. }
  26. return false
  27. }
  28. export const validate = (field, message) => {
  29. if (!field || !field.trim()) {
  30. errorMsg(message)
  31. return false
  32. }
  33. return true
  34. }
  35. export const errorMsg = (message) => {
  36. uni.showToast({
  37. title: message,
  38. icon: 'error',
  39. })
  40. }
  41. export const successMsg = (message) => {
  42. uni.showToast({
  43. title: message,
  44. icon: 'success',
  45. })
  46. }
  47. export const staticName = (type, code, def = '') => {
  48. if(type in optionsAll) {
  49. const opts = optionsAll[type]
  50. const o = opts.find(d => d.value === code)
  51. return o ? o.text : def
  52. }
  53. return def
  54. }
  55. export const parseField2Float = (obj, field) => {
  56. parseField2SomeType(obj, field, parseFloat)
  57. }
  58. export const parseField2Integer = (obj, field) => {
  59. parseField2SomeType(obj, field, parseInt)
  60. }
  61. export const parseField2String = (obj, field) => {
  62. parseField2SomeType(obj, field, (d) => {
  63. return d + ''
  64. })
  65. }
  66. export const parseField2SomeType = (obj, field, convertor) => {
  67. if(typeof field === 'string') {
  68. if(obj[field])
  69. obj[field] = convertor(obj[field])
  70. }else if(Array.isArray(field)) {
  71. for(let f of field) {
  72. if(notNull(obj[f]))
  73. obj[f] = convertor(obj[f])
  74. }
  75. }
  76. }
  77. export const convertAll = (arrs, fun) => {
  78. const ret = []
  79. for(const d of arrs) {
  80. ret.push(fun(d))
  81. }
  82. return ret
  83. }
  84. export const deleteField = (obj, field) => {
  85. if(typeof field === 'string') {
  86. delete obj[field]
  87. }else if(Array.isArray(field)) {
  88. for(let f of field) {
  89. delete obj[f]
  90. }
  91. }
  92. }
  93. export const deleteListField = (list, field) => {
  94. if(list && list.length > 0) {
  95. list.forEach(d => {
  96. deleteField(d, field)
  97. })
  98. }
  99. }
  100. export const isNull = (obj) => null === obj || undefined === obj
  101. export const notNull = (obj) => ! isNull(obj)