前置條件:
非個人已認證小程序
1.在微信開發(fā)者工具中點擊 云開發(fā)->更多->靜態(tài)網(wǎng)站郊酒,開通靜態(tài)網(wǎng)站
2.創(chuàng)建云函數(shù) 微信開發(fā)者工具中點擊 云開發(fā)->云函數(shù)->新建
函數(shù)名稱可隨意取 例如:public
3.在原有小程序工程或新建小程序工程,project.config.json文件中添加"cloudfunctionRoot": "static/cloudbase/",
在uni-app中manifest.json->源碼視圖 "mp-weixin"下添加"cloudfunctionRoot":"static/cloudbase/"
4.在static文件夾下創(chuàng)建cloudbase/public文件夾叹俏,這個public為創(chuàng)建云函數(shù)時取得名稱
image.png
5.新建index.js文件
// 云函數(shù)入口文件
const cloud = require('wx-server-sdk')
cloud.init()
// 云函數(shù)入口函數(shù)
exports.main = async (event, context) => {
const wxContext = cloud.getWXContext()
switch (event.action) {
case 'getUrlScheme': {
return getUrlScheme(event.ocd)
}
}
return 'action not found'
}
async function getUrlScheme(ocd) {
return cloud.openapi.urlscheme.generate({
jumpWxa: {
path: '/page/xxx/xxx', // 不能帶參數(shù) 由query傳入
query: 'id=111',//參數(shù)可由此傳入
},
// 如果想不過期則置為 false教馆,并可以存到數(shù)據(jù)庫
isExpire: false,
// 一分鐘有效期
expireTime: parseInt(Date.now() / 1000 + 60),
})
}
6.編譯后在public文件夾上點擊右鍵 上傳并部署
7.新建index.html文件并上傳到靜態(tài)網(wǎng)站芙沥,訪問即可
<html>
<head>
<title>打開小程序</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1, maximum-scale=1">
<script>
window.onerror = e => {
console.error(e)
alert('發(fā)生錯誤' + e)
}
</script>
<!-- weui 樣式 -->
<link rel="stylesheet" ></link>
<!-- 調試用的移動端 console -->
<!-- <script src="https://cdn.jsdelivr.net/npm/eruda"></script> -->
<!-- <script>eruda.init();</script> -->
<!-- 公眾號 JSSDK -->
<script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
<!-- 云開發(fā) Web SDK -->
<script src="https://res.wx.qq.com/open/js/cloudbase/1.1.0/cloud.js"></script>
<script>
function getUrlParam(name) {
//構造一個含有目標參數(shù)的正則表達式對象
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
//匹配目標參數(shù)
var r = window.location.search.substr(1).match(reg);
//返回參數(shù)值
if(r != null) {
return decodeURI(r[2]);
}
return null;
}
function docReady(fn) {
if (document.readyState === 'complete' || document.readyState === 'interactive') {
fn()
} else {
document.addEventListener('DOMContentLoaded', fn);
}
}
docReady(async function() {
var ocd = getUrlParam("ocd")
var ua = navigator.userAgent.toLowerCase()
var isWXWork = ua.match(/wxwork/i) == 'wxwork'
var isWeixin = !isWXWork && ua.match(/micromessenger/i) == 'micromessenger'
var isMobile = false
var isDesktop = false
if (navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|IEMobile)/i)) {
isMobile = true
} else {
isDesktop = true
}
console.warn('ua', ua)
console.warn(ua.match(/MicroMessenger/i) == 'micromessenger')
var m = ua.match(/MicroMessenger/i)
console.warn(m && m[0] === 'micromessenger')
if (isWeixin) {
var path = "/subpages/driver/qutation/goodssourceNologDriverDetail?ocd="+ocd
var containerEl = document.getElementById('launch-btn')
containerEl.setAttribute('path',path)
var containerEl = document.getElementById('wechat-web-container')
containerEl.classList.remove('hidden')
containerEl.classList.add('full', 'wechat-web-container')
var launchBtn = document.getElementById('launch-btn')
launchBtn.addEventListener('ready', function (e) {
console.log('開放標簽 ready')
})
launchBtn.addEventListener('launch', function (e) {
console.log('開放標簽 success')
})
launchBtn.addEventListener('error', function (e) {
console.log('開放標簽 fail', e.detail)
})
wx.config({
// debug: true, // 調試時可開啟
appId: 'XXXXXX', // <!-- replace -->
timestamp: 0, // 必填鳞绕,填任意數(shù)字即可
nonceStr: 'nonceStr', // 必填,填任意非空字符串即可
signature: 'signature', // 必填捡鱼,填任意非空字符串即可
jsApiList: ['chooseImage'], // 必填,隨意一個接口即可
openTagList:['wx-open-launch-weapp'], // 填入打開小程序的開放標簽名
})
} else if (isDesktop) {
// 在 pc 上則給提示引導到手機端打開
var containerEl = document.getElementById('desktop-web-container')
containerEl.classList.remove('hidden')
containerEl.classList.add('full', 'desktop-web-container')
} else {
var containerEl = document.getElementById('public-web-container')
containerEl.classList.remove('hidden')
containerEl.classList.add('full', 'public-web-container')
var c = new cloud.Cloud({
// 必填酷愧,表示是未登錄模式
identityless: true,
// 資源方 AppID
resourceAppid: 'XXXXXX', // <!-- replace -->
// 資源方環(huán)境 ID
resourceEnv: 'xxxxxx', // <!-- replace -->
})
await c.init()
window.c = c
var buttonEl = document.getElementById('public-web-jump-button')
var buttonLoadingEl = document.getElementById('public-web-jump-button-loading')
try {
await openWeapp(() => {
buttonEl.classList.remove('weui-btn_loading')
buttonLoadingEl.classList.add('hidden')
})
} catch (e) {
buttonEl.classList.remove('weui-btn_loading')
buttonLoadingEl.classList.add('hidden')
throw e
}
}
})
async function openWeapp(onBeforeJump) {
var ocd = getUrlParam("ocd")
var c = window.c
const res = await c.callFunction({
name: 'public',
data: {
action: 'getUrlScheme',
ocd:ocd
},
})
console.warn(res)
if (onBeforeJump) {
onBeforeJump()
}
location.href = res.result.openlink
}
</script>
<style>
.hidden {
display: none;
}
.full {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.public-web-container {
display: flex;
flex-direction: column;
align-items: center;
}
.public-web-container p {
position: absolute;
top: 40%;
}
.public-web-container a {
position: absolute;
bottom: 40%;
}
.wechat-web-container {
display: flex;
flex-direction: column;
align-items: center;
}
.wechat-web-container p {
position: absolute;
top: 40%;
}
.wechat-web-container wx-open-launch-weapp {
position: absolute;
bottom: 40%;
left: 0;
right: 0;
display: flex;
flex-direction: column;
align-items: center;
}
.desktop-web-container {
display: flex;
flex-direction: column;
align-items: center;
}
.desktop-web-container p {
position: absolute;
top: 40%;
}
</style>
</head>
<body>
<div class="page full">
<div id="public-web-container" class="hidden">
<p class="">正在打開 “xxxxxx”...</p> <!-- replace -->
<a id="public-web-jump-button" href="javascript:" class="weui-btn weui-btn_primary weui-btn_loading" onclick="openWeapp()">
<span id="public-web-jump-button-loading" class="weui-primary-loading weui-primary-loading_transparent"><i class="weui-primary-loading__dot"></i></span>
打開小程序
</a>
</div>
<div id="wechat-web-container" class="hidden">
<p class="">點擊以下按鈕打開 “xxxxxx”</p> <!-- replace -->
<!-- 跳轉小程序的開放標簽驾诈。文檔 https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_Open_Tag.html -->
<wx-open-launch-weapp id="launch-btn" username="gh_"
path=""> <!-- replace -->
<template>
<button style="width: 200px; height: 45px; text-align: center; font-size: 17px; display: block; margin: 0 auto; padding: 8px 24px; border: none; border-radius: 4px; background-color: #07c160; color:#fff;">打開小程序</button>
</template>
</wx-open-launch-weapp>
</div>
<div id="desktop-web-container" class="hidden">
<p class="">請在手機打開網(wǎng)頁鏈接</p>
</div>
</div>
</body>
</html>