功能需求
H5 页面点击按钮后跳转到对应小程序
开端实现方案 - 1
使用微信 sdk 的 API navigateToMiniProgram
代码
@/utils/wxApi.js 封装 - 微信初始化代码
- import wx from 'weixin-js-sdk'
- import { wechat_config_init_get } from '@/api/cancel.js'
- import { isAndroid } from '@/utils/utils'
- import { Toast } from 'vant'
- export function wxConfig() {
- return new Promise((resolve, reject) => {
- const url = isAndroid() ? window.location.href : window.location.href.replace(location.hash, '')
- const that = this
- wechat_config_init_get({ url: url })
- .then(result => {
- if (result.code === 200) {
- const config = result.data
- // that.appid = config.appid
- wx.config({
- debug: false,
- beta: true,
- appId: config.appid,
- timestamp: config.timestamp,
- nonceStr: config.nonceStr,
- signature: config.signature,
- jsApiList: [
- 'requestWxFacePictureVerify',
- 'checkIsSupportFaceDetect',
- 'navigateToMiniProgram'
- // 所有要调用的 API 都要加到这个列表中
- ]
- })
- wx.error(function (res) {
- reject(res)
- console.log(res)
- // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
- Toast.fail('微信配置初始化失败')
- })
- wx.ready(function (data) {
- resolve(data)
- console.log(data)
- })
- } else {
- Toast.fail(result.code + '----' + result)
- }
- })
- .catch(error => {
- reject(error)
- Toast.clear()
- const msg = '获取微信配置失败!'
- Toast.fail(error + msg)
- })
- })
- }
复制代码 PoliceAssistant.vue 点击按钮举行小程序跳转 代码
- <!-- 交警助手 -->
- <template>
- <div class="PoliceAssistant" @click="toAI">
- <img src="@/assets/images/police-assistant@2x.png" />
- <span>交警助手</span>
- </div>
- </template>
- <script>
- import wx from 'weixin-js-sdk'
- import { wxConfig } from '@/utils/wxApi'
- import { Toast } from 'vant'
- export default {
- name: 'PoliceAssistant',
- components: {},
- data() {
- return {}
- },
- computed: {},
- watch: {},
- created() {},
- mounted() {
- if (!this.$isZLbOrWxZlb()) {
- wxConfig()
- }
- },
- methods: {
- toAI() {
- wx.navigateToMiniProgram({
- appId: '小程序的AppID',
- path: 'pages/index/index', // 要跳转的小程序的页面
- success(res) {
- console.log('success', res)
- },
- fail(res) {
- Toast.fail('跳转失败!')
- }
- })
- }
- }
- }
- </script>
- <style lang='scss' scoped>
- .PoliceAssistant {
- position: absolute;
- bottom: 10%;
- right: 0;
- img {
- width: 85px;
- height: 80px;
- }
- span {
- position: absolute;
- bottom: 15px;
- left: 20px;
- font-size: 10px;
- }
- }
- </style>
复制代码 题目
由图中 绿色 框框住的内容可看出,navigateToMiniProgram 并未初始化成功,找了半天题目缘故原由,试着排查权限题目
- 找公众号负责人排查当前公众号是否开放当前API权限,也找不到在哪里开权限。
- 无奈,只能使用下一个办法↓↓↓
开端实现方案 - 2
使用开放标签 wx-open-launch-weapp 举行H5跳转小程序
代码
index.html
- <script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
复制代码 vue.config.js
- module.exports = {
- chainWebpack: config => {
- config.module
- .rule('vue')
- .use('vue-loader')
- .tap(options => {
- options.compilerOptions = {
- ...options.compilerOptions,
- isCustomElement: tag => tag.startsWith('wx-')
- }
- return options
- })
- }
- }
复制代码 @/utils/wxApi.js 封装 - 微信初始化代码
- import wx from 'weixin-js-sdk'
- import { wechat_config_init_get } from '@/api/cancel.js'
- import { isAndroid } from '@/utils/utils'
- import { Toast } from 'vant'
- export function wxConfig() {
- return new Promise((resolve, reject) => {
- const url = isAndroid() ? window.location.href : window.location.href.replace(location.hash, '')
- // const url = 'https://jj.gaj.ningbo.gov.cn/qxksh5/'
- const that = this
- wechat_config_init_get({ url: url })
- .then(result => {
- if (result.code === 200) {
- const config = result.data
- // that.appid = config.appid
- wx.config({
- debug: false,
- beta: true,
- appId: config.appid,
- timestamp: config.timestamp,
- nonceStr: config.nonceStr,
- signature: config.signature,
- jsApiList: [
- 'requestWxFacePictureVerify',
- 'checkIsSupportFaceDetect',
- 'navigateToMiniProgram'
- // 所有要调用的 API 都要加到这个列表中
- ],
- openTagList: ['wx-open-launch-weapp']
- })
- wx.error(function (res) {
- reject(res)
- console.log(res)
- // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
- Toast.fail('微信配置初始化失败')
- })
- wx.ready(function (data) {
- resolve(data)
- console.log(data)
- })
- } else {
- Toast.fail(result.code + '----' + result)
- }
- })
- .catch(error => {
- reject(error)
- Toast.clear()
- const msg = '获取微信配置失败!'
- Toast.fail(error + msg)
- })
- })
- }
复制代码 PoliceAssistant.vue 点击按钮举行小程序跳转 代码
- <!-- 交警助手 -->
- <template>
- <div class="PoliceAssistant" @click="toAI">
- <!-- 【办法一】 -->
- <!-- <wx-open-launch-weapp id="launch-btn" username="gh_开头的原始id" path="pages/index/index">
- <img src="@/assets/images/police-assistant@2x.png" />
- <span>交警助手</span>
- </wx-open-launch-weapp> -->
- <!-- 【办法2】 -->
- <wx-open-launch-weapp id="launch-btn" username="gh_开头的原始id" path="pages/index/index">
- <script type="text/wxtag-template">
- <style>
- img {
- width: 85px;
- height: 80px;
- }
- span {
- position: absolute;
- bottom: 15px;
- left: 20px;
- font-size: 10px;
- }
- </style>
- <img src="../assets/images/police-assistant@2x.png" />
- <span>交警助手</span>
- </script>
- </wx-open-launch-weapp>
- </div>
- </template>
- <script>
- import wx from 'weixin-js-sdk'
- import { wxConfig } from '@/utils/wxApi'
- import { Toast } from 'vant'
- export default {
- name: 'PoliceAssistant',
- components: {},
- data() {
- return {}
- },
- computed: {},
- watch: {},
- created() {},
- mounted() {
- if (!this.$isZLbOrWxZlb()) {
- wxConfig()
- .then(() => {
- this.initWxConfig()
- })
- .catch(err => {
- console.error('wx config failed:', err)
- Toast.fail('微信配置失败')
- })
- }
- },
- methods: {
- initWxConfig() {
- wx.ready(() => {
- console.log('wx-jssdk 初始化成功')
- this.bindWxEvents()
- })
- wx.error(err => {
- console.error('wx-jssdk 初始化失败:', err)
- Toast.fail('微信初始化失败')
- })
- },
- bindWxEvents() {
- const btnElem = document.getElementById('launch-btn')
- if (btnElem) {
- btnElem.addEventListener('launch', e => {
- console.log('跳转小程序成功')
- })
- btnElem.addEventListener('error', e => {
- console.log('跳转小程序失败', e.detail)
- Toast.fail('跳转失败!')
- })
- }
- },
- toAI() {
- // 移除这个方法中的事件绑定逻辑,因为现在在 wx.ready 后就绑定了
- }
- }
- }
- </script>
- <style lang='scss' scoped>
- .PoliceAssistant {
- position: absolute;
- bottom: 10%;
- right: 0;
- img {
- width: 85px;
- height: 80px;
- }
- span {
- position: absolute;
- bottom: 15px;
- left: 20px;
- font-size: 10px;
- }
- }
- </style>
复制代码 题目
PoliceAssistant.vue 文件中
- 【办法一】的按钮能出来,但是点击没有任何反应
- 【办法二】的按钮不表现
- 再去排查开放标签题目 ↓↓↓
如图所示 红色 框框住的内容,开放标签 'wx-open-launch-weapp' 没有初始化成功,微信初始化 debug 报错 {"errMsg":"config k","verifyJsApiList":["requestWxFacePictureVerify","checkIsSupportFaceDetect"],"verifyOpenTagList":[]} ,
开启 DeepSeek 之路 ↓
搜刮报错信息
参考 deepseek 给的复兴,办理方法 1-7 中只有 3 大概有题目,就去排查 查抄开放标签权限 ,但是在 接口权限 页面中并没有找到 wx-open-launch-weapp ,怀疑是不是微信公众号性质 微信订阅号 / 微信服务号 的题目。
微信订阅号 wx-open-launch-weapp 开放标签权限
- 通过以上 deepseek 答复,明白大概大概是因为我们的公众号是 订阅号 的题目,
- 想去小程序后台天生 url 跳转 ,但是找不到 工具 就找不到在哪里天生 url 跳转
- 继续deepseek ↓↓↓
小程序后台找不到工具栏
最终办理办法
调用后端接口天生 url 直接跳转 url 所在打开小程序
获取加密URLLink
@/api/common.js
- import request from '@/utils/request'
- /** 获取宁波AI小程序地址 **/
- export function getUrlLink(params) {
- return request({
- url: '/application/miniProgram/getUrlLink?path=/pages/index/index',
- method: 'get',
- params
- })
- }
复制代码 PoliceAssistant.vue 点击按钮举行小程序跳转 代码
- <!-- 交警助手 -->
- <template>
- <div class="PoliceAssistant" @click="toAI">
- <img src="@/assets/images/police-assistant@2x.png" />
- <span>交警助手</span>
- </div>
- </template>
- <script>
- import { getUrlLink } from '@/api/common'
- import { Toast } from 'vant'
- export default {
- name: 'PoliceAssistant',
- components: {},
- data() {
- return {}
- },
- computed: {},
- watch: {},
- created() {},
- mounted() {},
- methods: {
- toAI() {
- getUrlLink()
- .then(res => {
- if (res.data) {
- window.location.href = res.data
- } else {
- Toast.fail('跳转小程序失败,请稍后重试!')
- }
- })
- .catch(() => {
- Toast.fail('跳转小程序失败,请稍后重试!')
- })
- }
- }
- }
- </script>
- <style lang='scss' scoped>
- .PoliceAssistant {
- position: absolute;
- bottom: 10%;
- right: 0;
- img {
- width: 85px;
- height: 80px;
- }
- span {
- position: absolute;
- bottom: 15px;
- left: 20px;
- font-size: 10px;
- }
- }
- </style>
复制代码 最终效果
别人类似的题目
https://juejin.cn/post/6932023969759363080
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |