自主项目,食堂系统,前端uniapp
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
4.3 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. export default{
  2. data(){
  3. return {
  4. pageScrollTop: 0, // 页面距离顶部的距离
  5. currentPage: '' // 当前页面路径
  6. }
  7. },
  8. onPageScroll(res) {
  9. this.pageScrollTop = res.scrollTop;
  10. },
  11. onLoad(){
  12. let pages = getCurrentPages(); // 获取页面栈
  13. this.currentPage = pages[pages.length - 1].route; // 当前页面
  14. let key_name = this.$getPageType() +'_token';
  15. let user_token = uni.getStorageSync(key_name);
  16. if(!user_token){
  17. this.$redirectToLogin();
  18. }
  19. },
  20. methods: {
  21. $getPageType(){
  22. // 获取当前页面是属于供应端还是食堂端
  23. if(this.currentPage.indexOf('pages/supply') == 0){
  24. return 'supply';
  25. }else if(this.currentPage.indexOf('pages/canteen') == 0){
  26. return 'canteen';
  27. }
  28. },
  29. $isRight(val){
  30. return this.$shared.isRight(val);
  31. },
  32. $check(str, type) {
  33. switch (type) {
  34. case 'mobile': //手机号码
  35. return /^1[3|4|5|6|7|8|9][0-9]{9}$/.test(str);
  36. case 'tel': //座机
  37. return /^(0\d{2,3}-\d{7,8})(-\d{1,4})?$/.test(str);
  38. case 'card': //身份证
  39. return /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(str);
  40. case 'mobileCode': //6位数字验证码
  41. return /^[0-9]{6}$/.test(str)
  42. case 'pwd': //密码以字母开头,长度在6~18之间,只能包含字母、数字和下划线
  43. return /^([a-zA-Z0-9_]){6,20}$/.test(str)
  44. case 'payPwd': //支付密码 6位纯数字
  45. return /^[0-9]{6}$/.test(str)
  46. case 'postal': //邮政编码
  47. return /[1-9]\d{5}(?!\d)/.test(str);
  48. case 'QQ': //QQ号
  49. return /^[1-9][0-9]{4,9}$/.test(str);
  50. case 'email': //邮箱
  51. return /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(str);
  52. case 'money': //金额(小数点2位)
  53. return /^\d*(?:\.\d{0,2})?$/.test(str);
  54. case 'URL': //网址
  55. return /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/.test(str)
  56. case 'IP': //IP
  57. return /((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))/.test(str);
  58. case 'date': //日期时间
  59. return /^(\d{4})\-(\d{2})\-(\d{2}) (\d{2})(?:\:\d{2}|:(\d{2}):(\d{2}))$/.test(str) || /^(\d{4})\-(\d{2})\-(\d{2})$/
  60. .test(str)
  61. case 'number': //数字
  62. return /^[0-9]$/.test(str);
  63. case 'english': //英文
  64. return /^[a-zA-Z]+$/.test(str);
  65. case 'chinese': //中文
  66. return /^[\\u4E00-\\u9FA5]+$/.test(str);
  67. case 'lower': //小写
  68. return /^[a-z]+$/.test(str);
  69. case 'upper': //大写
  70. return /^[A-Z]+$/.test(str);
  71. case 'HTML': //HTML标记
  72. return /<("[^"]*"|'[^']*'|[^'">])*>/.test(str);
  73. default:
  74. return true;
  75. }
  76. },
  77. $msg(title = '', param = {}) {
  78. return new Promise((resolve, reject) => {
  79. if(!title){
  80. reject();
  81. return;
  82. }
  83. uni.showToast({
  84. title,
  85. duration: param.duration || 1500,
  86. mask: param.mask || true, // 默认应该加mask 禁止提示时操作
  87. icon: param.icon || 'none',
  88. complete: result => {
  89. setTimeout(() => {
  90. resolve();
  91. }, param.duration || 1500);
  92. }
  93. });
  94. })
  95. },
  96. $url(url, options = {}){
  97. this.$u.throttle(() => {
  98. if(options.type && options.type !== ''){
  99. if(options.type === 'redirect'){ // 关闭当前,跳转
  100. uni.redirectTo({ url })
  101. }else if(options.type === 'switch'){ // 跳转
  102. uni.switchTab({ url })
  103. }else if(options.type === 'launch'){ // 关闭所有,跳转
  104. uni.reLaunch({ url })
  105. }
  106. }else{
  107. uni.navigateTo({ url }) // 跳转
  108. }
  109. }, 100);
  110. },
  111. $toBack(page = 1){
  112. let pages = getCurrentPages(); // 当前页
  113. let beforePage = pages[pages.length - (page + 1)]; // 上个页面
  114. if(beforePage && beforePage.route){
  115. uni.navigateBack({delta: page});
  116. }else{
  117. let path_url = '/pages/'+ this.$getPageType() +'/index/index';
  118. this.$url(path_url, {type: 'launch'});
  119. }
  120. },
  121. $redirectToLogin(text){
  122. let intercept_route = [
  123. 'pages/supply/login/index',
  124. 'pages/canteen/login/index',
  125. 'pages/empty/index',
  126. '/preview-image'
  127. ];
  128. if(intercept_route.includes(this.currentPage)){
  129. return; // 忽略提示跳转的路由
  130. }
  131. let path_url = '/pages/'+ this.$getPageType() +'/login/index';
  132. text = text || '您的状态异常, 即将跳转...';
  133. this.$msg(text, {duration: 800}).then(() => {
  134. this.$url(path_url, {type: 'launch'}); // 重定向到登录页
  135. })
  136. }
  137. }
  138. }