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

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