update:新增网页中转和二维码跳转微信小程序
This commit is contained in:
parent
31ee325d93
commit
edf1359dc4
@ -3,13 +3,9 @@
|
||||
*/
|
||||
import http from '../utils/http'
|
||||
|
||||
export function GetScheme (){
|
||||
return new Promise((resolve, reject) => {
|
||||
http({
|
||||
url:'/api//admin/shop/wxqrcode/gen/common/wxurlscheme',
|
||||
method:'post',
|
||||
}).then(res=>{
|
||||
resolve(res)
|
||||
}).catch(e => reject(console.warn(e)))
|
||||
export function GetScheme() {
|
||||
return http({
|
||||
url: '/api/admin/shop/wxqrcode/common/wxurlscheme',
|
||||
method: 'post',
|
||||
})
|
||||
}
|
||||
}
|
||||
BIN
src/assets/image/indexQRcode.png
Normal file
BIN
src/assets/image/indexQRcode.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 102 KiB |
@ -1,48 +1,72 @@
|
||||
<template>
|
||||
<div class="wrapper">
|
||||
<p>正在打开小程序,请稍候…</p>
|
||||
<button id="openBtn" class="btn" @click="forceOpen">
|
||||
<div>
|
||||
<img
|
||||
src="@/assets/image/indexQRcode.png"
|
||||
alt="小程序二维码"
|
||||
class="qr-img"
|
||||
style="width: 300px; height: 300px; margin-bottom: 20px;"
|
||||
>
|
||||
</div>
|
||||
|
||||
<p style="font-size: 30px;">小发同城,和您一起享受本地生活服务</p>
|
||||
<button id="openBtn" class="btn" @click="go">
|
||||
立即打开
|
||||
</button>
|
||||
<button class="btn" @click="handleScheme">
|
||||
测试获取
|
||||
</button>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { onMounted } from 'vue'
|
||||
import {GetScheme} from '@/api/scheme.js'
|
||||
// 1. 微信 URL Scheme
|
||||
const scheme = 'weixin://dl/business/?t=9FVo0FY1jLk'
|
||||
import { ref, onMounted } from 'vue'
|
||||
import { GetScheme } from '@/api/scheme.js'
|
||||
import { ElMessage } from 'element-plus'
|
||||
|
||||
// 2. 判断是否在微信内
|
||||
const scheme = ref('weixin://dl/business/?t=RNOoY1cRSDv')
|
||||
|
||||
// 判断是否在微信内
|
||||
const isWechat = /MicroMessenger/i.test(navigator.userAgent)
|
||||
|
||||
// 3. 微信内:优先用 JSSDK
|
||||
// 微信内:优先用 JSSDK 跳转
|
||||
function wxJump() {
|
||||
if (!isWechat) return false
|
||||
if (!window.wx) return false
|
||||
wx.miniProgram.navigateTo({ url: 'pages/index/index' })
|
||||
if (!window.wx) {
|
||||
ElMessage.warning('微信JSSDK未加载,将使用Scheme跳转')
|
||||
return false
|
||||
}
|
||||
wx.miniProgram.navigateTo({ url: '/pages/index/index' }) // 路径补全/,避免报错
|
||||
return true
|
||||
}
|
||||
function handleScheme(){
|
||||
let res=GetScheme();
|
||||
console.log("数据",res);
|
||||
|
||||
// 异步获取Scheme
|
||||
async function handleScheme() {
|
||||
try {
|
||||
const res = await GetScheme()
|
||||
if (res.code === 0 && res.data?.openlink) {
|
||||
scheme.value = res.data.openlink // 动态更新Scheme
|
||||
} else {
|
||||
ElMessage.error('获取Scheme失败:' + (res.msg || '无有效链接'))
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('接口请求异常:', err)
|
||||
ElMessage.error('网络异常,请重试!')
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 兜底:URL Scheme
|
||||
function schemeJump() {
|
||||
location.href = scheme
|
||||
if (!scheme.value) {
|
||||
ElMessage.warning('Scheme为空,无法跳转')
|
||||
return
|
||||
}
|
||||
location.href = scheme.value
|
||||
}
|
||||
|
||||
// 5. 统一入口
|
||||
//跳转小程序
|
||||
function go() {
|
||||
if (!wxJump()) schemeJump()
|
||||
}
|
||||
|
||||
// 6. 页面加载后自动尝试
|
||||
onMounted(() => {
|
||||
//页面加载后自动获取Scheme,并尝试跳转
|
||||
onMounted(async () => {
|
||||
await handleScheme() // 先获取最新Scheme,再跳转
|
||||
go()
|
||||
})
|
||||
</script>
|
||||
@ -55,6 +79,7 @@ onMounted(() => {
|
||||
justify-content: center;
|
||||
height: 100vh;
|
||||
font-size: 16px;
|
||||
margin: 0; /* 清除默认边距 */
|
||||
}
|
||||
.btn {
|
||||
margin-top: 30px;
|
||||
@ -64,5 +89,9 @@ onMounted(() => {
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
font-size: 16px;
|
||||
cursor: pointer;
|
||||
}
|
||||
.btn:hover {
|
||||
background: #06b058;
|
||||
}
|
||||
</style>
|
||||
Loading…
Reference in New Issue
Block a user