更改密码

This commit is contained in:
Dai 2024-08-09 18:03:45 +08:00
parent fbf5d34a19
commit 6f9e5870e9
4 changed files with 156 additions and 28 deletions

View File

@ -24,6 +24,19 @@ class StudentController extends BaseController
return $this->json($res); 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 * @desc 获取code
* @param Request $request * @param Request $request

View File

@ -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);
}
} }

View File

@ -41,9 +41,9 @@ class StudentService
'data' => $token, 'data' => $token,
'msg' => 'success' 'msg' => 'success'
]; ];
}else{ } else {
$parent = StudentParent::where(['account' => $request['account']])->findOrEmpty(); $parent = StudentParent::where(['account' => $request['account']])->findOrEmpty();
if(!$parent->isEmpty()){ if (!$parent->isEmpty()) {
if (md5($request['password'] . $parent->salt) != $parent->password) { if (md5($request['password'] . $parent->salt) != $parent->password) {
throw new Exception('密码错误,请填写正确的密码'); 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 登录 * @desc 登录
* @param $request * @param $request
* @return array * @return array
@ -81,14 +142,14 @@ class StudentService
if (empty((array)$request->student) && empty((array)$request->parent)) { if (empty((array)$request->student) && empty((array)$request->parent)) {
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,nickname,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,nickname')->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,7 +182,7 @@ class StudentService
public function updateOpenid($request) public function updateOpenid($request)
{ {
try { try {
if(empty((array)$request->student) || empty((array)$request->parent)){ if (empty((array)$request->student) || empty((array)$request->parent)) {
throw new Exception('请先登陆'); throw new Exception('请先登陆');
} }
@ -135,15 +196,15 @@ class StudentService
$user_info = WechatSubscriptController::getUserInfo($access_token['openid'], $access_token['access_token']); $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(); $student = Student::where(['id' => $request->student->id])->findOrEmpty();
if($student->isEmpty()){ if ($student->isEmpty()) {
throw new Exception('未找到学生信息'); throw new Exception('未找到学生信息');
} }
if($student->openid && $student->openid != $user_info['openid']){ if ($student->openid && $student->openid != $user_info['openid']) {
throw new Exception('当前账号已绑定其它学生,不能重复绑定'); throw new Exception('当前账号已绑定其它学生,不能重复绑定');
}else{ } else {
$student->save([ $student->save([
'openid' => $user_info['openid'], 'openid' => $user_info['openid'],
'nickname' => $user_info['nickname'], 'nickname' => $user_info['nickname'],
@ -151,14 +212,14 @@ class StudentService
]); ]);
} }
}else{ } else {
$parent = StudentParent::where(['id' => $request->parent->id])->findOrEmpty(); $parent = StudentParent::where(['id' => $request->parent->id])->findOrEmpty();
if($parent->isEmpty()){ if ($parent->isEmpty()) {
throw new Exception('未找到家长信息'); throw new Exception('未找到家长信息');
} }
if($parent->openid && $parent->openid != $user_info['openid']){ if ($parent->openid && $parent->openid != $user_info['openid']) {
throw new Exception('当前账号已绑定其它家长,不能重复绑定'); throw new Exception('当前账号已绑定其它家长,不能重复绑定');
}else{ } else {
$parent->save([ $parent->save([
'openid' => $user_info['openid'], 'openid' => $user_info['openid'],
'nickname' => $user_info['nickname'], 'nickname' => $user_info['nickname'],
@ -170,7 +231,7 @@ class StudentService
'code' => ResponseCode::SUCCESS, 'code' => ResponseCode::SUCCESS,
'msg' => 'success' 'msg' => 'success'
]; ];
}catch (Exception $e){ } catch (Exception $e) {
return [ return [
'code' => ResponseCode::FAIL, 'code' => ResponseCode::FAIL,
'msg' => $e->getMessage() 'msg' => $e->getMessage()

View File

@ -61,11 +61,11 @@ class TeacherService
public function setTimeZone($request) public function setTimeZone($request)
{ {
try { try {
if(empty($request->teacher)){ if (empty($request->teacher)) {
throw new Exception('请教师登陆后再设置'); throw new Exception('请教师登陆后再设置');
} }
$teacher = Teacher::where(['id' => $request->teacher->id])->findOrEmpty(); $teacher = Teacher::where(['id' => $request->teacher->id])->findOrEmpty();
if($teacher->isEmpty()){ if ($teacher->isEmpty()) {
throw new Exception('未找到教师信息,设置失败'); throw new Exception('未找到教师信息,设置失败');
} }
@ -77,7 +77,7 @@ class TeacherService
'time_zone_abbr' => $time_zone->abbr, 'time_zone_abbr' => $time_zone->abbr,
'time_zone_offset' => $time_zone->offset, 'time_zone_offset' => $time_zone->offset,
]); ]);
if(!$res){ if (!$res) {
throw new Exception('设置失败'); throw new Exception('设置失败');
} }
@ -109,7 +109,47 @@ class TeacherService
'code' => ResponseCode::SUCCESS, 'code' => ResponseCode::SUCCESS,
'data' => $teacher, '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 [ return [
'code' => ResponseCode::FAIL, 'code' => ResponseCode::FAIL,
'msg' => $e->getMessage() 'msg' => $e->getMessage()
@ -126,11 +166,11 @@ class TeacherService
public function updateOpenid($request) public function updateOpenid($request)
{ {
try { try {
if(empty($request->teacher)){ if (empty($request->teacher)) {
throw new Exception('请先教师登陆'); throw new Exception('请先教师登陆');
} }
$teacher = Teacher::where(['id' => $request->teacher->id])->findOrEmpty(); $teacher = Teacher::where(['id' => $request->teacher->id])->findOrEmpty();
if($teacher->isEmpty()){ if ($teacher->isEmpty()) {
throw new Exception('未找到教师信息'); throw new Exception('未找到教师信息');
} }
@ -141,10 +181,10 @@ class TeacherService
throw new Exception('获取信息失败'); throw new Exception('获取信息失败');
} }
$openid = $user_info['openid']; $openid = $user_info['openid'];
$teacher = Teacher::where(['id'=>$request->teacher->id])->findOrEmpty(); $teacher = Teacher::where(['id' => $request->teacher->id])->findOrEmpty();
if($teacher->openid && $teacher->openid != $openid){ if ($teacher->openid && $teacher->openid != $openid) {
throw new Exception('当前账号已绑定其它教师,不能重复绑定'); throw new Exception('当前账号已绑定其它教师,不能重复绑定');
}else{ } else {
$teacher->save([ $teacher->save([
'openid' => $openid, 'openid' => $openid,
]); ]);
@ -154,7 +194,7 @@ class TeacherService
'code' => ResponseCode::SUCCESS, 'code' => ResponseCode::SUCCESS,
'msg' => 'success' 'msg' => 'success'
]; ];
}catch (Exception $e){ } catch (Exception $e) {
return [ return [
'code' => ResponseCode::FAIL, 'code' => ResponseCode::FAIL,
'msg' => $e->getMessage() 'msg' => $e->getMessage()