时空网前端
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.

76 lines
2.1 KiB

5 years ago
  1. <template>
  2. <view></view>
  3. </template>
  4. <script>
  5. /* 路由分发页面仅供跳转页面 */
  6. export default {
  7. onLoad(options){
  8. console.log('wai',options)
  9. if(this.$shared.isValueType(options.scene) != 'undefined'){
  10. const scene = decodeURIComponent(options.scene); // 二维码扫码进入
  11. const par = this.strToObj(scene);
  12. this.routeToPage(par);
  13. console.log('内部',options)
  14. console.log('====',par)
  15. }else{
  16. this.routeToPage(options); // 普通小程序分享进入
  17. }
  18. },
  19. methods: {
  20. // 路由分发
  21. routeToPage(options){
  22. if(options.route == 'goods_detail' || options.route == 'goods'){
  23. options.page_url = '/pages/goodsDetail/index';
  24. this.joinPagePath(options);
  25. }else if(options.route == 'home'){
  26. options.page_url = '/pages/index/index';
  27. options.is_tabbar = true; // 是否为tabbar,如果是需要多传入该参数
  28. this.joinPagePath(options);
  29. }else{
  30. let obj = {
  31. page_url: '/pages/index/index', // 啥都不填,默认跳转到首页
  32. is_tabbar: true
  33. };
  34. this.joinPagePath(obj);
  35. }
  36. },
  37. // 拼接地址,并相应跳转
  38. joinPagePath(par){
  39. let path = par.page_url;
  40. let options = {}; // 传给页面的参数,用于解决switch不能传参的问题
  41. let flag = true; // 标志,用于判断拼接次数,?只能出现一次
  42. for(let i in par){
  43. if(i != 'route' && i != 'page_url' && i != 'is_tabbar'){ // 跳过route、page_url、is_tabbar
  44. options[i] = par[i];
  45. if(flag){
  46. path += '?'+ i +'='+ par[i];
  47. flag = false;
  48. }else{
  49. path += '&'+ i +'='+ par[i];
  50. }
  51. }
  52. }
  53. console.log(par);
  54. if(par.is_tabbar){
  55. uni.setStorageSync('homePageOptions', options);
  56. this.$url(par.page_url, {type: 'switch'});
  57. }else{
  58. this.$url(path, {type: 'redirect'});
  59. }
  60. },
  61. // 将key=value&key=value形式的字符串转为对象
  62. strToObj(str){
  63. let obj = {};
  64. if(!str) return obj;
  65. let arr = str.split('&');
  66. arr.map(item => {
  67. let a = item.split('=');
  68. obj[a[0]] = a[1];
  69. });
  70. return obj;
  71. }
  72. }
  73. }
  74. </script>