获取用户nickname openid
This commit is contained in:
parent
f7d6448863
commit
271a0dc30a
@ -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 {
|
try {
|
||||||
|
|
||||||
$client = new Client(['base_uri' => self::BASE_URI]);
|
$client = new Client(['base_uri' => self::BASE_URI]);
|
||||||
$response = $client->request('get', 'sns/userinfo', [
|
$response = $client->request('get', 'sns/userinfo', [
|
||||||
'query' => [
|
'query' => [
|
||||||
'access_token' => getenv('APP_ID'),
|
'access_token' => $access_token,
|
||||||
'openid' => $openid,
|
'openid' => getenv('APP_ID'),
|
||||||
'lang' => 'zh_CN',
|
'lang' => 'zh_CN',
|
||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
|
@ -82,14 +82,14 @@ class StudentService
|
|||||||
throw new Exception('请登陆后再查看');
|
throw new Exception('请登陆后再查看');
|
||||||
}
|
}
|
||||||
if(!empty((array)$request->student)){
|
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()) {
|
if ($student->isEmpty()) {
|
||||||
throw new Exception('未找到学生信息');
|
throw new Exception('未找到学生信息');
|
||||||
}
|
}
|
||||||
$info = $student->toArray();
|
$info = $student->toArray();
|
||||||
$info['role'] = 'student';
|
$info['role'] = 'student';
|
||||||
}elseif (!empty((array)$request->parent)){
|
}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()) {
|
if ($parent->isEmpty()) {
|
||||||
throw new Exception('未找到家长信息');
|
throw new Exception('未找到家长信息');
|
||||||
}
|
}
|
||||||
@ -121,32 +121,51 @@ class StudentService
|
|||||||
public function updateOpenid($request)
|
public function updateOpenid($request)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
if(empty($request->student)){
|
if(empty((array)$request->student) || empty((array)$request->parent)){
|
||||||
throw new Exception('请先教师登陆');
|
throw new Exception('请先登陆');
|
||||||
}
|
|
||||||
$teacher = Student::where(['id' => $request->student->id])->findOrEmpty();
|
|
||||||
if($teacher->isEmpty()){
|
|
||||||
throw new Exception('未找到教师信息');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$code = $request->post('code');
|
$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('获取信息失败');
|
throw new Exception('获取信息失败');
|
||||||
}
|
}
|
||||||
$openid = $user_info['openid'];
|
|
||||||
|
|
||||||
$student = Student::where(['id'=>$request->student->id])->findOrEmpty();
|
$user_info = WechatSubscriptController::getUserInfo($access_token['access_token']);
|
||||||
if($student->openid && $student->openid != $openid){
|
|
||||||
throw new Exception('当前账号已绑定其它学生,不能重复绑定');
|
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{
|
}else{
|
||||||
$student->save([
|
$parent = StudentParent::where(['id' => $request->parent->id])->findOrEmpty();
|
||||||
'openid' => $openid,
|
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 [
|
return [
|
||||||
'code' => ResponseCode::SUCCESS,
|
'code' => ResponseCode::SUCCESS,
|
||||||
'msg' => 'success'
|
'msg' => 'success'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user