uniapp微信小程序授权登录
文章描述:
PHP+uniapp微信小程序授权一键登录
准备
小程序AppId、Secret
uniapp
template
<template>
<view class="userinfo" v-if="userInfo !=''">
<view class="userimg">
<image :src="userInfo.avatar" mode=""></image>
</view>
<view class="nickName">
{{userInfo.nickname}}
</view>
</view>
<view class="userinfo" v-else>
<view class="userimg">
</view>
<button type="primary" @click="onGotUserInfo">微信用户快速登录</button>
</view>
</template>
script
export default {
data() {
return {
avatar:'',
userInfo:'',
}
},
onLoad() {
},
methods:{
/**
* 用户同意授权个人微信信息
* @param {Object} e 用户的信息
*/
async onGotUserInfo() {
uni.getUserProfile({
desc:'正在获取',//不写不弹提示框
success: res=> {
// console.log(res.userInfo)
uni.login({
success: async res1=> {
if (res1.code) {
const query = {
code: res1.code,
nickName: res.userInfo.nickName,
avatar: res.userInfo.avatarUrl
}
const res2 = await uni.$http.post('/index.php/shop/user/wxlogin', query)
this.userInfo = res2.data.data
console.log(res2.data.data)
} else {
uni.showModal({
title: '提示',
content: '网络繁忙,请稍后再试'
})
}
}
})
},
fail: err=> {
uni.showToast({
title: '请点击授权进行登录',
icon: 'none'
});
}
})
},
}
}
style
page{
background: #fff;
}
.userinfo{
box-sizing: border-box;
padding: 20upx 30upx;
}
.userimg{
display: flex;
align-items: center;
margin: 0 auto;
flex-direction: column;
justify-content: center;
margin-top: 30upx;
margin-bottom: 30upx;
}
.userimg image{
width: 200upx;
height: 200upx;
}
.nickName{
display: flex;
justify-content: center;
}
PHP
public function wxlogin(){
$appId = '';
$secret = '';
$authorization_code= 'authorization_code';
if(empty(input('code'))){
return json(['code'=>500,'msg'=>'error','data'=>'is null']);
}else{
$js_code = input('code');
$curl = curl_init();
//使用curl_setopt() 设置要获得url地址
$url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$appId.'&secret='.$secret.'&js_code='.$js_code.'&grant_type=authorization_code';
curl_setopt($curl, CURLOPT_URL, $url);
//设置是否输出header
curl_setopt($curl, CURLOPT_HEADER, false);
//设置是否输出结果
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//设置是否检查服务器端的证书
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
//使用curl_exec()将curl返回的结果转换成正常数据并保存到一个变量中
$data = curl_exec($curl);
$data =json_decode($data,true);
// print_r($data);
//关闭会话
curl_close($curl);
if(isset($data['openid'])){
$str = md5(uniqid(md5(microtime(true)),true));
$token = sha1($str.$data['openid']);
$addArray = [
'avatar' =>input('avatar'),
'nickname' =>input('nickName'),
'openid' =>$data['openid'],
'created_at' =>time(),
'updated_at' =>time(),
'ip' =>$_SERVER['REMOTE_ADDR'],
'token'=>$token
];
$updateArray = [
'avatar' =>input('avatar'),
'nickname' =>input('nickName'),
'openid' =>$data['openid'],
'updated_at' =>time(),
'ip' =>$_SERVER['REMOTE_ADDR'],
];
$find = Db::name('weixin_dy_user')->where(array('openid'=>$data['openid']))->find();
if($find){
$res = Db::name('weixin_dy_user')->where(array('openid'=>$data['openid']))->update($updateArray);
}else{
$res = Db::name('weixin_dy_user')->insert($addArray);
}
if($res){
$findRes = Db::name('weixin_dy_user')->where(array('openid'=>$data['openid']))->find();
return json(['code'=>200, 'msg'=>'成功', 'data'=>$findRes]);
}
}else{
return json(['code'=>500, 'msg'=>'失败']);
}
}
}
数据库
设计数据库表字段,将获取到的用户信息存入数据库里面。
运行效果:
发布时间:2022/06/20
发表评论