当前位置: 首页 > news >正文

封装一个不同跳转方式的通用方法(跳转外部链接,跳转其他小程序,跳转半屏小程序)

封装一个不同跳转方式的通用方法(跳转外部链接,跳转其他小程序,跳转半屏小程序)

jumpType:
普通链接  0    例如:https://www.baidu.com/
跳转当前小程序内部  1     
外部半屏小程序  2  
外部全屏小程序  3

背景:
uniapp, vue2, 小程序 

comMethods.differentJumpType = (data, callbacks = {}) => {console.log('differentJumpType', data, 'callbacks', callbacks)// 参数结构const {jumpType,      // 跳转类型:0普通链接 1内部 2外部半屏 3外部全屏appid,         // 小程序appidpath,          // 全屏,半屏 跳转路径 page/home/indexpagePath,      // 内部:页面路径 page/home/indexlinkUrl,       // 链接地址  https://www.baidu.com/baseTabbarUrl = [], // 跳转tabbar页面路径列表extraData,   //跳转小程序携带的参数} = data;// 回调函数const {beforeJump,    // 跳转前执行 - 可以在这里添加权限校验等业务逻辑onSuccess,     // 跳转成功回调onFail,        // 跳转失败回调onComplete     // 跳转完成回调(无论成功失败都会执行)} = callbacks;// 跳转前执行 - 可以添加业务逻辑if (beforeJump && typeof beforeJump === 'function') {console.log('跳转前执行回调函数')beforeJump({ data }); }// 根据跳转类型执行不同的跳转逻辑switch (jumpType) {case 0:// 普通链接跳转// 如果是http链接,跳转到webviewif (linkUrl.indexOf('://')) {comMethods.checkUrlWebview(linkUrl);if (onSuccess) onSuccess({ type: 'webview', linkUrl });return;}break;case 1:// 内部小程序跳转setTimeout(() => {// 检查是否是tabbar页面let isTabbarPage = false;if(baseTabbarUrl && baseTabbarUrl.length > 0) {for (let i = 0; i < baseTabbarUrl.length; i++) {if (pagePath.indexOf(baseTabbarUrl[i]) !== -1) {isTabbarPage = true;break;}}}// 选择跳转方式let jumpMethod = isTabbarPage ? 'switchTab' : 'navigateTo';uni[jumpMethod]({url: pagePath,success: (res) => {if (onSuccess) onSuccess({ type: 'internal', method: jumpMethod, isTabbar: isTabbarPage,result: res });},fail: (err) => {if (onFail) onFail(err);}});}, 100); // 延迟100ms跳转break;case 2:// 外部半屏小程序跳转uni.openEmbeddedMiniProgram({appId: appid,path: path,extraData: extraData,success: (res) => {if (onSuccess) onSuccess({ type: 'embedded', appid, path,result: res });},fail: (err) => {if (onFail) onFail(err);}});break;case 3:// 外部全屏小程序跳转// 同应用跳转if (appid === wxAppId) {comMethods.pageNavigation('nav', path);if (onSuccess) onSuccess({ type: 'external_same_app', appid, path });return;}// 跨小程序跳转uni.navigateToMiniProgram({appId: appid,path: path,extraData: extraData,envVersion: envVersion,success: (res) => {console.log(`[${new Date().toISOString()}]跳转成功`);if (onSuccess) onSuccess({ type: 'external', appid, path,result: res });},fail: (err) => {console.error(`[${new Date().toISOString()}]跳转失败`);uni.showToast({title: '跳转已取消',icon: 'none'});if (onFail) onFail(err);}});break;default:uni.showToast({title: '不支持的跳转类型',icon: 'none'});console.error('不支持的跳转类型:', jumpType);break;}
}

调用示例
 

 //跳转外部链接this.$comMethods.differentJumpType({jumpType: 0,path: 'https://www.example.com'});// 内部页面跳转this.$comMethods.differentJumpType({jumpType: 1,path: '/pages/user/profile',baseTabbarUrl: ['/pages/index/index', '/pages/user/user']});// 外部半屏小程序this.$comMethods.differentJumpType({jumpType: 2,appid: 'wx1234567890',path: 'pages/index/index'});// 外部全屏小程序this.$comMethods.differentJumpType({jumpType: 3,appid: 'wx1234567890',path: 'pages/detail/detail'});

这是原有的checkUrlWebview 方法:

// 控制链接跳转-可以走路由还是走webview,url为要跳转的链接
comMethods.checkUrlWebview = (url) => {if(url.indexOf('你的项目名称前缀') > -1) {url = url.split('你的项目名称前缀')[1];comMethods.pageNavigation('nav', url);}else {comMethods.pageNavigation('nav', "/pages/tool/webview?url=" + url);}
}

http://www.dtcms.com/a/496240.html

相关文章:

  • 行业分享丨成都航天模塑如何助力汽车内外饰加速发展?
  • 去年做哪个网站能致富深圳网站排名优化公司
  • xxe靶场通关
  • TCP/IP 四层模型
  • C标准库--地域<locale.h>
  • 网站响应样式怎么让google收录网站
  • wordpress站内信插件wordpress安装后浏览首页错位
  • 《遥感大模型时空建模技术系列2-时空依赖性建模理论与基础架构》
  • 【ROS2】行为树 BehaviorTree(七):QtNodes和BehaviorTree.ROS2
  • 做药的常用网站网站空间是虚拟主机吗
  • Spring Boot 初始化钩子
  • 能打开所有网站的浏览器软件商店app下载安装
  • 斜纹水印全屏水印一键添加软件 批量处理 文字水印 图片水印 条纹水印
  • 【OpenHarmony】sensors_miscdevice小器件模块架构
  • 做物流网站有哪些内容网站 动态内容加速
  • Spring Boot 3零基础教程,WEB 开发 默认的自动配置,笔记25
  • 关键词推广软件哈尔滨网站优化页面
  • FREE下载:V2X方案之RSU介绍
  • 长春建站模板搭建网站用品推广网页
  • 推广网站哪家做的好网站是怎么盈利的
  • 台州免费自助建站模板怎么自己制作网站免费
  • Python处理淘宝API的JSONP与跨域问题
  • 多光谱图像颜色特征用于茶叶分类的研究进展
  • 做网站要学什么专业包装设计网站有哪些
  • 百度新网站收录做网站图片多少钱
  • 湖北网站推广可以做热图的工具网站
  • 实力网站优化公司首选哈尔滨模板建站系统
  • 辽宁市场网站建设销售做网站维护需要懂什么
  • 湘潭网站设计公司潍坊网站建设技术外包
  • 36.渗透-端口