小程序基础页面实现登录(含后台接口安全验证)


前言

提示:这里可以添加本文要记录的大概内容:
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。


提示:以下是本篇文章正文内容,下面案例可供参考

一、wxml

<view class="login-box">
  <image src='../img/3.jpg'></image>
</view>
<view class="container">
  <form bindsubmit="formSubmit" catchreset="formReset">
    <view>
      <view class="title">账号</view>
      <input type="text" name="username" placeholder="请输入账号" placeholder-style="color: blue;" />
    </view>
    <view>
      <view class="title">密码</view>
      <input type="password" name="password" placeholder="请输入密码" placeholder-style="color: blue;" />
    </view>
    <view class="btn-area">
      <button style="margin-top: 35rpx" type="primary" form-type="submit">登陆</button>
      <button style="margin-top: 30rpx" formType="reset">重置</button>
    </view>
  </form>
</view>

二、wxss

.title{
  font-size: 20px;
  width: 500rpx;
  text-align: center;
  margin-top: 50rpx;
  color: orange;
}
input{
  margin-top: 20rpx;
  width: 500rpx;
  height: 50rpx;
  text-align: center;
  border: 2px solid #ffffff;
  border-radius: 20rpx;
  color: blue;
  background: #ffffff;
  
}
image{
  /* width: 400rpx;
  height: 400rpx; */
  position: absolute;
  left: 0;
  bottom: 0;
  display: block;
  width: 100%;
  height: 100%;
  z-index: -999;
}
.login-box {
  height: 100%;
}

.send{
  margin-top: 20rpx;
  margin-left: 20rpx;
}

三、js

 formSubmit(e) {
    console.log('form发生了submit事件,携带数据为:', e.detail.value);
    
    wx.request({
      url: 'http://www.very.com/demo/api/wxlogin',
      data:{
        username:e.detail.value.username,
        password:e.detail.value.password,
      },
      success(res){
        console.log(res.data.code);
        if(res.data.code == 200){
          wx.showToast({
            title: '登录成功',
          })
          //跳转至tabar页面
         wx.switchTab({
           url: '/pages/user/user',
         })
        }
      }

    })
  },

三、thinkphp后台(生成token)

public function wxLogin(Request $request)
    {
        //接收参数
        $data = $request->param();

        //参数验证
        $validate = \think\facade\Validate::rule([
            'username|用户名'  => 'require|max:25',
            'password|密码'=>'require',
        ]);

        //验证参数
        if (!$validate->check($data)) {
            return json(['code'=>500,'msg'=>$validate->getError()]);
        }

        $bool = User::where('username', $data['username'])->find();

        if ($bool) {

            if (md5($data['password']) == $bool['password']){
                $token = Token::make('111',['uid' => $bool['id'],'username' => $bool['username']]);
                return json(['code' => Config::get('status.api.success'),'msg' => '登录成功','token' => $token]);
            }

            return json(['code' => Config::get('status.api.error'),'msg' => '密码错误']);
        }

        return json(['code' => Config::get('status.api.error'),'msg' => '账户不存在']);
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值