记录使用EasyWeChat做微信小程序登陆和其他操作
1.微信小程序登陆
关于后端:fastadmin加密生成token-CSDN博客
思路:
通过easywechat+fastadmin,
(1)== 用户端登陆(获取code) -> 请求后端接口获取session_key -> 用户端保存session_key
use EasyWeChat\Foundation\Application;
use EasyWeChat\Factory;
public function getSessionKey(){
$code = $this->request->post('code');
$config = [
'app_id' => 'wx65ae3f41d0714e10',
'secret' => 'e79ea0badcc0a4f1c549c55a530660cf',
'token' => 'easywechat',
'aes_key' => '......',
'http' => [
'throw' => true, // 状态码非 200、300 时是否抛出异常,默认为开启
'timeout' => 5.0,
'retry' => true, // 使用默认重试配置
],
];
$app = Factory::miniProgram($config); // 这个是小程序的
// $app = Factory::officialAccount($config); // 这个是公众号的
// 使用 SDK 中的相关功能来获取 session_key
$data = $app->auth->session($code);
$this->success('获取session_key成功', $data);
}
此刻就获取到了:session_key(很重要)、openid。
注意这里的this指向。
// 前端uniapp
getSessionKey() {
var that = this
uni.login({
success(info) {
console.log('login信息', info)
var postData = {
code: info.code
}
getApp().posturl('Wechat/getSessionKey', postData).then(res => {
console.log(res)
that.session_key = res.data.session_key
console.log('========',that.session_key)
});
}
})
}
2.微信小程序授权用户信息
用户端获取用户信息(获取 iv + encryptedData + signature + session_key )-> 请求后端接口获取token -> 用户端保存token (可以控制时间)
public function getUserInfo(){
// 获取得到avatarUrl、nickName、openId,进行存储
$data = $this -> app -> encryptor->decryptData($this -> post['session_key'], $this -> post['iv'], $this -> post['encryptedData']);
$userModel = new HcdrspUser();
$result = $userModel->where('openid', $data['openId'])->find();
if($result){
$this->success('获取用户信息成功', $data);
}else{
$userModel->openid = $data['openId'];
$userModel->avatarUrl = $data['avatarUrl'];
$userModel->name = $data['nickName'];
// 创建时间
$userModel->createtime = time();
$userModel->save();
$this->success('欢迎新用户', $data);
}
}
getuserinfo(e){
console.log('getuserinfo用户信息',e);
console.log('session_key信息',this.session_key);
var postData = {
iv: e.detail.iv,
encryptedData:e.detail.encryptedData,
session_key: this.session_key
}
getApp().posturl('Wechat/getUserInfo', postData).then(res => {
console.log(res)
this.avatarUrl = res.data.avatarUrl
this.nickName = res.data.avatarUrl
let openId = res.data.openId
uni.setStorageSync('openId',openId)
});
},
3. 微信小程序获取手机号
前端:
<button open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber">授权并获取手机号</button>
onGetPhoneNumber(e){
if(this.nickName === ''){
uni.showToast({
icon:'none',
title:'请先登陆'
})
return
}
console.log('getPhoneNumber用户信息',e);
console.log('session_key信息',this.session_key);
var postData = {
iv: e.detail.iv,
encryptedData:e.detail.encryptedData,
session_key: this.session_key
}
getApp().posturl('Wechat/getPhoneNumber', postData).then(res => {
console.log(res)
this.phoneNumber = res.data.phoneNumber
});
},
后端:
/*
* 获取手机号
*/
public function getPhoneNumber(){
$data = $this -> app -> encryptor->decryptData($this -> post['session_key'], $this -> post['iv'], $this -> post['encryptedData']);
$userModel = new HcdrspUser();
$result = $userModel->where('phone', $data['phoneNumber'])->find();
if($result){
$this->success('已存在手机号', $data);
}else{
$userModel->phone = $data['phoneNumber'];
$userModel->save();
$this->success('获取手机号成功', $data);
}
}