Development log: WeChat applet Suncode incoming parameter analysis

When entering the applet through the QR code, you can get the parameters in the onload function.

onLoad(options) {
    
    
	console.log(options)
}

This is the printed result. So, what we need to use here is options.scene.
Incoming raw data

The first step is to use decodeURIComponent to parse it to get the incoming parameters.

post_id=74413&category_id=360

But this is a string, which is inconvenient to retrieve and store values ​​later.

In the second step , the ordinary strings in options.scene can be spliced ​​and modified into json strings, and then converted into objects using JSON.parse.
The final result
This becomes convenient data.

The sum is: where you need to get the parameters, just call the function this.sceneToObj()

onLoad(options) {
    
    
	if (options.scene) {
    
      // 扫码进来
		let query = this.sceneToObj(options.scene)
        this.setData({
    
    
        	postID: query.post_id || '',
        	categoryID: query.category_id || ''
        })
    }
},
sceneToObj(str) {
    
    
	const scene = decodeURIComponent(str)
	let str1 = scene.replace(/=/g, '":')
	let str2 = str1.replace(/&/g, ',"')
	let obj = JSON.parse('{"' + str2 + '}')
	return obj
}

Guess you like

Origin blog.csdn.net/qq_37992222/article/details/112198531