- 按 docs/renew-code-comment.md 将 PHP 文件头改为带边框的 Author 注释\n- 注释中的 crmeb.com 替换为 uj345.cn(代码字符串中的外链未改)\n- 新增 docs/renew-code-comment.md 说明 Made-with: Cursor
188 lines
6.1 KiB
PHP
188 lines
6.1 KiB
PHP
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | Author: ScottPan Team
|
|
// +----------------------------------------------------------------------
|
|
|
|
namespace app\controller\api\v1\user;
|
|
|
|
use app\validate\api\user\StoreServiceFeedbackValidate;
|
|
use app\Request;
|
|
use app\services\message\service\StoreServiceFeedbackServices;
|
|
use app\services\message\service\StoreServiceLogServices;
|
|
use app\services\message\service\StoreServiceRecordServices;
|
|
use app\services\message\service\StoreServiceServices;
|
|
use app\services\other\CacheServices;
|
|
use crmeb\services\CacheService;
|
|
use think\annotation\Inject;
|
|
use think\db\exception\DataNotFoundException;
|
|
use think\db\exception\DbException;
|
|
use think\db\exception\ModelNotFoundException;
|
|
|
|
/**
|
|
* 客服类
|
|
* Class StoreService
|
|
* @package app\api\controller\user
|
|
*/
|
|
class StoreService
|
|
{
|
|
/**
|
|
* @var StoreServiceLogServices
|
|
*/
|
|
#[Inject]
|
|
protected StoreServiceLogServices $services;
|
|
|
|
/**
|
|
* 客服列表
|
|
* @param StoreServiceServices $services
|
|
* @return mixed
|
|
* @throws DataNotFoundException
|
|
* @throws DbException
|
|
* @throws ModelNotFoundException
|
|
*/
|
|
public function lst(StoreServiceServices $services)
|
|
{
|
|
$serviceInfoList = $services->getServiceList(['status' => 1]);
|
|
if (!count($serviceInfoList)) return app('json')->successful([]);
|
|
return app('json')->successful($serviceInfoList['list']);
|
|
}
|
|
|
|
/**
|
|
* 客服聊天记录
|
|
* @param Request $request
|
|
* @param StoreServiceServices $services
|
|
* @param StoreServiceRecordServices $recordServices
|
|
* @return array
|
|
* @throws DataNotFoundException
|
|
* @throws DbException
|
|
* @throws ModelNotFoundException
|
|
*/
|
|
public function record(Request $request, StoreServiceServices $services, StoreServiceRecordServices $recordServices)
|
|
{
|
|
[$uidTo] = $request->getMore([
|
|
['uidTo', 0]
|
|
], true);
|
|
$serviceInfoList = $services->getServiceList(['status' => 1]);
|
|
if (!count($serviceInfoList)) return app('json')->fail('暂无客服人员在线,请稍后联系');
|
|
$uid = (int)$request->uid();
|
|
$uids = array_column($serviceInfoList['list'], 'uid');
|
|
if (!$uidTo) {
|
|
//自己是客服
|
|
if (in_array($uid, $uids)) {
|
|
$uids = array_merge(array_diff($uids, [$uid]));
|
|
if (!$uids) return app('json')->fail('不能和自己聊天');
|
|
}
|
|
} else {
|
|
if (in_array($uid, $uids)) {
|
|
$uid = $uidTo;
|
|
}
|
|
}
|
|
if (!$uids) {
|
|
return app('json')->fail('暂无客服人员在线,请稍后联系');
|
|
}
|
|
//上次聊天客服优先对话
|
|
$toUid = $recordServices->value(['user_id' => $uid], 'to_uid');
|
|
if (!in_array($toUid, $uids)) {
|
|
$toUid = 0;
|
|
}
|
|
if (!$toUid) {
|
|
mt_srand();
|
|
$toUid = $uids[array_rand($uids)] ?? 0;
|
|
}
|
|
|
|
if (!$toUid) return app('json')->fail('暂无客服人员在线,请稍后联系');
|
|
$result = ['serviceList' => [], 'uid' => $toUid];
|
|
$serviceLogList = $this->services->getChatList(['uid' => $uid], $uid);
|
|
if (!$serviceLogList) return app('json')->successful($result);
|
|
$idArr = array_column($serviceLogList, 'id');
|
|
array_multisort($idArr, SORT_ASC, $serviceLogList);
|
|
$result['serviceList'] = $serviceLogList;
|
|
return app('json')->successful($result);
|
|
}
|
|
|
|
/**
|
|
* 获取客服页面广告内容
|
|
* @return mixed
|
|
*/
|
|
public function getKfAdv()
|
|
{
|
|
/** @var CacheServices $cache */
|
|
$cache = app()->make(CacheServices::class);
|
|
$content = $cache->getDbCache('kf_adv', '');
|
|
return app('json')->successful(compact('content'));
|
|
}
|
|
|
|
/**
|
|
* 保存反馈信息
|
|
* @param Request $request
|
|
* @param StoreServiceFeedbackServices $services
|
|
* @return mixed
|
|
*/
|
|
public function saveFeedback(Request $request, StoreServiceFeedbackServices $services)
|
|
{
|
|
$data = $request->postMore([
|
|
['rela_name', ''],
|
|
['phone', ''],
|
|
['content', ''],
|
|
]);
|
|
|
|
validate(StoreServiceFeedbackValidate::class)->check($data);
|
|
|
|
$data['content'] = htmlspecialchars($data['content']);
|
|
$data['add_time'] = time();
|
|
$data['uid'] = $request->uid();
|
|
$services->save($data);
|
|
return app('json')->success('提交成功');
|
|
}
|
|
|
|
/**
|
|
* 客服反馈页面头部文字
|
|
* @return mixed
|
|
*/
|
|
public function getFeedbackInfo()
|
|
{
|
|
return app('json')->success(['feedback' => sys_config('service_feedback')]);
|
|
}
|
|
|
|
/**
|
|
* 确认登录
|
|
* @param Request $request
|
|
* @param StoreServiceServices $services
|
|
* @param string $code
|
|
* @return mixed
|
|
*/
|
|
public function setLoginCode(Request $request, StoreServiceServices $services, string $code)
|
|
{
|
|
if (!$code) {
|
|
return app('json')->fail('登录CODE不存在');
|
|
}
|
|
$cacheCode = CacheService::get($code);
|
|
if ($cacheCode === false || $cacheCode === null) {
|
|
return app('json')->fail('二维码已过期请重新扫描');
|
|
}
|
|
$userInfo = $services->get(['uid' => $request->uid()]);
|
|
if (!$userInfo) {
|
|
return app('json')->fail('您不是客服无法登录');
|
|
}
|
|
$userInfo->uniqid = $code;
|
|
$userInfo->save();
|
|
CacheService::set($code, '0', 600);
|
|
return app('json')->success('登录成功');
|
|
}
|
|
|
|
/**
|
|
* 获取当前客服和用户的聊天记录
|
|
* @param StoreServiceRecordServices $services
|
|
* @param Request $request
|
|
* @param string $nickname
|
|
* @param int $is_tourist
|
|
* @return mixed
|
|
* @throws DataNotFoundException
|
|
* @throws DbException
|
|
* @throws ModelNotFoundException
|
|
*/
|
|
public function recordList(StoreServiceRecordServices $services, Request $request, string $nickname = '', $is_tourist = 0)
|
|
{
|
|
return app('json')->success($services->getServiceList((int)$request->uid(), $nickname, (int)$is_tourist));
|
|
}
|
|
}
|