course/app/common/service/StudentParentService.php
2024-07-21 18:40:09 +08:00

52 lines
1.3 KiB
PHP

<?php
namespace app\common\service;
use app\common\model\StudentParent;
use app\constant\ResponseCode;
use think\Exception;
use Tinywan\Jwt\JwtToken;
class StudentParentService
{
/**
* @desc 登录
* @param $request
* @return array
*/
public function login($request)
{
try {
$student = StudentParent::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' => 'parent'
];
$token = JwtToken::generateToken($token_data);
return [
'code' => ResponseCode::SUCCESS,
'data' => $token,
'msg' => 'success'
];
} catch (Exception $e) {
return [
'code' => ResponseCode::FAIL,
'msg' => $e->getMessage()
];
}
}
}