超管后台 - 新建项目返工

This commit is contained in:
柳清爽
2025-04-17 14:34:31 +08:00
parent b18c70b7c6
commit 42e189ac01
10 changed files with 415 additions and 102 deletions

View File

@@ -0,0 +1,18 @@
<?php
namespace app\common\model;
use think\Model;
/**
* 项目模型
*/
class Company extends Model
{
// 设置数据表名
protected $name = 'company';
// 自动写入时间戳
protected $autoWriteTimestamp = true;
protected $createTime = 'createTime';
protected $updateTime = 'updateTime';
}

View File

@@ -14,7 +14,7 @@ Route::group('v1/', function () {
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::post('', 'app\\cunkebao\\controller\\device\\PostAddDeviceV1Controller@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'); // 更新设备任务配置

View File

@@ -171,97 +171,5 @@ class Device extends Controller
}
}
/**
* 获取设备操作记录
* @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()
]);
}
}
}

View File

@@ -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);

View File

@@ -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 = [];

View File

@@ -0,0 +1,132 @@
<?php
namespace app\cunkebao\controller\device;
use app\cunkebao\controller\BaseController;
use app\common\model\Device as DeviceModel;
use app\common\model\DeviceHandleLog as DeviceHandleLogModel;
use app\library\s2\CurlHandle;
use Couchbase\ViewOptions;
use think\Db;
use think\Validate;
use think\facade\Request;
/**
* 设备管理控制器
*/
class PostAddDeviceV1Controller extends BaseController
{
/**
* 验证IMEI是否已存在
*
* @return void
* @throws \Exception
*/
protected function checkDeviceIsExist(): void
{
if ($this->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()
]);
}
}
}

View File

@@ -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();

View File

@@ -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');
});

View File

@@ -0,0 +1,45 @@
<?php
namespace app\superadmin\controller;
use think\Controller;
/**
* 设备管理控制器
*/
class BaseController extends Controller
{
/**
* 用户信息
* @var object
*/
protected $user;
/**
* 初始化
*/
protected function initialize()
{
parent::initialize();
date_default_timezone_set('Asia/Shanghai');
}
/**
* 获取用户信息
*
* @param string $column
* @return mixed
* @throws \Exception
*/
protected function getUserInfo(string $column = '')
{
$user = $this->request->userInfo;
if (!$user) {
throw new \Exception('未授权访问,缺少有效的身份凭证', 401);
}
return $column ? $user[$column] : $user;
}
}

View File

@@ -0,0 +1,206 @@
<?php
namespace app\superadmin\controller\company;
use app\common\model\Company as CompanyModel;
use app\common\model\User as UsersModel;
use app\library\s2\CurlHandle;
use app\superadmin\controller\BaseController;
use think\Db;
use think\Validate;
/**
* 公司控制器
*/
class CreateCompanyController extends BaseController
{
/**
* 获取 CURL
*
* @return CurlHandle|null
*/
protected function getCurl()
{
return CurlHandle::getInstant()->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()
]);
}
}
}