From 6f9e5870e9510657c3373f8ac1a3d9920194b7ca Mon Sep 17 00:00:00 2001 From: Dai Date: Fri, 9 Aug 2024 18:03:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E5=AF=86=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/StudentController.php | 15 +++- app/api/controller/TeacherController.php | 16 ++++- app/common/service/StudentService.php | 91 ++++++++++++++++++++---- app/common/service/TeacherService.php | 62 +++++++++++++--- 4 files changed, 156 insertions(+), 28 deletions(-) diff --git a/app/api/controller/StudentController.php b/app/api/controller/StudentController.php index 24c92d6..227d312 100644 --- a/app/api/controller/StudentController.php +++ b/app/api/controller/StudentController.php @@ -24,6 +24,19 @@ class StudentController extends BaseController return $this->json($res); } + + /** + * @desc 更改密码 + * @param Request $request + * @return \support\Response + */ + public function resetPassword(Request $request) + { + $service = new StudentService(); + $res = $service->resetPassword($request->post()); + return $this->json($res); + } + /** * @desc 获取code * @param Request $request @@ -209,4 +222,4 @@ class StudentController extends BaseController return $week_array[$week]; } -} \ No newline at end of file +} diff --git a/app/api/controller/TeacherController.php b/app/api/controller/TeacherController.php index 21df9d3..c3fe92d 100644 --- a/app/api/controller/TeacherController.php +++ b/app/api/controller/TeacherController.php @@ -70,6 +70,20 @@ class TeacherController extends BaseController } + /** + * @desc 重置密码 + * @param Request $request + * @return \support\Response + */ + public function resetPassword(Request $request) + { + $service = new TeacherService(); + $res = $service->resetPassword($request); + return $this->json($res); + + } -} \ No newline at end of file + + +} diff --git a/app/common/service/StudentService.php b/app/common/service/StudentService.php index 370c575..c8fa795 100644 --- a/app/common/service/StudentService.php +++ b/app/common/service/StudentService.php @@ -41,9 +41,9 @@ class StudentService 'data' => $token, 'msg' => 'success' ]; - }else{ + } else { $parent = StudentParent::where(['account' => $request['account']])->findOrEmpty(); - if(!$parent->isEmpty()){ + if (!$parent->isEmpty()) { if (md5($request['password'] . $parent->salt) != $parent->password) { throw new Exception('密码错误,请填写正确的密码'); } @@ -70,7 +70,68 @@ class StudentService ]; } } -/** + + /** + * @desc 充值密码 + * @param $request + * @return array + */ + public function resetPassword($request) + { + try { + if (empty((array)$request->student) && empty((array)$request->parent)) { + throw new Exception('请登陆后再操作'); + } + + $requestData = $request->post(); + + 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('未找到学生信息'); + } + if(empty($requestData['pwd']) || strlen(trim($requestData['pwd'])) < 6){ + throw new Exception('请输入密码或者长度大于6位'); + } + + $salt = random_str(16); + $password = md5(trim($requestData['pwd'] . $salt)); + $student->save([ + 'password' => $password, + 'salt' => $salt + ]); + + } 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('未找到家长信息'); + } + if(empty($requestData['pwd']) || strlen(trim($requestData['pwd'])) < 6){ + throw new Exception('请输入密码或者长度大于6位'); + } + + $salt = random_str(16); + $password = md5(trim($requestData['pwd'] . $salt)); + $parent->save([ + 'password' => $password, + 'salt' => $salt + ]); + } + + return [ + 'code' => ResponseCode::SUCCESS, + 'msg' => '更改成功' + ]; + + } catch (Exception $e) { + return [ + 'code' => ResponseCode::FAIL, + 'msg' => $e->getMessage() + ]; + } + } + + /** * @desc 登录 * @param $request * @return array @@ -81,14 +142,14 @@ class StudentService if (empty((array)$request->student) && empty((array)$request->parent)) { 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,nickname,parent_id')->findOrEmpty(); if ($student->isEmpty()) { throw new Exception('未找到学生信息'); } $info = $student->toArray(); $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,nickname')->findOrEmpty(); if ($parent->isEmpty()) { throw new Exception('未找到家长信息'); @@ -121,7 +182,7 @@ class StudentService public function updateOpenid($request) { try { - if(empty((array)$request->student) || empty((array)$request->parent)){ + if (empty((array)$request->student) || empty((array)$request->parent)) { throw new Exception('请先登陆'); } @@ -135,15 +196,15 @@ class StudentService $user_info = WechatSubscriptController::getUserInfo($access_token['openid'], $access_token['access_token']); - if(!empty((array)$request->student)){ + if (!empty((array)$request->student)) { $student = Student::where(['id' => $request->student->id])->findOrEmpty(); - if($student->isEmpty()){ + if ($student->isEmpty()) { throw new Exception('未找到学生信息'); } - if($student->openid && $student->openid != $user_info['openid']){ + if ($student->openid && $student->openid != $user_info['openid']) { throw new Exception('当前账号已绑定其它学生,不能重复绑定'); - }else{ + } else { $student->save([ 'openid' => $user_info['openid'], 'nickname' => $user_info['nickname'], @@ -151,14 +212,14 @@ class StudentService ]); } - }else{ + } else { $parent = StudentParent::where(['id' => $request->parent->id])->findOrEmpty(); - if($parent->isEmpty()){ + if ($parent->isEmpty()) { throw new Exception('未找到家长信息'); } - if($parent->openid && $parent->openid != $user_info['openid']){ + if ($parent->openid && $parent->openid != $user_info['openid']) { throw new Exception('当前账号已绑定其它家长,不能重复绑定'); - }else{ + } else { $parent->save([ 'openid' => $user_info['openid'], 'nickname' => $user_info['nickname'], @@ -170,7 +231,7 @@ class StudentService 'code' => ResponseCode::SUCCESS, 'msg' => 'success' ]; - }catch (Exception $e){ + } catch (Exception $e) { return [ 'code' => ResponseCode::FAIL, 'msg' => $e->getMessage() diff --git a/app/common/service/TeacherService.php b/app/common/service/TeacherService.php index 023f479..22bf21a 100644 --- a/app/common/service/TeacherService.php +++ b/app/common/service/TeacherService.php @@ -61,11 +61,11 @@ class TeacherService public function setTimeZone($request) { try { - if(empty($request->teacher)){ + if (empty($request->teacher)) { throw new Exception('请教师登陆后再设置'); } $teacher = Teacher::where(['id' => $request->teacher->id])->findOrEmpty(); - if($teacher->isEmpty()){ + if ($teacher->isEmpty()) { throw new Exception('未找到教师信息,设置失败'); } @@ -77,7 +77,7 @@ class TeacherService 'time_zone_abbr' => $time_zone->abbr, 'time_zone_offset' => $time_zone->offset, ]); - if(!$res){ + if (!$res) { throw new Exception('设置失败'); } @@ -109,7 +109,47 @@ class TeacherService 'code' => ResponseCode::SUCCESS, 'data' => $teacher, ]; - }catch (Exception $e){ + } catch (Exception $e) { + return [ + 'code' => ResponseCode::FAIL, + 'msg' => $e->getMessage() + ]; + } + } + + /** + * @desc 教师信息 + * @param $request + * @return array + */ + public function resetPassword($request) + { + try { + if (empty($request->teacher)) { + throw new Exception('请先教师登陆'); + } + $teacher = Teacher::where(['id' => $request->teacher->id])->findOrEmpty(); + if ($teacher->isEmpty()) { + throw new Exception('未找到教师信息'); + } + + $requestData = $request->post(); + if (empty($requestData['pwd']) || strlen(trim($requestData['pwd'])) < 6) { + throw new Exception('请输入密码或者长度大于6位'); + } + + $salt = random_str(16); + $password = md5(trim($requestData['pwd'] . $salt)); + $teacher->save([ + 'password' => $password, + 'salt' => $salt + ]); + + return [ + 'code' => ResponseCode::SUCCESS, + 'msg' => '操作成功', + ]; + } catch (Exception $e) { return [ 'code' => ResponseCode::FAIL, 'msg' => $e->getMessage() @@ -126,11 +166,11 @@ class TeacherService public function updateOpenid($request) { try { - if(empty($request->teacher)){ + if (empty($request->teacher)) { throw new Exception('请先教师登陆'); } $teacher = Teacher::where(['id' => $request->teacher->id])->findOrEmpty(); - if($teacher->isEmpty()){ + if ($teacher->isEmpty()) { throw new Exception('未找到教师信息'); } @@ -141,10 +181,10 @@ class TeacherService throw new Exception('获取信息失败'); } $openid = $user_info['openid']; - $teacher = Teacher::where(['id'=>$request->teacher->id])->findOrEmpty(); - if($teacher->openid && $teacher->openid != $openid){ + $teacher = Teacher::where(['id' => $request->teacher->id])->findOrEmpty(); + if ($teacher->openid && $teacher->openid != $openid) { throw new Exception('当前账号已绑定其它教师,不能重复绑定'); - }else{ + } else { $teacher->save([ 'openid' => $openid, ]); @@ -154,7 +194,7 @@ class TeacherService 'code' => ResponseCode::SUCCESS, 'msg' => 'success' ]; - }catch (Exception $e){ + } catch (Exception $e) { return [ 'code' => ResponseCode::FAIL, 'msg' => $e->getMessage() @@ -162,4 +202,4 @@ class TeacherService } } -} \ No newline at end of file +}