136 lines
4.2 KiB
PHP
136 lines
4.2 KiB
PHP
<?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\admin\logic\user;
|
||
|
||
use app\common\enum\user\UserTerminalEnum;
|
||
use app\common\logic\BaseLogic;
|
||
use app\common\model\user\User;
|
||
use think\facade\Db;
|
||
use app\common\service\FileService;
|
||
use Webman\Config;
|
||
|
||
/**
|
||
* 用户逻辑层
|
||
* Class UserLogic
|
||
* @package app\admin\logic\user
|
||
*/
|
||
class UserLogic extends BaseLogic
|
||
{
|
||
/**
|
||
* @notes 添加用户列表
|
||
* @param array $params
|
||
* @return bool
|
||
* @author likeadmin
|
||
* @date 2024/04/25 10:20
|
||
*/
|
||
public static function add(array $params)
|
||
{
|
||
$passwordSalt = Config::get('project.unique_identification');
|
||
$password = create_password($params['password'], $passwordSalt);
|
||
$defaultAvatar = config('project.default_image.admin_avatar');
|
||
$avatar = !empty($params['avatar']) ? FileService::setFileUrl($params['avatar']) : $defaultAvatar;
|
||
|
||
Db::startTrans();
|
||
try {
|
||
$res=User::create([
|
||
'avatar' => $avatar,
|
||
'real_name' => $params['real_name'],
|
||
'nickname' => $params['nickname'],
|
||
'account' => $params['account'],
|
||
'password' => $password,
|
||
'mobile' => $params['mobile'],
|
||
'sex' => $params['sex'],
|
||
'is_disable' => $params['is_disable'],
|
||
'user_money' => $params['user_money']
|
||
]);
|
||
|
||
Db::commit();
|
||
return $res;
|
||
} catch (\Exception $e) {
|
||
Db::rollback();
|
||
self::setError($e->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
/**
|
||
* @notes 编辑用户列表
|
||
* @param array $params
|
||
* @return bool
|
||
* @author likeadmin
|
||
* @date 2024/04/25 10:20
|
||
*/
|
||
public static function edit(array $params): bool
|
||
{
|
||
Db::startTrans();
|
||
try {
|
||
User::where('id', $params['id'])->update([
|
||
'avatar' => $params['avatar'] ?? '',
|
||
'real_name' => $params['real_name'],
|
||
'nickname' => $params['nickname'],
|
||
'account' => $params['account'],
|
||
'password' => $params['password'] ?? '',
|
||
'mobile' => $params['mobile'] ?? '',
|
||
'sex' => $params['sex'] ?? 0,
|
||
'is_disable' => $params['is_disable'] ?? 0,
|
||
'user_money' => $params['user_money'] ?? 0
|
||
]);
|
||
|
||
Db::commit();
|
||
return true;
|
||
} catch (\Exception $e) {
|
||
Db::rollback();
|
||
self::setError($e->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @notes 用户详情
|
||
* @param int $userId
|
||
* @return array
|
||
* @author 乔峰
|
||
* @date 2022/9/22 16:32
|
||
*/
|
||
public static function detail(int $userId): array
|
||
{
|
||
$field = [
|
||
'id', 'account', 'nickname', 'avatar', 'real_name',
|
||
'sex', 'mobile', 'create_time', 'login_time', 'channel'
|
||
];
|
||
|
||
$user = User::where(['id' => $userId])->field($field)
|
||
->findOrEmpty();
|
||
|
||
$user['channel'] = UserTerminalEnum::getTermInalDesc($user['channel']);
|
||
$user->sex = $user->getData('sex');
|
||
return $user->toArray();
|
||
}
|
||
|
||
|
||
/**
|
||
* @notes 更新用户信息
|
||
* @param array $params
|
||
* @return User
|
||
* @author 乔峰
|
||
* @date 2022/9/22 16:38
|
||
*/
|
||
public static function setUserInfo(array $params)
|
||
{
|
||
return User::update([
|
||
'id' => $params['id'],
|
||
$params['field'] => $params['value']
|
||
]);
|
||
}
|
||
}
|