parse.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import { ref, type Ref } from "vue";
  2. import { forEach, forInObject, isArray, isObject, isString } from "./comm";
  3. /**
  4. * 解析数据
  5. * @example parse<Response>(res.data)
  6. */
  7. export function parse<T>(data: any): T | null {
  8. // #ifdef APP-ANDROID
  9. // @ts-ignore
  10. return (data as UTSJSONObject).parse<T>();
  11. // #endif
  12. // #ifndef APP-ANDROID
  13. return data as T;
  14. // #endif
  15. }
  16. /**
  17. * 解析JSON对象
  18. * @param data 要解析的数据
  19. * @returns 解析后的JSON对象
  20. */
  21. export function parseObject<T>(data: string): T | null {
  22. // #ifdef APP-ANDROID
  23. return JSON.parseObject<T>(data);
  24. // #endif
  25. // #ifndef APP-ANDROID
  26. return JSON.parse(data) as T;
  27. // #endif
  28. }
  29. /**
  30. * 解析透传样式对象
  31. * @param data 要解析的数据
  32. * @returns 解析后的透传样式对象
  33. * @template T 透传样式对象的类型
  34. */
  35. export function parsePt<T>(data: any): T {
  36. // #ifdef APP-ANDROID
  37. // @ts-ignore
  38. return (data as UTSJSONObject).parse<T>() ?? ({} as T);
  39. // #endif
  40. // #ifndef APP-ANDROID
  41. return data as T;
  42. // #endif
  43. }
  44. /**
  45. * 解析对象为类名字符串
  46. * @param obj 要解析的对象,key为类名,value为布尔值表示是否启用该类名
  47. * @returns 解析后的类名字符串,多个类名以空格分隔
  48. * @example
  49. * parseClass({ 'active': true, 'disabled': false }) // 返回 'active'
  50. * parseClass(['ml-2', 'mr-2']) // 返回 'ml-2 mr-2'
  51. * parseClass([{ 'mr-2': true, 'mt-2': false }]) // 返回 'mr-2'
  52. * parseClass([[true, 'mr-2 pt-2', 'mb-2']]) // 返回 'mr-2 pt-2'
  53. */
  54. export const parseClass = (data: any): string => {
  55. // 存储启用的类名
  56. const names: string[] = [];
  57. // 解析数据
  58. function deep(d: any) {
  59. // 如果obj是数组,则将数组中的每个元素添加到names中
  60. if (isArray(d)) {
  61. forEach(d as any[], (value: any) => {
  62. if (isString(value)) {
  63. // @example 2
  64. names.push(value as string);
  65. } else if (isArray(value)) {
  66. // @example 4
  67. const [a, b] = value as any[];
  68. if (a as boolean) {
  69. names.push(b as string);
  70. } else {
  71. if (value.length > 2) {
  72. names.push(value[2] as string);
  73. }
  74. }
  75. } else if (isObject(value)) {
  76. // @example 3
  77. deep(value);
  78. }
  79. });
  80. }
  81. // 遍历对象的每个属性
  82. if (isObject(d)) {
  83. // @example 1
  84. forInObject(d, (value, key) => {
  85. // 如果属性值为true,则将类名添加到数组中
  86. if (value == true && key != "") {
  87. names.push(key.trim());
  88. }
  89. });
  90. }
  91. }
  92. deep(data);
  93. // 将类名数组用空格连接成字符串返回
  94. return names.join(" ");
  95. };
  96. /**
  97. * 将自定义类型数据转换为UTSJSONObject对象
  98. * @param data 要转换的数据
  99. * @returns 转换后的UTSJSONObject对象
  100. */
  101. export function parseToObject<T>(data: T): UTSJSONObject {
  102. // #ifdef APP-ANDROID
  103. return JSON.parseObject(JSON.stringify(data)!)!;
  104. // #endif
  105. // #ifndef APP-ANDROID
  106. return JSON.parse(JSON.stringify(data)) as UTSJSONObject;
  107. // #endif
  108. }
  109. /**
  110. * 将数值或字符串转换为rpx单位的字符串
  111. * @param val 要转换的值,可以是数字或字符串
  112. * @returns 转换后的rpx单位字符串
  113. * @example
  114. * parseRpx(10) // 返回 '10rpx'
  115. * parseRpx('10rpx') // 返回 '10rpx'
  116. * parseRpx('10px') // 返回 '10px'
  117. */
  118. export const parseRpx = (val: number | string): string => {
  119. if (typeof val == "number") {
  120. return val + "rpx";
  121. }
  122. return val;
  123. };
  124. /**
  125. * 将rpx单位转换为px单位
  126. * @param rpx 要转换的rpx值
  127. * @returns 转换后的px值
  128. * @example
  129. */
  130. export const rpx2px = (rpx: number): number => {
  131. let px: number;
  132. // #ifdef MP
  133. px = rpx / (750 / uni.getWindowInfo().windowWidth);
  134. // #endif
  135. // #ifndef MP
  136. px = uni.rpx2px(rpx);
  137. // #endif
  138. return px;
  139. };
  140. /**
  141. * 将px单位转换为rpx单位
  142. * @param px 要转换的px值
  143. * @returns 转换后的rpx值
  144. * @example
  145. */
  146. export const px2rpx = (px: number): number => {
  147. return px / rpx2px(1);
  148. };
  149. /**
  150. * 解析px单位,支持rpx单位转换
  151. * @param val 要解析的值,可以是数字或字符串
  152. * @returns 解析后的px单位
  153. */
  154. export const getPx = (val: string) => {
  155. const num = parseInt(val);
  156. if (val.includes("rpx")) {
  157. return rpx2px(num);
  158. }
  159. return Math.floor(num);
  160. };