Requirements:

In any environment (except PC), users can click live broadcast to jump to the corresponding live broadcast room of the small program

Analysis:

Environment includes: wechat browser, small program, other browsers, mobile palm hall

Wechat browser: you can use wechat open TAB or cloud functions
Small program: can be called directlywx.miniProgram.navigateTo
Other browsers: cloud functions
Mobile Palmar hall: Internal method of palmar Hall (omitted)

From the above analysis, mainly through the cloud function to achieve more convenient, open

The development of

Create a cloud function

Generally, there will be a cloudFunctions folder in the new project. If the current project does not have this folder, you can create a new folder, and then right-click to create a cloudfunction. If there is no such menu option, you can directly create a small program, copy the folder inside

This is the directory structure after creation

H5 jump applet, mainly through the small program scheme code for jump, because the demand is to need dynamic scheme code, can only use cloud function to obtain, we change the entry function

Const cloud = require('wx-server-sdk') cloud.init() // cloud function exports.main = async (event) => {return cloud.openapi.urlscheme.generate({ jumpWxa: { path: event.path, query: event.query, }, isExpire: true, expire_time: Math.round(new Date().getTime() / 1000) + 3600 }) }Copy the code

Cloud. Openapi. Urlscheme. The generate () the specific call can refer to the official document, the event is invoked the incoming parameters

Second, upload cloud function

First modify the security rules in the cloud function permissions to allow all users to access

"*": { "invoke": "auth ! = null" }, "public": { "invoke": true } }Copy the code

upload

Third, the client jump public method

function getCurrentEnv() { let ua = navigator.userAgent.toLowerCase(); return new Promise((resolve, reject) => { if (ua.match(/MicroMessenger/i) == "micromessenger") { wx.miniProgram.getEnv((res) => { if (res.miniProgram) {// resolve('1')} else {// resolve('2')} else {// resolve('4')}})}; Function goWxminiprog(path) {getCurrentEnv(). Then (env => { If (env = = = '1') {/ / applet wx. MiniProgram. NavigateTo ({url: path}); } else {wx. Config ({/ / debug: true, / / when debugging, can open the appId: '* * * * * * * * * *', / / <! Timestamp: 0, timestamp: 0, timestamp: 0, timestamp: 0, timestamp: 0, timestamp: 0 'signature', // Mandatory, fill in any non-empty string jsApiList: ['chooseImage'], // OpenTagList :[' wx-open-launch-appellate '],}) var c = new cloud.appellate ({// do, Identityless: true, // resourceAppid resourceAppid: '**********', <! - replace - > ID/party/resources environment resourceEnv: '* * * * * * * * * *', <! }) let data = path.split("?" ) path = data[0][0]==="/"? Data [0]. Substr (1) : data [0] / / remove the "/" let query = data [1] | | 'console. The log (' path:' + path) console. The log (+ query query:) c.init().then(()=>{ c.callFunction({ name: 'openMini', data: { path:path, query:query }, }).then(res=>{console.warn(res) location.href = res.result.openlink})}).catch(()=>{console.log(' the current environment does not support jump applet ') }); }; export default goWxminiprogCopy the code

The input parameter is the complete jump path, including parameters, name in callFunction is the name of the cloud function, data is the parameter of the cloud function, that is, the event parameter in the entrance function of the cloud function, all ready, to call

As you can see, the jump path and parameters have been obtained

Success!!