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

90 lines
2.5 KiB

4 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. this.getTokenValue(scene);
  16. }else{
  17. // this.routeToPage(options);
  18. // 普通小程序分享进入
  19. this.getTokenValue(options.token);
  20. }
  21. },
  22. methods: {
  23. getTokenValue(token){
  24. this.$http(this.API.API_WAREHOUSE_GET, {key: token}).then(res => {
  25. console.log("getTokenValue", res);
  26. let options = JSON.parse(res.data);
  27. this.routeToPage(options);
  28. }).catch(err => {
  29. // 当token获取失败,则默认跳转到首页
  30. this.$url('/pages/index/index', {type: 'switch'});
  31. })
  32. },
  33. // 路由分发
  34. routeToPage(options){
  35. if(options.route == 'goods_detail' || options.route == 'goods'){
  36. options.page_url = '/pages/goodsDetail/index';
  37. this.joinPagePath(options);
  38. }else if(options.route == 'home'){
  39. options.page_url = '/pages/index/index';
  40. options.is_tabbar = true; // 是否为tabbar,如果是需要多传入该参数
  41. this.joinPagePath(options);
  42. }else{
  43. let obj = {
  44. page_url: '/pages/index/index', // 啥都不填,默认跳转到首页
  45. is_tabbar: true
  46. };
  47. this.joinPagePath(obj);
  48. }
  49. },
  50. // 拼接地址,并相应跳转
  51. joinPagePath(par){
  52. let path = par.page_url;
  53. let options = {}; // 传给页面的参数,用于解决switch不能传参的问题
  54. let flag = true; // 标志,用于判断拼接次数,?只能出现一次
  55. for(let i in par){
  56. if(i != 'route' && i != 'page_url' && i != 'is_tabbar'){ // 跳过route、page_url、is_tabbar
  57. options[i] = par[i];
  58. if(flag){
  59. path += '?'+ i +'='+ par[i];
  60. flag = false;
  61. }else{
  62. path += '&'+ i +'='+ par[i];
  63. }
  64. }
  65. }
  66. console.log(par);
  67. if(par.is_tabbar){
  68. uni.setStorageSync('homePageOptions', options);
  69. this.$url(par.page_url, {type: 'switch'});
  70. }else{
  71. this.$url(path, {type: 'redirect'});
  72. }
  73. },
  74. // 将key=value&key=value形式的字符串转为对象
  75. strToObj(str){
  76. let obj = {};
  77. if(!str) return obj;
  78. let arr = str.split('&');
  79. arr.map(item => {
  80. let a = item.split('=');
  81. obj[a[0]] = a[1];
  82. });
  83. return obj;
  84. }
  85. }
  86. }
  87. </script>