2024-07-11 23:17:02 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace app\common\service;
|
|
|
|
|
|
|
|
use app\common\model\Student;
|
|
|
|
use app\constant\ResponseCode;
|
|
|
|
use think\Exception;
|
|
|
|
use Tinywan\Jwt\JwtToken;
|
|
|
|
|
|
|
|
class StudentService
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @desc 登录
|
|
|
|
* @param $request
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function login($request)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$student = Student::where(['account' => $request['account']])->findOrEmpty();
|
|
|
|
if ($student->isEmpty()) {
|
|
|
|
throw new Exception('账号不存在');
|
|
|
|
}
|
|
|
|
if (empty($request['password'])) {
|
|
|
|
throw new Exception('请填写密码');
|
|
|
|
}
|
|
|
|
|
|
|
|
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'
|
|
|
|
];
|
|
|
|
|
|
|
|
} 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 {
|
|
|
|
if (empty($request->student)) {
|
|
|
|
throw new Exception('请学生登陆后再查看');
|
|
|
|
}
|
|
|
|
$student = Student::where(['id' => $request->student->id])->field('id,student_name,account,openid')->findOrEmpty();
|
|
|
|
if ($student->isEmpty()) {
|
|
|
|
throw new Exception('未找到学生信息');
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'code' => ResponseCode::SUCCESS,
|
|
|
|
'data' => $student,
|
|
|
|
'msg' => 'success'
|
|
|
|
];
|
|
|
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return [
|
|
|
|
'code' => ResponseCode::FAIL,
|
|
|
|
'msg' => $e->getMessage()
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
2024-07-11 23:17:02 +08:00
|
|
|
|
|
|
|
}
|