public.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // 全局uni对象
  2. export function debounce<T extends (...args: any[]) => any>(fn: T, delay: number = 500): (...args: Parameters<T>) => void {
  3. let timer: number | null = null;
  4. return function (...args: Parameters<T>) {
  5. if (timer !== null) {
  6. clearTimeout(timer);
  7. }
  8. timer = setTimeout(() => {
  9. fn.apply(null, args);
  10. timer = null;
  11. }, delay);
  12. };
  13. }
  14. export function calculateAge(birthday: string): number {
  15. const today = new Date(); // 获取当前日期时间
  16. const birthdate = new Date(birthday); // 将生日字符串转换为日期格式
  17. let age = today.getFullYear() - birthdate.getFullYear(); // 根据年份计算年龄
  18. if (today < birthdate || today.getMonth() < birthdate.getMonth()) {
  19. age--; // 如果今天的月份小于生日的月份或者今天的日期小于生日的日期,则年龄需要减1
  20. }
  21. return age;
  22. }
  23. // 获取当前路由信息
  24. export function getCurrentPage(): any {
  25. const pages = getCurrentPages();
  26. return pages[pages.length - 1];
  27. }
  28. export function copyText(text: string): void {
  29. uni.setClipboardData({
  30. data: text,
  31. success: function () {
  32. // 可以添加用户友好的提示,例如使用uni.showToast提示复制成功
  33. uni.showToast({
  34. title: '复制成功',
  35. icon: 'success',
  36. duration: 2000
  37. });
  38. },
  39. fail: function () {
  40. console.log('复制失败');
  41. // 可以添加错误处理或用户友好的提示
  42. }
  43. });
  44. }
  45. // 去登录
  46. export function goLogin(): void {
  47. uni.$u.route({
  48. type: 'reLaunch',
  49. url: '/pages/login/login',
  50. params: {
  51. redirect: getCurrentPage()?.route
  52. }
  53. });
  54. }
  55. export const setCipByNum = (val: string, startNum: number, num: number, isCip: boolean = false): string => {
  56. if (isCip) {
  57. return val;
  58. }
  59. if (!val) return '-';
  60. const a = val.slice(0, startNum);
  61. const b = val.substring(startNum + num);
  62. return a + '*'.repeat(num) + b;
  63. };
  64. export const handleContact = (): void => {
  65. // 判断是否是微信
  66. if (uni.$u.platform === 'weixin') {
  67. try {
  68. uni.openCustomerServiceChat({
  69. extInfo: { url: 'https://work.weixin.qq.com/kfid/kfcaf0368dcb1cbb94d' },
  70. corpId: 'wwbad9dcbcb6a57196', // 客服消息接收方 corpid
  71. success: (res: any) => {
  72. console.log('打开客服会话成功', res);
  73. },
  74. fail: (err: any) => {
  75. console.error('打开客服会话失败', err);
  76. }
  77. });
  78. } catch (error) {
  79. console.error('客服功能不可用', error);
  80. }
  81. }
  82. };
  83. // 拨打电话
  84. export const makePhoneCall = (phoneNumber: string): void => {
  85. if (!phoneNumber) {
  86. uni.showToast({
  87. title: '电话号码不能为空',
  88. icon: 'none'
  89. });
  90. return;
  91. }
  92. uni.makePhoneCall({
  93. phoneNumber: phoneNumber,
  94. success: () => {
  95. console.log('拨打电话成功');
  96. },
  97. fail: (err) => {
  98. console.error('拨打电话失败', err);
  99. }
  100. });
  101. };