Staff登录

This commit is contained in:
2024-12-02 14:35:03 +08:00
parent 59e77906f0
commit 410a838e8b
3 changed files with 25 additions and 4 deletions

View File

@ -10,17 +10,31 @@ class Login extends Controller
public function login()
{
$this->title = '登录';
$this->staff = InspectionStaff::mk()->where('phone', $this->request->post('phone'))->find();
if ($this->request->post('phone')) {
$where = $this->_vali([
'phone.require' => '手机号码不能为空',
]);
$this->staff = InspectionStaff::mk()->where($where)->find();
} else {
$where = $this->_vali([
'account.require' => '登录账号不能为空',
]);
$this->staff = InspectionStaff::mk()->where($where)->find();
}
if (empty($this->staff)) {
$this->error('用户不存在');
}
if ($this->staff->password !== md5($this->request->post('password'))) {
["password" => $password] = $this->_vali([
'password.require' => '登录密码不能为空',
]);
if ($this->staff->password !== $password) {
$this->error('密码错误');
}
$this->staff->save(['last_login_at' => date('Y-m-d H:i:s')]);
// $this->staff->tokens()->where('token', '<>', '')->delete();
$this->staff->tokens()->where('token', '<>', '')->delete();
$this->success('登录成功', [
'token' => $this->staff->tokens()->save(['token' => md5(uniqid())])->token,
'user' => $this->staff->toArray(),
]);
}
}

View File

@ -6,6 +6,13 @@ use plugin\inspection\controller\api\Auth;
class Staff extends Auth
{
public function check() {
$this->success('登录成功', [
'token' => $this->tokenInfo->token,
'user' => $this->staff
]);
}
public function logout() {
$this->staff->tokens()->delete();
$this->success('退出成功');

View File

@ -8,6 +8,6 @@ class InspectionStaffToken extends Model
{
public function staff()
{
return $this->belongsTo(InspectionStaff::class, 'id', 'staff_id');
return $this->belongsTo(InspectionStaff::class, 'staff_id');
}
}