$request['account']])->findOrEmpty(); if (!$student->isEmpty()) { if (md5($request['password'] . $student->salt) != $student->password) { throw new Exception('密码错误,请填写正确的密码'); } $token_data = [ 'id' => $student->id, 'role' => 'student' ]; $token = JwtToken::generateToken($token_data); return [ 'code' => ResponseCode::SUCCESS, 'data' => $token, 'msg' => 'success' ]; }else{ $parent = StudentParent::where(['account' => $request['account']])->findOrEmpty(); if(!$parent->isEmpty()){ if (md5($request['password'] . $parent->salt) != $parent->password) { throw new Exception('密码错误,请填写正确的密码'); } $token_data = [ 'id' => $parent->id, 'role' => 'parent' ]; $token = JwtToken::generateToken($token_data); return [ 'code' => ResponseCode::SUCCESS, 'data' => $token, 'msg' => 'success' ]; } } throw new Exception('请检查账号信息,未匹配到任何学生或家长'); } catch (Exception $e) { return [ 'code' => ResponseCode::FAIL, 'msg' => $e->getMessage() ]; } } /** * @desc 登录 * @param $request * @return array */ public function userInfo($request) { try { if (empty((array)$request->student) && empty((array)$request->parent)) { throw new Exception('请登陆后再查看'); } if(!empty((array)$request->student)){ $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,nickname')->findOrEmpty(); if ($parent->isEmpty()) { throw new Exception('未找到家长信息'); } $info = $parent->toArray(); $info['role'] = 'parent'; } return [ 'code' => ResponseCode::SUCCESS, 'data' => $info, 'msg' => 'success' ]; } catch (Exception $e) { return [ 'code' => ResponseCode::FAIL, 'msg' => $e->getMessage() ]; } } /** * @desc 更新code * @param $request * @return array|void * @throws \GuzzleHttp\Exception\GuzzleException */ public function updateOpenid($request) { try { if(empty((array)$request->student) || empty((array)$request->parent)){ throw new Exception('请先登陆'); } $code = $request->post('code'); $access_token = WechatSubscriptController::getCodeAccessToken($code); if (isset($access_token['code'])) { throw new Exception('获取信息失败'); } $user_info = WechatSubscriptController::getUserInfo($access_token['openid'], $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{ $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' ]; }catch (Exception $e){ return [ 'code' => ResponseCode::FAIL, 'msg' => $e->getMessage() ]; } } }