index.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /**
  2. * 提示工具类
  3. */
  4. export class TipsUtils {
  5. /**
  6. *轻提示
  7. * @param text - 提示内容
  8. */
  9. static tips_toast(text: string) {
  10. uni.showToast({
  11. icon: "none",
  12. title: text,
  13. duration: 2000,
  14. });
  15. }
  16. /**
  17. * 强提示
  18. * @param text - 提示内容
  19. * @param showCancel - 是否显示取消按钮
  20. */
  21. static tips_alert(text: string, showCancel: boolean) {
  22. return new Promise((resolve, reject) => {
  23. uni.showModal({
  24. title: '提示',
  25. content: text,
  26. showCancel: showCancel,
  27. confirmText: '确定',
  28. confirmColor: '#9BB051',
  29. success(res) {
  30. resolve(res)
  31. },
  32. fail(err) {
  33. reject(err)
  34. }
  35. });
  36. })
  37. }
  38. }
  39. /**
  40. * 验证工具类
  41. */
  42. export class ValidateUtils {
  43. /**
  44. * 验证手机号格式
  45. * @param phone - 手机号字符串
  46. * @returns 是否有效
  47. */
  48. static validatePhone(phone: string): boolean {
  49. return /^1[3-9]\d{9}$/.test(phone);
  50. }
  51. /**
  52. * 验证邮箱格式
  53. * @param email - 邮箱字符串
  54. * @returns 是否有效
  55. */
  56. static validateEmail(email: string): boolean {
  57. return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
  58. }
  59. /**
  60. * 验证身份证号格式
  61. * @param idCard - 身份证号字符串
  62. * @returns 是否有效
  63. */
  64. static validateIdCard(idCard: string): boolean {
  65. 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);
  66. }
  67. }
  68. /**
  69. * 路由工具类
  70. */
  71. export class RouterUtils {
  72. /**
  73. * 返回上一页
  74. */
  75. static back() {
  76. uni.navigateBack();
  77. }
  78. /**
  79. * 跳转到指定页面
  80. * @param path - 页面路径
  81. */
  82. static to_page(path: string) {
  83. // 定义不需要token验证的页面白名单
  84. const noTokenRequiredPages = [
  85. '/pages/index/index',
  86. '/pages/index/venue/index',
  87. // '/pages/index/detail/index',
  88. // '/pages/index/gymDetail/index',
  89. // '/pages/index/courseDetail/index',
  90. '/pages/index/training/index',
  91. '/pages/index/events/index',
  92. '/pages/index/eventsDetail/index',
  93. '/pages/index/allInstructor/index',
  94. // '/pages/index/instructorDetail/index',
  95. '/pages/index/search/index',
  96. '/pages/index/searchPage/index',
  97. '/pages/index/vr/index',
  98. '/pages/index/mapPage/index',
  99. '/pages/mine/index'
  100. ];
  101. // 检查当前路径是否在白名单中
  102. const isNoTokenRequired = noTokenRequiredPages.some(page => path.startsWith(page));
  103. // 如果不在白名单中且没有token,则进行登录验证
  104. if (!isNoTokenRequired && !uni.getStorageSync('TOKEN')) {
  105. TipsUtils.tips_alert('请先登录~', true).then((res) => {
  106. console.log(res, 'res');
  107. if (res.confirm) {
  108. uni.switchTab({
  109. url: '/pages/index/index'
  110. })
  111. }
  112. })
  113. return
  114. }
  115. uni.navigateTo({
  116. url: path
  117. })
  118. }
  119. }
  120. /**
  121. * 日期工具类
  122. */
  123. export class DateUtils {
  124. /**
  125. * 日期时间格式化工具
  126. * @param date - 日期对象或时间戳
  127. * @param format - 格式字符串,默认:'YYYY-MM-DD HH:mm:ss'
  128. * @returns 格式化后的日期字符串
  129. */
  130. static formatDate = (date: Date | number, format: string = 'YYYY-MM-DD HH:mm:ss'): string => {
  131. let d: Date = typeof date === 'number' ? new Date(date) : date;
  132. const padZero = (num: number): string => num.toString().padStart(2, '0');
  133. const replacements: Record<string, string> = {
  134. 'YYYY': d.getFullYear().toString(),
  135. 'MM': padZero(d.getMonth() + 1),
  136. 'DD': padZero(d.getDate()),
  137. 'HH': padZero(d.getHours()),
  138. 'mm': padZero(d.getMinutes()),
  139. 'ss': padZero(d.getSeconds()),
  140. 'SSS': padZero(d.getMilliseconds()).padStart(3, '0')
  141. };
  142. return format.replace(/YYYY|MM|DD|HH|mm|ss|SSS/g, match => replacements[match]);
  143. };
  144. /**
  145. * YYYY-MM-DD HH:mm:ss 转转MM-DD
  146. * @param dateString - 日期字符串
  147. * @returns MM-DD 格式的日期字符串
  148. */
  149. static formatDateToMMDD(dateString: any) {
  150. let str = dateString;
  151. if (typeof str === 'string' && str.indexOf('T') === -1 && str.indexOf(' ') > 0) {
  152. str = str.replace(' ', 'T');
  153. }
  154. const date = new Date(str);
  155. const month = (date.getMonth() + 1).toString().padStart(2, '0');
  156. const day = date.getDate().toString().padStart(2, '0');
  157. return `${month}-${day}`;
  158. }
  159. /**
  160. * 日期字符串转为HH:mm格式
  161. * @param dateString - 日期字符串
  162. * @returns HH:mm 格式的时间字符串
  163. */
  164. static formatDateToHHmm(dateString: any) {
  165. let str = dateString;
  166. if (typeof str === 'string' && str.indexOf('T') === -1 && str.indexOf(' ') > 0) {
  167. str = str.replace(' ', 'T');
  168. }
  169. const date = new Date(str);
  170. const hours = date.getHours().toString().padStart(2, '0');
  171. const minutes = date.getMinutes().toString().padStart(2, '0');
  172. return `${hours}:${minutes}`;
  173. }
  174. /**
  175. *
  176. * @param dateString - 日期字符串
  177. * @param days - 要添加的天数
  178. * @returns
  179. */
  180. static addDays(dateString: string, days: number) {
  181. const date = new Date(dateString);
  182. date.setDate(date.getDate() + days);
  183. const year = date.getFullYear();
  184. const month = String(date.getMonth() + 1).padStart(2, '0');
  185. const day = String(date.getDate()).padStart(2, '0');
  186. return `${year}-${month}-${day}`;
  187. }
  188. }
  189. /**
  190. * 生成随机颜色
  191. * @returns 十六进制颜色字符串
  192. */
  193. export const randomColor = (): string => {
  194. return `#${Math.floor(Math.random() * 0xffffff).toString(16).padStart(6, '0')}`;
  195. };
  196. /**
  197. * 预览图片
  198. * @param url - 图片地址
  199. */
  200. export const _previewImage = (urls: any, current?: string) => {
  201. let previewUrls: string[] = [];
  202. let previewCurrent: string | undefined = current;
  203. // 如果 urls 是字符串,转换为数组
  204. if (typeof urls === 'string') {
  205. previewUrls = [urls];
  206. // 如果没有指定 current,则使用第一个图片作为当前图片
  207. if (!previewCurrent) {
  208. previewCurrent = urls;
  209. }
  210. }
  211. // 如果 urls 是数组,直接使用
  212. else if (Array.isArray(urls)) {
  213. previewUrls = urls;
  214. // 如果没有指定 current 且数组不为空,则使用数组第一个元素
  215. if (!previewCurrent && urls.length > 0) {
  216. previewCurrent = urls[0];
  217. }
  218. }
  219. uni.previewImage({
  220. urls: previewUrls,
  221. current: previewCurrent,
  222. longPressActions: {
  223. itemList: ['发送给朋友', '保存图片', '收藏'],
  224. success: function (data) {
  225. uni.showToast({
  226. title: '操作成功',
  227. icon: 'none'
  228. })
  229. },
  230. fail: function (err) {
  231. console.log(err.errMsg);
  232. }
  233. }
  234. });
  235. }
  236. /**
  237. * 身份证号脱敏
  238. * @param card - 身份证号字符串
  239. * @returns 脱敏后的身份证号字符串
  240. */
  241. export const idCardHide = (card: any) => {
  242. const reg = /^(.{2})(?:\d+)(.{2})$/;
  243. const maskedIdCard = card.replace(reg, '$1***********$2');
  244. return maskedIdCard;
  245. }
  246. /**
  247. * 手机号脱敏
  248. * @param phone - 手机号字符串
  249. * @returns 脱敏后的手机号字符串
  250. */
  251. export const phoneHide = (phone: any) => {
  252. if (!phone) return '';
  253. const reg = /^(\d{3})\d{4}(\d{4})$/;
  254. const maskedPhone = phone.replace(reg, '$1****$2');
  255. return maskedPhone;
  256. }
  257. /**
  258. *处理富文本图片溢出
  259. * @param html - 富文本字符串
  260. * @returns
  261. */
  262. export const fixImgStyle = (html: string) => {
  263. if (!html) return html;
  264. return html.replace(/<img/gi, '<img style="max-width:100%;height:auto;display:block;"');
  265. }
  266. /**
  267. * 防抖函数
  268. * @param fn 需要防抖的函数
  269. * @param delay 延迟时间,单位毫秒
  270. * @returns 返回防抖处理后的函数
  271. */
  272. export function debounce<T extends (...args: any[]) => any>(
  273. fn: T,
  274. delay: number
  275. ): (...args: Parameters<T>) => void {
  276. let timer: ReturnType<typeof setTimeout> | null = null
  277. return function (this: any, ...args: Parameters<T>): void {
  278. if (timer) clearTimeout(timer)
  279. timer = setTimeout(() => {
  280. fn.apply(this, args)
  281. timer = null
  282. }, delay)
  283. }
  284. }