2024-07-11 00:10:33 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace app\common\service;
|
|
|
|
|
2024-07-23 21:59:15 +08:00
|
|
|
use app\api\controller\WechatSubscriptController;
|
2024-07-11 00:10:33 +08:00
|
|
|
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 {
|
2024-08-09 18:03:45 +08:00
|
|
|
if (empty($request->teacher)) {
|
2024-07-11 23:17:02 +08:00
|
|
|
throw new Exception('请教师登陆后再设置');
|
|
|
|
}
|
2024-07-11 00:10:33 +08:00
|
|
|
$teacher = Teacher::where(['id' => $request->teacher->id])->findOrEmpty();
|
2024-08-09 18:03:45 +08:00
|
|
|
if ($teacher->isEmpty()) {
|
2024-07-11 23:17:02 +08:00
|
|
|
throw new Exception('未找到教师信息,设置失败');
|
|
|
|
}
|
2024-07-11 00:10:33 +08:00
|
|
|
|
|
|
|
$time_zone = TimeZone::where(['id' => $request->post('time_zone_id')])->findOrEmpty();
|
|
|
|
|
2024-07-11 23:17:02 +08:00
|
|
|
$res = $teacher->save([
|
2024-07-11 00:10:33 +08:00
|
|
|
'time_zone_id' => $time_zone->id,
|
|
|
|
'time_zone_name' => $time_zone->name,
|
|
|
|
'time_zone_abbr' => $time_zone->abbr,
|
|
|
|
'time_zone_offset' => $time_zone->offset,
|
|
|
|
]);
|
2024-08-09 18:03:45 +08:00
|
|
|
if (!$res) {
|
2024-07-11 23:17:02 +08:00
|
|
|
throw new Exception('设置失败');
|
|
|
|
}
|
2024-07-11 00:10:33 +08:00
|
|
|
|
2024-07-11 23:17:02 +08:00
|
|
|
return [
|
|
|
|
'code' => ResponseCode::SUCCESS,
|
|
|
|
'msg' => '设置成功'
|
|
|
|
];
|
2024-07-11 00:10:33 +08:00
|
|
|
} catch (Exception $e) {
|
|
|
|
return [
|
|
|
|
'code' => ResponseCode::FAIL,
|
|
|
|
'msg' => $e->getMessage()
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-23 07:38:53 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @desc 教师信息
|
|
|
|
* @param $request
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function teacherInfo($request)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$teacher = Teacher::where(['id' => $request->teacher->id])->field('id,account,teacher_name,time_zone_offset,time_zone_id,time_zone_name')->findOrEmpty();
|
2024-07-27 14:05:13 +08:00
|
|
|
$teacher = $teacher->toArray();
|
|
|
|
$teacher['role'] = 'teacher';
|
2024-07-23 07:38:53 +08:00
|
|
|
return [
|
|
|
|
'code' => ResponseCode::SUCCESS,
|
|
|
|
'data' => $teacher,
|
|
|
|
];
|
2024-08-09 18:03:45 +08:00
|
|
|
} 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) {
|
2024-07-23 07:38:53 +08:00
|
|
|
return [
|
|
|
|
'code' => ResponseCode::FAIL,
|
|
|
|
'msg' => $e->getMessage()
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-08-09 18:03:45 +08:00
|
|
|
if (empty($request->teacher)) {
|
2024-07-23 21:59:15 +08:00
|
|
|
throw new Exception('请先教师登陆');
|
|
|
|
}
|
|
|
|
$teacher = Teacher::where(['id' => $request->teacher->id])->findOrEmpty();
|
2024-08-09 18:03:45 +08:00
|
|
|
if ($teacher->isEmpty()) {
|
2024-07-23 21:59:15 +08:00
|
|
|
throw new Exception('未找到教师信息');
|
|
|
|
}
|
|
|
|
|
2024-07-23 22:30:24 +08:00
|
|
|
$code = $request->post('code');
|
|
|
|
$user_info = WechatSubscriptController::getCodeAccessToken($code);
|
|
|
|
|
2024-07-23 21:59:15 +08:00
|
|
|
if (isset($result['code'])) {
|
2024-07-24 22:37:22 +08:00
|
|
|
throw new Exception('获取信息失败');
|
2024-07-23 21:59:15 +08:00
|
|
|
}
|
|
|
|
$openid = $user_info['openid'];
|
2024-08-09 18:03:45 +08:00
|
|
|
$teacher = Teacher::where(['id' => $request->teacher->id])->findOrEmpty();
|
|
|
|
if ($teacher->openid && $teacher->openid != $openid) {
|
2024-07-25 14:05:57 +08:00
|
|
|
throw new Exception('当前账号已绑定其它教师,不能重复绑定');
|
2024-08-09 18:03:45 +08:00
|
|
|
} else {
|
2024-07-25 14:05:57 +08:00
|
|
|
$teacher->save([
|
|
|
|
'openid' => $openid,
|
|
|
|
]);
|
|
|
|
}
|
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 18:03:45 +08:00
|
|
|
}
|