小程序前端验证和发送请求和后端验证

logingo:function(){
    //  es6 结构语法
    const {phone,password} = this.data;
    console.log(this.data)
    //手机号不能为空
    if(!phone){
      wx.showToast({
        title: '手机号不能为空',
        icon:'none'
      })
      return;
    }

    //手机号必须符合格式
    const regexp= /^1[358]\d{9}$/;
  if(!regexp.test(phone)){
    wx.showToast({
      title: '请输入合法的手机号码',
      icon:"none"
    })
    return;
  }

    //手机号不能为空
    if (!password) {
      wx.showToast({
        title: '密码不能为空',
        icon: 'none'
      })
      return;
    }
    const preg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,10}$/;
    if (!preg.test(password)) {
      wx.showToast({
        title: '密码必须由6-10字母数字',
        icon: "none"
      })
      return;
    }

    // 能走到这里,表明校验成功,可以向后端发送请求了!
  // 电话号码登录的request
  // phone  18451578808 
  // psw  action001
  
  wx.request({
    url: `http://localhost:3000/login/cellphone?phone=${phone}&password=${password}`,
    success(res) {
      console.log(res.data)

      let data = res.data;
      let code = data.code;
  if(code==200){
      console.log(data)
      return;
  }

      if(code==400){
        wx.showToast({
          title: '您的手机号码没有注册',
          icon:'none'       
           })
        return;
      }

      if(code==502){
        wx.showToast({
          title: data.message,
          icon: 'none'
        })

        return;
      }


    },
    fail(res){
      wx.showToast({
        title: '请稍后重试'
      })
    }
  })


 
  }
,

前端验证非常的简单,就是js 的验证

如果验证成功 ,就用wx.request 发送请,这里

后端 用到了 我们自己本地的临时server

还有正常的返回

{
  "loginType": 1,
  "code": 200,
  "account": {
    "id": 3917524002,
    "userName": "1_18451578808",
    "type": 1,
    "status": 0,
    "whitelistAuthority": 0,
    "createTime": 1601521256582,
    "salt": "",
    "tokenVersion": 0,
    "ban": 0,
    "baoyueVersion": 0,
    "donateVersion": 0,
    "vipType": 0,
    "viptypeVersion": 0,
    "anonimousUser": false
  },
  "token": "4bcfa49e9035e469e266f5b88d7ff0767b6645211280e9b2c3a79361dc36012633a649814e309366",
  "profile": {
    "backgroundUrl": "https://p4.music.126.net/2zSNIqTcpHL2jIvU6hG0EA==/109951162868128395.jpg",
    "followed": false,
    "detailDescription": "",
    "defaultAvatar": true,
    "accountStatus": 0,
    "gender": 1,
    "avatarImgId": 109951163250233890,
    "birthday": -2209017600000,
    "nickname": "action201001",
    "djStatus": 0,
    "experts": {
      
    },
    "mutual": false,
    "remarkName": null,
    "expertTags": null,
    "authStatus": 0,
    "vipType": 0,
    "city": 320300,
    "backgroundImgId": 109951162868128400,
    "avatarUrl": "https://p3.music.126.net/ma8NC_MpYqC-dK_L81FWXQ==/109951163250233892.jpg",
    "province": 320000,
    "description": "",
    "userId": 3917524002,
    "userType": 0,
    "avatarImgIdStr": "109951163250233892",
    "backgroundImgIdStr": "109951162868128395",
    "signature": "",
    "authority": 0,
    "avatarImgId_str": "109951163250233892",
    "followeds": 0,
    "follows": 3,
    "eventCount": 0,
    "avatarDetail": null,
    "playlistCount": 1,
    "playlistBeSubscribedCount": 0
  },
  "bindings": [
    {
      "expired": false,
      "refreshTime": 1601521286,
      "expiresIn": 2147483647,
      "bindingTime": 1601521286588,
      "tokenJsonStr": "{\"countrycode\":\"\",\"cellphone\":\"18451578808\",\"hasPassword\":true}",
      "url": "",
      "userId": 3917524002,
      "id": 11111130267,
      "type": 1
    },
    {
      "expired": true,
      "refreshTime": 1601521256,
      "expiresIn": 7200,
      "bindingTime": 1601521256713,
      "tokenJsonStr": "{\"access_token\":\"37_rCRjj7kiZw-rtMCdaLT-4wPwNnXykFOJydDhnlLe2ji6DjQ_CebLZDokmX8LPw__QDp6nQz-t5wd8aWD7rkuB_TnoU1jVtnGJqC9Q4Eu5zw\",\"refresh_token\":\"37_SwO-TjVa-E57b02fP_zfFPsN_iGh81Bk5CCR-XYGUOYFU7TFmtLDNmHNY2jaUao0pA8CW1AeAC1dc-lNxOIaKi_t3cnBbm83AvOMRam0GNs\",\"unionid\":\"oZoefuKAfQiGIXmiPD4dVe6A8GjQ\",\"openid\":\"okvmMjtQzpo7IcDz9VpipraH8MXE\",\"scope\":\"snsapi_userinfo\",\"nickname\":\"action\",\"expires_in\":7200}",
      "url": "",
      "userId": 3917524002,
      "id": 11111143455,
      "type": 10
    }
  ]
}

行,正确数据拿回来之后,剩下的逻辑,我们回头继续!

猜你喜欢

转载自blog.csdn.net/qq_15009739/article/details/112648145