123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- <script>
- export default {
- globalData: {
- schemeUrl: null
- },
- onLaunch: function(options) {
- console.log('App Launch', options)
- // 处理启动参数中的URL Scheme
- this.handleSchemeUrl(options);
- },
- onShow: function(options) {
- console.log('App Show', options)
- // 处理从后台进入前台时的URL Scheme
- this.handleSchemeUrl(options);
- },
- onHide: function() {
- console.log('App Hide')
- },
- methods: {
- // 处理URL Scheme
- handleSchemeUrl(options) {
- try {
- console.log('检查URL Scheme启动参数:', options);
-
- // 获取URL Scheme
- let schemeUrl = '';
-
- // 处理不同平台的参数
- if (options && options.query && options.query.url) {
- // H5平台
- schemeUrl = decodeURIComponent(options.query.url);
- } else if (options && options.path && options.query) {
- // 小程序平台
- schemeUrl = options.path + '?' + Object.keys(options.query).map(key => {
- return key + '=' + options.query[key];
- }).join('&');
- } else if (options && options.scene) {
- // 小程序码
- schemeUrl = decodeURIComponent(options.scene);
- } else if (uni.getEnv && uni.getEnv().uniPlatform === 'app-plus') {
- // App平台
- if (options && options.arguments) {
- if (typeof options.arguments === 'string') {
- // 尝试解析URL
- if (options.arguments.indexOf('traveler://') === 0) {
- schemeUrl = options.arguments;
- }
- } else if (options.arguments.data && typeof options.arguments.data === 'string') {
- schemeUrl = options.arguments.data;
- }
- }
- }
-
- // 如果有URL Scheme,处理跳转
- if (schemeUrl && schemeUrl.indexOf('traveler://') === 0) {
- console.log('发现URL Scheme:', schemeUrl);
- this.globalData.schemeUrl = schemeUrl;
- this.parseSchemeUrl(schemeUrl);
- }
- } catch (e) {
- console.error('处理URL Scheme出错:', e);
- }
- },
-
- // 解析URL Scheme并执行相应操作
- parseSchemeUrl(url) {
- try {
- // 移除traveler://前缀
- const path = url.replace('traveler://', '');
-
- // 解析路径和参数
- const [routePath, paramStr] = path.split('?');
- const params = {};
-
- if (paramStr) {
- paramStr.split('&').forEach(item => {
- const [key, value] = item.split('=');
- if (key && value) {
- // 对编码过的值进行解码
- params[key] = decodeURIComponent(value);
- }
- });
- }
-
- console.log('解析URL Scheme:', routePath, params);
-
- // 根据路径执行不同的操作
- switch (routePath) {
- case 'trip/detail':
- case 'trip': // 兼容简化的URL格式
- // 跳转到行程详情页
- if (params.id) {
- console.log('准备跳转到行程详情页:', params.id);
-
- // 延迟执行,确保应用已准备就绪
- setTimeout(() => {
- uni.navigateTo({
- url: '/pages/travel-detail/index?planId=' + params.id,
- success: () => {
- console.log('成功跳转到行程详情页');
- },
- fail: (err) => {
- console.error('跳转失败:', err);
-
- // 如果跳转失败,尝试使用switchTab
- uni.switchTab({
- url: '/pages/planning/index',
- success: () => {
- // 成功切换到规划Tab后,通过事件通知加载指定行程
- uni.$emit('loadTripDetail', { tripId: params.id });
- }
- });
- }
- });
- }, 1000);
- } else if (params.name && params.days && params.budget) {
- // 如果没有ID但有其他信息,创建临时行程
- const tempTrip = {
- id: 'temp_' + Date.now(),
- name: params.name,
- days: params.days,
- budget: params.budget,
- startDate: params.startDate || new Date().toISOString().split('T')[0],
- peopleCount: params.peopleCount || 1
- };
-
- // 保存临时行程到本地
- const savedTrips = uni.getStorageSync('savedTrips') || [];
- savedTrips.push(tempTrip);
- uni.setStorageSync('savedTrips', savedTrips);
-
- // 跳转到行程详情页
- setTimeout(() => {
- uni.navigateTo({
- url: '/pages/travel-detail/index?planId=' + tempTrip.id
- });
- }, 1000);
- }
- break;
-
- default:
- console.log('未知的URL Scheme路径:', routePath);
- // 默认跳转到首页
- setTimeout(() => {
- uni.switchTab({
- url: '/pages/index/index'
- });
- }, 1000);
- break;
- }
-
- } catch (e) {
- console.error('解析URL Scheme出错:', e);
- }
- }
- }
- }
- </script>
- <style>
- /*每个页面公共css */
- </style>
|