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

微信小程序开发实用技巧篇

微信小程序开发实用技巧篇

1. 小程序跳转第三方链接(以应用场景 小程序跳转H5 为例)

1.1 获取京东短链接:确保你有一个有效的京东短链接。

1.2. 使用微信小程序的 web-view 组件:由于微信小程序的安全策略限制,直接使用 navigator 或 link 无法跳转到外部链接。可以使用 web-view 组件加载一个中间页面,在该页面中通过 JavaScript 进行跳转。

<template >
  <view class="container">
     <web-view src="{{redirectUrl}}"></web-view>
  </view>
</template>

1.3 在小程序中动态生成链接在你的小程序页面的 JS 文件中,动态生成包含目标短链接的 URL:

<script>
export default {
  data() {
    return{
      redirectUrl:''
    }
  },
  computed: {
  },
  watch:{
  },
  onLoad() {
    this.goWebPage()
  },
  onReady(){
  },
  methods:{
    goWebPage(){
      var jdShortLink = 'https://u.jd.com/Q1HLqhV'; // 替换为实际链接
      var encodedUrl = encodeURIComponent(jdShortLink);
      // 定义常量
      const REDIRECT_BASE_URL = 'https://jingdong.xingfu.com/redirect.html?url=';
      try {
        // 确保 encodedUrl 已经正确编码
        const safeEncodedUrl = encodeURIComponent(encodedUrl);
        this.redirectUrl = `${REDIRECT_BASE_URL}${safeEncodedUrl}`;
        console.log("this.redirectUrl"+this.redirectUrl);
      } catch (error) {
        console.error('设置 redirectUrl 时发生错误:', error);
        // 可以根据需求进行进一步处理,例如回退到默认值或通知用户
      }
    }
  }
}
</script>

1.4 准备中间页面。确保你的中间页面 redirect.html 能够接收一个参数来指定跳转的 URL。修改 redirect.html。

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>跳转中...</title>
    <script type="text/javascript">
        window.onload = function() {
            // 获取 URL 参数
            var urlParams = new URLSearchParams(window.location.search);
            var targetUrl = urlParams.get('url');
            if (targetUrl) {
                window.location.href = decodeURIComponent(targetUrl);
            } else {
                document.body.innerHTML = '<p>无效的跳转链接</p>';
            }
        };
    </script>
</head>
<body>
    <p>正在跳转,请稍候...</p>
</body>
</html>

1.5 在微信小程序管理后台的 开发设置 中,添加你的服务器域名到 业务域名 白名单中,确保小程序可以访问该域名。

在这里插入图片描述

1.6

在这里插入图片描述

注:一般情况需要将校验文件加到域名所在服务器的文件目录下。

2. 微信小程序跳转其他小程序

一个简单应用场景模版,仅供参考(以跳转订购电影票小程序为例),

<template >
  <view class="container">
  </view>
</template>
<script>
export default {
  data() {
    return{
      message: 'Light one more thing',
      redirectUrl:''
    }
  },
  computed: {
  },
  watch:{
  },
  onLoad(op) {
    this.goMiniapp()
  },
  onReady(){
  },
  methods:{
    goMiniapp() {
      uni.openEmbeddedMiniProgram({
        appId: 'wx6ce7b07bf7fe6048', // 要唤起的小程序的appid
        path: '/pages/index/index?subplatformid=274958jutuikechaojushenghuo',//要打开B小程序的页面
        extraData: { },//需要传递给目标小程序的数据,目标小程序可在 App.onLaunch,App.onShow 中获取到这份数据
        envVersion: 'release',//打开的对应小程序环境:开发develop、体验trial、生产release
        success(res) {
        // 跳转成功
          console.log('success')
        },
        fail(err) {
          console.log(err)
          // 打开失败/取消
          uni.showToast({
            title: '请稍后重试'
          })
        }
      })
    },
  }
}

</script>
<style>
</style>

相关文章:

  • 支持列表拖拽嵌套,AI流式输出的多模态文档编辑器flowmix/docx: 全面升级
  • SQL Server:查看内存使用情况
  • 信号与系统第二章学习(五)
  • npm安装时无法访问github域名的解决方法
  • 网络安全事件分级
  • 图像变换:了解透视变换原理及OpenCV实现
  • qt QToolButton使用总结
  • qt中部件存储自定义数据
  • 《网络编程卷2:进程间通信》第四章:管道与FIFO深度解析
  • Spring Boot(8)深入理解 @Autowired 注解:使用场景与实战示例
  • 面试经典150题——位运算
  • 本地部署DeepSeek集成VSCode创建自己的AI助手
  • 国内 网络安全沙箱
  • IC-Portrait:打造逼真个性化肖像的新纪元!
  • 什么是AI Agent、Chat、RAG、MoE
  • http 模块
  • 网络安全协议之比较(SSH、PKI、SET、SSL)
  • 宏基传奇swift edge偶尔开机BIOS重置
  • kafka了解-笔记
  • 【React】组件通信
  • 中国海警就菲向非法“坐滩”仁爱礁军舰运补发表谈话
  • 牛市早报|年内首次存款利率下调启动,5月LPR今公布
  • MiniMax发布新一代语音大模型
  • 电子凭证会计数据标准推广至全国
  • 人民日报大家谈:为基层减负,治在根子上减到点子上
  • 英国警方再逮捕一名涉嫌参与首相住宅纵火案嫌疑人