需求描述
微信授权URL
,给重定向的Url
传参数,这时我们需要给state
传递多个参数,就需要将多参数处理成Json
字符串,如 增加 id=1,name=xiaoming
。
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx5fb25c9dc619f190&redirect_uri=http://test-recycle-h5.jxypapp.com&response_type=code&scope=snsapi_base&state=#wechat_redirect
处理需求
将id=1,name=xiaoming
转化为Json
字符串,并将转化后Json
字符串encode
。
let state = encodeURIComponent(JSON.stringify({
id:'1',
name:'xiaoming'
})) ;
encode的state输出结果:%7B%22id%22%3D%221%22%2C%22name%22%3A%22xiaoming%22%7D
Json化输出state的结果:{"id"="1","name":"xiaoming"}
输出结果
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx5fb25c9dc619f190&redirect_uri=http://test-recycle-h5.jxypapp.com&response_type=code&scope=snsapi_base&state=%7B%22id%22%3D%221%22%2C%22name%22%3A%22xiaoming%22%7D#wechat_redirect
取state传递的值
取state
值,先将params.state
decode化
,再将 decode
后的数据JSON.parse` 转化为对象
let state = JSON.parse(decodeURIComponent(params.state));
输出结果:
{
id:'1',
name:'xiaoming'
}
网友评论