Files
usedcar/app/console/service/AdminsService.php
T
2025-06-06 10:38:31 +08:00

98 lines
3.2 KiB
PHP

<?php
namespace app\console\service;
use app\console\model\AdminsModel;
use app\mzl\common\services\BaseService;
use app\mzl\common\utils\JwtUtil;
use app\mzl\modules\translation\service\TranslationService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use support\exception\BusinessException;
class AdminsService extends BaseService
{
/**
* 定义模型
*
* @return string
*/
public function getModel(): string
{
return AdminsModel::class;
}
/**
* 多场景配置
*/
public function queryScenes(): array
{
return [
// 'default' => function ($params) {
// $keyword = $params['keyword'] ?? '';
// return [
// 'filters' => [
// ['field' => '***', 'operator' => '=', 'value' => '***']
// ],
// 'fields' => ['id'],
// 'relations' => [],
// 'custom' => function(Builder $q) use ($keyword) {
// // 示例
// if ($keyword) {
// $q->where(function($q2) use($keyword) {
// $q2->whereHas('***', function($q3) use($keyword) {
// $q3->where('***', 'like', "%{$keyword}%");
// })->orWhereHas('***', function($q4) use($keyword) {
// $q4->where('***', 'like', "%{$keyword}%");
// });
// });
// }
// },
// 'sorts' => [
// ['field' => 'created_at', 'direction' => 'desc']
// ],
// 'pagination' => [
// 'page' => $params['page'] ?? 1,
// 'per_page' => $params['per_page'] ?? 10,
// ]
// ];
// }
];
}
public function login(array $params)
{
$sourceLocale = locale();
try {
if (empty($params['username'])) {
throw new BusinessException('用户名不能为空');
}
if (empty($params['password'])) {
throw new BusinessException('密码不能为空');
}
$admin = AdminsModel::where(['username'=>$params['username']])->first();
if (!$admin) {
throw new BusinessException('用户不存在');
}
$userId = $admin['id'];
$adminPassword = $admin['password'];
// $hashedPassword = password_hash($params['password'], PASSWORD_DEFAULT); //注册时的加密方法
if (!password_verify($params['password'], $adminPassword)) {
throw new BusinessException('密码错误');
}
//密码验证成功后加密生成 Token
$token = JwtUtil::generateToken($userId);
$result = $admin;
unset($result['password']);
$result['token'] = $token;
return $admin;
} catch (\Throwable $th) {
throw $th;
}
}
}