mixin.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import {mapState} from 'vuex';
  2. import Tools from '@/common/tools.js';
  3. export default{
  4. data() {
  5. return{
  6. }
  7. },
  8. computed:{
  9. ...mapState('userInfo',{
  10. userInfo: (state) => state.userInfo
  11. }),
  12. //isOpen 为字符串'1或0'
  13. // ...mapState('baseConfig',{
  14. // isOpen: (state) => state.isOpen
  15. // }),
  16. },
  17. methods: {
  18. hasInfo(){
  19. // console.log("mixinx.js----",this.userInfo,Boolean(Object.keys(this.userInfo).length > 0),this.userInfo.id)
  20. return this.userInfo && Boolean(Object.keys(this.userInfo).length > 0) && this.userInfo.id;
  21. },
  22. /**
  23. * 跳转再封装,不支持复杂传参。
  24. * const openAnimation=['slide-in-right','slide-in-left','slide-in-top','slide-in-bottom','pop-in','fade-in','zoom-out','zoom-fade-out','none'];
  25. * const closeAnimation=['slide-out-right','slide-out-left','slide-out-top','slide-out-bottom','pop-out','fade-out','zoom-in','zoom-fade-in','none'];
  26. * animationDuration:500,
  27. * animationType:openAnimation[~~(Math.random()*openAnimation.length-1)],
  28. *
  29. */
  30. navTo(path, params = {}, opt={}) {
  31. //params不能{a:JSON.stringify...}
  32. if(Tools.inAction('navTo',this)){
  33. return;
  34. }
  35. if(!path){
  36. // Tools.msg('您跳转地址不存在哦~')
  37. return;
  38. }
  39. opt={type:'push',isCheckLogin:false ,...opt}; //opt传一值补全
  40. if(opt.isCheckLogin && !this.hasInfo()){
  41. uni.navigateTo({
  42. url:'/pages/login/login'
  43. })
  44. return;
  45. }
  46. let objParams = params;
  47. let navObj={
  48. push:'navigateTo',
  49. replace:'redirectTo',
  50. replaceAll:'reLaunch',
  51. pushTab:'switchTab',
  52. }
  53. let pathUrl=path;
  54. if(objParams && typeof objParams ==='object' && Object.keys(objParams).length > 0){
  55. let pathArr=[];
  56. Object.keys(objParams).forEach(k =>{
  57. pathArr.push(`${k}=${objParams[k]}`);
  58. })
  59. pathUrl = path +'?'+pathArr.join('&');
  60. }
  61. // console.log('---',uni[navObj[opt.type]],pathUrl)
  62. uni[navObj[opt.type]]({
  63. url:pathUrl
  64. })
  65. },
  66. goBack(delta=1) {
  67. if (getCurrentPages().length < 2 && 'undefined' !== typeof __wxConfig) {
  68. let url = '/' + __wxConfig.pages[0]
  69. return uni.redirectTo({url})
  70. }
  71. uni.navigateBack({
  72. delta
  73. });
  74. },
  75. // stopPrevent(){},
  76. // developing(){
  77. // Tools.msg('开发中....')
  78. // },
  79. // goDownload(){
  80. // this.navTo('/pages/index/download')
  81. // },
  82. },
  83. }