获取用户nickname openid

This commit is contained in:
Dai 2024-08-09 17:07:35 +08:00
parent f7d6448863
commit 271a0dc30a
2 changed files with 49 additions and 23 deletions

View File

@ -68,15 +68,22 @@ class WechatSubscriptController extends BaseController
}
}
public static function getUserinfo($openid, $access_token)
/**
* @desc 获取微信用户信息
* @param $openid
* @param $access_token
* @return array|mixed|void
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public static function getUserInfo($access_token)
{
try {
$client = new Client(['base_uri' => self::BASE_URI]);
$response = $client->request('get', 'sns/userinfo', [
'query' => [
'access_token' => getenv('APP_ID'),
'openid' => $openid,
'access_token' => $access_token,
'openid' => getenv('APP_ID'),
'lang' => 'zh_CN',
]
]);

View File

@ -82,14 +82,14 @@ class StudentService
throw new Exception('请登陆后再查看');
}
if(!empty((array)$request->student)){
$student = Student::where(['id' => $request->student->id])->with(['parentArr'])->field('id,student_name,account,openid,avatar,parent_id')->findOrEmpty();
$student = Student::where(['id' => $request->student->id])->with(['parentArr'])->field('id,student_name,account,openid,avatar,nickname,parent_id')->findOrEmpty();
if ($student->isEmpty()) {
throw new Exception('未找到学生信息');
}
$info = $student->toArray();
$info['role'] = 'student';
}elseif (!empty((array)$request->parent)){
$parent = StudentParent::where(['id' => $request->parent->id])->with(['studentArr'])->field('id,parent_name,account,openid,avatar')->findOrEmpty();
$parent = StudentParent::where(['id' => $request->parent->id])->with(['studentArr'])->field('id,parent_name,account,openid,avatar,nickname')->findOrEmpty();
if ($parent->isEmpty()) {
throw new Exception('未找到家长信息');
}
@ -121,32 +121,51 @@ class StudentService
public function updateOpenid($request)
{
try {
if(empty($request->student)){
throw new Exception('请先教师登陆');
}
$teacher = Student::where(['id' => $request->student->id])->findOrEmpty();
if($teacher->isEmpty()){
throw new Exception('未找到教师信息');
if(empty((array)$request->student) || empty((array)$request->parent)){
throw new Exception('请先登陆');
}
$code = $request->post('code');
$user_info = WechatSubscriptController::getCodeAccessToken($code);
$access_token = WechatSubscriptController::getCodeAccessToken($code);
if (isset($result['code'])) {
if (isset($access_token['code'])) {
throw new Exception('获取信息失败');
}
$openid = $user_info['openid'];
$student = Student::where(['id'=>$request->student->id])->findOrEmpty();
if($student->openid && $student->openid != $openid){
throw new Exception('当前账号已绑定其它学生,不能重复绑定');
$user_info = WechatSubscriptController::getUserInfo($access_token['access_token']);
if(!empty((array)$request->student)){
$student = Student::where(['id' => $request->student->id])->findOrEmpty();
if($student->isEmpty()){
throw new Exception('未找到学生信息');
}
if($student->openid && $student->openid != $user_info['openid']){
throw new Exception('当前账号已绑定其它学生,不能重复绑定');
}else{
$student->save([
'openid' => $user_info['openid'],
'nickname' => $user_info['nickname'],
'avatar' => $user_info['headimgurl'],
]);
}
}else{
$student->save([
'openid' => $openid,
]);
$parent = StudentParent::where(['id' => $request->parent->id])->findOrEmpty();
if($parent->isEmpty()){
throw new Exception('未找到家长信息');
}
if($parent->openid && $parent->openid != $user_info['openid']){
throw new Exception('当前账号已绑定其它家长,不能重复绑定');
}else{
$parent->save([
'openid' => $user_info['openid'],
'nickname' => $user_info['nickname'],
'avatar' => $user_info['headimgurl'],
]);
}
}
return [
'code' => ResponseCode::SUCCESS,
'msg' => 'success'