211 lines
6.0 KiB
PHP
211 lines
6.0 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Service;
|
||
|
|
|
||
|
|
use App\BaseApp\BaseService;
|
||
|
|
use App\Enum\UserStatusEnum;
|
||
|
|
use App\Models\CollectionModel;
|
||
|
|
use App\Models\RoleModel;
|
||
|
|
use App\Models\UserModel;
|
||
|
|
use App\Models\UserPayModel;
|
||
|
|
use Exception;
|
||
|
|
use Psr\Container\ContainerExceptionInterface;
|
||
|
|
use Psr\Container\NotFoundExceptionInterface;
|
||
|
|
|
||
|
|
class UserService extends BaseService
|
||
|
|
{
|
||
|
|
public function __construct()
|
||
|
|
{
|
||
|
|
parent::__construct();
|
||
|
|
$this->selectField = [ 'id', 'account', 'nick_name', 'avatar', 'email', 'balance', 'is_sys_notifications', 'is_collection_notifications', 'is_marketing_notifications', 'qr_code', 'role_id', 'ip', 'ip_table', 'status', 'last_reset_password_at', 'created_at', 'updated_at', 'deleted_at', ];
|
||
|
|
$this->model = UserModel::class;
|
||
|
|
$this->queryField = [
|
||
|
|
'account' => 'like',
|
||
|
|
'nick_name' => 'like',
|
||
|
|
'email' => 'like',
|
||
|
|
'role_id' => '=',
|
||
|
|
'status' => '=',
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 获取用户列表
|
||
|
|
* @return array
|
||
|
|
* @throws ContainerExceptionInterface
|
||
|
|
* @throws NotFoundExceptionInterface
|
||
|
|
*/
|
||
|
|
public function list(): array
|
||
|
|
{
|
||
|
|
$this->with = [
|
||
|
|
'roles'
|
||
|
|
];
|
||
|
|
$result = $this->getPageList();
|
||
|
|
|
||
|
|
foreach ($result['items'] as &$v) {
|
||
|
|
$v['ip_table'] = json_decode($v['ip_table'], true);
|
||
|
|
$v['status_text'] = UserStatusEnum::from($v['status'])->description();
|
||
|
|
$v['last_reset_password_at'] = format_time($v['last_reset_password_at']);
|
||
|
|
}
|
||
|
|
|
||
|
|
return $result;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function detail($id)
|
||
|
|
{
|
||
|
|
$result = $this->getDetail($id);
|
||
|
|
$result['ip_table'] = json_decode($result['ip_table'], true);
|
||
|
|
$result['status_text'] = UserStatusEnum::from($result['status'])->description();
|
||
|
|
return $result;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function option()
|
||
|
|
{
|
||
|
|
$this->optionField = ['id', 'nick_name'];
|
||
|
|
return $this->getOption();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 创建用户
|
||
|
|
* @param $params
|
||
|
|
* @return mixed
|
||
|
|
* @throws Exception
|
||
|
|
*/
|
||
|
|
public function create($params): mixed
|
||
|
|
{
|
||
|
|
$params['ip_table'] = json_encode([]);
|
||
|
|
$params['password'] = password_hash($params['password'], PASSWORD_DEFAULT);
|
||
|
|
$existsUser = $this->model::where('account', $params['account'])->whereOr('email', $params['email'])->exists();
|
||
|
|
if ($existsUser) {
|
||
|
|
$this->utils->errorThrow('账号或邮箱已存在');
|
||
|
|
}
|
||
|
|
|
||
|
|
return $this->insert($params);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 编辑用户
|
||
|
|
* @param $id
|
||
|
|
* @param $params
|
||
|
|
* @return mixed
|
||
|
|
* @throws Exception
|
||
|
|
*/
|
||
|
|
public function update($id, $params): mixed
|
||
|
|
{
|
||
|
|
return $this->save($id, $params);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 删除用户
|
||
|
|
* @param $ids
|
||
|
|
* @return mixed|true
|
||
|
|
* @throws Exception
|
||
|
|
*/
|
||
|
|
public function delete($ids): mixed
|
||
|
|
{
|
||
|
|
return $this->del($ids);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 修改密码
|
||
|
|
* @param $password
|
||
|
|
* @param $newPassword
|
||
|
|
* @return mixed
|
||
|
|
* @throws Exception
|
||
|
|
*/
|
||
|
|
public function changePassword($password, $newPassword): mixed
|
||
|
|
{
|
||
|
|
$info = $this->model::find($this->userId);
|
||
|
|
if (empty($info)) {
|
||
|
|
return $this->utils->notFound('数据不存在');
|
||
|
|
}
|
||
|
|
if (!password_verify($password, $info['password'])) {
|
||
|
|
return $this->utils->errorThrow('密码错误');
|
||
|
|
}
|
||
|
|
if (password_verify($newPassword, $info['password'])) {
|
||
|
|
return $this->utils->errorThrow('新密码不能与旧密码相同');
|
||
|
|
}
|
||
|
|
$result = $this->model::where('id', $this->userId)->update([
|
||
|
|
'password' => password_hash($newPassword, PASSWORD_DEFAULT),
|
||
|
|
'last_reset_password_at' => get_time()
|
||
|
|
]);
|
||
|
|
if (!$result) {
|
||
|
|
return $this->utils->errorThrow('重置密码失败');
|
||
|
|
}
|
||
|
|
return $result;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 重置密码
|
||
|
|
* @param $id
|
||
|
|
* @param $password
|
||
|
|
* @param $newPassword
|
||
|
|
* @return mixed
|
||
|
|
* @throws Exception
|
||
|
|
*/
|
||
|
|
public function resetPassword($id, $password, $newPassword): mixed
|
||
|
|
{
|
||
|
|
if ($password !== $newPassword) {
|
||
|
|
return $this->utils->errorThrow('两次密码不一致');
|
||
|
|
}
|
||
|
|
$info = $this->model::find($id);
|
||
|
|
if (empty($info)) {
|
||
|
|
return $this->utils->notFound('数据不存在');
|
||
|
|
}
|
||
|
|
if (password_verify($newPassword, $info['password'])) {
|
||
|
|
return $this->utils->errorThrow('新密码不能与旧密码相同');
|
||
|
|
}
|
||
|
|
$result = $this->model::where('id', $id)->update([
|
||
|
|
'password' => password_hash($newPassword, PASSWORD_DEFAULT),
|
||
|
|
'last_reset_password_at' => get_time()
|
||
|
|
]);
|
||
|
|
if (!$result) {
|
||
|
|
return $this->utils->errorThrow('重置密码失败');
|
||
|
|
}
|
||
|
|
return $result;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 获取用户信息
|
||
|
|
* @return mixed
|
||
|
|
* @throws Exception
|
||
|
|
*/
|
||
|
|
public function myInfo(): mixed
|
||
|
|
{
|
||
|
|
$this->with = [
|
||
|
|
'roles'
|
||
|
|
];
|
||
|
|
$result = $this->getDetail($this->userId);
|
||
|
|
$result['created_day_at'] = format_time(strtotime($result['created_at']), 'Y-m-d');
|
||
|
|
$result['account'] = desensitization($result['account']);
|
||
|
|
return $result;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 用户统计
|
||
|
|
* @return array[]
|
||
|
|
*/
|
||
|
|
public function userStats()
|
||
|
|
{
|
||
|
|
$payProject = UserPayModel::where('user_id', $this->userId)->count();
|
||
|
|
$collection = CollectionModel::where('user_id', $this->userId)->count();
|
||
|
|
$role = $this->userInfo['role_name'];
|
||
|
|
return [
|
||
|
|
'pay_project' => [
|
||
|
|
'label' => '已购项目',
|
||
|
|
'value' => $payProject,
|
||
|
|
'icon' => 'shopping-bag'
|
||
|
|
],
|
||
|
|
'collection' => [
|
||
|
|
'label' => '我的收藏',
|
||
|
|
'value' => $collection,
|
||
|
|
'icon' => 'heart'
|
||
|
|
],
|
||
|
|
'role' => [
|
||
|
|
'label' => '用户类型',
|
||
|
|
'value' => $role,
|
||
|
|
'icon' => 'user'
|
||
|
|
],
|
||
|
|
];
|
||
|
|
}
|
||
|
|
}
|