From 3912a4c489603df4a977d207f2168d2cae35cc56 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 10:15:26 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=93=8D=E7=9B=98=E6=89=8B=E7=AB=AF=20-=20?= =?UTF-8?q?=E8=AE=BE=E5=A4=87=E5=85=B3=E8=81=94=E8=B4=A6=E5=8F=B7=E8=BF=94?= =?UTF-8?q?=E5=B7=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Server/application/cunkebao/config/route.php | 2 +- .../cunkebao/controller/BaseController.php | 8 +- .../device/GetDeviceListV1Controller.php | 9 +- .../device/GetRelatedAccountsV1Controller.php | 173 ++++++++++++++++++ .../cunkebao/model/DeviceWechatLogin.php | 28 +-- 5 files changed, 183 insertions(+), 37 deletions(-) create mode 100644 Server/application/cunkebao/controller/device/GetRelatedAccountsV1Controller.php diff --git a/Server/application/cunkebao/config/route.php b/Server/application/cunkebao/config/route.php index 59ef114d9..7444d1fd8 100644 --- a/Server/application/cunkebao/config/route.php +++ b/Server/application/cunkebao/config/route.php @@ -10,7 +10,7 @@ Route::group('v1/', function () { // 设备管理相关 Route::group('devices', function () { - Route::get(':id/related-accounts', 'app\\cunkebao\\controller\\Device@getRelatedAccounts'); // 设备关联微信账号路由 + Route::get(':id/related-accounts', 'app\\cunkebao\\controller\\device\\GetRelatedAccountsV1Controller@index'); // 设备关联微信账号路由 Route::get(':id/handle-logs', 'app\\cunkebao\\controller\\Device@handleLogs'); // 获取设备操作记录 Route::get('', 'app\\cunkebao\\controller\\device\\GetDeviceListV1Controller@index'); // 获取设备列表 Route::get('count', 'app\\cunkebao\\controller\\Device@count'); // 获取设备总数 diff --git a/Server/application/cunkebao/controller/BaseController.php b/Server/application/cunkebao/controller/BaseController.php index 59aa8f585..1189cafa6 100644 --- a/Server/application/cunkebao/controller/BaseController.php +++ b/Server/application/cunkebao/controller/BaseController.php @@ -23,8 +23,6 @@ class BaseController extends Controller parent::initialize(); date_default_timezone_set('Asia/Shanghai'); - - $this->user = request()->userInfo; } /** @@ -36,10 +34,12 @@ class BaseController extends Controller */ protected function getUserInfo(string $column = '') { - if (!$this->user) { + $user = $this->request->userInfo; + + if (!$user) { throw new \Exception('未授权访问,缺少有效的身份凭证', 401); } - return $column ? $this->user[$column] : $this->user; + return $column ? $user[$column] : $user; } } \ No newline at end of file diff --git a/Server/application/cunkebao/controller/device/GetDeviceListV1Controller.php b/Server/application/cunkebao/controller/device/GetDeviceListV1Controller.php index dd87b040a..b3d47db7a 100644 --- a/Server/application/cunkebao/controller/device/GetDeviceListV1Controller.php +++ b/Server/application/cunkebao/controller/device/GetDeviceListV1Controller.php @@ -5,7 +5,6 @@ use app\common\model\Device as DeviceModel; use app\common\model\DeviceUser as DeviceUserModel; use app\common\model\WechatFriend; use app\cunkebao\controller\BaseController; -use think\facade\Request; /** * 设备管理控制器 @@ -23,12 +22,12 @@ class GetDeviceListV1Controller extends BaseController $where = []; // 关键词搜索(同时搜索IMEI和备注) - if (!empty($keyword = Request::param('keyword'))) { + if (!empty($keyword = $this->request->param('keyword'))) { $where[] = ['exp', "d.imei LIKE '%{$keyword}%' OR d.memo LIKE '%{$keyword}%'"]; } // 设备在线状态 - if (is_numeric($alive = Request::param('alive'))) { + if (is_numeric($alive = $this->request->param('alive'))) { $where['d.alive'] = $alive; } @@ -116,8 +115,8 @@ class GetDeviceListV1Controller extends BaseController public function index() { try { - $page = (int)Request::param('page', 1); - $limit = (int)Request::param('limit', 10); + $page = (int)$this->request->param('page', 1); + $limit = (int)$this->request->param('limit', 10); if ($this->getUserInfo('isAdmin') == 1) { $where = $this->makeWhere(); diff --git a/Server/application/cunkebao/controller/device/GetRelatedAccountsV1Controller.php b/Server/application/cunkebao/controller/device/GetRelatedAccountsV1Controller.php new file mode 100644 index 000000000..2891db7ee --- /dev/null +++ b/Server/application/cunkebao/controller/device/GetRelatedAccountsV1Controller.php @@ -0,0 +1,173 @@ + $deviceId, + 'userId' => $this->getUserInfo('id'), + 'companyId' => $this->getUserInfo('companyId') + ]; + + $hasPermission = DeviceUserModel::where($where)->count() > 0; + + if (!$hasPermission) { + throw new \Exception('您没有权限查看该设备', '403'); + } + } + + /** + * 查询设备关联的微信ID列表 + * + * @param int $deviceId 设备ID + * @return array 微信ID列表 + */ + protected function getDeviceWechatIds(int $deviceId): array + { + $where = [ + 'deviceId' => $deviceId, + 'companyId' => $this->getUserInfo('companyId') + ]; + + return DeviceWechatLoginModel::where($where)->group('wechatId')->column('wechatId'); + } + + /** + * 通过设备关联的微信号列表获取微信账号 + * + * @param array $wechatIds + * @return array + */ + protected function getWechatAccountsByIds(array $wechatIds): array + { + return (array)WechatAccountModel::alias('a') + ->field([ + 'a.wechatId', 'a.nickname', 'a.avatar', 'a.gender', 'a.createTime' + ]) + ->whereIn('a.wechatId', $wechatIds) + ->select() + ->toArray(); + } + + /** + * 通过微信id获取微信最后活跃时间 + * + * @param int $wechatId + * @return string + */ + protected function getWechatLastActiveTime($wechatId): string + { + return date('Y-m-d H:i:s', $wechatId ?: time()); + } + + /** + * 加友状态 + * + * @param string $wechatId + * @return string + */ + protected function getWechatStatusText(string $wechatId): string + { + return 1 ? '可加友' : '已停用'; + } + + /** + * 账号状态 + * + * @param string $wechatId + * @return string + */ + protected function getWechatAliveText(string $wechatId): string + { + return 1 ? '正常' : '异常'; + } + + /** + * 统计微信好友 + * + * @param string $wechatId + * @return int + */ + protected function countFriend(string $wechatId): int + { + return WechatFriend::where(['ownerWechatId' => $wechatId])->count(); + } + + /** + * 获取设备关联的微信账号信息 + * + * @param int $deviceId 设备ID + * @return array 微信账号信息列表 + */ + protected function getDeviceRelatedAccounts(int $deviceId) + { + // 获取设备关联的微信ID列表 + $wechatIds = $this->getDeviceWechatIds($deviceId); + + if (!empty($wechatIds)) { + $results = $this->getWechatAccountsByIds($wechatIds); + + foreach ($results as &$account) { + $account['lastActive'] = $this->getWechatLastActiveTime($account['createTime']); + $account['statusText'] = $this->getWechatStatusText($account['wechatId']); + $account['totalFriend'] = $this->countFriend($account['wechatId']); + $account['wechatAliveText'] = $this->getWechatAliveText($account['wechatId']); + } + + return $results; + } + + return []; + } + + /** + * 获取设备关联的微信账号 + * @return \think\response\Json + */ + public function index() + { + try { + $deviceId = $this->request->param('id/d'); + + if ($this->getUserInfo('isAdmin') != 1) { + $this->checkUserDevicePermission($deviceId); + } + + // 获取设备关联的微信账号 + $wechatAccounts = $this->getDeviceRelatedAccounts($deviceId); + + return json([ + 'code' => 200, + 'msg' => '获取成功', + 'data' => [ + 'deviceId' => $deviceId, + 'accounts' => $wechatAccounts, + 'total' => count($wechatAccounts) + ] + ]); + } catch (\Exception $e) { + return json([ + 'code' => $e->getCode(), + 'msg' => $e->getMessage() + ]); + } + } +} \ No newline at end of file diff --git a/Server/application/cunkebao/model/DeviceWechatLogin.php b/Server/application/cunkebao/model/DeviceWechatLogin.php index 602edb35d..206cbbd48 100644 --- a/Server/application/cunkebao/model/DeviceWechatLogin.php +++ b/Server/application/cunkebao/model/DeviceWechatLogin.php @@ -11,33 +11,7 @@ class DeviceWechatLogin extends Model // 设置表名 protected $name = 'device_wechat_login'; - /** - * 查询设备关联的微信ID列表 - * @param int $deviceId 设备ID - * @param int $companyId 公司/租户ID - * @return array 微信ID列表 - */ - public static function getDeviceWechatIds($deviceId, $companyId = null) - { - $query = self::where('deviceId', $deviceId); - - // 如果提供了公司ID,则添加对应的条件 - if ($companyId !== null) { - $query->where('companyId', $companyId); - } - - // 提取微信ID - $records = $query->select(); - $wechatIds = []; - - foreach ($records as $record) { - if (!empty($record['wechatId'])) { - $wechatIds[] = $record['wechatId']; - } - } - - return $wechatIds; - } + /** * 根据微信ID查询关联的设备 From b18c70b7c6758240eabf653e3247fd6dbe2a13dc 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 10:39:18 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=93=8D=E7=9B=98=E6=89=8B=E7=AB=AF=20-=20?= =?UTF-8?q?=E8=AE=BE=E5=A4=87=E6=93=8D=E4=BD=9C=E8=AE=B0=E5=BD=95=E8=BF=94?= =?UTF-8?q?=E5=B7=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Server/application/cunkebao/config/route.php | 11 ++- .../cunkebao/controller/Device.php | 74 +--------------- .../GetDeviceHandleLogsV1Controller.php | 87 +++++++++++++++++++ .../device/GetDeviceListV1Controller.php | 11 +-- 4 files changed, 98 insertions(+), 85 deletions(-) create mode 100644 Server/application/cunkebao/controller/device/GetDeviceHandleLogsV1Controller.php diff --git a/Server/application/cunkebao/config/route.php b/Server/application/cunkebao/config/route.php index 7444d1fd8..1c7986d60 100644 --- a/Server/application/cunkebao/config/route.php +++ b/Server/application/cunkebao/config/route.php @@ -11,14 +11,13 @@ Route::group('v1/', function () { // 设备管理相关 Route::group('devices', function () { Route::get(':id/related-accounts', 'app\\cunkebao\\controller\\device\\GetRelatedAccountsV1Controller@index'); // 设备关联微信账号路由 - Route::get(':id/handle-logs', 'app\\cunkebao\\controller\\Device@handleLogs'); // 获取设备操作记录 - Route::get('', 'app\\cunkebao\\controller\\device\\GetDeviceListV1Controller@index'); // 获取设备列表 - Route::get('count', 'app\\cunkebao\\controller\\Device@count'); // 获取设备总数 - Route::get(':id', 'app\\cunkebao\\controller\\device\\GetDeviceDetailV1Controller@index'); // 获取设备详情 + Route::get(':id/handle-logs', 'app\\cunkebao\\controller\\device\\GetDeviceHandleLogsV1Controller@index'); // 获取设备操作记录 + Route::get('', 'app\\cunkebao\\controller\\device\\GetDeviceListV1Controller@index'); // 获取设备列表 + Route::get(':id', 'app\\cunkebao\\controller\\device\\GetDeviceDetailV1Controller@index'); // 获取设备详情 Route::post('', 'app\\cunkebao\\controller\\Device@save'); // 添加设备 - Route::put('refresh', 'app\\cunkebao\\controller\\device\\RefreshDeviceDetailV1Controller@index'); // 刷新设备状态 + Route::put('refresh', 'app\\cunkebao\\controller\\device\\RefreshDeviceDetailV1Controller@index'); // 刷新设备状态 Route::delete(':id', 'app\\cunkebao\\controller\\Device@delete'); // 删除设备 - Route::post('task-config', 'app\\cunkebao\\controller\\device\\UpdateDeviceTaskConfigV1Controller@index'); // 更新设备任务配置 + Route::post('task-config', 'app\\cunkebao\\controller\\device\\UpdateDeviceTaskConfigV1Controller@index'); // 更新设备任务配置 }); // 设备微信相关 diff --git a/Server/application/cunkebao/controller/Device.php b/Server/application/cunkebao/controller/Device.php index b9fdbf694..ddead438e 100644 --- a/Server/application/cunkebao/controller/Device.php +++ b/Server/application/cunkebao/controller/Device.php @@ -1,11 +1,9 @@ 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' => '设备不存在或已删除' - ]); - } - - // 获取设备关联的微信账号 - $wechatAccounts = DeviceWechatLogin::getDeviceRelatedAccounts($deviceId, $userInfo['companyId']); - - return json([ - 'code' => 200, - 'msg' => '获取成功', - 'data' => [ - 'deviceId' => $deviceId, - 'accounts' => $wechatAccounts, - 'total' => count($wechatAccounts) - ] - ]); - } catch (\Exception $e) { - return json([ - 'code' => 500, - 'msg' => '获取失败:' . $e->getMessage() - ]); - } - } - /** * 获取设备操作记录 * @return \think\response\Json diff --git a/Server/application/cunkebao/controller/device/GetDeviceHandleLogsV1Controller.php b/Server/application/cunkebao/controller/device/GetDeviceHandleLogsV1Controller.php new file mode 100644 index 000000000..cec957e1d --- /dev/null +++ b/Server/application/cunkebao/controller/device/GetDeviceHandleLogsV1Controller.php @@ -0,0 +1,87 @@ + $deviceId, + 'userId' => $this->getUserInfo('id'), + 'companyId' => $this->getUserInfo('companyId') + ]; + + $hasPermission = DeviceUserModel::where($where)->count() > 0; + + if (!$hasPermission) { + throw new \Exception('您没有权限查看该设备', '403'); + } + } + + /** + * 查询设备操作记录,并关联用户表获取操作人信息 + * + * @param int $deviceId + * @return \think\Paginator + */ + protected function getHandleLogs(int $deviceId): \think\Paginator + { + return DeviceHandleLog::alias('l') + ->field([ + 'l.id', + 'l.content', + 'l.createTime', + 'u.username' + ]) + ->leftJoin('users u', 'l.userId = u.id') + ->where('l.deviceId', $deviceId) + ->order('l.createTime desc') + ->paginate($this->request->param('limit/d', 10), false, ['page' => $this->request->param('page/d', 1)]); + } + + /** + * 获取设备操作记录 + * + * @return \think\response\Json + */ + public function index() + { + try { + $deviceId = $this->request->param('id/d'); + + if ($this->getUserInfo('isAdmin') != 1) { + $this->checkUserDevicePermission($deviceId); + } + + $logs = $this->getHandleLogs($deviceId); + + return json([ + 'code' => 200, + 'msg' => '获取成功', + 'data' => [ + 'total' => $logs->total(), + 'list' => $logs->items() + ] + ]); + } catch (\Exception $e) { + return json([ + 'code' => $e->getCode(), + 'msg' => $e->getMessage() + ]); + } + } +} \ No newline at end of file diff --git a/Server/application/cunkebao/controller/device/GetDeviceListV1Controller.php b/Server/application/cunkebao/controller/device/GetDeviceListV1Controller.php index b3d47db7a..235e9de85 100644 --- a/Server/application/cunkebao/controller/device/GetDeviceListV1Controller.php +++ b/Server/application/cunkebao/controller/device/GetDeviceListV1Controller.php @@ -66,7 +66,7 @@ class GetDeviceListV1Controller extends BaseController * @param int $limit 每页数量 * @return \think\Paginator 分页对象 */ - protected function getDeviceList(array $where, int $page = 1, int $limit = 10) + protected function getDeviceList(array $where, int $page = 1, int $limit = 10): \think\Paginator { $query = DeviceModel::alias('d') ->field(['d.id', 'd.imei', 'd.memo', 'l.wechatId', 'd.alive', '0 totalFriend']) @@ -82,7 +82,7 @@ class GetDeviceListV1Controller extends BaseController $query->where($key, $value); } - return $query->paginate($limit, false, ['page' => $page]); + return $query->paginate($this->request->param('limit/d', 10), false, ['page' => $this->request->param('page/d', 1)]); } /** @@ -115,15 +115,12 @@ class GetDeviceListV1Controller extends BaseController public function index() { try { - $page = (int)$this->request->param('page', 1); - $limit = (int)$this->request->param('limit', 10); - if ($this->getUserInfo('isAdmin') == 1) { $where = $this->makeWhere(); - $result = $this->getDeviceList($where, $page, $limit); + $result = $this->getDeviceList($where); } else { $where = $this->makeWhere($this->makeDeviceIdsWhere()); - $result = $this->getDeviceList($where, $page, $limit); + $result = $this->getDeviceList($where); } return json([ 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 3/3] =?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 000000000..c6c4ec649 --- /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 f31041577..05bf1b93e 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 235e9de85..33b14e1eb 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 000000000..9d808a0bc --- /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 ecf5db603..0b1fb4c7c 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 247412433..8a409957b 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 000000000..12698c536 --- /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 000000000..39a96d09e --- /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