| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- /**
- * 提示工具类
- */
- export class TipsUtils {
- /**
- *轻提示
- * @param text - 提示内容
- */
- static tips_toast(text: string) {
- uni.showToast({
- icon: "none",
- title: text,
- duration: 2000,
- });
- }
- /**
- * 强提示
- * @param text - 提示内容
- * @param showCancel - 是否显示取消按钮
- */
- static tips_alert(text: string, showCancel: boolean) {
- return new Promise((resolve, reject) => {
- uni.showModal({
- title: '提示',
- content: text,
- showCancel: showCancel,
- confirmText: '确定',
- confirmColor: '#9BB051',
- success(res) {
- resolve(res)
- },
- fail(err) {
- reject(err)
- }
- });
- })
- }
- }
- /**
- * 验证工具类
- */
- export class ValidateUtils {
- /**
- * 验证手机号格式
- * @param phone - 手机号字符串
- * @returns 是否有效
- */
- static validatePhone(phone: string): boolean {
- return /^1[3-9]\d{9}$/.test(phone);
- }
- /**
- * 验证邮箱格式
- * @param email - 邮箱字符串
- * @returns 是否有效
- */
- static validateEmail(email: string): boolean {
- return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
- }
- /**
- * 验证身份证号格式
- * @param idCard - 身份证号字符串
- * @returns 是否有效
- */
- static validateIdCard(idCard: string): boolean {
- return /^[1-9]\d{5}(19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$/.test(idCard);
- }
- }
- /**
- * 路由工具类
- */
- export class RouterUtils {
- /**
- * 返回上一页
- */
- static back() {
- uni.navigateBack();
- }
- /**
- * 跳转到指定页面
- * @param path - 页面路径
- */
- static to_page(path: string) {
- // 定义不需要token验证的页面白名单
- const noTokenRequiredPages = [
- '/pages/index/index',
- '/pages/index/venue/index',
- // '/pages/index/detail/index',
- // '/pages/index/gymDetail/index',
- // '/pages/index/courseDetail/index',
- '/pages/index/training/index',
- '/pages/index/events/index',
- '/pages/index/eventsDetail/index',
- '/pages/index/allInstructor/index',
- // '/pages/index/instructorDetail/index',
- '/pages/index/search/index',
- '/pages/index/searchPage/index',
- '/pages/index/vr/index',
- '/pages/index/mapPage/index',
- '/pages/mine/index'
- ];
- // 检查当前路径是否在白名单中
- const isNoTokenRequired = noTokenRequiredPages.some(page => path.startsWith(page));
- // 如果不在白名单中且没有token,则进行登录验证
- if (!isNoTokenRequired && !uni.getStorageSync('TOKEN')) {
- TipsUtils.tips_alert('请先登录~', true).then((res) => {
- console.log(res, 'res');
- if (res.confirm) {
- uni.switchTab({
- url: '/pages/index/index'
- })
- }
- })
- return
- }
- uni.navigateTo({
- url: path
- })
- }
- }
- /**
- * 日期工具类
- */
- export class DateUtils {
- /**
- * 日期时间格式化工具
- * @param date - 日期对象或时间戳
- * @param format - 格式字符串,默认:'YYYY-MM-DD HH:mm:ss'
- * @returns 格式化后的日期字符串
- */
- static formatDate = (date: Date | number, format: string = 'YYYY-MM-DD HH:mm:ss'): string => {
- let d: Date = typeof date === 'number' ? new Date(date) : date;
- const padZero = (num: number): string => num.toString().padStart(2, '0');
- const replacements: Record<string, string> = {
- 'YYYY': d.getFullYear().toString(),
- 'MM': padZero(d.getMonth() + 1),
- 'DD': padZero(d.getDate()),
- 'HH': padZero(d.getHours()),
- 'mm': padZero(d.getMinutes()),
- 'ss': padZero(d.getSeconds()),
- 'SSS': padZero(d.getMilliseconds()).padStart(3, '0')
- };
- return format.replace(/YYYY|MM|DD|HH|mm|ss|SSS/g, match => replacements[match]);
- };
- /**
- * YYYY-MM-DD HH:mm:ss 转转MM-DD
- * @param dateString - 日期字符串
- * @returns MM-DD 格式的日期字符串
- */
- static formatDateToMMDD(dateString: any) {
- let str = dateString;
- if (typeof str === 'string' && str.indexOf('T') === -1 && str.indexOf(' ') > 0) {
- str = str.replace(' ', 'T');
- }
- const date = new Date(str);
- const month = (date.getMonth() + 1).toString().padStart(2, '0');
- const day = date.getDate().toString().padStart(2, '0');
- return `${month}-${day}`;
- }
- /**
- * 日期字符串转为HH:mm格式
- * @param dateString - 日期字符串
- * @returns HH:mm 格式的时间字符串
- */
- static formatDateToHHmm(dateString: any) {
- let str = dateString;
- if (typeof str === 'string' && str.indexOf('T') === -1 && str.indexOf(' ') > 0) {
- str = str.replace(' ', 'T');
- }
- const date = new Date(str);
- const hours = date.getHours().toString().padStart(2, '0');
- const minutes = date.getMinutes().toString().padStart(2, '0');
- return `${hours}:${minutes}`;
- }
- /**
- *
- * @param dateString - 日期字符串
- * @param days - 要添加的天数
- * @returns
- */
- static addDays(dateString: string, days: number) {
- const date = new Date(dateString);
- date.setDate(date.getDate() + days);
- const year = date.getFullYear();
- const month = String(date.getMonth() + 1).padStart(2, '0');
- const day = String(date.getDate()).padStart(2, '0');
- return `${year}-${month}-${day}`;
- }
- }
- /**
- * 生成随机颜色
- * @returns 十六进制颜色字符串
- */
- export const randomColor = (): string => {
- return `#${Math.floor(Math.random() * 0xffffff).toString(16).padStart(6, '0')}`;
- };
- /**
- * 预览图片
- * @param url - 图片地址
- */
- export const _previewImage = (urls: any, current?: string) => {
- let previewUrls: string[] = [];
- let previewCurrent: string | undefined = current;
- // 如果 urls 是字符串,转换为数组
- if (typeof urls === 'string') {
- previewUrls = [urls];
- // 如果没有指定 current,则使用第一个图片作为当前图片
- if (!previewCurrent) {
- previewCurrent = urls;
- }
- }
- // 如果 urls 是数组,直接使用
- else if (Array.isArray(urls)) {
- previewUrls = urls;
- // 如果没有指定 current 且数组不为空,则使用数组第一个元素
- if (!previewCurrent && urls.length > 0) {
- previewCurrent = urls[0];
- }
- }
- uni.previewImage({
- urls: previewUrls,
- current: previewCurrent,
- longPressActions: {
- itemList: ['发送给朋友', '保存图片', '收藏'],
- success: function (data) {
- uni.showToast({
- title: '操作成功',
- icon: 'none'
- })
- },
- fail: function (err) {
- console.log(err.errMsg);
- }
- }
- });
- }
- /**
- * 身份证号脱敏
- * @param card - 身份证号字符串
- * @returns 脱敏后的身份证号字符串
- */
- export const idCardHide = (card: any) => {
- const reg = /^(.{2})(?:\d+)(.{2})$/;
- const maskedIdCard = card.replace(reg, '$1***********$2');
- return maskedIdCard;
- }
- /**
- * 手机号脱敏
- * @param phone - 手机号字符串
- * @returns 脱敏后的手机号字符串
- */
- export const phoneHide = (phone: any) => {
- if (!phone) return '';
- const reg = /^(\d{3})\d{4}(\d{4})$/;
- const maskedPhone = phone.replace(reg, '$1****$2');
- return maskedPhone;
- }
- /**
- *处理富文本图片溢出
- * @param html - 富文本字符串
- * @returns
- */
- export const fixImgStyle = (html: string) => {
- if (!html) return html;
- return html.replace(/<img/gi, '<img style="max-width:100%;height:auto;display:block;"');
- }
- /**
- * 防抖函数
- * @param fn 需要防抖的函数
- * @param delay 延迟时间,单位毫秒
- * @returns 返回防抖处理后的函数
- */
- export function debounce<T extends (...args: any[]) => any>(
- fn: T,
- delay: number
- ): (...args: Parameters<T>) => void {
- let timer: ReturnType<typeof setTimeout> | null = null
- return function (this: any, ...args: Parameters<T>): void {
- if (timer) clearTimeout(timer)
- timer = setTimeout(() => {
- fn.apply(this, args)
- timer = null
- }, delay)
- }
- }
|