From 42e189ac012d56849c5b995e57d6cf5f6a5d22b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9F=B3=E6=B8=85=E7=88=BD?= Date: Thu, 17 Apr 2025 14:34:31 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B6=85=E7=AE=A1=E5=90=8E=E5=8F=B0=20-=20?= =?UTF-8?q?=E6=96=B0=E5=BB=BA=E9=A1=B9=E7=9B=AE=E8=BF=94=E5=B7=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Server/application/common/model/Company.php | 18 ++ Server/application/cunkebao/config/route.php | 2 +- .../cunkebao/controller/Device.php | 94 +------- .../device/GetDeviceDetailV1Controller.php | 5 +- .../device/GetDeviceListV1Controller.php | 4 +- .../device/PostAddDeviceV1Controller.php | 132 +++++++++++ .../UpdateDeviceTaskConfigV1Controller.php | 9 +- .../application/superadmin/config/route.php | 2 +- .../superadmin/controller/BaseController.php | 45 ++++ .../company/CreateCompanyController.php | 206 ++++++++++++++++++ 10 files changed, 415 insertions(+), 102 deletions(-) create mode 100644 Server/application/common/model/Company.php create mode 100644 Server/application/cunkebao/controller/device/PostAddDeviceV1Controller.php create mode 100644 Server/application/superadmin/controller/BaseController.php create mode 100644 Server/application/superadmin/controller/company/CreateCompanyController.php diff --git a/Server/application/common/model/Company.php b/Server/application/common/model/Company.php new file mode 100644 index 00000000..c6c4ec64 --- /dev/null +++ b/Server/application/common/model/Company.php @@ -0,0 +1,18 @@ +userInfo; - - // 获取设备ID - $deviceId = $this->request->param('id/d'); - if (empty($deviceId)) { - return json([ - 'code' => 400, - 'msg' => '设备ID不能为空' - ]); - } - - // 检查用户是否有权限访问该设备 - if ($userInfo['isAdmin'] != 1) { - // 非管理员需要检查是否有权限访问该设备 - $hasPermission = \app\common\model\DeviceUser::checkUserDevicePermission( - $userInfo['id'], - $deviceId, - $userInfo['companyId'] - ); - - if (!$hasPermission) { - return json([ - 'code' => 403, - 'msg' => '您没有权限查看该设备' - ]); - } - } - - // 获取设备信息,确认设备存在 - $device = DeviceModel::where('id', $deviceId) - ->where('isDeleted', 0) - ->find(); - - if (!$device) { - return json([ - 'code' => 404, - 'msg' => '设备不存在或已删除' - ]); - } - - // 获取分页参数 - $page = (int)Request::param('page', 1); - $limit = (int)Request::param('limit', 10); - - // 查询设备操作记录,并关联用户表获取操作人信息 - $logs = Db::table('tk_device_handle_log') - ->alias('l') - ->join('tk_users u', 'l.userId = u.id', 'left') - ->where('l.imei', $device['imei']) - ->where('l.companyId', $userInfo['companyId']) - ->field([ - 'l.id', - 'l.content', - 'l.createTime', - 'u.username' - ]) - ->order('l.createTime desc') - ->paginate($limit, false, ['page' => $page]); - - // 格式化返回数据 - $items = []; - foreach ($logs as $log) { - $items[] = [ - 'id' => $log['id'], - 'content' => $log['content'], - 'username' => $log['username'] ? $log['username'] : '未知用户', - 'createTime' => $log['createTime'] - ]; - } - - return json([ - 'code' => 200, - 'msg' => '获取成功', - 'data' => [ - 'total' => $logs->total(), - 'list' => $items - ] - ]); - } catch (\Exception $e) { - return json([ - 'code' => 500, - 'msg' => '获取失败:' . $e->getMessage() - ]); - } - } + } \ No newline at end of file diff --git a/Server/application/cunkebao/controller/device/GetDeviceDetailV1Controller.php b/Server/application/cunkebao/controller/device/GetDeviceDetailV1Controller.php index f3104157..05bf1b93 100644 --- a/Server/application/cunkebao/controller/device/GetDeviceDetailV1Controller.php +++ b/Server/application/cunkebao/controller/device/GetDeviceDetailV1Controller.php @@ -2,13 +2,12 @@ namespace app\cunkebao\controller\device; -use app\common\model\DeviceUser as DeviceUserModel; use app\common\model\Device as DeviceModel; use app\common\model\DeviceTaskconf as DeviceTaskconfModel; +use app\common\model\DeviceUser as DeviceUserModel; use app\common\model\DeviceWechatLogin; use app\common\model\WechatFriend; use app\cunkebao\controller\BaseController; -use think\facade\Request; /** * 设备管理控制器 @@ -154,7 +153,7 @@ class GetDeviceDetailV1Controller extends BaseController { try { // 获取设备ID - $id = Request::param('id/d'); + $id = $this->request->param('id/d'); if ($this->getUserInfo('isAdmin') != 1) { $this->checkUserDevicePermission($id); diff --git a/Server/application/cunkebao/controller/device/GetDeviceListV1Controller.php b/Server/application/cunkebao/controller/device/GetDeviceListV1Controller.php index 235e9de8..33b14e1e 100644 --- a/Server/application/cunkebao/controller/device/GetDeviceListV1Controller.php +++ b/Server/application/cunkebao/controller/device/GetDeviceListV1Controller.php @@ -88,10 +88,10 @@ class GetDeviceListV1Controller extends BaseController /** * 统计微信好友 * - * @param object $list + * @param \think\Paginator $list * @return array */ - protected function countFriend(object $list): array + protected function countFriend(\think\Paginator $list): array { $result = []; diff --git a/Server/application/cunkebao/controller/device/PostAddDeviceV1Controller.php b/Server/application/cunkebao/controller/device/PostAddDeviceV1Controller.php new file mode 100644 index 00000000..9d808a0b --- /dev/null +++ b/Server/application/cunkebao/controller/device/PostAddDeviceV1Controller.php @@ -0,0 +1,132 @@ +request->param('imei')) { + $where = [ + 'imei' => $this->request->param('imei'), + 'companyId' => $this->getUserInfo('companyId'), + 'deleteTime' => 0 + ]; + + $exist = DeviceModel::where($where)->count() > 0; + + if ($exist) { + throw new \Exception('设备IMEI已存在', 400); + } + } else { + throw new \Exception('设备IMEI不能为空', 400); + } + } + + + protected function addDeviceToS2() + { + $curl = CurlHandle::getInstant(); + + // $curl->setMethod() + } + + /** + * 添加设备 + * + * @return void + */ + protected function addDevice() + { + + $id = DeviceModel::addDevice( + $this->request->post() + ); + } + + /** + * 添加设备操作记录 + * + * @param int $deviceId + * @return void + * @throws \Exception + */ + protected function addDeviceHandleLog(int $deviceId): void + { + DeviceHandleLogModel::addLog( + [ + 'deviceId' => $deviceId, + 'content' => '添加设备', + 'userId' => $this->getUserInfo('id'), + 'companyId' => $this->getUserInfo('companyId'), + ] + ); + } + + /** + * 数据验证 + * + * @return $this + * @throws \Exception + */ + protected function dataValidate(): self + { + $validate = Validate::make([ + 'imei' => 'require|length:32', + 'memo' => 'require|/\S+/' + ]); + + if (!$validate->check($this->request->post())) { + throw new \Exception($validate->getError(), 400); + } + + return $this; + } + + /** + * 添加设备 + * @return \think\response\Json + */ + public function index() + { + try { + $this->dataValidate()->checkDeviceIsExist(); + + Db::startTrans(); + + $deviceId = $this->addDevice(); + $this->addDeviceHandleLog($deviceId); + + Db::commit(); + + return json([ + 'code' => 200, + 'msg' => '添加成功' + ]); + } catch (\Exception $e) { + Db::rollback(); + + return json([ + 'code' => $e->getCode(), + 'msg' => $e->getMessage() + ]); + } + } +} \ No newline at end of file diff --git a/Server/application/cunkebao/controller/device/UpdateDeviceTaskConfigV1Controller.php b/Server/application/cunkebao/controller/device/UpdateDeviceTaskConfigV1Controller.php index ecf5db60..0b1fb4c7 100644 --- a/Server/application/cunkebao/controller/device/UpdateDeviceTaskConfigV1Controller.php +++ b/Server/application/cunkebao/controller/device/UpdateDeviceTaskConfigV1Controller.php @@ -67,12 +67,17 @@ class UpdateDeviceTaskConfigV1Controller extends BaseController protected function addHandleLog(int $deviceId): void { $data = $this->request->post(); + $content = null; if (isset($data['autoAddFriend']))/**/$content = $data['autoAddFriend'] ? '开启自动添加好友' : '关闭自动添加好友'; if (isset($data['autoReply']))/* */$content = $data['autoReply'] ? '开启自动回复' : '关闭自动回复'; if (isset($data['momentsSync']))/* */$content = $data['momentsSync'] ? '开启朋友圈同步' : '关闭朋友圈同步'; if (isset($data['aiChat']))/* */$content = $data['aiChat'] ? '开启AI会话' : '关闭AI会话'; + if (empty($content)) { + throw new \Exception('参数错误', '400'); + } + DeviceHandleLogModel::addLog( [ 'deviceId' => $deviceId, @@ -109,7 +114,7 @@ class UpdateDeviceTaskConfigV1Controller extends BaseController */ public function index() { - $id = Request::param('deviceId/d'); + $id = $this->request->param('deviceId/d'); $this->checkDeviceExists($id); @@ -120,8 +125,8 @@ class UpdateDeviceTaskConfigV1Controller extends BaseController try { Db::startTrans(); - $this->setTaskconf($id); $this->addHandleLog($id); + $this->setTaskconf($id); Db::commit(); diff --git a/Server/application/superadmin/config/route.php b/Server/application/superadmin/config/route.php index 24741243..8a409957 100644 --- a/Server/application/superadmin/config/route.php +++ b/Server/application/superadmin/config/route.php @@ -33,7 +33,7 @@ Route::group('', function () { // 公司路由 Route::group('company', function () { - Route::post('create', 'app\\superadmin\\controller\\CompanyController@create'); + Route::post('create', 'app\\superadmin\\controller\\company\\CreateCompanyController@index'); Route::get('list', 'app\\superadmin\\controller\\CompanyController@getList'); Route::get('detail/:id', 'app\\superadmin\\controller\\CompanyController@getDetail'); }); diff --git a/Server/application/superadmin/controller/BaseController.php b/Server/application/superadmin/controller/BaseController.php new file mode 100644 index 00000000..12698c53 --- /dev/null +++ b/Server/application/superadmin/controller/BaseController.php @@ -0,0 +1,45 @@ +request->userInfo; + + if (!$user) { + throw new \Exception('未授权访问,缺少有效的身份凭证', 401); + } + + return $column ? $user[$column] : $user; + } +} \ No newline at end of file diff --git a/Server/application/superadmin/controller/company/CreateCompanyController.php b/Server/application/superadmin/controller/company/CreateCompanyController.php new file mode 100644 index 00000000..39a96d09 --- /dev/null +++ b/Server/application/superadmin/controller/company/CreateCompanyController.php @@ -0,0 +1,206 @@ +setMethod('post')->setBaseUrl('http://yishi.com/'); + } + + /** + * S2 创建部门并返回id + * + * @param array $params + * @return array + */ + protected function s2CreateDepartment(array $params): array + { + $response = $this->getCurl()->setMethod('post')->send('v1/api/account/department/create', [ + 'name' => $params['name'], + 'memo' => $params['description'], + ]); + + $result = json_decode($response, true); + + if ($result['code'] != 200) { + throw new \Exception($result['msg'], '20011'); + } + + return $result['data']; + } + + /** + * S2 创建部门账号 + * + * @param array $params + * @param int $departmentId + * @return array + * @throws \Exception + */ + protected function s2CreateUserAccountWithinDepartment(array $params, int $departmentId): array + { + $response = $this->getCurl()->send('v1/api/account/create', [ + 'userName' => $params['account'], + 'password' => $params['password'], + 'realName' => $params['realName'], + 'nickname' => $params['nickname'], + 'departmentId' => $departmentId + ]); + + $result = json_decode($response, true); + + if ($result['code'] != 200) { + throw new \Exception($result['msg'], '20011'); + } + } + + /** + * 数据验证 + * + * @return $this + * @throws \Exception + */ + protected function dataValidate(): self + { + $validate = Validate::make([ + 'name' => 'require|max:50|/\S+/', + 'nickname' => 'require|max:20|/\S+/', + 'account' => 'require|regex:/^1[3-9]\d{9}$/', + 'password' => 'require|/\S+/', + 'realName' => 'require|/\S+/', + 'description' => 'require|/\S+/', + ]); + + if (!$validate->check($this->request->post())) { + throw new \Exception($validate->getError(), 400); + } + + return $this; + } + + /** + * S2 部分 + * + * @param array $params + * @return array + * @throws \Exception + */ + protected function creatS2About(array $params): array + { + // 1. 调用创建部门接口 + $department = $this->s2CreateDepartment($params); + + // 2. 调用创建账号接口 + $this->s2CreateUserAccountWithinDepartment($params, $department['id']); + + return $department; + } + + /** + * 存客宝创建项目 + * + * @param array $params + * @return array + * @throws \Exception + */ + protected function ckbCreateCompany(array $params): array + { + $result = CompanyModel::create( + [ + 'companyId' => $departmentData['id'], + 'name' => $departmentData['name'], + 'mome' => $departmentData['memo'] + ] + ); + + if (!$result) { + throw new \Exception('创建公司记录失败', 402); + } + } + + /** + * 存客宝创建账号 + * + * @param array $params + * @return array + * @throws \Exception + */ + protected function ckbCreateUser(array $params): array + { + $result = UsersModel::create( + [ + 'account' => $params['account'], + 'passwordMd5' => md5($params['password']), + 'passwordLocal' => $params['password'], + 'companyId' => $departmentData['data']['id'] + ] + ); + + if (!$result) { + throw new \Exception('创建用户记录失败', 402); + } + } + + /** + * @param array $params + * @return array + * @throws \Exception + */ + protected function createCkbAbout(array $params): array + { + // 1. 存客宝创建项目 + $this->ckbCreateCompany($params); + + // 2. 存客宝创建操盘手总账号 + $this->ckbCreateUser(); + } + + /** + * 创建新项目 + * @return \think\response\Json + */ + public function index() + { + try { + $params = $this->request->only(['name', 'nickname', 'account', 'password', 'realName', 'description']); + + var_dump($params); + die; + $department = $this->dataValidate($params)->creatS2About($params); + + Db::startTrans(); + $this->createCkbAbout($department); + Db::commit(); + + return json([ + 'code' => 200, + 'msg' => '创建成功' + ]); + + } catch (\Exception $e) { + Db::rollback(); + + return json([ + 'code' => $e->getCode(), + 'msg' => $e->getMessage() + ]); + } + } +} \ No newline at end of file