登录小程序介绍
当获取到用户信息时,自动登陆。如果没有获取到用户信息,则跳转到登录界面。
登录小程序实例代码
1.创建一个新工程
2.删除logs文件夹
3.建立login和user界面
4.login.js:
loginBtnClick: function () {
// 用户名和密码验证的过程
app.appData.userinfo = { username:this. data.username, password:this.data.password }
wx.switchTab({ url: "../user/user" })
},
usernameInput: function (event) {
console.log(event)
this.setData({ username:event.detail.value })
},
passwordInput: function (event) {
this.setData({ password: event.detail.value })
}
login.wxml
<view class="mcontainer">
<view class="item">
<image src="../../images/20.png" class="image"/>
</view>
<view class="item">
<view class="login-item">
<view class="login-item-info">用户名</view>
<view><input bindinput="usernameInput" /></view>
</view>
<view class="login-item">
<view class="login-item-info">密码</view>
<view class="login-pwd">
<input style="flex-grow:1" password="true" bindinput="passwordInput"/>
<text> 忘记密码 </text>
</view>
</view>
<view class="login-item bottom">
<button class="login-btn" bindtap="loginBtnClick">登录</button>
</view>
</view>
<view class="item bottom-info">
<view> 没有账户? <text> 注册 </text> </view>
<view class="wechat-icon">
<image src="../../images/21.png" />
</view>
</view>
</view>
login.wxss
.mcontainer {
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-between;
}
.item{
flex-grow:1;
height: 30%;
overflow: hidden;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
width: 96%;
padding: 0 2%;
}
.image{
width: 500rpx;
height: 150rpx;
}
.login-item{
width: 90%;
margin-top: 30rpx;
border-bottom: 1px solid #eee;
flex-grow:1;
display: flex;
flex-direction: column;
justify-content: flex-end;
padding-bottom: 20rpx;
}
.bottom{
border-bottom: 0px;
}
.login-item-info{
font-size: 8px;
color: #888;
padding-bottom: 20rpx;
}
.login-pwd{
display: flex;
justify-content: space-between;
flex-grow: 1;
align-items: center;
}
.login-pwd text{
height: 100%;
font-size: 14px;
color: #888;
display: flex;
}
.login-btn{
width: 80%;
color: white;
background-color: green;
border-radius: 0;
font-size: 14px;
}
.login-btn:hover{
width: 80%;
color: white;
border-radius: 0;
}
.bottom-info{
}
.wechat-icon{
margin-top: 30px;
width: 30px;
height: 30px;
border: 1px solid seagreen;
border-radius: 50%;
padding: 10px;
}
.wechat-icon image{
width: 33px;
height: 33px;
}
5.user.js
data: {
username: null
},
onLoad: function (options) {
if (app.appData.userinfo ==null) {
wx.redirectTo({ url: "../login/login" })
} else {
this.setData({ username: app.appData.userinfo.username })
}
},
user.wxml
<view class="index-container">
<image src="../../images/uc.jpg" ></image>
<view class="nickname">
{{username}}
</view>
</view>
user.wxss
.index-container{
width: 100%;
height: 100%;
position: relative;
}
.index-container image{
width: 100%;
height: 550px;
}
.nickname{
position: absolute;
top: 170px;
left: 120px;
font-size: 25px;
}
6.示例
来源:CSDN
作者:qq_44760153
链接:https://blog.csdn.net/qq_44760153/article/details/104598646