转自:https://idig8.com/2018/09/24/xiaochengxujavashizhanxiaochengxudeyemianzhongdingxiang59/

在我们正常的浏览网站的时候,未登录点击vip专区的时候,需要登录,登录后还会回到最初要进入的网站,这就是页面重定向,在小程序里面也需要完成这样的功能。源码:https://github.com/limingios/wxProgram.git 中No.15

小程序代码

对于搜索,可以类似淘宝的功能,无需登录就可以进行搜索,但是文件上传这个功能就需要进行登录后才可以进行上传,登录后在跳转到原来的页面进行操作。

  • 增加data中的默认页面对象,本页面的回调地址

 

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的拦截器。

posted on 2019-01-24 18:46  Sharpest  阅读(819)  评论(0编辑  收藏  举报