存客宝应用接口初始化
This commit is contained in:
@@ -0,0 +1,288 @@
|
||||
<?php
|
||||
|
||||
namespace app\superadmin\controller\company;
|
||||
|
||||
use app\api\controller\DeviceController;
|
||||
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 Exception;
|
||||
use library\ResponseHelper;
|
||||
use library\s2\CurlHandle;
|
||||
use think\Db;
|
||||
use think\facade\Env;
|
||||
use think\response\Json;
|
||||
use think\Validate;
|
||||
|
||||
/**
|
||||
* 公司控制器
|
||||
*/
|
||||
class CreateCompanyController extends BaseController
|
||||
{
|
||||
/**
|
||||
* S2 创建用户。
|
||||
*
|
||||
* @param array $params
|
||||
* @return mixed|null
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function s2CreateUser(array $params): ?array
|
||||
{
|
||||
$params = ArrHelper::getValue('account=userName,password,username=realName,username=nickname,companyId=departmentId', $params);
|
||||
|
||||
// 创建账号
|
||||
$response = CurlHandle::getInstant()
|
||||
->setBaseUrl(Env::get('rpc.API_BASE_URL'))
|
||||
->setMethod('post')
|
||||
->send('/v1/api/account/create', $params);
|
||||
|
||||
$result = json_decode($response, true);
|
||||
|
||||
if ($result['code'] != 200) {
|
||||
throw new Exception($result['msg'], 210 . $result['code']);
|
||||
}
|
||||
|
||||
return $result['data'] ?: null;
|
||||
}
|
||||
|
||||
/**
|
||||
* S2 创建部门并返回id
|
||||
*
|
||||
* @param array $params
|
||||
* @return array
|
||||
*/
|
||||
protected function s2CreateDepartmentAndUser(array $params): ?array
|
||||
{
|
||||
$params = ArrHelper::getValue('name=departmentName,memo=departmentMemo,account=accountName,password=accountPassword,username=accountRealName,username=accountNickname,accountMemo', $params);
|
||||
|
||||
// 创建公司部门
|
||||
$response = CurlHandle::getInstant()
|
||||
->setBaseUrl(Env::get('rpc.API_BASE_URL'))
|
||||
->setMethod('post')
|
||||
->send('/v1/api/account/createNewAccount', $params);
|
||||
|
||||
$result = json_decode($response, true);
|
||||
|
||||
if ($result['code'] != 200) {
|
||||
throw new Exception($result['msg'], 210 . $result['code']);
|
||||
}
|
||||
|
||||
return $result['data'] ?: null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据验证
|
||||
*
|
||||
* @param array $params
|
||||
* @return $this
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function dataValidate(array $params): self
|
||||
{
|
||||
$validate = Validate::make([
|
||||
'name' => 'require|max:50|/\S+/',
|
||||
'account' => 'require|regex:^[a-zA-Z0-9]+$|max:20|/\S+/',
|
||||
'username' => 'require|max:20|/\S+/',
|
||||
'phone' => 'require|regex:/^1[3-9]\d{9}$/',
|
||||
'status' => 'require|in:0,1',
|
||||
'password' => 'require|/\S+/',
|
||||
'memo' => '/\S+/',
|
||||
], [
|
||||
'name.require' => '请输入项目名称',
|
||||
'account.require' => '请输入账号',
|
||||
'account.max' => '账号长度受限',
|
||||
'account.regex' => '账号只能用数字或者字母或者数字字母组合',
|
||||
'username.require' => '请输入用户昵称',
|
||||
'phone.require' => '请输入手机号',
|
||||
'phone.regex' => '手机号格式错误',
|
||||
'status.require' => '缺少重要参数',
|
||||
'status.in' => '非法参数',
|
||||
'password.require' => '请输入密码',
|
||||
]);
|
||||
|
||||
if (!$validate->check($params)) {
|
||||
throw new Exception($validate->getError(), 400);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设备创建分组
|
||||
*
|
||||
* @param array $params
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function s2CreateDeviceGroup(array $params): void
|
||||
{
|
||||
$respon = (new DeviceController())->createGroup($params, true);
|
||||
$respon = json_decode($respon, true);
|
||||
|
||||
if ($respon['code'] != 200) {
|
||||
throw new Exception('设备分组添加错误', 210 . $respon['code']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* S2 部分
|
||||
*
|
||||
* @param array $params
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function creatS2About(array $params): array
|
||||
{
|
||||
$department = $this->s2CreateDepartmentAndUser($params);
|
||||
|
||||
if (!$department || !isset($department['id']) || !isset($department['departmentId'])) {
|
||||
throw new Exception('S2返参异常', 210402);
|
||||
}
|
||||
|
||||
// 设备创建分组
|
||||
$this->s2CreateDeviceGroup(['groupName' => $params['name']]);
|
||||
|
||||
return array_merge($params, [
|
||||
'companyId' => $department['departmentId'],
|
||||
's2_accountId' => $department['id'],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 存客宝创建项目
|
||||
*
|
||||
* @param array $params
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function ckbCreateCompany(array $params): void
|
||||
{
|
||||
$params = ArrHelper::getValue('companyId=id,companyId,name,memo,status', $params);
|
||||
$result = CompanyModel::create($params);
|
||||
|
||||
if (!$result) {
|
||||
throw new Exception('创建公司记录失败', 402);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建功能账号,不可登录,也非管理员,用户也不可见.
|
||||
*
|
||||
* @param array $params
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function createFuncUsers(array $params): void
|
||||
{
|
||||
$seedCols = [
|
||||
['account' => $params['account'] . '_01', 'username' => $params['username'] . '_子账号01', 'status' => UsersModel::ADMIN_STP, 'isAdmin' => UsersModel::ADMIN_OTP, 'typeId' => UsersModel::MASTER_USER],
|
||||
['account' => $params['account'] . '_02', 'username' => $params['username'] . '_子账号02', 'status' => UsersModel::ADMIN_STP, 'isAdmin' => UsersModel::ADMIN_OTP, 'typeId' => UsersModel::MASTER_USER],
|
||||
['account' => $params['account'] . '_03', 'username' => $params['username'] . '_子账号03', 'status' => UsersModel::ADMIN_STP, 'isAdmin' => UsersModel::ADMIN_OTP, 'typeId' => UsersModel::MASTER_USER],
|
||||
['account' => $params['account'] . '_offline', 'username' => $params['username'] . '_处理离线专用', 'status' => UsersModel::STATUS_STOP, 'isAdmin' => UsersModel::ADMIN_OTP, 'typeId' => UsersModel::NOT_USER],
|
||||
['account' => $params['account'] . '_delete', 'username' => $params['username'] . '_处理删除专用', 'status' => UsersModel::STATUS_STOP, 'isAdmin' => UsersModel::ADMIN_OTP, 'typeId' => UsersModel::NOT_USER],
|
||||
];
|
||||
|
||||
foreach ($seedCols as $seeds) {
|
||||
$this->s2CreateUser(array_merge($params, ArrHelper::getValue('account,username', $seeds)));
|
||||
$this->ckbCreateUser(array_merge($params, $seeds));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 存客宝创建账号
|
||||
*
|
||||
* @param array $params
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function ckbCreateUser(array $params): void
|
||||
{
|
||||
$params = ArrHelper::getValue('username,account,password,companyId,s2_accountId,status,phone,isAdmin,typeId', $params);
|
||||
|
||||
$params = array_merge($params, [
|
||||
'passwordLocal' => localEncrypt($params['password']),
|
||||
'passwordMd5' => md5($params['password']),
|
||||
]);
|
||||
|
||||
if (!UsersModel::create($params)) {
|
||||
throw new Exception('创建用户记录失败', 402);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function createCkbAbout(array $params)
|
||||
{
|
||||
// 1. 存客宝创建项目
|
||||
$this->ckbCreateCompany($params);
|
||||
|
||||
// 2. 存客宝创建操盘手总账号
|
||||
$this->ckbCreateUser(array_merge($params, [
|
||||
'isAdmin' => UsersModel::ADMIN_STP, // 主要账号默认1
|
||||
'typeId' => UsersModel::MASTER_USER, // 类型:运营后台/操盘手传1、 门店传2
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查项目名称是否已存在
|
||||
*
|
||||
* @param array $where
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function checkCompanyNameOrAccountOrPhoneExists(array $where): void
|
||||
{
|
||||
extract($where);
|
||||
|
||||
// 项目名称尽量不重名
|
||||
$exists = CompanyModel::where(compact('name'))->count() > 0;
|
||||
if ($exists) {
|
||||
throw new Exception('项目名称已存在', 403);
|
||||
}
|
||||
|
||||
// 账号不重名
|
||||
$exists = UsersModel::where(compact('account'))->count() > 0;
|
||||
if ($exists) {
|
||||
throw new Exception('用户账号已存在', 403);
|
||||
}
|
||||
|
||||
// 手机号不重名
|
||||
$exists = UsersModel::where(compact('phone'))->count() > 0;
|
||||
if ($exists) {
|
||||
throw new Exception('手机号已存在', 403);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 创建新项目
|
||||
*
|
||||
* @return Json
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
try {
|
||||
$params = $this->request->only(['name', 'status', 'username', 'account', 'password', 'phone', 'memo']);
|
||||
$params = $this->dataValidate($params)->creatS2About($params);
|
||||
|
||||
Db::startTrans();
|
||||
|
||||
$this->checkCompanyNameOrAccountOrPhoneExists(ArrHelper::getValue('name,account,phone', $params));
|
||||
$this->createCkbAbout($params);
|
||||
|
||||
// 创建功能账号,不可登录,也非管理员,用户也不可见
|
||||
$this->createFuncUsers($params);
|
||||
Db::commit();
|
||||
|
||||
return ResponseHelper::success();
|
||||
} catch (Exception $e) {
|
||||
Db::rollback();
|
||||
return ResponseHelper::error($e->getMessage(), $e->getCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
<?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 library\ResponseHelper;
|
||||
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'];
|
||||
|
||||
Db::startTrans();
|
||||
|
||||
$this->dataValidate($params)->delteCkbAbout($companId)->deleteS2About($companId);
|
||||
|
||||
Db::commit();
|
||||
return ResponseHelper::success();
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
return ResponseHelper::error($e->getMessage(), $e->getCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace app\superadmin\controller\company;
|
||||
|
||||
use app\common\model\Company as CompanyModel;
|
||||
use app\common\model\Device as DeviceModel;
|
||||
use app\common\model\DeviceWechatLogin as DeviceWechatLoginModel;
|
||||
use app\common\model\User as UserModel;
|
||||
use app\common\model\WechatFriendShip as WechatFriendModel;
|
||||
use app\superadmin\controller\BaseController;
|
||||
use library\ResponseHelper;
|
||||
|
||||
/**
|
||||
* 公司控制器
|
||||
*/
|
||||
class GetCompanyDetailForProfileController extends BaseController
|
||||
{
|
||||
/**
|
||||
* 获取登录设备的所有微信
|
||||
*
|
||||
* @param int $companyId
|
||||
* @return array
|
||||
*/
|
||||
protected function getDeveiceWechats(int $companyId): array
|
||||
{
|
||||
$wechatIds = DeviceWechatLoginModel::where('companyId', $companyId)->column('wechatId');
|
||||
|
||||
return array_unique($wechatIds);
|
||||
}
|
||||
|
||||
/**
|
||||
* 统计微信好友数量
|
||||
*
|
||||
* @param int $companyId
|
||||
* @return int
|
||||
*/
|
||||
protected function getFriendCountByCompanyId(int $companyId): int
|
||||
{
|
||||
$wechatIds = $this->getDeveiceWechats($companyId);
|
||||
|
||||
return WechatFriendModel::whereIn('ownerWechatId', $wechatIds)->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 CompanyId 获取设备数量
|
||||
*
|
||||
* @param int $companyId
|
||||
* @return int
|
||||
*/
|
||||
protected function getDeviceCountByCompanyId(int $companyId): int
|
||||
{
|
||||
return DeviceModel::where('companyId', $companyId)->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 CompanyId 获取子账号数量
|
||||
*
|
||||
* @param int $companyId
|
||||
* @return int
|
||||
*/
|
||||
protected function getUsersCountByCompanyId(int $companyId): int
|
||||
{
|
||||
$where = array_merge(compact('companyId'), array('isAdmin' => UserModel::ADMIN_OTP));
|
||||
|
||||
return UserModel::where($where)->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取项目详情
|
||||
*
|
||||
* @param int $id
|
||||
* @return CompanyModel
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function getCompanyDetail(int $id): array
|
||||
{
|
||||
$detail = CompanyModel::alias('c')
|
||||
->field([
|
||||
'c.id', 'c.name', 'c.memo', 'c.companyId', 'c.createTime',
|
||||
'u.account', 'u.phone'
|
||||
])
|
||||
->leftJoin('users u', 'c.companyId = u.companyId and u.isAdmin = ' . UserModel::ADMIN_STP)
|
||||
->find($id);
|
||||
|
||||
if (!$detail) {
|
||||
throw new \Exception('项目不存在', 404);
|
||||
}
|
||||
|
||||
return $detail->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取项目详情
|
||||
*
|
||||
* @param int $id
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function index($id)
|
||||
{
|
||||
try {
|
||||
$data = $this->getCompanyDetail($id);
|
||||
|
||||
$userCount = $this->getUsersCountByCompanyId($id);
|
||||
$deviceCount = $this->getDeviceCountByCompanyId($id);
|
||||
$friendCount = $this->getFriendCountByCompanyId($id);
|
||||
|
||||
return ResponseHelper::success(
|
||||
array_merge($data, compact('deviceCount', 'friendCount', 'userCount'))
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
return ResponseHelper::error($e->getMessage(), $e->getCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
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 UserModel;
|
||||
use app\superadmin\controller\BaseController;
|
||||
use library\ResponseHelper;
|
||||
|
||||
/**
|
||||
* 公司控制器
|
||||
*/
|
||||
class GetCompanyDetailForUpdateController extends BaseController
|
||||
{
|
||||
/**
|
||||
* 根据 CompanyId 获取设备列表
|
||||
*
|
||||
* @param int $companyId
|
||||
* @return array
|
||||
*/
|
||||
protected function getDevicesByCompanyId(int $companyId): array
|
||||
{
|
||||
return DeviceModel::alias('d')
|
||||
->field([
|
||||
'd.id', 'd.memo', 'd.model', 'd.brand', 'd.phone', 'd.imei', 'd.createTime', 'd.alive',
|
||||
])
|
||||
->where('companyId', $companyId)
|
||||
->select()
|
||||
->toArray() ?: [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取项目详情
|
||||
*
|
||||
* @param int $id
|
||||
* @return CompanyModel
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function getCompanyDetail(int $id): array
|
||||
{
|
||||
$detail = CompanyModel::alias('c')
|
||||
->field([
|
||||
'c.id', 'c.name', 'c.status', 'c.memo', 'c.companyId',
|
||||
'u.account', 'u.username', 'u.phone', 'u.s2_accountId'
|
||||
])
|
||||
->leftJoin('users u', 'c.companyId = u.companyId and u.isAdmin = ' . UserModel::ADMIN_STP)
|
||||
->find($id);
|
||||
|
||||
if (!$detail) {
|
||||
throw new \Exception('项目不存在', 404);
|
||||
}
|
||||
|
||||
return $detail->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取项目详情
|
||||
*
|
||||
* @param int $id
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function index($id)
|
||||
{
|
||||
try {
|
||||
$data = $this->getCompanyDetail($id);
|
||||
$devices = $this->getDevicesByCompanyId($data['companyId']);
|
||||
|
||||
return ResponseHelper::success(
|
||||
array_merge($data, compact('devices'))
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
return ResponseHelper::error($e->getMessage(), $e->getCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
namespace app\superadmin\controller\company;
|
||||
|
||||
use app\common\model\Device as DeviceModel;
|
||||
use app\common\model\DeviceWechatLogin as DeviceWechatLoginModel;
|
||||
use app\common\model\WechatFriendShip as WechatFriendShipModel;
|
||||
use Eison\Utils\Helper\ArrHelper;
|
||||
use library\ResponseHelper;
|
||||
use think\Controller;
|
||||
|
||||
/**
|
||||
* 设备管理控制器
|
||||
*/
|
||||
class GetCompanyDevicesForProfileController extends Controller
|
||||
{
|
||||
/**
|
||||
* 获取项目下的所有设备
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getDevicesWithCompanyId(): array
|
||||
{
|
||||
$companyId = $this->request->param('companyId/d', 0);
|
||||
|
||||
$devices = DeviceModel::alias('d')
|
||||
->field([
|
||||
'd.id', 'd.memo', 'd.imei', 'd.phone', 'd.model', 'd.brand', 'd.alive', 'd.id deviceId'
|
||||
])
|
||||
->where(compact('companyId'))
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
if (empty($devices)) {
|
||||
throw new \Exception('暂无设备', 200);
|
||||
}
|
||||
|
||||
return $devices;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询设备与微信的关联关系.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getDeviceWechatRelationsByDeviceIds(array $deviceIds): array
|
||||
{
|
||||
// 获取设备最新登录记录的id
|
||||
$latestLogs = DeviceWechatLoginModel::getDevicesLatestLogin($deviceIds);
|
||||
|
||||
// 获取最新登录记录id
|
||||
$latestIds = array_column($latestLogs, 'lastedId');
|
||||
|
||||
return DeviceWechatLoginModel::alias('d')
|
||||
->field([
|
||||
'd.deviceId', 'd.wechatId', 'd.alive wAlive'
|
||||
])
|
||||
->whereIn('id', $latestIds)
|
||||
->select()
|
||||
->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取设备的微信好友统计
|
||||
*
|
||||
* @param array $ownerWechatId
|
||||
* @return void
|
||||
*/
|
||||
protected function getWechatFriendsCount(array $deviceIds): array
|
||||
{
|
||||
// 查询设备与微信的关联关系
|
||||
$relations = $this->getDeviceWechatRelationsByDeviceIds($deviceIds);
|
||||
|
||||
// 统计微信好友数量
|
||||
$friendCounts = WechatFriendShipModel::alias('f')
|
||||
->field([
|
||||
'f.ownerWechatId wechatId', 'count(*) friendCount'
|
||||
])
|
||||
->whereIn('ownerWechatId', array_column($relations, 'wechatId'))
|
||||
->group('ownerWechatId')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
return ArrHelper::leftJoin($relations, $friendCounts, 'wechatId');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取公司关联的设备列表
|
||||
*
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
try {
|
||||
$devices = $this->getDevicesWithCompanyId();
|
||||
$friendCount = $this->getWechatFriendsCount(array_column($devices, 'id'));
|
||||
|
||||
$result = ArrHelper::leftJoin($devices, $friendCount, 'deviceId');
|
||||
|
||||
return ResponseHelper::success($result);
|
||||
} catch (\Exception $e) {
|
||||
return ResponseHelper::error($e->getMessage(), $e->getCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
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;
|
||||
use library\ResponseHelper;
|
||||
|
||||
/**
|
||||
* 公司控制器
|
||||
*/
|
||||
class GetCompanyListController extends BaseController
|
||||
{
|
||||
/**
|
||||
* 构建查询条件
|
||||
*
|
||||
* @param array $params
|
||||
* @return array
|
||||
*/
|
||||
protected function makeWhere(array $params = []): array
|
||||
{
|
||||
$where = [];
|
||||
|
||||
// 如果有搜索关键词
|
||||
if (!empty($keyword = $this->request->param('keyword/s', ''))) {
|
||||
$where[] = ['name', 'like', "%{$keyword}%"];
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取项目列表
|
||||
*
|
||||
* @param array $where 查询条件
|
||||
* @param int $page 页码
|
||||
* @param int $limit 每页数量
|
||||
* @return \think\Paginator 分页对象
|
||||
*/
|
||||
protected function getCompanyList(array $where): \think\Paginator
|
||||
{
|
||||
$query = CompanyModel::alias('c')
|
||||
->field([
|
||||
'c.id', 'c.name', 'c.status', 'c.companyId', 'c.memo', 'c.createTime'
|
||||
]);
|
||||
|
||||
foreach ($where as $key => $value) {
|
||||
if (is_numeric($key) && is_array($value) && isset($value[0]) && $value[0] === 'exp') {
|
||||
$query->whereExp('', $value[1]);
|
||||
continue;
|
||||
}
|
||||
|
||||
$query->where($key, $value);
|
||||
}
|
||||
|
||||
return $query->order('id', 'desc')
|
||||
->paginate($this->request->param('limit/d', 10), false, ['page' => $this->request->param('page/d', 1)]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 统计项目下的用户数量
|
||||
*
|
||||
* @param int $companyId
|
||||
* @return int
|
||||
*/
|
||||
protected function countUserInCompany(int $companyId): int
|
||||
{
|
||||
return UsersModel::where('companyId', $companyId)->count('id');
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建返回数据
|
||||
*
|
||||
* @param \think\Paginator $Companylist
|
||||
* @return array
|
||||
*/
|
||||
protected function makeReturnedResult(\think\Paginator $Companylist): array
|
||||
{
|
||||
$result = [];
|
||||
$devices = $this->getDevices();
|
||||
|
||||
foreach ($Companylist->items() as $item) {
|
||||
$item->userCount = $this->countUserInCompany($item->companyId);
|
||||
$item->deviceCount = $devices[$item->companyId]['numCount'] ?? 0;
|
||||
|
||||
array_push($result, $item->toArray());
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取项目列表
|
||||
*
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$where = $this->makeWhere();
|
||||
$result = $this->getCompanyList($where);
|
||||
|
||||
return ResponseHelper::success(
|
||||
[
|
||||
'list' => $this->makeReturnedResult($result),
|
||||
'total' => $result->total(),
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace app\superadmin\controller\company;
|
||||
|
||||
use app\common\model\User as UserModel;
|
||||
use library\ResponseHelper;
|
||||
use think\Controller;
|
||||
|
||||
/**
|
||||
* 设备管理控制器
|
||||
*/
|
||||
class GetCompanySubusersForProfileController extends Controller
|
||||
{
|
||||
/**
|
||||
* 获取项目下的所有子账号
|
||||
*
|
||||
* @return CompanyModel
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function getSubusers(): array
|
||||
{
|
||||
$where = [
|
||||
'companyId' => $this->request->param('companyId/d', 0),
|
||||
'isAdmin' => UserModel::ADMIN_OTP
|
||||
];
|
||||
|
||||
return UserModel::alias('u')
|
||||
->field([
|
||||
'u.id', 'u.account', 'u.phone', 'u.username', 'u.avatar', 'u.status', 'u.createTime', 'u.typeId'
|
||||
])
|
||||
->where($where)
|
||||
->select()
|
||||
->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取公司关联的设备列表
|
||||
*
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$users = $this->getSubusers();
|
||||
|
||||
foreach ($users as &$user) {
|
||||
$user['createTime'] = date('Y-m-d H:i:s', $user['createTime']);
|
||||
}
|
||||
|
||||
return ResponseHelper::success($users);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,228 @@
|
||||
<?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 library\ResponseHelper;
|
||||
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
|
||||
{
|
||||
$where = [
|
||||
'isAdmin' => UsersModel::MASTER_USER, // 必须保证 isAdmin 有且只有一个
|
||||
'companyId' => $this->companyId,
|
||||
];
|
||||
|
||||
$user = UsersModel::where($where)->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,phone,status', $params);
|
||||
$params = ArrHelper::rmValue($params);
|
||||
|
||||
if (isset($params['password'])) {
|
||||
$params['passwordMd5'] = md5($params['password']);
|
||||
$params['passwordLocal'] = localEncrypt($params['password']);
|
||||
}
|
||||
|
||||
$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 checkCompanyNameOrAccountOrPhoneExists(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);
|
||||
}
|
||||
|
||||
// 手机号不重复
|
||||
$exists = UsersModel::where(compact('phone'))->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:^[a-zA-Z0-9]+$|max:20|/\S+/',
|
||||
'phone' => 'require|regex:/^1[3-9]\d{9}$/',
|
||||
'status' => 'require|in:0,1'
|
||||
], [
|
||||
'id.require' => '非法请求',
|
||||
'name.require' => '请输入项目名称',
|
||||
'username.require' => '请输入用户昵称',
|
||||
'account.require' => '请输入账号',
|
||||
'account.regex' => '账号只能用数字或者字母或者数字字母组合',
|
||||
'account.max' => '账号长度受限',
|
||||
'phone.require' => '请输入手机号',
|
||||
'phone.regex' => '手机号格式错误',
|
||||
'status.require' => '缺少重要参数',
|
||||
'status.in' => '非法参数',
|
||||
]);
|
||||
|
||||
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', 'phone', 'memo']);
|
||||
|
||||
// 数据验证
|
||||
$this->dataValidate($params);
|
||||
$this->checkCompanyNameOrAccountOrPhoneExists(ArrHelper::getValue('id,name,account,phone', $params));
|
||||
|
||||
Db::startTrans();
|
||||
$this->updateCkbAbout($params)->updateS2About($params);
|
||||
Db::commit();
|
||||
|
||||
return ResponseHelper::success();
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
return ResponseHelper::error($e->getMessage(), $e->getCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user