util.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. export const formatTime = (date: Date) => {
  2. const year = date.getFullYear()
  3. const month = date.getMonth() + 1
  4. const day = date.getDate()
  5. const hour = date.getHours()
  6. const minute = date.getMinutes()
  7. const second = date.getSeconds()
  8. return (
  9. [year, month, day].map(formatNumber).join('/') +
  10. ' ' +
  11. [hour, minute, second].map(formatNumber).join(':')
  12. )
  13. }
  14. const formatNumber = (n: number) => {
  15. const s = n.toString()
  16. return s[1] ? s : '0' + s
  17. }
  18. export function groupBy<T>(items: Iterable<T>, callbackFn: (element: T, index: number) => any): Record<string, T[]> {
  19. const obj = Object.create(null);
  20. let k = 0;
  21. for (const value of items) {
  22. const key = callbackFn(value, k++);
  23. if (key in obj) { obj[key].push(value); } else { obj[key] = [value]; }
  24. }
  25. return obj;
  26. }
  27. export const HealthReportSymptomItemConfig = {
  28. 有一点: 2.0,
  29. 偶尔: 2.0,
  30. 轻: 2.0,
  31. 有些: 3.0,
  32. 有时: 3.0,
  33. 中: 3.0,
  34. 相当: 4.0,
  35. 经常: 4.0,
  36. 重: 4.0,
  37. 非常: 5.0,
  38. 总是: 5.0,
  39. 非常重: 5.0,
  40. } as const;
  41. export interface HealthReportSymptomItemVo {
  42. /**
  43. * 症状ID
  44. */
  45. id: string;
  46. /**
  47. * 症状名称
  48. */
  49. name: string;
  50. /**
  51. * 症状描述
  52. */
  53. label: string;
  54. /**
  55. * 症状得分
  56. */
  57. value: number;
  58. }
  59. export interface HealthReportSymptomVo {
  60. items: HealthReportSymptomItemVo[];
  61. value?: string;
  62. duration?: string;
  63. influence?: string;
  64. }
  65. export interface HealthReportDTO {
  66. tonguefaceAnalysisReportId: string;
  67. healthAnalysisReportId: string;
  68. reportTime: string;
  69. pickedSymptomList?: { id: string; name: string; value: string; score: number }[];
  70. pickedSymptom?: string;
  71. duration?: string;
  72. influenceDegree?: string;
  73. willillStateName: string;
  74. willillDegreeName: string;
  75. willillFunctionName: string;
  76. willillSocialName: string;
  77. constitutionGroupName: string;
  78. constitutionGroupDefinition: string;
  79. factorItemSummary: string;
  80. diagnoseSyndromeSummary: string;
  81. }
  82. export function fromHealthReportSymptom(data: Partial<HealthReportDTO>, config = HealthReportSymptomItemConfig): HealthReportSymptomVo {
  83. const result: HealthReportSymptomVo = {
  84. value: data.pickedSymptom,
  85. items:
  86. data.pickedSymptomList?.map(({ id, name, ...item }) => ({
  87. id,
  88. name,
  89. label: item.value,
  90. value: +item.score,
  91. })) ?? [],
  92. duration: data.duration,
  93. influence: data.influenceDegree,
  94. };
  95. if (!result.items?.length && result.value) {
  96. const matches = result.value?.matchAll(/([^,(]+)(([^)]+))/g) ?? [];
  97. for (const [_, name, label] of matches) {
  98. // @ts-ignore
  99. const value = config[label] ?? 0;
  100. result.items.push({ id: name, name, label, value });
  101. }
  102. }
  103. return result;
  104. }
  105. /**
  106. * 唤起微信支付
  107. * @param {Object} paymentParams - 支付参数对象
  108. * @param {string} paymentParams.timeStamp - 时间戳
  109. * @param {string} paymentParams.packageVal - 统一下单接口返回的 prepay_id 参数值,格式为 "prepay_id=xxx"(兼容 packageValue)
  110. * @param {string} paymentParams.packageValue - 统一下单接口返回的 prepay_id 参数值,格式为 "prepay_id=xxx"(API返回的字段名)
  111. * @param {string} paymentParams.paySign - 支付签名
  112. * @param {string} paymentParams.nonceStr - 随机字符串
  113. * @param {string} paymentParams.signType - 签名类型,默认为 'RSA'
  114. * @param {string} paymentParams.appId - 小程序 appId(可选)
  115. * @param {Function} onSuccess - 支付成功回调函数
  116. * @param {Function} onFail - 支付失败回调函数
  117. */
  118. export const handleWeChatPayment = (paymentParams: any, onSuccess: any, onFail: any = () => {}) => {
  119. // 兼容 packageValue 和 packageVal 两种字段名
  120. const packageVal = paymentParams.packageValue || paymentParams.packageVal
  121. // 验证支付参数
  122. if (!paymentParams || !paymentParams.timeStamp || !packageVal || !paymentParams.paySign) {
  123. const error = new Error('支付参数不完整')
  124. console.error('支付参数错误:', error)
  125. if (onFail) {
  126. onFail(error)
  127. }
  128. return
  129. }
  130. // wx.showLoading({
  131. // title: '正在调起支付...',
  132. // mask: true
  133. // })
  134. wx.requestPayment({
  135. provider: 'wxpay',
  136. timeStamp: String(paymentParams.timeStamp), // 确保是字符串类型
  137. nonceStr: paymentParams.nonceStr,
  138. package: packageVal, // 兼容 packageValue 和 packageVal
  139. signType: paymentParams.signType || 'RSA',
  140. paySign: paymentParams.paySign,
  141. success: (res) => {
  142. wx.hideLoading()
  143. if (onSuccess) {
  144. onSuccess(res)
  145. }
  146. },
  147. fail: (err) => {
  148. if (onFail) {
  149. onFail(err)
  150. }
  151. if (err.errMsg === 'requestPayment:fail cancel') {
  152. wx.showToast({
  153. title: '支付已取消',
  154. icon: 'none'
  155. })
  156. } else {
  157. console.error('支付失败:', err)
  158. }
  159. }
  160. })
  161. }