轻源码

  • QingYuanMa.com
  • 全球最大的互联网技术和资源下载平台
搜索
一起源码网 门户 终极进阶 查看主题

小程序学习笔记《四》用户登录

发布者: luofen | 发布时间: 2018-6-10 01:29| 查看数: 5441| 评论数: 1|帖子模式

利用quickstart工程,实现用户登录过程,并简单校验用户名和密码是否正确。

1.底部有两个tabBar(首页/个人中心) 
2.个人中心显示用户信息。 
首次切换到个人中心,需要先登录,才能看到个人信息。登录后,就一直显示用户信息。

改app.json文件,添加tabBar

  1. app.json
  2. {
  3. "pages":[
  4. "pages/index/index",
  5. "pages/user/user",
  6. "pages/login/login"
  7. ],
  8. "window":{
  9. "backgroundTextStyle":"light",
  10. "navigationBarBackgroundColor": "#fff",
  11. "navigationBarTitleText": "WeChat",
  12. "navigationBarTextStyle":"black"
  13. },
  14. "tabBar": {
  15. "list": [{
  16. "pagePath": "pages/index/index",
  17. "text": "首页"
  18. },{
  19. "pagePath": "pages/user/user",
  20. "text": "个人中心"
  21. }]
  22. }
  23. }

添加用户登录页面login 
两个输入框(用户名和密码) 
一个按钮 
当点击按钮时,验证用户名和密码是否正确,如果正确,则跳转到用户页面(用户的个人信息)。 
否则在登录页面有个错误提示"请输入正确的用户名和密码"。

login.wxml

  1. <view class="mcontainer">
  2. <view class="item">
  3. <view class="login-item">
  4. <view class="login-item-info">用户名</view>
  5. <view><input /></view>
  6. </view>
  7. <view class="login-item">
  8. <view class="login-item-info">密码</view>
  9. <view class="login-pwd">
  10. <input style="flex-grow:1" password="true" />
  11. <text> 忘记密码 </text>
  12. </view>
  13. </view>
  14. <view class="login-item bottom">
  15. <button class="login-btn" >登录</button>
  16. </view>
  17. <view class="login-item bottom">
  18. <text> 用户名或密码错误 </text>
  19. </view>
  20. </view>
  21. </view>

login.wxss

  1. .mcontainer {
  2. height: 100%;
  3. display: flex;
  4. flex-direction: column;
  5. align-items: center;
  6. justify-content: space-between;
  7. }
  8. .item{
  9. flex-grow:1;
  10. height: 30%;
  11. overflow: hidden;
  12. display: flex;
  13. justify-content: center;
  14. align-items: center;
  15. flex-direction: column;
  16. width: 96%;
  17. padding: 0 2%;
  18. }
  19. .login-item{
  20. width: 90%;
  21. margin-top: 30rpx;
  22. border-bottom: 1px solid #eee;
  23. flex-grow:1;
  24. display: flex;
  25. flex-direction: column;
  26. justify-content: flex-end;
  27. padding-bottom: 20rpx;
  28. }
  29. .bottom{
  30. border-bottom: 0px;
  31. }
  32. .login-item-info{
  33. font-size: 8px;
  34. color: #888;
  35. padding-bottom: 20rpx;
  36. }
  37. .login-pwd{
  38. display: flex;
  39. justify-content: space-between;
  40. flex-grow: 1;
  41. align-items: center;
  42. }
  43. .login-pwd text{
  44. height: 100%;
  45. font-size: 14px;
  46. color: #888;
  47. display: flex;
  48. }
  49. .login-btn{
  50. width: 80%;
  51. color: white;
  52. background-color: green;
  53. border-radius: 0;
  54. font-size: 14px;
  55. }
  56. .login-btn:hover{
  57. width: 80%;
  58. color: white;
  59. border-radius: 0;
  60. }

绑定bindinput事件

  1. usernameInput/passwordInput
  2. <view><input bindinput="usernameInput" /></view>
  3. <input style="flex-grow:1" password="true" bindinput="passwordInput"/>

login.js

  1. 获得input输入框里的数据并保存到变量usernamepassword
  2. page
  3. var app = getApp();
  4. usernameInput : function (event){
  5. console.log(event) //打印事件看看
  6. console.log(event.detail.value)
  7. this.setData({username:event.detail.value})
  8. },
  9. passwordInput : function (event){
  10. this.setData({password:event.detail.value})
  11. }

index.js 
page 
app.js 
APP 
建一个用户信息页面 
user.wxml

  1. <view class="index-container">
  2. <view class="nickname">
  3. 用户名和密码
  4. </view>
  5. </view>

user.wxss

  1. .index-container{
  2. width: 100%;
  3. height: 100%;
  4. position: relative;
  5. }
  6. .nickname{
  7. position: absolute;
  8. top: 170px;
  9. left: 120px;
  10. font-size: 25px;
  11. }

app.js

  1. 定义全局用户信息变量 userInfo
  2. appData:{
  3. userInfo: null
  4. }

user.js

  1. app全局变量userInfo == null,跳到用户登录页面。
  2. page
  3. var app = getApp();
  4. data:{
  5. username:null,
  6. },
  7. onLoad:function(options){
  8. // 生命周期函数--监听页面加载
  9. //如果信息为空,跳转到登录页面,页面跳转navigateTo/redirectTo
  10. //wx.navigateTo() 有返回按钮,不适合此页
  11. if(app.appData.userInfo == null){
  12. wx.redirectTo({
  13. url: '../login/login',
  14. success: function(res){
  15. // success
  16. },
  17. fail: function() {
  18. // fail
  19. },
  20. complete: function() {
  21. // complete
  22. }
  23. })
  24. }
  25. else{
  26. this.setData({username:app.appData.userInfo.username})
  27. }
  28. }

login.wxml

  1. 绑定点击事件,点击登录按钮时,将输入框里的信息(用户名和密码)保存到userInfo
  2. bindtap="loginBtnClick"
  3. <button class="login-btn" bindtap="loginBtnClick">登录</button>

login.js

  1. 校验用户名和密码是否是admin用户,若是,跳转到用户信息页面(user),并将获得的username/password
  2. 信息存到全局变量userInfo中。
  3. 使用wx.switchTab,跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面
  4. 若不是admin用户,则弹出提示"用户名或密码不正确,请重新输入"
  5. 使用wx.showModal,显示模态弹窗。
  6. var app = getApp()
  7. data:{
  8. username:null,
  9. password:null
  10. },
  11. loginBtnClick:function(){
  12. //用户名和密码验证的过程
  13. if(this.data.username == "admin" && this.data.password == "admin"){
  14. app.appData.userInfo = {username:this.data.username,password:this.data.password}
  15. wx.switchTab({
  16. url: '../user/user',
  17. success: function(res){
  18. // success
  19. },
  20. fail: function() {
  21. // fail
  22. },
  23. complete: function() {
  24. // complete
  25. }
  26. })
  27. }else{
  28. wx.showModal({
  29. title: '提示',
  30. content: '用户名或密码不正确,请重新输入',
  31. showCancel: false,
  32. success: function(res) {
  33. if (res.confirm) {
  34. console.log('用户点击确定')
  35. }
  36. }
  37. })
  38. }
  39. }

demo下载:用户登录.7z

最新评论

凡宇轩 发表于 2022-6-24 13:10
编程代码大全免费

轻源码让程序更轻更快

QingYuanMa.com

工作时间 周一至周六 8:00-17:30

侵权处理

客服QQ点击咨询

关注抖音号

定期抽VIP

Copyright © 2016-2021 https://www.171739.xyz/ 滇ICP备13200218号

快速回复 返回顶部 返回列表