小程序前台
<!-- 用户登录 -->
<view class="container">
<view class="page-body">
<form catchsubmit="formSubmit" catchreset="formReset">
<view class="page-section">
<view class="page-section-title">用户名</view>
<view class="weui-cells weui-cells_after-title">
<view class="weui-cell weui-cell_input">
<view class="weui-cell__bd" style="margin: 30rpx 0" >
<input class="weui-input" name="name" placeholder="这是一个输入框" />
</view>
</view>
</view>
</view>
<view class="page-section">
<view class="page-section-title">密码</view>
<view class="weui-cells weui-cells_after-title">
<view class="weui-cell weui-cell_input">
<view class="weui-cell__bd" style="margin: 30rpx 0" >
<input class="weui-input" password name="pwd" placeholder="这是一个密码框" />
</view>
</view>
</view>
</view>
<view class="btn-area">
<button style="margin: 30rpx 0" type="primary" formType="submit">Submit</button>
</view>
</form>
</view>
</view>
小程序css
.userinfo {
display: flex;
flex-direction: column;
align-items: center;
color: #aaa;
}
.userinfo-avatar {
overflow: hidden;
width: 128rpx;
height: 128rpx;
margin: 20rpx;
border-radius: 50%;
}
.usermotto {
margin-top: 200px;
}
小程序js
// 用户登录
formSubmit(e) {
//console.log(e.detail.value.name);
//console.log(e.detail.value.pwd);
wx.request({
url: 'http://day68.home.com/api/login', //仅为示例,并非真实的接口地址
data: {
name:e.detail.value.name,
pwd:e.detail.value.pwd,
},
method:'POST',
dataType:"json",
header: {
'content-type': 'application/x-www-form-urlencoded' // 默认值
},
success (res) {
var jwt = res.data.jwt;
//console.log(jwt)
//将获取到的token存入缓存
wx.setStorage({
data: jwt,
key: 'jwt',
})
if(res.data.code==500){
wx.showToast({
title:'用户名密码不能空',
icon:'none'
})
return false
}
if(res.data.code==501){
wx.showToast({
title:'密码错误',
icon:'none'
})
return false
}
if(res.data.code==502){
wx.showToast({
title:'用户名错误',
icon:'none'
})
return false
}
if(res.data.code==200){
wx.switchTab({
url: '/pages/my/my',
})
}
}
})
},
后台用户登录
//用户登录
public function login(Request $request)
{
//echo md5(md5(123456));die();
//接收数据
$name=$request->input('name');
$pwd=$request->input('pwd');
//dd($name);
//验证
if (empty($name) || empty($pwd)){
return ['code'=>500,'data'=>'','msg'=>'用户名或密码不能为空'];
}
//登录,判断用户名和密码
$names=Users::where('name',$name)->first();
if ($names){
$pwds=Users::where('pwd',md5(md5($pwd)))->first();
if ($pwds){
//将用与数据存入cache缓存
Cache::put('user',$pwds);
//调用方法,将token带入小程序
$jwt=$this->lssue();
return ['code'=>200,'data'=>$pwds,'jwt'=>$jwt,'msg'=>'登陆成功'];
}else{
return ['code'=>501,'data'=>'','msg'=>'密码错误'];
}
}else{
return ['code'=>502,'data'=>'','msg'=>'用户名错误'];
}
}
composer安装token插件
composer require firebase/php-jwt
//签发Token
public function lssue()
{
//取出用户信息
$user=Cache::get('user');
// print_r($user['id']);die();
$key = '666'; //key
$time = time(); //当前时间
$token = [
'iss' => 'http://www.helloweba.net', //签发者 可选
'aud' => 'http://www.helloweba.net', //接收该JWT的一方,可选
'iat' => $time, //签发时间
'nbf' => $time , //(Not Before):某个时间点后才能访问,比如设置time+30,表示当前时间30秒后才能使用
'exp' => $time+7200, //过期时间,这里设置2个小时
'data' => [ //自定义信息,不要定义敏感信息
'userid' => $user['id'],
'username' => $user['name']
]
];
return JWT::encode($token, $key); //输出Token
}
//根据缓存中的用户id查询出用户信息方便用户个人页面的展示
public function my(Request $request)
{
$user=Cache::get('user');
$data=Users::where('id',$user['id'])->first();
$data['image']='http://day68.home.com/'.$data['image'];
return ['code'=>200,'data'=>$data,'msg'=>'查询成功'];
}