小程序调用接口

168 阅读1分钟

调用接口 登录功能

<view>邮箱:<input class="input1" bindinput="bindinput1" /></view>

<view>密码:<input class="input1" bindinput="bindinput2" /></view>

<button class="sto" size="mini" type="primary" bindtap="login" data-val="{{val}}" data-password="{{password}}">登录</button>

页面的初始数据 data: { num: null, val: '', password: '', img: [] },

账号和密码输入框事件 bindinput1: function (e) { let { detail: { value } } = e this.data.val = value this.setData({ val: this.data.val }) },

bindinput2: function (e) { let { detail: { value } } = e this.data.password = value this.setData({ password: this.data.password }) },

// 调用接口 login: function (e) {

wx.request({
  url: 'https://api.shop.eduwork.cn/api/auth/login',
  method: 'POST',
  data: {
    email: this.data.val,
    password: this.data.password
  },
  success(res) {
    console.log(res);
    if (res.statusCode == 200) {
      wx.showToast({
        title: '登录成功',
      })
    }
判断
    if (res.statusCode == 401) {
      wx.showToast({
        title: '邮箱或密码错误',
        icon: 'error'
      })
    }
    if (res.statusCode == 422) {
      wx.showToast({
        title: res.data.errors.password[0],
        icon: 'error'
      })
    }
    if (res.statusCode == 422) {
      wx.showToast({
        title: res.data.errors.email[0],
        icon: 'error'
      })
    }

  }
})

},