在咱們正常的瀏覽網站的時候,未登陸點擊vip專區的時候,須要登陸,登陸後還會回到最初要進入的網站,這就是頁面重定向,在小程序裏面也須要完成這樣的功能。源碼:https://github.com/limingios/wxProgram.git 中No.15ios
對於搜索,能夠相似淘寶的功能,無需登陸就能夠進行搜索,可是文件上傳這個功能就須要進行登陸後才能夠進行上傳,登陸後在跳轉到原來的頁面進行操做。git
增長data中的默認頁面對象,本頁面的回調地址github
var videoUtils = require('../../utils/videoUtils.js')const app = getApp() Page({ data: { cover:'cover', videoContext:"", videoInfo:{}, videId:'', src:'' }, showSearch:function(){ wx.navigateTo({ url: '../videoSearch/videoSearch', }) }, onLoad:function(params){ var me = this; me.videoContext = wx.createVideoContext('myVideo', me); var videoInfo = JSON.parse(params.videoInfo); var videoWidth = videoInfo.videoWidth; var videoHeight = videoInfo.videoHeight; var cover = 'cover'; if (videoWidth > videoHeight){ cover = ''; } me.setData({ videId: videoInfo.id, src: app.serverUrl + videoInfo.videoPath, videoInfo: videoInfo, cover: cover }) }, showIndex:function(){ wx.redirectTo({ url: '../index/index', }) }, onShow:function(){ var me = this; me.videoContext.play(); }, onHide:function(){ var me = this; me.videoContext.pause(); }, upload:function(){ var me = this; var userInfo = app.getGlobalUserInfo(); var videoInfo = JSON.stringify(me.data.videoInfo); var realUrl = '../videoInfo/videoInfo#videoInfo@' + videoInfo; if (userInfo.id == '' || userInfo.id == undefined) { wx.navigateTo({ url: '../userLogin/userLogin?realUrl=' + realUrl, }) } else { videoUtils.uploadVideo(); } }, showMine: function () { var me = this; var userInfo = app.getGlobalUserInfo(); var videoInfo = JSON.parse if (userInfo.id == '' || userInfo.id == undefined){ wx.navigateTo({ url: '../userLogin/userLogin', }) }else{ wx.navigateTo({ url: '../mine/mine', }) } }, })
const app = getApp() Page({ data: { realUrl:'' }, onLoad:function(params){ var realUrl = params.realUrl; var me = this; realUrl = realUrl.replace(/#/g,"?"); realUrl = realUrl.replace(/@/g, "="); me.setData({ realUrl: realUrl }) }, doLogin: function (e) { var formObject = e.detail.value; var username = formObject.username; var password = formObject.password; var me = this; // 簡單驗證 if (username.length == 0 || password.length == 0) { wx.showToast({ title: '用戶名或密碼不能爲空', icon: 'none', duration: 3000 }) } else { wx.showLoading({ title: '正在登陸中。。。' }); wx.request({ url: app.serverUrl + "/login", method: "POST", data: { username: username, password: password }, header: { 'content-type': 'application/json' // 默認值 }, success: function (res) { console.log(res.data); var status = res.data.status; wx.hideLoading(); if (status == 200) { wx.showToast({ title: "用戶登錄成功~!", icon: 'none', duration: 3000 }) // app.userInfo = res.data.data; app.setGlobalUserInfo(res.data.data); var realUrl = me.data.realUrl; if (realUrl != null && realUrl != '' && realUrl != undefined){ wx.redirectTo({ url: realUrl, }) }else{ wx.redirectTo({ url: '../mine/mine', }) } } else if (status == 500) { wx.showToast({ title: res.data.msg, icon: 'none', duration: 3000 }) } } }) } }, goRegisterPage: function (e) { wx.redirectTo({ url: '../userRegister/userRegister', }) } })
PS:頁面重定向只是一種手段,有不少是經過後臺的方式來進行控制的,下次給老鐵說下springboot的攔截器。spring