95 lines
2.6 KiB
PHP
95 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace app\common\service;
|
|
|
|
use app\common\model\Teacher;
|
|
use app\common\model\TimeZone;
|
|
use app\constant\ResponseCode;
|
|
use think\Exception;
|
|
use Tinywan\Jwt\JwtToken;
|
|
|
|
class TeacherService
|
|
{
|
|
|
|
/**
|
|
* @desc 登录
|
|
* @param $request
|
|
* @return array
|
|
*/
|
|
public function login($request)
|
|
{
|
|
try {
|
|
$teacher = Teacher::where(['account' => $request['account']])->findOrEmpty();
|
|
if ($teacher->isEmpty()) {
|
|
throw new Exception('账号不存在');
|
|
}
|
|
if (empty($request['password'])) {
|
|
throw new Exception('请填写密码');
|
|
}
|
|
|
|
if (md5($request['password'] . $teacher->salt) != $teacher->password) {
|
|
throw new Exception('密码错误,请填写正确的密码');
|
|
}
|
|
|
|
$token_data = [
|
|
'id' => $teacher->id,
|
|
'role' => 'teacher'
|
|
];
|
|
$token = JwtToken::generateToken($token_data);
|
|
// unset($token['refresh_token']);
|
|
return [
|
|
'code' => ResponseCode::SUCCESS,
|
|
'data' => $token,
|
|
'msg' => 'success'
|
|
];
|
|
|
|
} catch (Exception $e) {
|
|
return [
|
|
'code' => ResponseCode::FAIL,
|
|
'msg' => $e->getMessage()
|
|
];
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @desc 设置时区
|
|
* @param $request
|
|
* @return array|void
|
|
*/
|
|
public function setTimeZone($request)
|
|
{
|
|
try {
|
|
if(empty($request->teacher)){
|
|
throw new Exception('请教师登陆后再设置');
|
|
}
|
|
$teacher = Teacher::where(['id' => $request->teacher->id])->findOrEmpty();
|
|
if($teacher->isEmpty()){
|
|
throw new Exception('未找到教师信息,设置失败');
|
|
}
|
|
|
|
$time_zone = TimeZone::where(['id' => $request->post('time_zone_id')])->findOrEmpty();
|
|
|
|
$res = $teacher->save([
|
|
'time_zone_id' => $time_zone->id,
|
|
'time_zone_name' => $time_zone->name,
|
|
'time_zone_abbr' => $time_zone->abbr,
|
|
'time_zone_offset' => $time_zone->offset,
|
|
]);
|
|
if(!$res){
|
|
throw new Exception('设置失败');
|
|
}
|
|
|
|
return [
|
|
'code' => ResponseCode::SUCCESS,
|
|
'msg' => '设置成功'
|
|
];
|
|
} catch (Exception $e) {
|
|
return [
|
|
'code' => ResponseCode::FAIL,
|
|
'msg' => $e->getMessage()
|
|
];
|
|
}
|
|
}
|
|
|
|
} |