Files
cunkebao_v3/Server/application/api/controller/AccountController.php

760 lines
28 KiB
PHP
Raw Normal View History

2025-03-18 14:56:14 +08:00
<?php
namespace app\api\controller;
2025-03-24 16:42:36 +08:00
use app\api\model\CompanyAccountModel;
2025-03-27 17:26:31 +08:00
use app\api\model\CompanyModel;
use Library\S2\Logics\AccountLogic;
2025-04-18 13:46:34 +08:00
use think\Db;
2025-03-18 14:56:14 +08:00
use think\facade\Request;
2025-04-09 16:15:53 +08:00
/**
* 账号管理控制器
* 包含账号管理和部门管理的相关功能
*/
2025-03-18 14:56:14 +08:00
class AccountController extends BaseController
{
2025-04-09 16:15:53 +08:00
/************************ 账号管理相关接口 ************************/
2025-04-18 10:38:50 +08:00
2025-03-18 14:56:14 +08:00
/**
* 获取公司账号列表
2025-04-09 16:15:53 +08:00
* @param string $pageIndex 页码
* @param string $pageSize 每页数量
2025-04-30 11:15:19 +08:00
* @param bool $isInner 是否为定时任务调用
2025-03-18 14:56:14 +08:00
* @return \think\response\Json
*/
2025-07-14 18:16:36 +08:00
public function getlist($data = [], $isInner = false)
2025-03-18 14:56:14 +08:00
{
2025-07-14 18:16:36 +08:00
$pageIndex = !empty($data['pageIndex']) ? $data['pageIndex'] : 0;
$pageSize = !empty($data['pageSize']) ? $data['pageSize'] : 20;
$showNormalAccount = !empty($data['showNormalAccount']) ? $data['showNormalAccount'] : '';
$keyword = !empty($data['keyword']) ? $data['keyword'] : '';
$departmentId = !empty($data['departmentId']) ? $data['departmentId'] : '';
2025-03-18 14:56:14 +08:00
// 获取授权token
2025-04-09 14:45:27 +08:00
$authorization = trim($this->request->header('authorization', $this->authorization));
2025-03-18 14:56:14 +08:00
if (empty($authorization)) {
2025-04-30 11:15:19 +08:00
if ($isInner) {
2025-04-18 10:38:50 +08:00
return json_encode(['code' => 500, 'msg' => '缺少授权信息']);
} else {
2025-04-09 14:45:27 +08:00
return errorJson('缺少授权信息');
}
2025-03-18 14:56:14 +08:00
}
try {
// 构建请求参数
$params = [
2025-07-14 18:16:36 +08:00
'showNormalAccount' => $showNormalAccount,
'keyword' => $keyword,
'departmentId' => $departmentId,
'pageIndex' => $pageIndex,
'pageSize' => $pageSize
2025-03-18 14:56:14 +08:00
];
// 设置请求头
$headerData = ['client:system'];
$header = setHeader($headerData, $authorization, 'plain');
// 发送请求获取公司账号列表
$result = requestCurl($this->baseUrl . 'api/Account/myTenantPageAccounts', $params, 'GET', $header);
$response = handleApiResponse($result);
2025-04-18 10:38:50 +08:00
2025-03-18 14:56:14 +08:00
// 保存数据到数据库
if (!empty($response['results'])) {
2025-10-14 17:03:05 +08:00
$this->saveAccount($response['results']);
2025-03-18 14:56:14 +08:00
}
2025-04-18 10:38:50 +08:00
2025-04-30 11:15:19 +08:00
if ($isInner) {
2025-04-18 10:38:50 +08:00
return json_encode(['code' => 200, 'msg' => '获取公司账号列表成功', 'data' => $response]);
} else {
2025-04-09 16:15:53 +08:00
return successJson($response);
2025-03-24 14:59:19 +08:00
}
} catch (\Exception $e) {
2025-04-30 11:15:19 +08:00
if ($isInner) {
2025-04-18 10:38:50 +08:00
return json_encode(['code' => 500, 'msg' => '获取公司账号列表失败:' . $e->getMessage()]);
} else {
2025-04-09 16:15:53 +08:00
return errorJson('获取公司账号列表失败:' . $e->getMessage());
}
2025-03-24 14:59:19 +08:00
}
}
/**
* 创建新账号
* @return \think\response\Json
*/
public function createAccount()
{
// 获取授权token
2025-04-09 14:45:27 +08:00
$authorization = trim($this->request->header('authorization', $this->authorization));
2025-03-24 14:59:19 +08:00
if (empty($authorization)) {
return errorJson('缺少授权信息');
}
try {
2025-04-09 16:15:53 +08:00
// 获取并验证请求参数
2025-03-24 14:59:19 +08:00
$userName = $this->request->param('userName', '');
$password = $this->request->param('password', '');
$realName = $this->request->param('realName', '');
$nickname = $this->request->param('nickname', '');
$memo = $this->request->param('memo', '');
2025-04-12 15:08:21 +08:00
$departmentId = $this->request->param('departmentId', 0);
2025-03-24 14:59:19 +08:00
2025-04-18 10:44:50 +08:00
2025-04-09 16:15:53 +08:00
// 参数验证
2025-03-24 14:59:19 +08:00
if (empty($userName)) {
return errorJson('用户名不能为空');
}
2025-04-15 17:08:52 +08:00
// if (!preg_match('/^[a-zA-Z][a-zA-Z0-9]{5,9}$/', $userName)) {
// return errorJson('用户名必须以字母开头只能包含字母和数字长度6-10位');
// }
2025-03-24 14:59:19 +08:00
if (empty($password)) {
return errorJson('密码不能为空');
}
2025-03-24 14:59:19 +08:00
if (empty($realName)) {
return errorJson('真实姓名不能为空');
}
2025-04-12 15:08:21 +08:00
if (empty($departmentId)) {
2025-03-27 09:52:27 +08:00
return errorJson('公司ID不能为空');
2025-03-24 14:59:19 +08:00
}
2025-04-18 10:44:50 +08:00
// 检查账号是否已存在
$existingAccount = CompanyAccountModel::where('userName', $userName)->find();
2025-04-18 13:46:34 +08:00
if (!empty($existingAccount)) {
return errorJson('账号已存在');
2025-04-18 10:44:50 +08:00
}
2025-03-24 14:59:19 +08:00
// 构建请求参数
$params = [
'userName' => $userName,
'password' => $password,
'realName' => $realName,
'nickname' => $nickname,
'memo' => $memo,
2025-04-12 15:08:21 +08:00
'departmentId' => $departmentId,
'departmentIdArr' => empty($departmentId) ? [914] : [914, $departmentId]
2025-03-24 14:59:19 +08:00
];
2025-04-09 16:15:53 +08:00
2025-03-24 14:59:19 +08:00
// 设置请求头
$headerData = ['client:system'];
$header = setHeader($headerData, $authorization, 'json');
// 发送请求创建账号
$result = requestCurl($this->baseUrl . 'api/account/newAccount', $params, 'POST', $header, 'json');
2025-04-18 10:38:50 +08:00
if (is_numeric($result)) {
2025-04-18 10:38:50 +08:00
$res = CompanyAccountModel::create([
'id' => $result,
'tenantId' => 242,
'userName' => $userName,
'realName' => $realName,
'nickname' => $nickname,
'passwordMd5' => md5($password),
'passwordLocal' => localEncrypt($password),
'memo' => $memo,
'accountType' => 11,
'departmentId' => $departmentId,
'createTime' => time(),
'privilegeIds' => json_encode([])
]);
return successJson($res);
2025-04-09 16:15:53 +08:00
} else {
2025-03-24 14:59:19 +08:00
return errorJson($result);
}
} catch (\Exception $e) {
return errorJson('创建账号失败:' . $e->getMessage());
}
}
/**
* 创建新账号(包含创建部门)
* @return \think\response\Json
*/
public function createNewAccount()
{
// 获取授权token
$authorization = trim($this->request->header('authorization', $this->authorization));
if (empty($authorization)) {
return errorJson('缺少授权信息');
}
2025-04-18 13:46:34 +08:00
DB::startTrans();
try {
// 获取参数
$departmentName = $this->request->param('departmentName', '');
$departmentMemo = $this->request->param('departmentMemo', '');
$accountName = $this->request->param('accountName', '');
$accountPassword = $this->request->param('accountPassword', '');
$accountRealName = $this->request->param('accountRealName', '');
$accountNickname = $this->request->param('accountNickname', '');
$accountMemo = $this->request->param('accountMemo', '');
// 验证参数
if (empty($departmentName)) {
return errorJson('部门名称不能为空');
}
if (empty($accountName)) {
return errorJson('账号名称不能为空');
}
if (empty($accountPassword)) {
return errorJson('账号密码不能为空');
}
// 检查部门是否已存在
$existingDepartment = CompanyModel::where('name', $departmentName)->find();
2025-04-18 13:46:34 +08:00
if (!empty($existingDepartment)) {
return errorJson('部门以存在');
}
// 检查账号是否已存在
$existingAccount = CompanyAccountModel::where('userName', $accountName)->find();
2025-04-18 13:46:34 +08:00
if (!empty($existingAccount)) {
return errorJson('账号已存在');
}
$headerData = ['client:system'];
$header = setHeader($headerData, $authorization, 'json');
2025-04-18 10:38:50 +08:00
// 1. 创建部门
2025-04-18 13:46:34 +08:00
$departmentParams = [
'name' => $departmentName,
'memo' => $departmentMemo,
'departmentIdArr' => [914],
'parentId' => 914
];
$departmentResult = requestCurl($this->baseUrl . 'api/Department/createDepartment', $departmentParams, 'POST', $header, 'json');
if (is_numeric($departmentResult)) {
// 保存部门到数据库
CompanyModel::create([
'id' => $departmentResult,
'name' => $departmentName,
'memo' => $departmentMemo,
2025-04-18 13:46:34 +08:00
'tenantId' => 242,
'isTop' => 0,
'level' => 1,
'parentId' => 914,
'privileges' => '',
'createTime' => time(),
'lastUpdateTime' => 0
]);
} else {
2025-04-18 13:46:34 +08:00
DB::rollback();
return errorJson('创建部门失败:' . $departmentResult);
}
2025-04-18 13:46:34 +08:00
// 2. 创建账号
$accountParams = [
'userName' => $accountName,
'password' => $accountPassword,
'realName' => $accountRealName,
'nickname' => $accountNickname,
'memo' => $accountMemo,
'departmentId' => $departmentResult,
'departmentIdArr' => [914, $departmentResult]
];
$accountResult = requestCurl($this->baseUrl . 'api/Account/newAccount', $accountParams, 'POST', $header, 'json');
if (is_numeric($accountResult)) {
$res = CompanyAccountModel::create([
'id' => $accountResult,
'tenantId' => 242,
2025-04-18 10:38:50 +08:00
'userName' => $accountName,
'realName' => $accountRealName,
'nickname' => $accountNickname,
2025-04-18 13:46:34 +08:00
'passwordMd5' => md5($accountPassword),
'passwordLocal' => localEncrypt($accountPassword),
2025-04-18 10:38:50 +08:00
'memo' => $accountMemo,
2025-04-18 13:46:34 +08:00
'accountType' => 11,
2025-04-18 10:38:50 +08:00
'departmentId' => $departmentResult,
2025-04-18 13:46:34 +08:00
'createTime' => time(),
'privilegeIds' => json_encode([])
]);
DB::commit();
return successJson($res, '账号创建成功');
2025-04-18 10:38:50 +08:00
} else {
2025-04-18 13:46:34 +08:00
// 如果创建账号失败,删除已创建的部门
2025-04-18 14:23:49 +08:00
$this->deleteDepartment($accountResult);
2025-04-18 13:46:34 +08:00
DB::rollback();
return errorJson('创建账号失败:' . $accountResult);
}
2025-04-18 13:46:34 +08:00
} catch (\Exception $e) {
2025-04-18 13:46:34 +08:00
DB::rollback();
return errorJson('创建账号失败:' . $e->getMessage());
}
}
2025-04-09 16:15:53 +08:00
/************************ 部门管理相关接口 ************************/
2025-03-27 17:26:31 +08:00
/**
* 获取部门列表
* @return \think\response\Json
*/
2025-04-30 11:15:19 +08:00
public function getDepartmentList($isInner = false)
2025-03-27 17:26:31 +08:00
{
// 获取授权token
2025-04-09 14:45:27 +08:00
$authorization = trim($this->request->header('authorization', $this->authorization));
2025-03-27 17:26:31 +08:00
if (empty($authorization)) {
2025-04-30 11:15:19 +08:00
if ($isInner) {
2025-04-18 10:38:50 +08:00
return json_encode(['code' => 500, 'msg' => '缺少授权信息']);
} else {
2025-04-12 15:08:21 +08:00
return errorJson('缺少授权信息');
}
2025-03-27 17:26:31 +08:00
}
try {
// 设置请求头
$headerData = ['client:system'];
$header = setHeader($headerData, $authorization, 'json');
// 发送请求获取部门列表
2025-04-09 16:15:53 +08:00
$url = $this->baseUrl . 'api/Department/fetchMyAndSubordinateDepartment';
2025-03-27 17:26:31 +08:00
$result = requestCurl($url, [], 'GET', $header, 'json');
2025-04-18 10:38:50 +08:00
2025-03-27 17:26:31 +08:00
// 处理返回结果
$response = handleApiResponse($result);
2025-04-18 10:38:50 +08:00
2025-03-27 17:26:31 +08:00
// 保存数据到数据库
if (!empty($response)) {
2025-04-18 14:23:49 +08:00
CompanyModel::where('1=1')->delete();
2025-04-09 16:15:53 +08:00
$this->processDepartments($response);
2025-03-27 17:26:31 +08:00
}
2025-04-18 10:38:50 +08:00
2025-04-30 11:15:19 +08:00
if ($isInner) {
2025-04-18 10:38:50 +08:00
return json_encode(['code' => 200, 'msg' => '获取部门列表成功', 'data' => $response]);
} else {
2025-04-12 15:08:21 +08:00
return successJson($response, '获取部门列表成功');
}
2025-03-27 17:26:31 +08:00
} catch (\Exception $e) {
2025-04-30 11:15:19 +08:00
if ($isInner) {
2025-04-18 10:38:50 +08:00
return json_encode(['code' => 500, 'msg' => '获取部门列表失败:' . $e->getMessage()]);
} else {
2025-04-12 15:08:21 +08:00
return errorJson('获取部门列表失败:' . $e->getMessage());
}
2025-03-27 17:26:31 +08:00
}
}
2025-04-09 16:15:53 +08:00
/**
* 创建部门
* @return \think\response\Json
*/
public function createDepartment()
{
// 获取授权token
$authorization = trim($this->request->header('authorization', $this->authorization));
if (empty($authorization)) {
return errorJson('缺少授权信息');
}
try {
// 获取并验证请求参数
$name = $this->request->param('name', '');
$memo = $this->request->param('memo', '');
if (empty($name)) {
return errorJson('请输入公司名称');
}
// 检查部门名称是否已存在
$departmentId = CompanyModel::where('name', $name)->find();
2025-04-09 16:15:53 +08:00
if (!empty($departmentId)) {
2025-04-18 13:46:34 +08:00
return errorJson('部门已存在');
2025-04-09 16:15:53 +08:00
}
// 构建请求参数
$params = [
'name' => $name,
'memo' => $memo,
'departmentIdArr' => [914],
'parentId' => 914
];
// 设置请求头
$headerData = ['client:system'];
$header = setHeader($headerData, $authorization, 'json');
// 发送请求创建部门
2025-04-18 10:38:50 +08:00
$result = requestCurl($this->baseUrl . 'api/Department/createDepartment', $params, 'POST', $header, 'json');
2025-04-09 16:15:53 +08:00
// 处理返回结果
if (is_numeric($result)) {
$res = CompanyModel::create([
'id' => $result,
'name' => $name,
'memo' => $memo,
'tenantId' => 242,
'isTop' => 0,
'level' => 1,
'parentId' => 914,
'privileges' => '',
'createTime' => time(),
'lastUpdateTime' => 0
2025-04-09 16:15:53 +08:00
]);
return successJson($res);
} else {
return errorJson($result);
}
} catch (\Exception $e) {
return errorJson('创建部门失败:' . $e->getMessage());
}
}
/**
* 修改部门信息
* @return \think\response\Json
*/
public function updateDepartment()
{
// 获取授权token
$authorization = trim($this->request->header('authorization', $this->authorization));
if (empty($authorization)) {
return errorJson('缺少授权信息');
}
try {
// 获取并验证请求参数
$id = $this->request->param('id', 0);
$name = $this->request->param('name', '');
$memo = $this->request->param('memo', '');
2025-04-18 10:38:50 +08:00
2025-04-09 16:15:53 +08:00
if (empty($id)) {
return errorJson('部门ID不能为空');
}
if (empty($name)) {
return errorJson('部门名称不能为空');
}
2025-04-18 10:38:50 +08:00
2025-04-09 16:15:53 +08:00
// 验证部门是否存在
$department = CompanyModel::where('id', $id)->find();
if (empty($department)) {
return errorJson('部门不存在');
}
2025-04-18 10:38:50 +08:00
2025-04-09 16:15:53 +08:00
// 构建请求参数
$departmentIdArr = $department->parentId == 914 ? [914] : [914, $department->parentId];
$params = [
'id' => $id,
'name' => $name,
'memo' => $memo,
'departmentIdArr' => $departmentIdArr,
'tenantId' => 242,
'createTime' => $department->createTime,
'isTop' => $department->isTop,
'level' => $department->level,
'parentId' => $department->parentId,
'lastUpdateTime' => $department->lastUpdateTime,
'privileges' => $department->privileges
];
// 设置请求头
$headerData = ['client:system'];
$header = setHeader($headerData, $authorization, 'json');
// 发送请求修改部门
$result = requestCurl($this->baseUrl . 'api/Department/department', $params, 'PUT', $header, 'json');
$response = handleApiResponse($result);
2025-04-18 10:38:50 +08:00
2025-04-09 16:15:53 +08:00
// 更新本地数据库
$department->name = $name;
$department->memo = $memo;
$department->save();
2025-04-18 10:38:50 +08:00
2025-04-09 16:15:53 +08:00
return successJson([], '部门修改成功');
} catch (\Exception $e) {
return errorJson('修改部门失败:' . $e->getMessage());
}
}
/**
* 删除部门
* @return \think\response\Json
*/
public function deleteDepartment($id = '')
2025-04-09 16:15:53 +08:00
{
// 获取授权token
$authorization = trim($this->request->header('authorization', $this->authorization));
if (empty($authorization)) {
return errorJson('缺少授权信息');
}
try {
// 获取并验证部门ID
$id = !empty($id) ? $id : $this->request->param('id', '');
2025-04-09 16:15:53 +08:00
if (empty($id)) {
return errorJson('部门ID不能为空');
}
// 验证部门是否存在
$department = CompanyModel::where('id', $id)->find();
if (empty($department)) {
return errorJson('部门不存在');
}
// 设置请求头
$headerData = ['client:system'];
2025-04-18 13:46:34 +08:00
$header = setHeader($headerData, $authorization, 'json');
2025-04-09 16:15:53 +08:00
// 发送删除请求
2025-04-18 14:23:49 +08:00
$result = requestCurl($this->baseUrl . 'api/Department/del/' . $id, [], 'DELETE', $header);
2025-04-18 10:38:50 +08:00
if ($result) {
2025-04-09 16:15:53 +08:00
return errorJson($result);
2025-04-18 10:38:50 +08:00
} else {
// 删除本地数据库记录
2025-04-09 16:15:53 +08:00
$department->delete();
return successJson([], '部门删除成功');
}
} catch (\Exception $e) {
return errorJson('删除部门失败:' . $e->getMessage());
}
}
/**
* 修改部门权限
* @return \think\response\Json
*/
2025-06-10 09:53:01 +08:00
public function setPrivileges($data = [])
{
// 获取授权token
$authorization = trim($this->request->header('authorization', $this->authorization));
if (empty($authorization)) {
return errorJson('缺少授权信息');
}
try {
// 获取并验证请求参数
2025-06-10 09:53:01 +08:00
$id = !empty($data['id']) ? $data['id'] : $this->request->param('id', 0);
if (empty($id)) {
return errorJson('部门ID不能为空');
}
2025-06-10 09:53:01 +08:00
$privilegeIds = !empty($data['privilegeIds']) ? $data['privilegeIds'] : '1001,1002,1004,1023,1406,20003,20021,20022,20023,20032,20041,20049,20054,20055,20060,20100,20102,20107';
$privilegeIds = explode(',',$privilegeIds);
// 验证部门是否存在
$department = CompanyModel::where('id', $id)->find();
if (empty($department)) {
return errorJson('部门不存在');
}
2025-06-10 09:53:01 +08:00
// 构建请求参数
$params = [
'departmentId' => $id,
2025-06-10 09:53:01 +08:00
'privilegeIds' => $privilegeIds,
'syncPrivilege' => true
];
// 设置请求头
$headerData = ['client:system'];
$header = setHeader($headerData, $authorization, 'json');
// 发送请求修改部门
$result = requestCurl($this->baseUrl . 'api/Department/privileges', $params, 'PUT', $header, 'json');
$response = handleApiResponse($result);
return successJson([], '部门权限修改成功');
} catch (\Exception $e) {
return errorJson('修改部门权限失败:' . $e->getMessage());
}
}
2025-08-05 10:26:55 +08:00
public function accountModify($data = [])
{
// 获取授权token
$authorization = $this->authorization;
if (empty($authorization)) {
return errorJson('缺少授权信息');
}
$id = !empty($data['id']) ? $data['id'] : '';
if (empty($id)) {
return errorJson('账号ID不能为空');
}
$account = CompanyAccountModel::where('id', $id)->find();
if (empty($account)) {
return errorJson('账号不存在');
}
$privilegeIds = json_decode($account->privilegeIds,true);
$privilegeIds = !empty($privilegeIds) ? $privilegeIds : [1001,1002,1004,1023,1406,20003,20021,20022,20023,20032,20041,20049,20054,20055,20060,20100,20102,20107,20055];
// 构建请求参数
$params = [
'accountType' => !empty($data['accountType']) ? $data['accountType'] : $account->accountType,
'alive' => !empty($data['alive']) ? $data['alive'] : $account->alive,
'avatar' => !empty($data['avatar']) ? $data['avatar'] : $account->avatar,
'createTime' => !empty($data['createTime']) ? $data['createTime'] : $account->createTime,
'creator' => !empty($data['creator']) ? $data['creator'] : $account->creator,
'creatorRealName' => !empty($data['creatorRealName']) ? $data['creatorRealName'] : $account->creatorRealName,
'creatorUserName' => !empty($data['creatorUserName']) ? $data['creatorUserName'] : $account->creatorUserName,
'departmentId' => !empty($data['departmentId']) ? $data['departmentId'] : $account->departmentId,
'departmentIdArr' => !empty($data['departmentIdArr']) ? $data['departmentIdArr'] : [914,$account->departmentId],
'departmentName' => !empty($data['departmentName']) ? $data['departmentName'] : $account->departmentName,
'hasXiakeAccount' => !empty($data['hasXiakeAccount']) ? $data['hasXiakeAccount'] : false,
'id' => !empty($data['id']) ? $data['id'] : $account->id,
'memo' => !empty($data['memo']) ? $data['memo'] : $account->memo,
'nickname' => !empty($data['nickname']) ? $data['nickname'] : $account->nickname,
'privilegeIds' => !empty($data['privilegeIds']) ? $data['privilegeIds'] : $privilegeIds,
'realName' => !empty($data['realName']) ? $data['realName'] : $account->realName,
'status' => !empty($data['status']) ? $data['status'] : $account->status,
'tenantId' => !empty($data['tenantId']) ? $data['tenantId'] : $account->tenantId,
'userName' => !empty($data['userName']) ? $data['userName'] : $account->userName,
];
// 设置请求头
$headerData = ['client:system'];
$header = setHeader($headerData, $authorization, 'json');
// 发送请求修改部门
$result = requestCurl($this->baseUrl . 'api/account/modify', $params, 'PUT', $header, 'json');
$response = handleApiResponse($result);
if(empty($response)){
$newData = [
'nickname' => $params['nickname'],
'avatar' => $params['avatar'],
];
CompanyAccountModel::where('id', $id)->update($newData);
return json_encode(['code' => 200, 'msg' => '账号修改成功']);
}else{
return json_encode(['code' => 500, 'msg' => $response]);
}
}
2025-04-09 16:15:53 +08:00
/************************ 私有辅助方法 ************************/
/**
* 递归处理部门列表
* @param array $departments 部门数据
*/
private function processDepartments($departments)
{
if (empty($departments) || !is_array($departments)) {
return;
}
2025-04-18 10:38:50 +08:00
2025-04-09 16:15:53 +08:00
foreach ($departments as $item) {
// 保存当前部门
$this->saveDepartment($item);
2025-04-18 10:38:50 +08:00
2025-04-09 16:15:53 +08:00
// 递归处理子部门
if (!empty($item['children']) && is_array($item['children'])) {
$this->processDepartments($item['children']);
}
}
}
/**
2025-03-27 17:26:31 +08:00
* 保存部门数据到数据库
* @param array $item 部门数据
*/
private function saveDepartment($item)
{
$data = [
'id' => isset($item['id']) ? $item['id'] : 0,
'name' => isset($item['name']) ? $item['name'] : '',
2025-04-09 16:15:53 +08:00
'memo' => isset($item['memo']) ? $item['memo'] : '',
'level' => isset($item['level']) ? $item['level'] : 0,
'isTop' => isset($item['isTop']) ? $item['isTop'] : false,
'parentId' => isset($item['parentId']) ? $item['parentId'] : 0,
'tenantId' => isset($item['tenantId']) ? $item['tenantId'] : 0,
'privileges' => isset($item['privileges']) ? (is_array($item['privileges']) ? json_encode($item['privileges']) : $item['privileges']) : '',
2025-04-18 10:38:50 +08:00
'createTime' => isset($item['createTime']) ? strtotime($item['createTime']) : 0,
'lastUpdateTime' => isset($item['lastUpdateTime']) ? ($item['lastUpdateTime'] == '0001-01-01T00:00:00' ? 0 : strtotime($item['lastUpdateTime'])) : 0
2025-03-27 17:26:31 +08:00
];
2025-04-09 16:15:53 +08:00
// 使用id作为唯一性判断
$department = CompanyModel::where('id', $item['id'])->find();
2025-03-27 17:26:31 +08:00
if ($department) {
$department->save($data);
} else {
CompanyModel::create($data);
}
}
2025-03-18 14:56:14 +08:00
/**
* 保存账号数据到数据库
* @param array $item 账号数据
*/
2025-10-14 17:03:05 +08:00
private function saveAccount($data)
2025-03-18 14:56:14 +08:00
{
// 将日期时间字符串转换为时间戳
$createTime = isset($item['createTime']) ? strtotime($item['createTime']) : null;
$deleteTime = isset($item['deleteTime']) ? strtotime($item['deleteTime']) : null;
2025-10-14 17:03:05 +08:00
$sqlData = [];
foreach ($data as $item) {
2025-10-15 16:39:10 +08:00
$rows[] = [
2025-10-14 17:03:05 +08:00
'id' => $item['id'],
'accountType' => isset($item['accountType']) ? $item['accountType'] : 0,
'status' => isset($item['status']) ? $item['status'] : 0,
'tenantId' => isset($item['tenantId']) ? $item['tenantId'] : 0,
'userName' => isset($item['userName']) ? $item['userName'] : '',
'realName' => isset($item['realName']) ? $item['realName'] : '',
'nickname' => isset($item['nickname']) ? $item['nickname'] : '',
'avatar' => isset($item['avatar']) ? $item['avatar'] : '',
'phone' => isset($item['phone']) ? $item['phone'] : '',
'memo' => isset($item['memo']) ? $item['memo'] : '',
'createTime' => $createTime,
'creator' => isset($item['creator']) ? $item['creator'] : 0,
'creatorUserName' => isset($item['creatorUserName']) ? $item['creatorUserName'] : '',
'creatorRealName' => isset($item['creatorRealName']) ? $item['creatorRealName'] : '',
'departmentId' => isset($item['departmentId']) ? $item['departmentId'] : 0,
'departmentName' => isset($item['departmentName']) ? $item['departmentName'] : '',
'privilegeIds' => isset($item['privilegeIds']) ? json_encode($item['privilegeIds']) : json_encode([]),
'alive' => isset($item['alive']) ? $item['alive'] : false,
'hasXiakeAccount' => isset($item['hasXiakeAccount']) ? $item['hasXiakeAccount'] : false,
'isDeleted' => isset($item['isDeleted']) ? $item['isDeleted'] : false,
'deleteTime' => $deleteTime
];
}
2025-03-18 14:56:14 +08:00
2025-10-15 16:39:10 +08:00
// 拆分插入/更新:按主键 id 判断
$ids = array_column($rows, 'id');
$existingIds = CompanyAccountModel::whereIn('id', $ids)->column('id');
$existSet = array_flip($existingIds);
2025-03-18 14:56:14 +08:00
2025-10-15 16:39:10 +08:00
$toInsert = [];
$toUpdate = [];
foreach ($rows as $r) {
if (isset($existSet[$r['id']])) $toUpdate[] = $r; else $toInsert[] = $r;
}
$changed = false;
if (!empty($toInsert)) {
$m = new CompanyAccountModel();
$m->insertAll($toInsert, true); // 允许外部主键
$changed = true;
}
if (!empty($toUpdate)) {
$m = new CompanyAccountModel();
$res = $m->saveAll($toUpdate);
if ($res) $changed = true;
}
return $changed;
2025-03-18 14:56:14 +08:00
}
}