2024-07-11 23:17:02 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace app\common\service;
|
|
|
|
|
2024-07-23 21:59:15 +08:00
|
|
|
use app\api\controller\WechatSubscriptController;
|
2024-07-11 23:17:02 +08:00
|
|
|
use app\common\model\Student;
|
2024-07-27 10:35:57 +08:00
|
|
|
use app\common\model\StudentParent;
|
2024-07-11 23:17:02 +08:00
|
|
|
use app\constant\ResponseCode;
|
|
|
|
use think\Exception;
|
|
|
|
use Tinywan\Jwt\JwtToken;
|
|
|
|
|
|
|
|
class StudentService
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @desc 登录
|
|
|
|
* @param $request
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function login($request)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
if (empty($request['password'])) {
|
|
|
|
throw new Exception('请填写密码');
|
|
|
|
}
|
2024-07-27 10:35:57 +08:00
|
|
|
$student = Student::where(['account' => $request['account']])->findOrEmpty();
|
2024-07-11 23:17:02 +08:00
|
|
|
|
2024-07-27 10:35:57 +08:00
|
|
|
if (!$student->isEmpty()) {
|
|
|
|
if (md5($request['password'] . $student->salt) != $student->password) {
|
|
|
|
throw new Exception('密码错误,请填写正确的密码');
|
|
|
|
}
|
2024-07-11 23:17:02 +08:00
|
|
|
|
2024-07-27 10:35:57 +08:00
|
|
|
$token_data = [
|
|
|
|
'id' => $student->id,
|
|
|
|
'role' => 'student'
|
|
|
|
];
|
|
|
|
$token = JwtToken::generateToken($token_data);
|
|
|
|
return [
|
|
|
|
'code' => ResponseCode::SUCCESS,
|
|
|
|
'data' => $token,
|
|
|
|
'msg' => 'success'
|
|
|
|
];
|
2024-08-09 18:03:45 +08:00
|
|
|
} else {
|
2024-07-27 10:35:57 +08:00
|
|
|
$parent = StudentParent::where(['account' => $request['account']])->findOrEmpty();
|
2024-08-09 18:03:45 +08:00
|
|
|
if (!$parent->isEmpty()) {
|
2024-07-27 10:35:57 +08:00
|
|
|
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('请检查账号信息,未匹配到任何学生或家长');
|
2024-07-11 23:17:02 +08:00
|
|
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return [
|
|
|
|
'code' => ResponseCode::FAIL,
|
|
|
|
'msg' => $e->getMessage()
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
2024-08-09 18:03:45 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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()
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-07-21 19:19:00 +08:00
|
|
|
* @desc 登录
|
|
|
|
* @param $request
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function userInfo($request)
|
|
|
|
{
|
|
|
|
try {
|
2024-07-27 10:35:57 +08:00
|
|
|
if (empty((array)$request->student) && empty((array)$request->parent)) {
|
|
|
|
throw new Exception('请登陆后再查看');
|
2024-07-21 19:19:00 +08:00
|
|
|
}
|
2024-08-09 18:03:45 +08:00
|
|
|
if (!empty((array)$request->student)) {
|
2024-10-21 22:19:11 +08:00
|
|
|
$student = Student::where(['id' => $request->student->id])->with(['parentArr'])->field('id,student_name,account,openid,avatar,nickname,parent_id,show_schedule,show_homework,show_feedback')->findOrEmpty();
|
2024-07-27 10:35:57 +08:00
|
|
|
if ($student->isEmpty()) {
|
|
|
|
throw new Exception('未找到学生信息');
|
|
|
|
}
|
|
|
|
$info = $student->toArray();
|
|
|
|
$info['role'] = 'student';
|
2024-08-09 18:03:45 +08:00
|
|
|
} elseif (!empty((array)$request->parent)) {
|
2024-08-09 17:07:35 +08:00
|
|
|
$parent = StudentParent::where(['id' => $request->parent->id])->with(['studentArr'])->field('id,parent_name,account,openid,avatar,nickname')->findOrEmpty();
|
2024-07-27 10:35:57 +08:00
|
|
|
if ($parent->isEmpty()) {
|
|
|
|
throw new Exception('未找到家长信息');
|
|
|
|
}
|
|
|
|
$info = $parent->toArray();
|
|
|
|
$info['role'] = 'parent';
|
2024-07-21 19:19:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'code' => ResponseCode::SUCCESS,
|
2024-07-27 10:35:57 +08:00
|
|
|
'data' => $info,
|
2024-07-21 19:19:00 +08:00
|
|
|
'msg' => 'success'
|
|
|
|
];
|
|
|
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return [
|
|
|
|
'code' => ResponseCode::FAIL,
|
|
|
|
'msg' => $e->getMessage()
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
2024-07-11 23:17:02 +08:00
|
|
|
|
2024-07-23 21:59:15 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @desc 更新code
|
|
|
|
* @param $request
|
|
|
|
* @return array|void
|
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException
|
|
|
|
*/
|
|
|
|
public function updateOpenid($request)
|
|
|
|
{
|
|
|
|
try {
|
2024-09-19 15:52:44 +08:00
|
|
|
if (empty((array)$request->student) && empty((array)$request->parent)) {
|
2024-08-09 17:07:35 +08:00
|
|
|
throw new Exception('请先登陆');
|
2024-07-23 21:59:15 +08:00
|
|
|
}
|
|
|
|
|
2024-07-23 22:30:24 +08:00
|
|
|
$code = $request->post('code');
|
2024-07-25 16:44:26 +08:00
|
|
|
|
2024-08-09 17:07:35 +08:00
|
|
|
$access_token = WechatSubscriptController::getCodeAccessToken($code);
|
2024-07-25 16:44:26 +08:00
|
|
|
|
2024-08-09 17:07:35 +08:00
|
|
|
if (isset($access_token['code'])) {
|
2024-07-25 14:05:57 +08:00
|
|
|
throw new Exception('获取信息失败');
|
2024-07-23 21:59:15 +08:00
|
|
|
}
|
2024-07-25 14:05:57 +08:00
|
|
|
|
2024-08-09 17:34:51 +08:00
|
|
|
$user_info = WechatSubscriptController::getUserInfo($access_token['openid'], $access_token['access_token']);
|
2024-08-09 17:07:35 +08:00
|
|
|
|
2024-08-09 18:03:45 +08:00
|
|
|
if (!empty((array)$request->student)) {
|
2024-08-09 17:07:35 +08:00
|
|
|
$student = Student::where(['id' => $request->student->id])->findOrEmpty();
|
2024-08-09 18:03:45 +08:00
|
|
|
if ($student->isEmpty()) {
|
2024-08-09 17:07:35 +08:00
|
|
|
throw new Exception('未找到学生信息');
|
|
|
|
}
|
|
|
|
|
2024-08-09 18:03:45 +08:00
|
|
|
if ($student->openid && $student->openid != $user_info['openid']) {
|
2024-08-09 17:07:35 +08:00
|
|
|
throw new Exception('当前账号已绑定其它学生,不能重复绑定');
|
2024-08-09 18:03:45 +08:00
|
|
|
} else {
|
2024-08-09 17:07:35 +08:00
|
|
|
$student->save([
|
|
|
|
'openid' => $user_info['openid'],
|
|
|
|
'nickname' => $user_info['nickname'],
|
|
|
|
'avatar' => $user_info['headimgurl'],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2024-08-09 18:03:45 +08:00
|
|
|
} else {
|
2024-08-09 17:07:35 +08:00
|
|
|
$parent = StudentParent::where(['id' => $request->parent->id])->findOrEmpty();
|
2024-08-09 18:03:45 +08:00
|
|
|
if ($parent->isEmpty()) {
|
2024-08-09 17:07:35 +08:00
|
|
|
throw new Exception('未找到家长信息');
|
|
|
|
}
|
2024-08-09 18:03:45 +08:00
|
|
|
if ($parent->openid && $parent->openid != $user_info['openid']) {
|
2024-08-09 17:07:35 +08:00
|
|
|
throw new Exception('当前账号已绑定其它家长,不能重复绑定');
|
2024-08-09 18:03:45 +08:00
|
|
|
} else {
|
2024-08-09 17:07:35 +08:00
|
|
|
$parent->save([
|
|
|
|
'openid' => $user_info['openid'],
|
|
|
|
'nickname' => $user_info['nickname'],
|
|
|
|
'avatar' => $user_info['headimgurl'],
|
|
|
|
]);
|
|
|
|
}
|
2024-07-25 14:05:57 +08:00
|
|
|
}
|
2024-07-23 21:59:15 +08:00
|
|
|
return [
|
|
|
|
'code' => ResponseCode::SUCCESS,
|
|
|
|
'msg' => 'success'
|
|
|
|
];
|
2024-08-09 18:03:45 +08:00
|
|
|
} catch (Exception $e) {
|
2024-07-23 21:59:15 +08:00
|
|
|
return [
|
|
|
|
'code' => ResponseCode::FAIL,
|
|
|
|
'msg' => $e->getMessage()
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-09 16:47:03 +08:00
|
|
|
}
|