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 @@ +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.php b/Server/application/cunkebao/controller/Device.php index b9fdbf69..749151ba 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 - */ - public function handleLogs() - { - try { - // 获取登录用户信息 - $userInfo = request()->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/GetDeviceHandleLogsV1Controller.php b/Server/application/cunkebao/controller/device/GetDeviceHandleLogsV1Controller.php new file mode 100644 index 00000000..cec957e1 --- /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 dd87b040..33b14e1e 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; } @@ -67,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']) @@ -83,16 +82,16 @@ 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)]); } /** * 统计微信好友 * - * @param object $list + * @param \think\Paginator $list * @return array */ - protected function countFriend(object $list): array + protected function countFriend(\think\Paginator $list): array { $result = []; @@ -116,15 +115,12 @@ class GetDeviceListV1Controller extends BaseController public function index() { try { - $page = (int)Request::param('page', 1); - $limit = (int)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([ diff --git a/Server/application/cunkebao/controller/device/GetRelatedAccountsV1Controller.php b/Server/application/cunkebao/controller/device/GetRelatedAccountsV1Controller.php new file mode 100644 index 00000000..2891db7e --- /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/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/cunkebao/model/DeviceWechatLogin.php b/Server/application/cunkebao/model/DeviceWechatLogin.php index 602edb35..206cbbd4 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查询关联的设备 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