multi-store/app/store/logic/LoginLogic.php

98 lines
3.4 KiB
PHP
Raw Normal View History

2024-06-01 16:07:53 +08:00
<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台PHP版
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用可去除界面版权logo
// | gitee下载https://gitee.com/likeshop_gitee/likeadmin
// | github下载https://github.com/likeshop-github/likeadmin
// | 访问官网https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------
namespace app\store\logic;
use app\common\logic\BaseLogic;
use app\common\model\auth\Admin;
use app\store\service\AdminTokenService;
use app\common\model\auth\AdminRole;
use app\common\service\FileService;
use app\MyBusinessException;
use think\facade\Db;
use Webman\Config;
/**
* 登录逻辑
* Class LoginLogic
* @package app\store\logic
*/
class LoginLogic extends BaseLogic
{
/**
* @notes 管理员账号登录
* @param $params
* @return false|mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author 乔峰
* @date 2021/6/30 17:00
*/
public function login($params)
{
$time = time();
$admin = Admin::where('account', '=', $params['account'])->find();
2024-06-03 10:01:51 +08:00
if ($params['is_admin'] == 0 && $admin) {
$auth_shop = Db::name('user_auth_shop')->where(['admin_id' => $admin['id'], 'status' => 1, 'apply_status' => 1, 'type' => 2])->find();
if (!$auth_shop) {
2024-06-01 16:07:53 +08:00
throw new MyBusinessException('该账户没有权限');
}
}
2024-06-03 10:01:51 +08:00
if ($admin && $params['is_admin'] == 1) {
$role_find = AdminRole::where('admin_id', $admin['id'])->where('role_id', 'in', [1, 2])->find();
if ($role_find) {
throw new MyBusinessException('没有权限访问');
}
2024-06-01 16:07:53 +08:00
}
//用户表登录信息更新
$admin->login_time = $time;
$admin->login_ip = request()->getLocalIp();
$admin->save();
//设置token
$adminInfo = AdminTokenService::setToken($admin->id, $params['terminal'], $admin->multipoint_login);
//返回登录信息
$avatar = $admin->avatar ? $admin->avatar : Config::get('project.default_image.admin_avatar');
$avatar = FileService::getFileUrl($avatar);
return [
'name' => $adminInfo['name'],
'avatar' => $avatar,
'role_name' => $adminInfo['role_name'],
'token' => $adminInfo['token'],
];
}
/**
* @notes 退出登录
* @param $adminInfo
* @return bool
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author 乔峰
* @date 2021/7/5 14:34
*/
public function logout($adminInfo)
{
//token不存在不注销
if (!isset($adminInfo['token'])) {
return false;
}
//设置token过期
return AdminTokenService::expireToken($adminInfo['token']);
}
}