App.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <script>
  2. export default {
  3. globalData: {
  4. schemeUrl: null
  5. },
  6. onLaunch: function(options) {
  7. console.log('App Launch', options)
  8. // 处理启动参数中的URL Scheme
  9. this.handleSchemeUrl(options);
  10. },
  11. onShow: function(options) {
  12. console.log('App Show', options)
  13. // 处理从后台进入前台时的URL Scheme
  14. this.handleSchemeUrl(options);
  15. },
  16. onHide: function() {
  17. console.log('App Hide')
  18. },
  19. methods: {
  20. // 处理URL Scheme
  21. handleSchemeUrl(options) {
  22. try {
  23. console.log('检查URL Scheme启动参数:', options);
  24. // 获取URL Scheme
  25. let schemeUrl = '';
  26. // 处理不同平台的参数
  27. if (options && options.query && options.query.url) {
  28. // H5平台
  29. schemeUrl = decodeURIComponent(options.query.url);
  30. } else if (options && options.path && options.query) {
  31. // 小程序平台
  32. schemeUrl = options.path + '?' + Object.keys(options.query).map(key => {
  33. return key + '=' + options.query[key];
  34. }).join('&');
  35. } else if (options && options.scene) {
  36. // 小程序码
  37. schemeUrl = decodeURIComponent(options.scene);
  38. } else if (uni.getEnv && uni.getEnv().uniPlatform === 'app-plus') {
  39. // App平台
  40. if (options && options.arguments) {
  41. if (typeof options.arguments === 'string') {
  42. // 尝试解析URL
  43. if (options.arguments.indexOf('traveler://') === 0) {
  44. schemeUrl = options.arguments;
  45. }
  46. } else if (options.arguments.data && typeof options.arguments.data === 'string') {
  47. schemeUrl = options.arguments.data;
  48. }
  49. }
  50. }
  51. // 如果有URL Scheme,处理跳转
  52. if (schemeUrl && schemeUrl.indexOf('traveler://') === 0) {
  53. console.log('发现URL Scheme:', schemeUrl);
  54. this.globalData.schemeUrl = schemeUrl;
  55. this.parseSchemeUrl(schemeUrl);
  56. }
  57. } catch (e) {
  58. console.error('处理URL Scheme出错:', e);
  59. }
  60. },
  61. // 解析URL Scheme并执行相应操作
  62. parseSchemeUrl(url) {
  63. try {
  64. // 移除traveler://前缀
  65. const path = url.replace('traveler://', '');
  66. // 解析路径和参数
  67. const [routePath, paramStr] = path.split('?');
  68. const params = {};
  69. if (paramStr) {
  70. paramStr.split('&').forEach(item => {
  71. const [key, value] = item.split('=');
  72. if (key && value) {
  73. // 对编码过的值进行解码
  74. params[key] = decodeURIComponent(value);
  75. }
  76. });
  77. }
  78. console.log('解析URL Scheme:', routePath, params);
  79. // 根据路径执行不同的操作
  80. switch (routePath) {
  81. case 'trip/detail':
  82. case 'trip': // 兼容简化的URL格式
  83. // 跳转到行程详情页
  84. if (params.id) {
  85. console.log('准备跳转到行程详情页:', params.id);
  86. // 延迟执行,确保应用已准备就绪
  87. setTimeout(() => {
  88. uni.navigateTo({
  89. url: '/pages/travel-detail/index?planId=' + params.id,
  90. success: () => {
  91. console.log('成功跳转到行程详情页');
  92. },
  93. fail: (err) => {
  94. console.error('跳转失败:', err);
  95. // 如果跳转失败,尝试使用switchTab
  96. uni.switchTab({
  97. url: '/pages/planning/index',
  98. success: () => {
  99. // 成功切换到规划Tab后,通过事件通知加载指定行程
  100. uni.$emit('loadTripDetail', { tripId: params.id });
  101. }
  102. });
  103. }
  104. });
  105. }, 1000);
  106. } else if (params.name && params.days && params.budget) {
  107. // 如果没有ID但有其他信息,创建临时行程
  108. const tempTrip = {
  109. id: 'temp_' + Date.now(),
  110. name: params.name,
  111. days: params.days,
  112. budget: params.budget,
  113. startDate: params.startDate || new Date().toISOString().split('T')[0],
  114. peopleCount: params.peopleCount || 1
  115. };
  116. // 保存临时行程到本地
  117. const savedTrips = uni.getStorageSync('savedTrips') || [];
  118. savedTrips.push(tempTrip);
  119. uni.setStorageSync('savedTrips', savedTrips);
  120. // 跳转到行程详情页
  121. setTimeout(() => {
  122. uni.navigateTo({
  123. url: '/pages/travel-detail/index?planId=' + tempTrip.id
  124. });
  125. }, 1000);
  126. }
  127. break;
  128. default:
  129. console.log('未知的URL Scheme路径:', routePath);
  130. // 默认跳转到首页
  131. setTimeout(() => {
  132. uni.switchTab({
  133. url: '/pages/index/index'
  134. });
  135. }, 1000);
  136. break;
  137. }
  138. } catch (e) {
  139. console.error('解析URL Scheme出错:', e);
  140. }
  141. }
  142. }
  143. }
  144. </script>
  145. <style>
  146. /*每个页面公共css */
  147. </style>