微信小程序登录功能

js

var app = getApp()
Page( {
  data: {
   
  },
  //输入框变化
  changeInputUser: function(e) {
    var value = e.detail.value;
    console.log(value);
    this.setData({
      userName : value,
    })
  },
  //输入框变化 
  changeInputPw: function(e) {
    var value = e.detail.value;
    console.log(value);
    this.setData({
      password : value,
    })
  },

  loginAction: function (event) {
    console.log("dsadsad");
    var pw = this.data.password;
    var user = this.data.userName;
      console.log(user);
      console.log(pw);
     wx.showLoading({
        title: '加载中',
        mask: true
      });
      wx.request({
            url: 'http://localhost:8080/userapp/user/loginCommon',
            method: 'POST',
            data: {
              phone:user,
              password:pw
            },
            header: {
                'content-type': 'application/x-www-form-urlencoded'
                // 'Accept': 'application/x-www-form-urlencoded'
            },
            complete: function(res) {
                wx.hideLoading();
            },
            success: function(res) {
              wx.setStorage({
                key:"userInfo",
                data:res.data
              })
              console.log(res);
              wx.hideLoading();
              wx.showToast({
                title: '请求成功',
                icon: 'success',
                mask: true,
              });
              wx.navigateBack({
                delta: 1
              })
      
            }
        })
  },
})

xml

<!--index.wxml-->
<view class="container">
  
  <view class="loginView">

    <view class="cell">
        <view class="cell_line">
          <image src="../../../images/icon_login_user.png"></image>
          <input placeholder="账号" bindinput="changeInputUser"/>
        </view>
      </view> 

      <view class="cell">
        <view class="cell_line">
          <image src="../../../images/icon_login_mm.png"></image>
          <input placeholder="密码" bindinput="changeInputPw"/>
        </view>  
      </view>    

  </view>

  <button class="normalButton" bindtap="loginAction">
    登录
  </button>
 
  <view class="register">
    <navigator url="../forgetpw/index" class="normalColor">
      <view class="left">忘记密码</view> 
    </navigator>
    <navigator url="../register/index" class="normalColor">
        <view class="right">立即注册</view> 
    </navigator>
  </view>

</view>

推荐阅读更多精彩内容