diff --git a/app/api/controller/WechatSubscriptController.php b/app/api/controller/WechatSubscriptController.php index 6f1ad98..a1671d5 100644 --- a/app/api/controller/WechatSubscriptController.php +++ b/app/api/controller/WechatSubscriptController.php @@ -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', ] ]); @@ -99,4 +106,4 @@ class WechatSubscriptController extends BaseController } } -} \ No newline at end of file +} diff --git a/app/common/service/StudentService.php b/app/common/service/StudentService.php index f2869e5..32d4a55 100644 --- a/app/common/service/StudentService.php +++ b/app/common/service/StudentService.php @@ -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'