common.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // 公共方法模块
  2. /**
  3. * 手机号校验
  4. * @param {手机号} tel
  5. * @param {区号} areaNum 默认86
  6. * @returns Boolean
  7. * 86 正常手机号校验
  8. * 非86 位数必须大于8位
  9. */
  10. export const telVerify = (tel, areaNum = 86) => {
  11. let flag = false;
  12. if (areaNum == 86 && /^1[3456789]\d{9}$/.test(tel)) {
  13. flag = true;
  14. }
  15. if (areaNum != 86 && tel.length > 8) {
  16. flag = true;
  17. }
  18. return flag;
  19. };
  20. /**
  21. * 邮箱校验
  22. * @param {邮箱} email
  23. * @returns Boolean
  24. */
  25. export const emailVerify = (email) => {
  26. let flag = false;
  27. let pattern = /^([A-Za-z0-9_\-\.])+@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
  28. let domains = ["qq.com", "163.com", "vip.163.com", "263.net", "yeah.net", "sohu.com", "sina.cn", "sina.com", "eyou.com", "gmail.com", "hotmail.com", "42du.cn"];
  29. if (pattern.test(email)) {
  30. var domain = email.substring(email.indexOf("@") + 1);
  31. for (var i = 0; i < domains.length; i++) {
  32. if (domain == domains[i]) {
  33. flag = true;
  34. }
  35. }
  36. }
  37. return flag;
  38. };
  39. /**
  40. * 校验是否有小程序新版本
  41. */
  42. export const hasUpdate = () => {
  43. const updateManager = uni.getUpdateManager();
  44. updateManager.onCheckForUpdate(function (res) {
  45. // 请求完新版本信息的回调
  46. console.log("是否有新版本", res.hasUpdate);
  47. });
  48. updateManager.onUpdateReady(function (res) {
  49. uni.showModal({
  50. title: "更新提示",
  51. content: "新版本已经准备好,是否重启应用?",
  52. success(res) {
  53. if (res.confirm) {
  54. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  55. updateManager.applyUpdate();
  56. }
  57. },
  58. });
  59. });
  60. };
  61. /**
  62. *
  63. * 防抖
  64. */
  65. export const debounce = (fn,t=300) => {
  66. let timer = null;
  67. return function() {
  68. clearTimeout(timer);
  69. timer = setTimeout(_ => {
  70. fn.call(this, arguments);
  71. }, t);
  72. };
  73. }