自主项目,食堂系统,前端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.

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