- 框架初始化
 - 安装插件
 - 修复PHP8.4报错
This commit is contained in:
2025-04-19 17:21:20 +08:00
commit c6a4e1f5f6
5306 changed files with 967782 additions and 0 deletions

View File

@@ -0,0 +1,129 @@
<?php
namespace app\admin\controller\shopro\chat;
use think\Db;
use app\admin\controller\shopro\Common;
use app\admin\model\shopro\chat\CommonWord as ChatCommonWord;
class CommonWord extends Common
{
public function _initialize()
{
parent::_initialize();
$this->model = new ChatCommonWord;
}
/**
* 常用语列表
*/
public function index()
{
if (!$this->request->isAjax()) {
return $this->view->fetch();
}
$commonWords = $this->model->sheepFilter()->paginate($this->request->param('list_rows', 10));
$this->success('获取成功', null, $commonWords);
}
/**
* 常用语添加
*/
public function add()
{
if (!$this->request->isAjax()) {
return $this->view->fetch();
}
$params = $this->request->only(['room_id', 'name', 'status', 'weigh']);
$params['content'] = $this->request->param('content', '', null); // content 不经过全局过滤
$this->svalidate($params, ".add");
$this->model->save($params);
$this->success('保存成功', null, $this->model);
}
/**
* 常用语详情
*
* @param $id
*/
public function detail($id)
{
$commonWord = $this->model->where('id', $id)->find();
if (!$commonWord) {
$this->error(__('No Results were found'));
}
$this->success('获取成功', null, $commonWord);
}
/**
* 常用语编辑
*
* @param $id
*/
public function edit($id = null)
{
if (!$this->request->isAjax()) {
return $this->view->fetch('add');
}
$params = $this->request->only(['room_id', 'name', 'status', 'weigh']);
$this->request->has('content') && $params['content'] = $this->request->param('content', '', null); // content 不经过全局过滤
$this->svalidate($params);
$id = explode(',', $id);
$list = $this->model->where('id', 'in', $id)->select();
$result = Db::transaction(function () use ($list, $params) {
$count = 0;
foreach ($list as $item) {
$params['id'] = $item->id;
$count += $item->save($params);
}
return $count;
});
if ($result) {
$this->success('更新成功', null, $result);
} else {
$this->error('更新失败,未改变任何记录');
}
}
/**
* 删除(支持批量)
*
* @param $id
*/
public function delete($id)
{
if (empty($id)) {
$this->error(__('Parameter %s can not be empty', 'id'));
}
$id = explode(',', $id);
$list = $this->model->where('id', 'in', $id)->select();
$result = Db::transaction(function () use ($list) {
$count = 0;
foreach ($list as $item) {
$count += $item->delete();
}
return $count;
});
if ($result) {
$this->success('删除成功', null, $result);
} else {
$this->error(__('No rows were deleted'));
}
}
}

View File

@@ -0,0 +1,207 @@
<?php
namespace app\admin\controller\shopro\chat;
use think\Db;
use app\admin\controller\shopro\Common;
use app\admin\model\shopro\chat\CustomerService as ChatCustomerService;
use app\admin\model\shopro\chat\CustomerServiceUser;
use app\admin\model\Admin;
class CustomerService extends Common
{
protected $noNeedRight = ['select'];
protected $adminModel;
public function _initialize()
{
parent::_initialize();
$this->model = new ChatCustomerService;
$this->adminModel = new Admin;
}
/**
* 客服列表
*/
public function index()
{
if (!$this->request->isAjax()) {
return $this->view->fetch();
}
$customerService = $this->model->sheepFilter()->with('customer_service_user')->paginate($this->request->param('list_rows', 10));
$this->success('获取成功', null, $customerService);
}
/**
* 客服添加
*/
public function add()
{
if (!$this->request->isAjax()) {
return $this->view->fetch();
}
$params = $this->request->only(['name', 'avatar', 'room_id', 'max_num', 'auth', 'auth_id']);
$this->svalidate($params, ".add");
if ($this->checkHasAuthId($params['auth'], $params['auth_id'], $params['room_id'])) {
error_stop('该身份已绑定其他客服');
}
$data = Db::transaction(function () use ($params) {
$this->model->allowField(true)->save($params);
$customerServiceUser = CustomerServiceUser::create([
'customer_service_id' => $this->model->id,
'auth' => $params['auth'],
'auth_id' => $params['auth_id'],
]);
return $customerServiceUser;
});
$this->success('保存成功', null, $data);
}
/**
* 客服详情
*
* @param $id
*/
public function detail($id)
{
$customerService = $this->model->with(['customer_service_user'])->where('id', $id)->find();
if (!$customerService) {
$this->error(__('No Results were found'));
}
$this->success('获取成功', null, $customerService);
}
/**
* 客服编辑
*
* @param $id
*/
public function edit($id = null)
{
if (!$this->request->isAjax()) {
return $this->view->fetch('add');
}
$params = $this->request->only(['name', 'avatar', 'room_id', 'max_num', 'auth', 'auth_id']);
$this->svalidate($params);
$id = explode(',', $id);
$list = $this->model->where('id', 'in', $id)->with('customer_service_user')->select();
Db::transaction(function () use ($list, $params) {
foreach ($list as $customerService) {
$customerService->allowField(true)->save($params);
$customerServiceUser = $customerService['customer_service_user'];
// 编辑了客服身份所有者
if ($params['auth'] != $customerServiceUser['auth'] || $params['auth_id'] != $customerServiceUser['auth_id']) {
// 验证新的身份是否已经被绑定别的客服
if ($this->checkHasAuthId($params['auth'], $params['auth_id'], $params['room_id'])) {
error_stop('该身份已绑定其他客服');
}
// 删除老的身份
CustomerServiceUser::{'auth' . ucfirst($customerServiceUser['auth'])}($customerServiceUser['auth_id'])
->where('customer_service_id', $customerService['id'])->delete();
// 添加新的身份
$customerServiceUser = CustomerServiceUser::create([
'customer_service_id' => $customerService->id,
'auth' => $params['auth'],
'auth_id' => $params['auth_id'],
]);
}
}
});
$this->success('更新成功');
}
/**
* 客服用语
*
* @param $id
*/
public function delete($id)
{
if (empty($id)) {
$this->error(__('Parameter %s can not be empty', 'id'));
}
$id = explode(',', $id);
$list = $this->model->where('id', 'in', $id)->select();
Db::transaction(function () use ($list) {
foreach ($list as $customerService) {
// 删除客服的身份
CustomerServiceUser::where('customer_service_id', $customerService['id'])->delete();
$customerService->delete();
}
});
$this->success('删除成功');
}
/**
* 检验是否已经被绑定了客服(一个管理员或者用户只能是一种客服)
*
* @param string $auth
* @param integer $auth_id
* @return void
*/
private function checkHasAuthId($auth, $auth_id, $room_id)
{
$customerServiceUser = CustomerServiceUser::{'auth' . ucfirst($auth)}($auth_id)->with('customer_service')->find();
if ($customerServiceUser) {
$customerService = $customerServiceUser['customer_service'];
if ($customerService && $customerService['room_id'] == $room_id) {
return true;
}
}
return false;
}
/**
* 获取管理员列表
*
* @return void
*/
public function select()
{
if (!$this->request->isAjax()) {
return $this->view->fetch();
}
$id = $this->request->param('id', 0);
$room_id = $this->request->param('room_id', 'admin');
// 已经被设置为客服的管理员
$adminIds = CustomerServiceUser::whereExists(function ($query) use ($room_id) {
$table_name = $this->model->getQuery()->getTable();
$query->table($table_name)->where('room_id', $room_id)->where('customer_service_id=id');
})->where('auth', 'admin')->where('customer_service_id', '<>', $id)->column('auth_id');
// 正常的,并且排除了已经设置为客服的管理员
$admins = $this->adminModel->where('status', 'normal')->whereNotIn('id', $adminIds)->select();
$this->success('获取成功', null, $admins);
}
}

View File

@@ -0,0 +1,38 @@
<?php
namespace app\admin\controller\shopro\chat;
use app\admin\controller\shopro\Common;
use app\admin\model\shopro\chat\CustomerService;
class Index extends Common
{
protected $noNeedRight = ['init'];
/**
* socket 初始化
*
* @return void
*/
public function init()
{
// 当前管理员
$admin = auth_admin();
$token = $this->getUnifiedToken('admin:' . $admin['id']); // 统一验证 token
// 客服配置
$chatSystem = sheep_config('chat.system');
// 初始化 socket ssl 类型, 默认 cert
$ssl = $chatSystem['ssl'] ?? 'none';
$chat_domain = ($ssl == 'none' ? 'http://' : 'https://') . request()->host(true) . ($ssl == 'reverse_proxy' ? '' : (':' . $chatSystem['port'])) . '/chat';
$data = [
'token' => $token,
'chat_domain' => $chat_domain,
'default_rooms' => CustomerService::defaultRooms()
];
$this->success('获取成功', null, $data);
}
}

View File

@@ -0,0 +1,133 @@
<?php
namespace app\admin\controller\shopro\chat;
use think\Db;
use app\admin\controller\shopro\Common;
use app\admin\model\shopro\chat\Question as ChatQuestion;
class Question extends Common
{
public function _initialize()
{
parent::_initialize();
$this->model = new ChatQuestion;
}
/**
* 猜你想问列表
*
* @return \think\Response
*/
public function index()
{
if (!$this->request->isAjax()) {
return $this->view->fetch();
}
$questions = $this->model->sheepFilter()->paginate($this->request->param('list_rows', 10));
$this->success('获取成功', null, $questions);
}
/**
* 猜你想问添加
*/
public function add()
{
if (!$this->request->isAjax()) {
return $this->view->fetch();
}
$params = $this->request->only(['room_id', 'title', 'status', 'weigh']);
$params['content'] = $this->request->param('content', '', null); // content 不经过全局过滤
$this->svalidate($params, ".add");
$this->model->save($params);
$this->success('保存成功', null, $this->model);
}
/**
* 猜你想问详情
*
* @param $id
*/
public function detail($id)
{
$question = $this->model->where('id', $id)->find();
if (!$question) {
$this->error(__('No Results were found'));
}
$this->success('获取成功', null, $question);
}
/**
* 猜你想问编辑
*
* @param $id
* @return \think\Response
*/
public function edit($id = null)
{
if (!$this->request->isAjax()) {
return $this->view->fetch('add');
}
$params = $this->request->only(['room_id', 'title', 'status', 'weigh']);
$this->request->has('content') && $params['content'] = $this->request->param('content', '', null); // content 不经过全局过滤
$this->svalidate($params);
$id = explode(',', $id);
$list = $this->model->where('id', 'in', $id)->select();
$result = Db::transaction(function () use ($list, $params) {
$count = 0;
foreach ($list as $item) {
$params['id'] = $item->id;
$count += $item->save($params);
}
return $count;
});
if ($result) {
$this->success('更新成功', null, $result);
} else {
$this->error('更新失败,未改变任何记录');
}
}
/**
* 删除猜你想问
*
* @param $id
* @return \think\Response
*/
public function delete($id)
{
if (empty($id)) {
$this->error(__('Parameter %s can not be empty', 'id'));
}
$id = explode(',', $id);
$list = $this->model->where('id', 'in', $id)->select();
$result = Db::transaction(function () use ($list) {
$count = 0;
foreach ($list as $item) {
$count += $item->delete();
}
return $count;
});
if ($result) {
$this->success('删除成功', null, $result);
} else {
$this->error(__('No rows were deleted'));
}
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace app\admin\controller\shopro\chat;
use think\Db;
use app\admin\controller\shopro\Common;
use app\admin\model\shopro\chat\Record as ChatRecord;
class Record extends Common
{
public function _initialize()
{
parent::_initialize();
$this->model = new ChatRecord;
}
/**
* 聊天列表
*/
public function index()
{
if (!$this->request->isAjax()) {
return $this->view->fetch();
}
$records = $this->model->sheepFilter()->order('id desc')->paginate($this->request->param('list_rows', 10));
$morphs = [
'customer' => \app\admin\model\shopro\chat\User::class,
'customer_service' => \app\admin\model\shopro\chat\CustomerService::class,
];
$records = morph_to($records, $morphs, ['sender_identify', 'sender_id']);
$this->success('获取成功', null, $records);
}
}

View File

@@ -0,0 +1,62 @@
<?php
namespace app\admin\controller\shopro\chat;
use think\Db;
use app\admin\controller\shopro\Common;
use app\admin\model\shopro\chat\User as ChatUser;
use app\admin\model\shopro\chat\ServiceLog;
use app\admin\model\shopro\chat\Record;
class User extends Common
{
public function _initialize()
{
parent::_initialize();
$this->model = new ChatUser;
}
/**
* 会话列表
*/
public function index()
{
if (!$this->request->isAjax()) {
return $this->view->fetch();
}
$user = $this->model->sheepFilter()->with(['user', 'customer_service'])->where('auth', 'user')->order('id desc')->paginate(request()->param('list_rows', 10));
$this->success('获取成功', null, $user);
}
/**·
* 删除会话
*
* @param $id
* @return \think\Response
*/
public function delete($id)
{
if (empty($id)) {
$this->error(__('Parameter %s can not be empty', 'id'));
}
$id = explode(',', $id);
$list = $this->model->where('id', 'in', $id)->select();
Db::transaction(function () use ($list) {
foreach ($list as $user) {
// 删除这个会话的所有服务记录
ServiceLog::where('chat_user_id', $user->id)->delete();
// 删除这个会话的所有聊天记录
Record::where('chat_user_id', $user->id)->delete();
// 删除这个会话
$user->delete();
}
});
$this->success('删除成功');
}
}