This commit is contained in:
2026-08-19 19:56:47 +08:00
parent 07a24c5297
commit d3866efcd8
11 changed files with 193 additions and 23 deletions

View File

@@ -16,7 +16,6 @@ class Tickets extends Model
protected $createTime = 'create_time';
protected $updateTime = 'update_time';
# 定义字段类型
protected $type = [
'id' => 'integer',
@@ -30,10 +29,17 @@ class Tickets extends Model
'remark' => 'string',
'lock_time' => 'integer',
'create_time' => 'integer',
'update_time' => 'integer',
'update_time' => 'integer',
];
# 定义状态常量
const STATUS_UNASSIGNED = 0;
const STATUS_PROCESSING = 1;
const STATUS_COMPLETED = 2;
const STATUS_CLOSED = 3;
/*
时间戳获取器
*/

View File

@@ -12,6 +12,10 @@ class User extends Model
{
protected $table = 'users';
protected $type = [
'can_view_all_tickets' => 'boolean',
];
public function company(){
return $this->belongsTo(Companys::class,'company_id','id');
}

View File

@@ -18,7 +18,6 @@ class AuthService
->find();
if (!$user || !password_verify($password, $user['password'])) {
throw new \Exception('用户名或密码错误');
}
@@ -31,13 +30,16 @@ class AuthService
$userInfo = [
'id' => $user['id'], // 用户ID
'company_id' => $user['company_id'], // 所属公司ID
'company_name' => $user['company']['company_name'], // 所属公司名称
'company_name' => $user['role'] === 'client' && (bool) $user['can_view_all_tickets']
? '全部客户'
: ($user['company']['company_name'] ?? ''), // 所属公司名称
'username' => $user['username'],
'role' => $user['role'],
'can_view_all_tickets' => (bool) $user['can_view_all_tickets'],
];
Session::set('user_info', $userInfo);
return $userInfo;
return $userInfo; // 为了方便前端展示用户信息
}
}

View File

@@ -0,0 +1,35 @@
<?php
namespace app\common\service;
/**
* 把当前用户能看哪些工单封装成一个明确的对象TicketScope,避免用零散变量或含义不清的数组传递数据范围
* final 表示不允许被其他类继承
*/
final class TicketScope
{
# readonly 仅在PHP8.1中存在,当前版本8.0.2
private function __construct(
public string $type,
public ?int $staffId = null,
public ?int $clientId = null
) {}
/** 管理员:不限制状态、不限制归属 */
public static function admin(): self
{
return new self('admin');
}
/** 员工:未分配全部可见,其余状态仅自己 */
public static function staff(int $staffId): self
{
return new self('staff', staffId: $staffId);
}
/** 客户:$clientId 只能看自己的,传null可以看当前公司所有的 */
public static function client(?int $clientId): self
{
return new self('client', clientId: $clientId);
}
}

View File

@@ -2,11 +2,15 @@
namespace app\common\service;
use app\admin\controller\Ticket;
use app\common\model\Tickets;
use app\common\model\CompanyOperators;
use app\common\model\Companys;
use app\common\model\User;
use think\facade\Db;
use app\common\service\TicketScope;
class TicketService
{
@@ -17,12 +21,14 @@ class TicketService
* @param int $limit 每页数量
* @param string $order 排序方式
*/
public function queryTickets(array $params, int $page, int $limit, string $order)
public function queryTickets(array $params, int $page, int $limit, string $order, TicketScope $scope)
{
try {
// 查询构造器
$query = Tickets::with(['ticketStaff', 'ticketCompany', 'ticketOperator']);
$this->applyScope($query, $scope);
$equalFilters = ['operator_id', 'user_id', 'status'];
foreach ($equalFilters as $field) {
if (isset($params[$field]) && $params[$field] !== '') {
@@ -56,6 +62,21 @@ class TicketService
}
private function applyScope($query, TicketScope $scope): void
{
match ($scope->type) {
'admin' => null,
'staff' => $query->where(fn($q) => $q
->where('status', Tickets::STATUS_UNASSIGNED)
->where('user_id', $scope->staffId)),
};
}
/**
* 创建工单业务逻辑
* @param array $data 工单数据