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