| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- // 全局uni对象
- export function debounce<T extends (...args: any[]) => any>(fn: T, delay: number = 500): (...args: Parameters<T>) => void {
- let timer: number | null = null;
- return function (...args: Parameters<T>) {
- if (timer !== null) {
- clearTimeout(timer);
- }
- timer = setTimeout(() => {
- fn.apply(null, args);
- timer = null;
- }, delay);
- };
- }
- export function calculateAge(birthday: string): number {
- const today = new Date(); // 获取当前日期时间
- const birthdate = new Date(birthday); // 将生日字符串转换为日期格式
- let age = today.getFullYear() - birthdate.getFullYear(); // 根据年份计算年龄
- if (today < birthdate || today.getMonth() < birthdate.getMonth()) {
- age--; // 如果今天的月份小于生日的月份或者今天的日期小于生日的日期,则年龄需要减1
- }
- return age;
- }
- // 获取当前路由信息
- export function getCurrentPage(): any {
- const pages = getCurrentPages();
- return pages[pages.length - 1];
- }
- export function copyText(text: string): void {
- uni.setClipboardData({
- data: text,
- success: function () {
- // 可以添加用户友好的提示,例如使用uni.showToast提示复制成功
- uni.showToast({
- title: '复制成功',
- icon: 'success',
- duration: 2000
- });
- },
- fail: function () {
- console.log('复制失败');
- // 可以添加错误处理或用户友好的提示
- }
- });
- }
- // 去登录
- export function goLogin(): void {
- uni.$u.route({
- type: 'reLaunch',
- url: '/pages/login/login',
- params: {
- redirect: getCurrentPage()?.route
- }
- });
- }
- export const setCipByNum = (val: string, startNum: number, num: number, isCip: boolean = false): string => {
- if (isCip) {
- return val;
- }
- if (!val) return '-';
- const a = val.slice(0, startNum);
- const b = val.substring(startNum + num);
- return a + '*'.repeat(num) + b;
- };
- export const handleContact = (): void => {
- // 判断是否是微信
- if (uni.$u.platform === 'weixin') {
- try {
- uni.openCustomerServiceChat({
- extInfo: { url: 'https://work.weixin.qq.com/kfid/kfcaf0368dcb1cbb94d' },
- corpId: 'wwbad9dcbcb6a57196', // 客服消息接收方 corpid
- success: (res: any) => {
- console.log('打开客服会话成功', res);
- },
- fail: (err: any) => {
- console.error('打开客服会话失败', err);
- }
- });
- } catch (error) {
- console.error('客服功能不可用', error);
- }
- }
- };
- // 拨打电话
- export const makePhoneCall = (phoneNumber: string): void => {
- if (!phoneNumber) {
- uni.showToast({
- title: '电话号码不能为空',
- icon: 'none'
- });
- return;
- }
- uni.makePhoneCall({
- phoneNumber: phoneNumber,
- success: () => {
- console.log('拨打电话成功');
- },
- fail: (err) => {
- console.error('拨打电话失败', err);
- }
- });
- };
|