110 lines
2.7 KiB
PHP
110 lines
2.7 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace app\common\logic;
|
||
|
|
|
||
|
|
use app\common\model\User;
|
||
|
|
use app\common\model\UserGroupMembers;
|
||
|
|
use app\common\traits\crud\CrudLogicTrait;
|
||
|
|
use support\exception\BusinessException;
|
||
|
|
use support\Request;
|
||
|
|
use app\common\model\UserRoles;
|
||
|
|
|
||
|
|
class UserLogic
|
||
|
|
{
|
||
|
|
use CrudLogicTrait;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 指定当前要操作的模型
|
||
|
|
*/
|
||
|
|
protected string $modelClass = User::class;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 用户信息
|
||
|
|
*/
|
||
|
|
public function userInfo(int $uid)
|
||
|
|
{
|
||
|
|
$user = User::with('reportToInfo')->find($uid);
|
||
|
|
|
||
|
|
if (!$user) {
|
||
|
|
throw new BusinessException('账户不存在', 404);
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($user->status != 1) {
|
||
|
|
throw new BusinessException('账号已禁用', 403);
|
||
|
|
}
|
||
|
|
|
||
|
|
return $user;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 用户信息
|
||
|
|
*/
|
||
|
|
public function edit(int $uid, array $data)
|
||
|
|
{
|
||
|
|
//
|
||
|
|
$user = User::find($uid);
|
||
|
|
|
||
|
|
if (!$user) {
|
||
|
|
throw new BusinessException('账户不存在', 404);
|
||
|
|
}
|
||
|
|
|
||
|
|
$user->fill($data);
|
||
|
|
$user->save();
|
||
|
|
|
||
|
|
// 绑定内勤 10.13 add
|
||
|
|
if (isset($data['office_clerk_id']) && $data['office_clerk_id'] > 0) {
|
||
|
|
$ug = UserGroupMembers::where('user_id', $uid)->where('status', 1)->first();
|
||
|
|
if ($ug) {
|
||
|
|
$ug->delete();
|
||
|
|
}
|
||
|
|
|
||
|
|
$group = UserGroupMembers::where('user_id', $data['office_clerk_id'])->where('is_leader', 1)->where('status', 1)->first();
|
||
|
|
if (!$group) {
|
||
|
|
throw new BusinessException('该内勤还未创建分组', 404);
|
||
|
|
}
|
||
|
|
|
||
|
|
UserGroupMembers::create([
|
||
|
|
'user_id' => $uid,
|
||
|
|
'group_id' => $group->group_id,
|
||
|
|
'is_leader' => 0,
|
||
|
|
'status' => 1
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
return $user;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 修改密码
|
||
|
|
*/
|
||
|
|
public function savePassword(int $uid, array $data)
|
||
|
|
{
|
||
|
|
$password = password_hash($data['password_new'], PASSWORD_DEFAULT);
|
||
|
|
$res = User::where('id',$uid)->update(['password' => $password]);
|
||
|
|
|
||
|
|
return $res;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 获取用户列表
|
||
|
|
*/
|
||
|
|
public function getList(Request $request)
|
||
|
|
{
|
||
|
|
$p = $request->all();
|
||
|
|
|
||
|
|
$user = User::where('status', 1);
|
||
|
|
if (isset($p['role_id']) && $p['role_id'] > 0) {
|
||
|
|
$userIds = UserRoles::where('role_id', $p['role_id'])->pluck('user_id');
|
||
|
|
$user->whereIn('id', $userIds);
|
||
|
|
}
|
||
|
|
$list = $user->orderBy('id', 'desc')->get();
|
||
|
|
$arr = [];
|
||
|
|
foreach ($list->toArray() as $item) {
|
||
|
|
$arr[] = [
|
||
|
|
'name' => $item['name'],
|
||
|
|
'id' => $item['id'],
|
||
|
|
];
|
||
|
|
}
|
||
|
|
return $arr;
|
||
|
|
}
|
||
|
|
}
|