zoukankan      html  css  js  c++  java
  • 「小程序JAVA实战」小程序 loading 提示框与页面跳转(37)

    转自:https://idig8.com/2018/09/02/xiaochengxujavashizhanxiaochengxu-loading-tishikuangyuyemiantiaozhuan37/

    登录注册都完成了,有可能会遇到一些问题,服务器繁忙的话,后台接口卡主了,也没任何提示,小程序端的用户比较暴力一直惦记怎么办。

    加载提示框,隐藏加载中提示框,页面跳转

    https://developers.weixin.qq.com/miniprogram/dev/api/api-react.html#wxshowtoastobject
    https://developers.weixin.qq.com/miniprogram/dev/api/api-react.html#wxhideloading

    跳转实例

    • 用户登录
     
    • 用户注册
    const app = getApp()
    
    Page({
      data: {
    
      },
    
      doLogin: function (e) {
        var formObject = e.detail.value;
        var username = formObject.username;
        var password = formObject.password;
    
        // 简单验证
        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;
              } else if (status == 500) {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none',
                  duration: 3000
                })
              }
            }
          })
        }
      },
      goLoginPage: function (e) {
        console.log("跳转到注册");
      }
    })
    

    • 用户注册
    const app = getApp()
    
    Page({
        data: {
    
        },
    
        doRegist: function(e) {
          var formObject = e.detail.value;
          var username = formObject.username;
          var password = formObject.password;
    
          // 简单验证
          if (username.length == 0 || password.length == 0) {
            wx.showToast({
              title: '用户名或密码不能为空',
              icon: 'none',
              duration: 3000
            })
          }else{
            wx.showLoading({
              title: '正在加载中。。。'
            });
            wx.request({
              url: app.serverUrl +"/regist", 
              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;
                }else if(status == 500){
                  wx.showToast({
                    title: res.data.msg,
                    icon: 'none',
                    duration: 3000
                  })
                }
              }
            })
          }
        },
        goLoginPage:function(e){
          console.log("跳转到登录");
        }
    })
    

    • 用户登录跳转
    const app = getApp()
    
    Page({
      data: {
    
      },
    
      doLogin: function (e) {
        var formObject = e.detail.value;
        var username = formObject.username;
        var password = formObject.password;
    
        // 简单验证
        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;
              } else if (status == 500) {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none',
                  duration: 3000
                })
              }
            }
          })
        }
      },
      goRegisterPage: function (e) {
        wx.redirectTo({
          url: '../userRegister/userRegister',
        })
      }
    })
    

    • 用户注册跳转
      userRegister.js
    const app = getApp()
    
    Page({
        data: {
    
        },
    
        doRegist: function(e) {
          var formObject = e.detail.value;
          var username = formObject.username;
          var password = formObject.password;
    
          // 简单验证
          if (username.length == 0 || password.length == 0) {
            wx.showToast({
              title: '用户名或密码不能为空',
              icon: 'none',
              duration: 3000
            })
          }else{
            wx.showLoading({
              title: '正在加载中。。。'
            });
            wx.request({
              url: app.serverUrl +"/regist", 
              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;
                }else if(status == 500){
                  wx.showToast({
                    title: res.data.msg,
                    icon: 'none',
                    duration: 3000
                  })
                }
              }
            })
          }
        },
        goLoginPage:function(e){
          wx.redirectTo({
            url: '../userLogin/userLogin',
          })
        }
    })
    

    PS:这次就是我们的跳转和loading的介绍。

  • 相关阅读:
    poj3673
    poj3438
    poj3461
    poj3518
    poj3672
    变秃了,也变强了!爆肝吐血整理出的超硬核JVM笔记分享!
    左手字节,右手阿里,我是如何通阿里架构师的java面试文档,拿到多家大厂offer的
    Java异常处理与常用类
    copy_{to, from}_user()的思考
    vi文本编辑器常用指令功能
  • 原文地址:https://www.cnblogs.com/sharpest/p/10296894.html
Copyright © 2011-2022 走看看