php代码
后台判断用户信息,入库操作
public static function content(){
$m_id=input('m_id');
$content=input('content');
$uid=(new Token)->getUserStatus('id');
$content=self::sensitive($content);
if (empty($uid)){
return json_encode(['code'=>400,'msg'=>"用户状态出错",'data'=>$uid]);
}else{
$data=[];
$data['m_id']=$m_id;
$data['uid']=$uid;
$data['content']=$content;
$user = new MovieModel();
$user->data($data);
$res=$user->save();
if ($res){
return json_encode(['code'=>200,'msg'=>"评论成功",'data'=>null]);
}else{
return json_encode(['code'=>500,'msg'=>"评论失败",'data'=>null]);
}
}
}
js代码
getUserProfile:function() {
wx.getUserProfile({
desc: '用户信息',
success:res=>{
wx.login({
success:e=>{
if(e.code){
wx.request({
url: '',
data:{
code:e.code,
nickname:res.userInfo.nickName,
avatar:res.userInfo.avatarUrl
},
success:result=>{
wx.setStorageSync('userInfo',result.data.data,'3600*24*6');
wx.setStorageSync('token',result.data.token,'3600*24*6');
wx.navigateBack({
delta: 1
})
}
})
}else{
console.log('登陆失败'+res.errMsg)
}
}
})
}
})
},
验证用户(php)
public static function getUserStatus($key){
$token=\think\Request::instance()->header('token');
$user=\think\Cache::get($token);
if (is_object($user)){
$user=$user->toArray();
}
$data=$user[$key];
return $data;
}
简单说实现微信小程序的登录其实很简单,大家要合理使用噢!
|