微信小程序实现登录页面

wxml文件:

<view class="container"> 

 <view class="login-icon"> 
 <image class="login-img" src="../../img/loginLog.jpg"></image> 
 </view> 
 <view class="login-from"> 

 <!--账号-->
 <view class="inputView"> 
  <image class="nameImage" src="../../img/name.png"></image> 
  <label class="loginLab">账号</label> 
  <input class="inputText" placeholder="请输入账号" bindinput="usernameInput" /> 
 </view> 
 <view class="line"></view> 

 <!--密码-->
 <view class="inputView"> 
  <image class="keyImage" src="../../img/key.png"></image> 
  <label class="loginLab">密码</label> 
  <input class="inputText" password="true" placeholder="请输入密码" bindinput="passwordInput" /> 
 </view> 
 
 <!--按钮-->
 <view class="loginBtnView"> 
  <button class="loginBtn" type="primary" size="{{primarySize}}" loading="{{loading}}" plain="{{plain}}" disabled="{{disabled}}" bindtap="login">登录</button> 
 </view> 
 </view> 
</view>

wxss文件:

page{ 
 height: 100%; 
} 

.container { 
 height: 100%; 
 display: flex; 
 flex-direction: column; 
 padding: 0; 
 box-sizing: border-box; 
 /* background-color: rgb(156, 23, 78) */
} 

/*登录图片*/
.login-icon{ 
 flex: none; 
} 

.login-img{ 
 width: 750rpx;
} 

/*表单内容*/
.login-from { 
 margin-top: 20px; 
 flex: auto; 
 height:100%; 
} 

.inputView { 
 /* background-color: #fff;  */
 line-height: 45px; 
 border-radius:20px;
  border:1px solid #999999;
} 

/*输入框*/
.nameImage, .keyImage { 
 margin-left: 22px; 
 width: 18px; 
 height: 16px
} 

.loginLab { 
 margin: 15px 15px 15px 10px; 
 color: #545454; 
 font-size: 14px
} 

.inputText { 
 flex: block; 
 float: right; 
 text-align: right; 
 margin-right: 22px; 
 margin-top: 11px;
 color: #cccccc; 
 font-size: 14px
} 
.line { 
 margin-top: 8px; 
} 

/* .line { 
 width: 100%; 
 height: 1px; 
 background-color: #cccccc; 
 margin-top: 1px; 
}  */

/*按钮*/
.loginBtnView { 
 width: 100%; 
 height: auto; 
 /* background-color:#DCDCDC;  */
 margin-top: 0px; 
 margin-bottom: 0px; 
 padding-bottom: 0px; 
} 

.loginBtn { 
 width: 90%; 
 margin-top: 40px; 
 border-radius:10px;
}

js文件:

//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    username: '',
    password: ''
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onShow: function () {
    // 生命周期函数--监听页面显示
    wx.hideTabBar({})
  },
  onLoad: function () {
   
  },


  // 获取输入账号 
  usernameInput: function (e) {
    this.setData({
      username: e.detail.value
    })
  },

  // 获取输入密码 
  passwordInput: function (e) {
    this.setData({
      password: e.detail.value
    })
  },

  // 登录处理
  login: function () {
    var that = this;
    if (this.data.username.length == 0 || this.data.password.length == 0) {
      wx.showToast({
        title: '账号或密码不能为空',
        icon: 'none',
        duration: 2000
      })
    } else {
      wx.request({
        url: app.globalData.globalReqUrl +'/login/login', // 仅为示例,并非真实的接口地址
        method: 'post',
        data: {
          username: that.data.username,
          password: that.data.password
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success(res) {
          if (res.data.code == "OK") {
            var unitName = res.data.data.User.unitName;
            var unitId = res.data.data.User.unitId;
            wx.setStorageSync('unitId', unitId);
            wx.setStorageSync('unitName', unitName);
            wx.switchTab({
              url: '../overviewData/realTimeData'
            })
          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }
  }
})

 

效果图:

   这里界面里用到的两个图标

 

 

 

 

 

                            ---------------长按二维码关注程序媛小姐姐公众号有更多彩蛋哦---------------

                                            

  • 62
    点赞
  • 553
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
微信小程序实现登录界面需要以下几个步骤: 1. 在小程序页面中创建一个表单,包含用户名和密码的输入框以及登录按钮。 2. 绑定表单提交事件,当用户点击登录按钮时触发。 3. 在事件处理函数中获取用户输入的用户名和密码,并进行校验。 4. 校验通过后向服务器发送登录请求,获取登录结果。 5. 根据登录结果进行相应的处理,如跳转到主界面或者提示登录失败。 下面是一个简单的实现登录界面的代码示例: ``` // login.wxml <view class="container"> <form bindsubmit="login"> <input name="username" placeholder="请输入用户名" /> <input name="password" type="password" placeholder="请输入密码" /> <button formType="submit">登录</button> </form> </view> ``` ``` // login.js Page({ login: function(e) { var username = e.detail.value.username; var password = e.detail.value.password; // 校验用户名和密码 if (username === '' || password === '') { wx.showToast({ title: '请输入用户名和密码', icon: 'none' }); return; } // 发送登录请求 wx.request({ url: 'http://example.com/login', method: 'POST', data: { username: username, password: password }, success: function(res) { if (res.data.code === 0) { // 登录成功,跳转到主界面 wx.navigateTo({ url: '/pages/home/home', }); } else { // 登录失败,提示错误信息 wx.showToast({ title: res.data.message, icon: 'none' }); } }, fail: function() { // 请求失败,提示网络错误 wx.showToast({ title: '网络错误', icon: 'none' }); } }); } }); ``` 在上面的代码中,我们定义了一个表单,绑定了提交事件 login。在事件处理函数中,获取用户输入的用户名和密码,并进行校验。如果校验通过,就向服务器发送登录请求,获取登录结果。根据登录结果进行相应的处理,如跳转到主界面或者提示登录失败。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值