例如当你注销之后也就是没登录观看视频的时候,当你点击上传视频或者查看个人信息的时候,会跳转到登录页面,登录成功后并不会跳转到你最初观看视频的页面,而是会跳转到首页,这时候可以使用重定向功能让从首页从定向到视频页。
重定向就是在没登录点击上传视频跳转到登录页面的时候传递需要重定向的地址,后面的redirectUrl就是需要重定向的地址,注意url参数只能是字符串,不能是json,所以需要把videoInfo转换为字符串,这个videoInfo是之前在首页点击某个视频传递到videoInfo页面的。
var videoInfo = JSON.stringify(me.data.videoInfo);
//重定向的地址
var realUrl = "../videoInfo/videoInfo?videoStr=" + videoInfo;
if (user == null || user == '' || user == undefined) {
wx.navigateTo({
//接着把重定向的地址传递给登录页面
url: '../userLogin/login?redirectUrl=' + realUrl,
})
} else {
videoUtil.uploadVideo();
}
},
接着首页获取发现没有后面的redirectUrl是"../videoInfo/videoInfo"没有了后面的videoInfo信息
onLoad:function(params){
//获取需要重定向的url
var redirectUrl = params.redirectUrl;
debugger;
},
因为跳转到登录页面的地址是
../userLogin/login?redirectUrl=../videoInfo/videoInfo?/videoInfo=" + videoInfo
有两个?和=,它会把后面的?和=给过滤掉,
../userLogin/login?redirectUrl=../videoInfo/videoInfo
所以我们需要把后面的?和=替换,然后在登录页使用正则表达式去转换,然后再进行重定向
var realUrl = "../videoInfo/videoInfo#videoStr@" + videoInfo;
在debugger就看到可以获取了
"../videoInfo/videoInfo#videoStr@{"id":"180912FN6DBPX2NC","userId":"180907FF8BK9AH4H","audioId":"4","videoDesc":"","videoPath":"/180907FF8BK9AH4H/video/03c32932-f55c-48ca-810c-b1bc536c60fa.mp4","videoSeconds":6.08,"videoWidth":480,"videoHeight":848,"coverPath":"/180907FF8BK9AH4H/video/wxf06d2224539a802a.jpg","likeCounts":0,"status":1,"createTime":1536755683000,"faceImage":"/180907FF8BK9AH4H/face/tmp_f55e5cb38f111441fbcfe63f040e9e3d805a0418dbff6599.jpg","nickname":"abc"}"
接着转换
onLoad: function(params) {
var me = this;
//获取需要重定向的url
var redirectUrl = params.redirectUrl;
redirectUrl = redirectUrl.replace(/#/g, "?");
redirectUrl = redirectUrl.replace(/@/g, "=");
me.redirectUrl = redirectUrl;
},
在首页要跳转的重定向
var redirectUrl = me.redirectUrl;
if (redirectUrl != null && redirectUrl != '' && redirectUrl != undefined) {
//需要重定向
wx.redirectTo({
url: redirectUrl,
})
}else{
//登录成功,跳转到首页页面,不需要重定向
wx.redirectTo({
url: '../index/index',
})
}
网友评论