Merge branch 'develop' of https://e.coding.net/g-xtcy5189/cunkebao/cunkebao_v3 into develop
This commit is contained in:
@@ -111,6 +111,24 @@ class ContentLibraryController extends Controller
|
||||
$library['timeEnd'] = date('Y-m-d', $library['timeEnd']);
|
||||
}
|
||||
|
||||
// 获取好友详细信息
|
||||
if (!empty($library['sourceFriends'])) {
|
||||
$friendIds = $library['sourceFriends'];
|
||||
$friendsInfo = [];
|
||||
|
||||
if (!empty($friendIds)) {
|
||||
// 查询好友信息,使用wechat_account表
|
||||
$friendsInfo = Db::name('wechat_account')
|
||||
->field('wechatId, nickname, avatar')
|
||||
->whereIn('wechatId', $friendIds)
|
||||
->select();
|
||||
}
|
||||
|
||||
// 将好友信息添加到返回数据中
|
||||
$library['selectedFriends'] = $friendsInfo;
|
||||
}
|
||||
|
||||
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
|
||||
@@ -7,6 +7,11 @@ Route::post('auth/login', 'app\superadmin\controller\auth\AuthLoginController@in
|
||||
|
||||
// 需要登录认证的路由组
|
||||
Route::group('', function () {
|
||||
// 仪表盘概述
|
||||
Route::group('dashboard', function () {
|
||||
Route::get('base', 'app\superadmin\controller\dashboard\GetBasestatisticsController@index');
|
||||
});
|
||||
|
||||
// 菜单管理相关路由
|
||||
Route::group('menu', function () {
|
||||
Route::get('tree', 'app\superadmin\controller\MenuController@getMenuTree');
|
||||
@@ -28,14 +33,16 @@ Route::group('', function () {
|
||||
|
||||
// 客户池管理路由
|
||||
Route::group('trafficPool', function () {
|
||||
Route::get('list', 'app\superadmin\controller\TrafficPoolController@getList'); // 获取客户池列表
|
||||
Route::get('detail', 'app\superadmin\controller\TrafficPoolController@getDetail'); // 获取客户详情
|
||||
Route::get('list', 'app\superadmin\controller\TrafficPoolController@getList');
|
||||
Route::get('detail', 'app\superadmin\controller\TrafficPoolController@getDetail');
|
||||
});
|
||||
|
||||
// 公司路由
|
||||
Route::group('company', function () {
|
||||
Route::post('create', 'app\superadmin\controller\company\CreateCompanyController@index');
|
||||
Route::post('update', 'app\superadmin\controller\company\UpdateCompanyController@index');
|
||||
Route::post('delete', 'app\superadmin\controller\company\DeleteCompanyController@index');
|
||||
Route::get('list', 'app\superadmin\controller\company\GetCompanyListController@index');
|
||||
Route::get('detail/:id', 'app\superadmin\controller\company\GetCompanyDetailForUpdateController@index');
|
||||
});
|
||||
})->middleware(['app\superadmin\middleware\AdminAuth']);
|
||||
})->middleware(['app\superadmin\middleware\AdminAuth']);
|
||||
@@ -1,327 +0,0 @@
|
||||
<?php
|
||||
namespace app\superadmin\controller;
|
||||
|
||||
use app\library\s2\CurlHandle;
|
||||
use app\superadmin\model\Company as companyModel;
|
||||
use app\superadmin\model\Users;
|
||||
use GuzzleHttp\Client;
|
||||
use think\Controller;
|
||||
use think\Db;
|
||||
use think\facade\Config;
|
||||
use think\facade\Request;
|
||||
use think\facade\Session;
|
||||
|
||||
/**
|
||||
* 公司控制器
|
||||
*/
|
||||
class CompanyController extends Controller
|
||||
{
|
||||
/**
|
||||
* 创建新项目
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
// 获取参数
|
||||
$params = Request::only(['name', 'nickname', 'account', 'password', 'realName', 'description']);
|
||||
|
||||
try {
|
||||
// 开启事务
|
||||
Db::startTrans();
|
||||
$curl = CurlHandle::getInstant()->setBaseUrl('http://yishi.com/');
|
||||
|
||||
// 1. 调用创建部门接口
|
||||
$departmentResponse = $curl->setMethod('post')->send('v1/api/account/department/create', [
|
||||
'name' => $params['name'],
|
||||
'memo' => $params['description'] ?: '',
|
||||
]);
|
||||
|
||||
$departmentData = json_decode($departmentResponse, true);
|
||||
if ($departmentData['code'] != 200) {
|
||||
throw new \Exception($departmentData['msg']);
|
||||
}
|
||||
|
||||
// 2. 调用创建账号接口
|
||||
$accountResponse = $curl->setMethod('post')->send('v1/api/account/create', [
|
||||
'userName' => $params['account'],
|
||||
'password' => $params['password'],
|
||||
'realName' => $params['realName'],
|
||||
'nickname' => $params['nickname'],
|
||||
'departmentId' => $departmentData['data']['id']
|
||||
]);
|
||||
|
||||
$accountData = json_decode($accountResponse, true);
|
||||
if ($accountData['code'] != 200) {
|
||||
throw new \Exception($accountData['msg']);
|
||||
}
|
||||
|
||||
// 3. 插入公司表
|
||||
$companyData = [
|
||||
'companyId' => $departmentData['data']['id'],
|
||||
'name' => $departmentData['data']['name'],
|
||||
'mome' => $departmentData['data']['memo']
|
||||
];
|
||||
|
||||
if (!companyModel::create($companyData)) {
|
||||
throw new \Exception('创建公司记录失败');
|
||||
}
|
||||
|
||||
// 4. 插入用户表
|
||||
$userData = [
|
||||
'account' => $params['account'],
|
||||
'passwordMd5' => md5($params['password']),
|
||||
'passwordLocal' => $params['password'],
|
||||
'companyId' => $departmentData['data']['id']
|
||||
];
|
||||
|
||||
if (!Users::create($userData)) {
|
||||
throw new \Exception('创建用户记录失败');
|
||||
}
|
||||
|
||||
// 提交事务
|
||||
Db::commit();
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '创建成功',
|
||||
'data' => [
|
||||
'companyId' => $departmentData['data']['id'],
|
||||
'name' => $departmentData['data']['name'],
|
||||
'memo' => $departmentData['data']['memo']
|
||||
]
|
||||
]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
// 回滚事务
|
||||
Db::rollback();
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => '创建失败:' . $e->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取项目列表
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function getList()
|
||||
{
|
||||
// 获取分页参数
|
||||
$page = $this->request->param('page/d', 1);
|
||||
$limit = $this->request->param('limit/d', 10);
|
||||
$keyword = $this->request->param('keyword/s', '');
|
||||
|
||||
// 构建查询条件
|
||||
$where = [];
|
||||
if (!empty($keyword)) {
|
||||
$where[] = ['name', 'like', "%{$keyword}%"];
|
||||
}
|
||||
|
||||
// 查询项目数据
|
||||
$total = companyModel::where($where)->count();
|
||||
$list = companyModel::where($where)
|
||||
->field('id, name, status, tenantId, companyId, memo, createTime')
|
||||
->order('id', 'desc')
|
||||
->page($page, $limit)
|
||||
->select();
|
||||
|
||||
// 获取每个项目的子账号数量
|
||||
$data = [];
|
||||
foreach ($list as $item) {
|
||||
// 查询该项目下的子账号数量
|
||||
$userCount = Users::where('companyId', $item['companyId'])
|
||||
->where('deleteTime', 0)
|
||||
->count();
|
||||
|
||||
$data[] = [
|
||||
'id' => $item['id'],
|
||||
'name' => $item['name'],
|
||||
'status' => $item['status'],
|
||||
'tenantId' => $item['tenantId'],
|
||||
'companyId' => $item['companyId'],
|
||||
'memo' => $item['memo'],
|
||||
'userCount' => $userCount,
|
||||
'createTime' => date('Y-m-d H:i:s', $item['createTime'])
|
||||
];
|
||||
}
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '获取成功',
|
||||
'data' => [
|
||||
'list' => $data,
|
||||
'total' => $total,
|
||||
'page' => $page,
|
||||
'limit' => $limit
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取项目详情
|
||||
* @param int $id 项目ID
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function getDetail($id)
|
||||
{
|
||||
$company = companyModel::get($id);
|
||||
if (!$company) {
|
||||
return json(['code' => 404, 'msg' => '项目不存在']);
|
||||
}
|
||||
|
||||
// 获取项目下的子账号数量
|
||||
$userCount = Users::where('companyId', $id)
|
||||
->where('deleteTime', 0)
|
||||
->count();
|
||||
|
||||
$data = [
|
||||
'id' => $company->id,
|
||||
'name' => $company->name,
|
||||
'status' => $company->status,
|
||||
'tenantId' => $company->tenantId,
|
||||
'companyId' => $company->companyId,
|
||||
'memo' => $company->memo,
|
||||
'userCount' => $userCount,
|
||||
'createTime' => date('Y-m-d H:i:s', $company->createTime)
|
||||
];
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '获取成功',
|
||||
'data' => $data
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新项目信息
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function update()
|
||||
{
|
||||
if (!$this->request->isPost()) {
|
||||
return json(['code' => 405, 'msg' => '请求方法不允许']);
|
||||
}
|
||||
|
||||
// 获取请求参数
|
||||
$id = $this->request->post('id/d', 0);
|
||||
$name = $this->request->post('name/s', '');
|
||||
$status = $this->request->post('status/d');
|
||||
$tenantId = $this->request->post('tenantId/d');
|
||||
$companyId = $this->request->post('companyId/d');
|
||||
$memo = $this->request->post('memo/s', '');
|
||||
|
||||
// 参数验证
|
||||
if (empty($id) || empty($name)) {
|
||||
return json(['code' => 400, 'msg' => '请填写必要参数']);
|
||||
}
|
||||
|
||||
// 查询项目
|
||||
$company = companyModel::get($id);
|
||||
if (!$company) {
|
||||
return json(['code' => 404, 'msg' => '项目不存在']);
|
||||
}
|
||||
|
||||
// 检查项目名称是否已存在(排除自身)
|
||||
$exists = companyModel::where('name', $name)
|
||||
->where('id', '<>', $id)
|
||||
->find();
|
||||
if ($exists) {
|
||||
return json(['code' => 400, 'msg' => '项目名称已存在']);
|
||||
}
|
||||
|
||||
// 更新数据
|
||||
$company->name = $name;
|
||||
if (isset($status)) $company->status = $status;
|
||||
if (isset($tenantId)) $company->tenantId = $tenantId;
|
||||
if (isset($companyId)) $company->companyId = $companyId;
|
||||
$company->memo = $memo;
|
||||
$company->updateTime = time();
|
||||
|
||||
if ($company->save()) {
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '更新成功'
|
||||
]);
|
||||
}
|
||||
|
||||
return json(['code' => 500, 'msg' => '更新失败']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除项目
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function delete()
|
||||
{
|
||||
if (!$this->request->isPost()) {
|
||||
return json(['code' => 405, 'msg' => '请求方法不允许']);
|
||||
}
|
||||
|
||||
$id = $this->request->post('id/d', 0);
|
||||
if (empty($id)) {
|
||||
return json(['code' => 400, 'msg' => '请指定要删除的项目']);
|
||||
}
|
||||
|
||||
// 查询项目
|
||||
$company = companyModel::get($id);
|
||||
if (!$company) {
|
||||
return json(['code' => 404, 'msg' => '项目不存在']);
|
||||
}
|
||||
|
||||
// 检查是否有关联的子账号
|
||||
$userCount = Users::where('companyId', $id)
|
||||
->where('deleteTime', 0)
|
||||
->count();
|
||||
if ($userCount > 0) {
|
||||
return json(['code' => 400, 'msg' => '该项目下还有关联的子账号,无法删除']);
|
||||
}
|
||||
|
||||
// 执行删除
|
||||
if ($company->delete()) {
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '删除成功'
|
||||
]);
|
||||
}
|
||||
|
||||
return json(['code' => 500, 'msg' => '删除失败']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新项目状态
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function updateStatus()
|
||||
{
|
||||
if (!$this->request->isPost()) {
|
||||
return json(['code' => 405, 'msg' => '请求方法不允许']);
|
||||
}
|
||||
|
||||
$id = $this->request->post('id/d', 0);
|
||||
$status = $this->request->post('status/d');
|
||||
|
||||
if (empty($id) || !isset($status)) {
|
||||
return json(['code' => 400, 'msg' => '参数不完整']);
|
||||
}
|
||||
|
||||
// 查询项目
|
||||
$company = companyModel::get($id);
|
||||
if (!$company) {
|
||||
return json(['code' => 404, 'msg' => '项目不存在']);
|
||||
}
|
||||
|
||||
// 更新状态
|
||||
$company->status = $status;
|
||||
$company->updateTime = time();
|
||||
|
||||
if ($company->save()) {
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '状态更新成功'
|
||||
]);
|
||||
}
|
||||
|
||||
return json(['code' => 500, 'msg' => '状态更新失败']);
|
||||
}
|
||||
}
|
||||
@@ -51,12 +51,12 @@ class UpdateAdministratorController extends BaseController
|
||||
'account' => 'require|/\S+/',
|
||||
'name' => 'require|/\S+/',
|
||||
'password' => '/\S+/',
|
||||
'permissionIds' => 'require|array',
|
||||
'permissionIds' => 'array',
|
||||
], [
|
||||
'id.require' => '缺少必要参数',
|
||||
'account.require' => '账号不能为空',
|
||||
'name.require' => '姓名不能为空',
|
||||
'permissionIds.require' => '请至少分配一种权限',
|
||||
'permissionIds.array' => '请至少分配一种权限',
|
||||
]);
|
||||
|
||||
if (!$validate->check($params)) {
|
||||
@@ -70,9 +70,10 @@ class UpdateAdministratorController extends BaseController
|
||||
* 判断是否有权限修改
|
||||
*
|
||||
* @param int $adminId
|
||||
* @param array $params
|
||||
* @return $this
|
||||
*/
|
||||
protected function checkPermission(int $adminId): self
|
||||
protected function checkPermission(int $adminId, array $params): self
|
||||
{
|
||||
$currentAdminId = $this->getAdminInfo('id');
|
||||
|
||||
@@ -80,6 +81,10 @@ class UpdateAdministratorController extends BaseController
|
||||
throw new \Exception('您没有权限修改其他管理员', 403);
|
||||
}
|
||||
|
||||
if ($params['id'] != 1 && empty($params['permissionIds'])) {
|
||||
throw new \Exception('请至少分配一种权限', 403);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -123,7 +128,7 @@ class UpdateAdministratorController extends BaseController
|
||||
// 被修改的管理员id
|
||||
$adminId = $params['id'] ?? 0;
|
||||
|
||||
$this->dataValidate($params)->checkPermission($adminId);
|
||||
$this->dataValidate($params)->checkPermission($adminId, $params);
|
||||
|
||||
Db::startTrans();
|
||||
|
||||
|
||||
@@ -106,7 +106,7 @@ class CreateCompanyController extends BaseController
|
||||
*/
|
||||
protected function ckbCreateCompany(array $params): void
|
||||
{
|
||||
$params = ArrHelper::getValue('companyId,name,memo,status', $params);
|
||||
$params = ArrHelper::getValue('companyId=id,companyId,name,memo,status', $params);
|
||||
$result = CompanyModel::create($params);
|
||||
|
||||
if (!$result) {
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
namespace app\superadmin\controller\company;
|
||||
|
||||
use app\common\model\Company as CompanyModel;
|
||||
use app\common\model\User as UserModel;
|
||||
use app\superadmin\controller\BaseController;
|
||||
use think\Db;
|
||||
use think\Validate;
|
||||
|
||||
/**
|
||||
* 公司控制器
|
||||
*/
|
||||
class DeleteCompanyController extends BaseController
|
||||
{
|
||||
/**
|
||||
* 数据验证
|
||||
*
|
||||
* @param array $params
|
||||
* @return $this
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function dataValidate(array $params): self
|
||||
{
|
||||
$validate = Validate::make([
|
||||
'id' => 'require|regex:/^[1-9]\d*$/',
|
||||
], [
|
||||
'id.regex' => '非法请求',
|
||||
'id.require' => '非法请求',
|
||||
]);
|
||||
|
||||
if (!$validate->check($params)) {
|
||||
throw new \Exception($validate->getError(), 400);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除项目
|
||||
*
|
||||
* @param int $id
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function deleteCompany(int $id): void
|
||||
{
|
||||
$company = CompanyModel::where('id', $id)->find();
|
||||
|
||||
if (!$company) {
|
||||
throw new \Exception('项目不存在', 404);
|
||||
}
|
||||
|
||||
if (!$company->delete()) {
|
||||
throw new \Exception('项目删除失败', 400);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除用户
|
||||
*
|
||||
* @param int $companId
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function deleteUsers(int $companId): void
|
||||
{
|
||||
$users = UserModel::where('companyId', $companId)->select();
|
||||
|
||||
foreach ($users as $user) {
|
||||
if (!$user->delete()) {
|
||||
throw new \Exception($user->username . ' 用户删除失败', 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除存客宝数据
|
||||
*
|
||||
* @param int $companId
|
||||
* @return self
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function delteCkbAbout(int $companId): self
|
||||
{
|
||||
// 1. 删除项目
|
||||
$this->deleteCompany($companId);
|
||||
|
||||
// 2. 删除用户
|
||||
$this->deleteUsers($companId);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除 s2 数据
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function deleteS2About()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除项目
|
||||
*
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
try {
|
||||
$params = $this->request->only('id');
|
||||
$companId = $params['id'];
|
||||
|
||||
$this->dataValidate($params);
|
||||
|
||||
Db::startTrans();
|
||||
$this->delteCkbAbout($companId)->deleteS2About($companId);
|
||||
Db::commit();
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '删除成功'
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
|
||||
return json([
|
||||
'code' => $e->getCode(),
|
||||
'msg' => $e->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -29,7 +29,7 @@ class GetCompanyDetailForUpdateController extends BaseController
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取下古墓详情
|
||||
* 获取项目详情
|
||||
*
|
||||
* @param int $id
|
||||
* @return CompanyModel
|
||||
|
||||
@@ -3,8 +3,10 @@
|
||||
namespace app\superadmin\controller\company;
|
||||
|
||||
use app\common\model\Company as CompanyModel;
|
||||
use app\common\model\Device as DeviceModel;
|
||||
use app\common\model\User as usersModel;
|
||||
use app\superadmin\controller\BaseController;
|
||||
use Eison\Utils\Helper\ArrHelper;
|
||||
|
||||
/**
|
||||
* 公司控制器
|
||||
@@ -29,6 +31,20 @@ class GetCompanyListController extends BaseController
|
||||
return array_merge($params, $where);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取设备统计
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getDevices()
|
||||
{
|
||||
$devices = DeviceModel::field('companyId, count(id) as numCount')->group('companyId')->select();
|
||||
$devices = $devices ? $devices->toArray() : array();
|
||||
|
||||
return ArrHelper::columnTokey('companyId', $devices);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取项目列表
|
||||
*
|
||||
@@ -71,15 +87,17 @@ class GetCompanyListController extends BaseController
|
||||
/**
|
||||
* 构建返回数据
|
||||
*
|
||||
* @param \think\Paginator $list
|
||||
* @param \think\Paginator $Companylist
|
||||
* @return array
|
||||
*/
|
||||
protected function makeReturnedResult(\think\Paginator $list): array
|
||||
protected function makeReturnedResult(\think\Paginator $Companylist): array
|
||||
{
|
||||
$result = [];
|
||||
$devices = $this->getDevices();
|
||||
|
||||
foreach ($list->items() as $item) {
|
||||
foreach ($Companylist->items() as $item) {
|
||||
$item->userCount = $this->countUserInCompany($item->companyId);
|
||||
$item->deviceCount = $devices[$item->companyId]['numCount'] ?? 0;
|
||||
|
||||
array_push($result, $item->toArray());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,217 @@
|
||||
<?php
|
||||
|
||||
namespace app\superadmin\controller\company;
|
||||
|
||||
use app\common\model\Company as CompanyModel;
|
||||
use app\common\model\User as UsersModel;
|
||||
use app\superadmin\controller\BaseController;
|
||||
use Eison\Utils\Helper\ArrHelper;
|
||||
use think\Db;
|
||||
use think\Validate;
|
||||
|
||||
/**
|
||||
* 公司控制器
|
||||
*/
|
||||
class UpdateCompanyController extends BaseController
|
||||
{
|
||||
/**
|
||||
* 通过id获取项目详情
|
||||
*
|
||||
* @return CompanyModel
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function getCompanyDetailById(): CompanyModel
|
||||
{
|
||||
$company = CompanyModel::find(
|
||||
$this->request->post('id/d', 0)
|
||||
);
|
||||
|
||||
if (!$company) {
|
||||
throw new \Exception('项目不存在', 404);
|
||||
}
|
||||
|
||||
// 外部使用
|
||||
$this->companyId = $company->id;
|
||||
|
||||
return $company;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过账号获取用户信息
|
||||
*
|
||||
* @return UsersModel
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function getUserDetailByCompanyId(): ?UsersModel
|
||||
{
|
||||
$user = UsersModel::where(['companyId' => $this->companyId])->find();
|
||||
|
||||
if (!$user) {
|
||||
throw new \Exception('用户不存在', 404);
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新项目信息
|
||||
*
|
||||
* @param array $params
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function updateCompany(array $params): void
|
||||
{
|
||||
$params = ArrHelper::getValue('name,status,memo', $params);
|
||||
$params = ArrHelper::rmValue($params);
|
||||
|
||||
$company = $this->getCompanyDetailById();
|
||||
if (!$company->save($params)) {
|
||||
throw new \Exception('项目更新失败', 403);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新账号信息
|
||||
*
|
||||
* @param array $params
|
||||
* @return void
|
||||
*/
|
||||
protected function updateUserAccount(array $params): void
|
||||
{
|
||||
$params = ArrHelper::getValue('username,account,password=passwordLocal,realName,status', $params);
|
||||
$params = ArrHelper::rmValue($params);
|
||||
|
||||
if (isset($params['passwordLocal'])) {
|
||||
$params['passwordMd5'] = md5($params['passwordLocal']);
|
||||
}
|
||||
|
||||
$user = $this->getUserDetailByCompanyId();
|
||||
if (!$user->save($params)) {
|
||||
throw new \Exception('用户账号更新失败', 403);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
* @return self
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function updateCkbAbout(array $params): self
|
||||
{
|
||||
// 1. 更新项目信息
|
||||
$this->updateCompany($params);
|
||||
|
||||
// 2. 更新账号信息
|
||||
$this->updateUserAccount($params);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
* @return self
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function updateS2About(array $params): self
|
||||
{
|
||||
// 1. 更新项目信息
|
||||
$this->updateCompany($params);
|
||||
|
||||
// 2. 更新账号信息
|
||||
$this->updateUserAccount($params);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查项目名称是否已存在(排除自身)
|
||||
*
|
||||
* @param array $where
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function checkCompanyNameAndAccountExists(array $where): void
|
||||
{
|
||||
extract($where);
|
||||
|
||||
// 项目名称尽量不重名
|
||||
$exists = CompanyModel::where(compact('name'))->where('id', '<>', $id)->count() > 0;
|
||||
if ($exists) {
|
||||
throw new \Exception('项目名称已存在', 403);
|
||||
}
|
||||
|
||||
// 账号尽量不重名
|
||||
// TODO(数据迁移时,存客宝,主账号先查询出id,通过id查询出S2的最新信息,然后更新。)
|
||||
$exists = UsersModel::where(compact('account'))->where('companyId', '<>', $id)->count() > 0;
|
||||
if ($exists) {
|
||||
throw new \Exception('用户账号已存在', 403);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据验证
|
||||
*
|
||||
* @param array $params
|
||||
* @return $this
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function dataValidate(array $params): self
|
||||
{
|
||||
$validate = Validate::make([
|
||||
'id' => 'require',
|
||||
'name' => 'require|max:50|/\S+/',
|
||||
'username' => 'require|max:20|/\S+/',
|
||||
'account' => 'require|regex:/^1[3-9]\d{9}$/',
|
||||
'status' => 'require|in:0,1',
|
||||
'realName' => 'require|/\S+/',
|
||||
], [
|
||||
'id.require' => '非法请求',
|
||||
'name.require' => '请输入项目名称',
|
||||
'username.require' => '请输入用户昵称',
|
||||
'account.require' => '请输入账号',
|
||||
'account.regex' => '账号为手机号',
|
||||
'status.require' => '缺少重要参数',
|
||||
'status.in' => '非法参数',
|
||||
'realName.require' => '请输入真实姓名',
|
||||
]);
|
||||
|
||||
if (!$validate->check($params)) {
|
||||
throw new \Exception($validate->getError(), 400);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新项目信息
|
||||
*
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
try {
|
||||
$params = $this->request->only(['id', 'name', 'status', 'username', 'account', 'password', 'realName', 'memo']);
|
||||
|
||||
// 数据验证
|
||||
$this->dataValidate($params);
|
||||
$this->checkCompanyNameAndAccountExists(ArrHelper::getValue('id,name,account', $params));
|
||||
|
||||
Db::startTrans();
|
||||
$this->updateCkbAbout($params)->updateS2About($params);
|
||||
Db::commit();
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '更新成功'
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
|
||||
return json([
|
||||
'code' => $e->getCode(),
|
||||
'msg' => $e->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace app\superadmin\controller\dashboard;
|
||||
|
||||
use app\common\model\Administrator as AdministratorModel;
|
||||
use app\common\model\Company as CompanyModel;
|
||||
use think\Controller;
|
||||
|
||||
/**
|
||||
* 仪表盘控制器
|
||||
*/
|
||||
class GetBasestatisticsController extends Controller
|
||||
{
|
||||
/**
|
||||
* 项目总数
|
||||
*
|
||||
* @return CompanyModel
|
||||
*/
|
||||
protected function getCompanyCount(): int
|
||||
{
|
||||
return CompanyModel::count('*');
|
||||
}
|
||||
|
||||
/**
|
||||
* 管理员数量
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
protected function getAdminCount(): int
|
||||
{
|
||||
return AdministratorModel::count('*');
|
||||
}
|
||||
|
||||
/**
|
||||
* 客户总数
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
protected function getCustomerCount(): int
|
||||
{
|
||||
return $this->getCompanyCount();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取基础统计信息
|
||||
*
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '获取成功',
|
||||
'data' => [
|
||||
'companyCount' => $this->getCompanyCount(),
|
||||
'adminCount' => $this->getAdminCount(),
|
||||
'customerCount' => $this->getCustomerCount(),
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user