Files
swiftadmin/app/admin/service/UserService.php

147 lines
4.8 KiB
PHP
Raw Normal View History

2023-07-03 10:08:34 +08:00
<?php
declare (strict_types=1);
// +----------------------------------------------------------------------
// | swiftAdmin 极速开发框架 [基于WebMan开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2030 http://www.swiftadmin.net
// +----------------------------------------------------------------------
// | swiftAdmin.net High Speed Development Framework
// +----------------------------------------------------------------------
// | Author: meystack <coolsec@foxmail.com> Apache 2.0 License
// +----------------------------------------------------------------------
namespace app\admin\service;
use app\common\exception\OperateException;
use app\common\model\system\User;
use app\common\model\system\UserGroup;
2023-07-04 18:42:41 +08:00
use system\IpLocation;
2023-07-03 10:08:34 +08:00
use system\Random;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Db;
class UserService
{
/**
*
* @param array $params
* @param array $conditions
* @return array
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public static function dataList(array $params = [], array $conditions = []): array
{
$page = (int)$params['page'] ?: 1;
$limit = (int)$params['limit'] ?: 10;
$status = !empty($params['status']) ? $params['status']-1:1;
if (!empty($params['nickname'])) {
$where[] = ['nickname','like','%'.$params['nickname'].'%'];
}
if (!empty($params['group_id'])) {
$where[] = ['group_id','find in set',$params['group_id']];
}
$where[]=['status','=',$status];
$conditions = array_merge($conditions, $where ?? []);
$model = new User();
$count = $model->where($conditions)->count();
$page = ($count <= $limit) ? 1 : $page;
$list = $model->where($conditions)->order("id asc")->limit($limit)->page($page)->select();
// 循环处理数据
$userGroup = (new UserGroup)->select()->toArray();
2023-07-04 18:42:41 +08:00
$qqWry = new IpLocation();
2023-07-03 10:08:34 +08:00
foreach ($list as $key => $value) {
$value->hidden(['pwd', 'salt']);
try {
2023-07-04 18:42:41 +08:00
$region = $qqWry->getLocation($value['login_ip']);
$region = $region['country'] . ' ' . $region['area'];
2023-07-03 10:08:34 +08:00
} catch (\Exception $e) {
2023-07-04 18:42:41 +08:00
$region = 'unKnown';
2023-07-03 10:08:34 +08:00
}
$list[$key]['region'] = $region;
$result = list_search($userGroup,['id'=> $value['group_id']]);
if (!empty($result)) {
$list[$key]['group'] = $result['title'];
}
}
return [$count, $list];
}
/**
* @param array $params
* @return bool
* @throws OperateException
*/
public static function add(array $params): bool
{
$model = new User();
$whereName[] = ['nickname','=',$params['nickname']];
$whereEmail[] = ['email','=',$params['email']];
$data = $model->whereOr([$whereName,$whereEmail])->findOrEmpty()->toArray();
if(!empty($data)) {
throw new OperateException('该用户ID或邮箱已经存在');
}
Db::startTrans();
try {
$model->create($params);
} catch (\Exception $e) {
Db::rollback();
throw new OperateException('添加失败!');
}
return true;
}
/**
* @param array $params
* @return bool
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @throws OperateException
*/
public static function edit(array $params): bool
{
$model = new User();
$data = $model->where('id', $params['id'])->findOrEmpty()->toArray();
if ($data['nickname'] != $params['nickname']) {
$whereName[] = ['nickname','=',$params['nickname']];
if($model->where($whereName)->find()) {
throw new OperateException('用户ID已经存在');
}
}
if ($data['email'] != $params['email']) {
$whereEmail[] = ['email','=',$params['email']];
if($model->where($whereEmail)->find()) {
throw new OperateException('用户邮箱已经存在!');
}
}
if (!empty($params['pwd'])) {
$salt = Random::alpha();
$params['salt'] = $salt;
$params['pwd'] = encryptPwd($params['pwd'],$params['salt']);
} else {
unset($params['pwd']);
}
Db::startTrans();
try {
$model->update($params);
} catch (\Exception $e) {
Db::rollback();
throw new OperateException('添加失败!');
}
return true;
}
}