小程序实战: wx.request 实现前端登录

此博客展示了如何使用WXML和WXSS构建一个前端登录和注册页面,包括账号和密码输入框,登录及注册按钮的事件绑定。在登录功能中,通过wx.request发送POST请求到后台接口进行验证。注册功能同样调用接口,将新用户信息插入数据库。
摘要由CSDN通过智能技术生成
<!--pages/login/login.wxml-->
<view>
  <view class="login-title">
  <image src="{{loginPhoto}}"></image>
  </view>
  <view class="body">
    <view class="weui-form__control-area">
      <view class="weui-cells__group weui-cells__group_form">
        <view class="weui-cells weui-cells_form">
          <view class="weui-cell">
            <view class="weui-cell__hd">
              <label class="weui-label">账号</label>
            </view>
            <view class="weui-cell__bd">
              <input class="weui-input" type="number" pattern="[0-9]*" placeholder="请输入账号" bindinput="userAccount"/>
            </view>
          </view>
          <view class="weui-cell">
            <view class="weui-cell__hd">
              <label class="weui-label">密码</label>
            </view>
            <view class="weui-cell__bd">
              <input class="weui-input" password="true" placeholder="请输入密码" bindinput="userPassword" />
            </view>
          </view>
        </view>
      </view>
    </view>

    <view class="login-button" >
      
      <button type="primary" bindtap="loginTap"> 登陆 </button>
      <text decode="{{true}}" space="{{true}}">&nbsp;&nbsp; &nbsp;</text>
      <button  type="default" bindtap="rigestTap"> 注册 </button>
  
    </view>
    
  </view>

</view>
/* pages/login/login.wxss */
.login-title{
  text-align: center;
  height: 150rpx;
  border-left: none ;
  border-right: none ;
  display: flex;
  align-items: center;
}
.login-title image{
  margin: 40%;
  margin-top: 70%;
  height: 120%;
  border-radius: 50%;
  overflow: hidden;
}
.body{
  margin-top: 30%;
}

.login-button{

  margin: 0 auto;
  width: 76%;
  display: flex;
  justify-content: space-between;

}
.login-button button{
  display: block;
  margin-top: 1vh;
  font-size: 3.5vh;
  width: 40%;

}




var app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    username:null,
    password:null,
    loginPhoto:'/images/background/6.jpg'
  },
/**
 * 从文本框中得到输入的值
 */
  userAccount:function(event){
    var userAccount = event.detail.value;
    this.setData({
      username:userAccount,
    })
    console.log(this.data.username)
  },

  userPassword:function(event){
    var userPassword = event.detail.value;
    this.setData({
      password:userPassword,
    })
    console.log(this.data.password)
  },

  loginTap:function(){
    wx.request({
      url: 'http://localhost:8088/admin/login', //仅为示例,并非真实的接口地址
      data: {
        username:this.data.username,
        password:this.data.password,
        userPhoto:"/images/background/2.jpg",
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'POST',
      success(res) {
        console.log(res.data)
        if(res.data.code==200){
        wx.switchTab({
          url: '/pages/menu/menu'
        })
      }
      },
      fail(res){
        console.log('----fail----')
      }
    })

  },
  rigestTap:function(){
    console.log(this.data.username +'--->'+this.data.password)
    var e = wx.getSystemInfoSync()
    console.log(e.safeArea)
    wx.request({
      url: 'http://localhost:8088/admin/insert', //仅为示例,并非真实的接口地址
      data: {
        username:this.data.username,
        password:this.data.password,
      },
      header: {
        'content-type': 'application/json' // 默认值
      
      },
      method: 'POST',
      success(res) {
        console.log(res.data)
        
        if(res.data.code==200){
          console.log('----注册successed----')
          
        }
      
      },
      fail(res){
        console.log(res.data)
        console.log('----注册fail----')
      }
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
    
})

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值