其余文件

This commit is contained in:
2026-04-14 17:46:22 +08:00
parent 294b68fe37
commit 3691f4db22
1343 changed files with 189847 additions and 0 deletions

View File

@ -0,0 +1,24 @@
<?php
// +----------------------------------------------------------------------
// | likeshop100%开源免费商用商城系统
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用可去除界面版权logo
// | 商业版本务必购买商业授权,以免引起法律纠纷
// | 禁止对系统程序代码以任何目的,任何形式的再发布
// | gitee下载https://gitee.com/likeshop_gitee
// | github下载https://github.com/likeshop-github
// | 访问官网https://www.likeshop.cn
// | 访问社区https://home.likeshop.cn
// | 访问手册http://doc.likeshop.cn
// | 微信公众号likeshop技术社区
// | likeshop团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeshopTeam
// +----------------------------------------------------------------------
return [
'middleware' => [
app\kefuapi\http\middleware\Login::class,//登录验证
],
];

View File

@ -0,0 +1,67 @@
<?php
// +----------------------------------------------------------------------
// | likeshop开源商城系统
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | gitee下载https://gitee.com/likeshop_gitee
// | github下载https://github.com/likeshop-github
// | 访问官网https://www.likeshop.cn
// | 访问社区https://home.likeshop.cn
// | 访问手册http://doc.likeshop.cn
// | 微信公众号likeshop技术社区
// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用未经许可不能去除前后端官方版权标识
// | likeshop系列产品收费版本务必购买商业授权购买去版权授权后方可去除前后端官方版权标识
// | 禁止对系统程序代码以任何目的,任何形式的再发布
// | likeshop团队版权所有并拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeshop.cn.team
// +----------------------------------------------------------------------
namespace app\kefuapi\controller;
use app\common\basics\KefuBase;
use app\kefuapi\logic\LoginLogic;
use app\common\server\JsonServer;
use app\kefuapi\validate\LoginValidate;
/**
* 客服账号登录
* Class Account
* @package app\shopapi\controller
*/
class Account extends KefuBase
{
public $like_not_need_login = ['login'];
/**
* @notes 账号密码登录
* @return \think\response\Json
* @author 段誉
* @date 2021/11/9 16:21
*/
public function login()
{
$post = $this->request->post();
(new LoginValidate())->goCheck();
$result = LoginLogic::accountLogin($post);
return JsonServer::success('登录成功', $result);
}
/**
* @notes 退出登录
* @return \think\response\Json
* @author 段誉
* @date 2021/11/9 15:49
*/
public function logout()
{
LoginLogic::logout($this->kefu_id, $this->client);
return JsonServer::success();
}
}

View File

@ -0,0 +1,166 @@
<?php
// +----------------------------------------------------------------------
// | likeshop开源商城系统
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | gitee下载https://gitee.com/likeshop_gitee
// | github下载https://github.com/likeshop-github
// | 访问官网https://www.likeshop.cn
// | 访问社区https://home.likeshop.cn
// | 访问手册http://doc.likeshop.cn
// | 微信公众号likeshop技术社区
// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用未经许可不能去除前后端官方版权标识
// | likeshop系列产品收费版本务必购买商业授权购买去版权授权后方可去除前后端官方版权标识
// | 禁止对系统程序代码以任何目的,任何形式的再发布
// | likeshop团队版权所有并拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeshop.cn.team
// +----------------------------------------------------------------------
namespace app\kefuapi\controller;
use app\common\basics\KefuBase;
use app\kefuapi\logic\ChatLogic;
use app\common\server\JsonServer;
use app\kefuapi\validate\ChatValidate;
class Chat extends KefuBase
{
public $like_not_need_login = ['config'];
/**
* @notes 用户列表
* @return \think\response\Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author 段誉
* @date 2021/12/14 15:42
*/
public function user()
{
$get = $this->request->get('');
// 对话过的用户
$result = ChatLogic::getChatUserList($this->kefu_id, $this->shop_id, $get, $this->page_no, $this->page_size);
return JsonServer::success('', $result);
}
/**
* @notes 指定用户聊天记录
* @return \think\response\Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author 段誉
* @date 2021/12/14 15:43
*/
public function record()
{
$user_id = $this->request->get('user_id/d');
(new ChatValidate())->goCheck();
$result = ChatLogic::getChatRecord($this->kefu_id, $user_id, $this->shop_id, $this->page_no, $this->page_size);
return JsonServer::success('', $result);
}
/**
* @notes 在线的客服
* @return \think\response\Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author 段誉
* @date 2021/12/14 15:43
*/
public function online()
{
$result = ChatLogic::getOnlineKefu($this->kefu_id, $this->shop_id);
return JsonServer::success('', $result);
}
/**
* @notes 快捷回复
* @return \think\response\Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author 段誉
* @date 2021/12/15 11:07
*/
public function reply()
{
$keyword = $this->request->get('keyword');
$result = ChatLogic::getReplyLists($this->shop_id, $keyword, $this->page_no, $this->page_size);
return JsonServer::success('', $result);
}
/**
* @notes 用户详情接口
* @return \think\response\Json
* @author 段誉
* @date 2021/12/15 15:05
*/
public function userInfo()
{
$user_id = $this->request->get('user_id/d');
$result = ChatLogic::getUserInfo($user_id);
if (false === $result) {
return JsonServer::error(ChatLogic::getError() ?: '系统错误');
}
return JsonServer::success('', $result);
}
/**
* @notes 获取指定订单列表
* @return \think\response\Json
* @author 段誉
* @date 2021/12/15 16:04
*/
public function order()
{
$get = $this->request->get();
$result = ChatLogic::getOrderLists($get, $this->shop_id, $this->page_no, $this->page_size);
if (false === $result) {
return JsonServer::error(ChatLogic::getError() ?: '系统错误');
}
return JsonServer::success('', $result);
}
/**
* @notes 客服详情
* @return \think\response\Json
* @author 段誉
* @date 2021/12/15 17:34
*/
public function kefuInfo()
{
$result = ChatLogic::getKefuInfo($this->kefu_id);
return JsonServer::success('', $result);
}
/**
* @notes 配置
* @return \think\response\Json
* @author 段誉
* @date 2021/12/16 17:06
*/
public function config()
{
$result = ChatLogic::getConfig();
return JsonServer::success('', $result);
}
}

View File

@ -0,0 +1,47 @@
<?php
// +----------------------------------------------------------------------
// | likeshop开源商城系统
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | gitee下载https://gitee.com/likeshop_gitee
// | github下载https://github.com/likeshop-github
// | 访问官网https://www.likeshop.cn
// | 访问社区https://home.likeshop.cn
// | 访问手册http://doc.likeshop.cn
// | 微信公众号likeshop技术社区
// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用未经许可不能去除前后端官方版权标识
// | likeshop系列产品收费版本务必购买商业授权购买去版权授权后方可去除前后端官方版权标识
// | 禁止对系统程序代码以任何目的,任何形式的再发布
// | likeshop团队版权所有并拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeshop.cn.team
// +----------------------------------------------------------------------
namespace app\kefuapi\controller;
use app\common\basics\KefuBase;
use app\common\server\FileServer;
use app\common\server\JsonServer;
class File extends KefuBase
{
public $like_not_need_login = [];
/**
* @notes upload
* @return \think\response\Json
* @author 段誉
* @date 2021/12/16 11:11
*/
public function formImage()
{
try{
$data = FileServer::image(0, $this->shop_id, 0, 'uploads/kefu/'.$this->kefu_id);
return JsonServer::success('上传成功', $data);
}catch(\Exception $e) {
return JsonServer::error($e->getMessage());
}
}
}

View File

@ -0,0 +1,109 @@
<?php
// +----------------------------------------------------------------------
// | likeshop开源商城系统
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | gitee下载https://gitee.com/likeshop_gitee
// | github下载https://github.com/likeshop-github
// | 访问官网https://www.likeshop.cn
// | 访问社区https://home.likeshop.cn
// | 访问手册http://doc.likeshop.cn
// | 微信公众号likeshop技术社区
// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用未经许可不能去除前后端官方版权标识
// | likeshop系列产品收费版本务必购买商业授权购买去版权授权后方可去除前后端官方版权标识
// | 禁止对系统程序代码以任何目的,任何形式的再发布
// | likeshop团队版权所有并拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeshop.cn.team
// +----------------------------------------------------------------------
namespace app\kefuapi\http\middleware;
use app\common\model\kefu\Kefu;
use think\exception\ValidateException;
use app\kefuapi\validate\TokenValidate;
class Login
{
/**
* 登录验证
* @param $request
* @param \Closure $next
* @return mixed|\think\response\Redirect
*/
public function handle($request, \Closure $next)
{
//允许跨域调用
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: Authorization, Sec-Fetch-Mode, DNT, X-Mx-ReqToken, Keep-Alive, User-Agent, If-Match, If-None-Match, If-Unmodified-Since, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Accept-Language, Origin, Accept-Encoding,Access-Token,token");
header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE');
header('Access-Control-Max-Age: 1728000');
header('Access-Control-Allow-Credentials:true');
if (strtoupper($request->method()) == "OPTIONS") {
return response();
}
$token = $request->header('token');
// 无需登录
if (empty($token) && $this->isNotNeedLogin($request)) {
return $next($request);
}
$validateError = '';
try {
validate(TokenValidate::class)->check(['token' => $token]);
$kefu_info = (new Kefu())->alias('k')
->join('kefu_session ks', 'k.id = ks.kefu_id')
->where(['ks.token' => $token])
->field('k.*,ks.token,ks.client')
->hidden(['password'])
->findOrEmpty();
$kefu_info = $kefu_info ? $kefu_info->toArray() : [];
// 设置用户信息
$request->kefu_info = $kefu_info;
return $next($request);
} catch (ValidateException $e) {
$validateError = $e->getError();
}
//无需要登录带token的情况
if ($this->isNotNeedLogin($request) && $token) {
return $next($request);
}
//登录失败
$result = array(
'code' => -1,
'show' => 1,
'msg' => $validateError,
'data' => []
);
return json($result);
}
/**
* @notes 是否需要登录
* @param $request
* @return bool // false-需要; true-不需要
* @author 段誉
* @date 2021/11/10 11:10
*/
private function isNotNeedLogin($request)
{
$controllerObj = invoke('\\app\\kefuapi\\controller\\' . $request->controller());
$data = $controllerObj->like_not_need_login;
if (empty($data)) {
return false;
}
return in_array($request->action(), $data);
}
}

View File

@ -0,0 +1,346 @@
<?php
// +----------------------------------------------------------------------
// | likeshop开源商城系统
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | gitee下载https://gitee.com/likeshop_gitee
// | github下载https://github.com/likeshop-github
// | 访问官网https://www.likeshop.cn
// | 访问社区https://home.likeshop.cn
// | 访问手册http://doc.likeshop.cn
// | 微信公众号likeshop技术社区
// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用未经许可不能去除前后端官方版权标识
// | likeshop系列产品收费版本务必购买商业授权购买去版权授权后方可去除前后端官方版权标识
// | 禁止对系统程序代码以任何目的,任何形式的再发布
// | likeshop团队版权所有并拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeshop.cn.team
// +----------------------------------------------------------------------
namespace app\kefuapi\logic;
use app\common\basics\Logic;
use app\common\enum\ChatRecordEnum;
use app\common\model\kefu\ChatRecord;
USE app\common\logic\ChatLogic as CommonChatLogic;
use app\common\model\kefu\ChatRelation;
use app\common\model\kefu\Kefu;
use app\common\model\kefu\KefuLang;
use app\common\model\order\Order;
use app\common\model\user\User;
use app\common\server\ConfigServer;
use app\common\server\UrlServer;
class ChatLogic extends Logic
{
/**
* @notes 曾对话过的用户
* @param $kefu_id
* @param $shop_id
* @param $get
* @param $page
* @param $size
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author 段誉
* @date 2021/12/14 12:11
*/
public static function getChatUserList($kefu_id, $shop_id, $get, $page, $size)
{
$where[] = ['kefu_id', '=', $kefu_id];
$where[] = ['shop_id', '=', $shop_id];
if (isset($get['nickname']) && $get['nickname']) {
$where[] = ['nickname', 'like', '%' . $get['nickname'] . '%'];
}
$online_user = CommonChatLogic::getOnlineUser();
$exp = 'update_time desc';
if (!empty($online_user)) {
$user_id = implode(",", $online_user);
$exp = "field(user_id," . $user_id . ") desc, update_time desc";
}
// 当前客服曾聊天的记录
$lists = ChatRelation::where($where)
->page($page, $size)
->orderRaw($exp)
->select();
$count = ChatRelation::where($where)->count();
foreach ($lists as &$item) {
$item['online'] = 0;
if (in_array($item['user_id'], $online_user)) {
$item['online'] = 1;
}
if (empty($item['msg'])) {
$item['update_time'] = '';
}
}
return [
'list' => $lists->toArray(),
'page' => $page,
'size' => $size,
'count' => $count,
'more' => is_more($count, $page, $size)
];
}
/**
* @notes 客服与用户的聊天记录
* @param $kefu_id
* @param $user_id
* @param $shop_id
* @param $page
* @param $size
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author 段誉
* @date 2021/12/14 14:41
*/
public static function getChatRecord($kefu_id, $user_id, $shop_id, $page, $size)
{
$map1 = [
['shop_id', '=', $shop_id],
['from_id', '=', $kefu_id],
['from_type', '=', 'kefu'],
['to_id', '=', $user_id],
['type', '=', ChatRecordEnum::TYPE_NORMAL]
];
$map2 = [
['shop_id', '=', $shop_id],
['to_id', '=', $kefu_id],
['to_type', '=', 'kefu'],
['from_id', '=', $user_id],
['type', '=', ChatRecordEnum::TYPE_NORMAL]
];
// 聊天记录
$records = ChatRecord::whereOr([$map1, $map2])
->order('id desc')
->page($page, $size)
->select()->toArray();
$count = ChatRecord::whereOr([$map1, $map2])->count();
$records = CommonChatLogic::formatChatRecords($records, $count, $page, $size);
return $records;
}
/**
* @notes 获取在线客服
* @param $shop_id
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author 段誉
* @date 2021/12/14 14:39
*/
public static function getOnlineKefu($kefu_id, $shop_id)
{
$online = CommonChatLogic::getOnlineKefu($shop_id);
if (empty($online)) {
return [];
}
$map = [
['id', 'in', $online],
['id', '<>', $kefu_id],
['shop_id', '=', $shop_id]
];
$lists = Kefu::where($map)
->field('id,nickname,avatar')
->select()
->toArray();
foreach ($lists as &$item) {
$item['avatar'] = UrlServer::getFileUrl($item['avatar']);
}
return $lists;
}
/**
* @notes 快捷回复列表
* @param $shop_id
* @param $keyword
* @param $page
* @param $size
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author 段誉
* @date 2021/12/15 11:07
*/
public static function getReplyLists($shop_id, $keyword, $page, $size)
{
$condition[] = ['title', 'like', '%' . $keyword . '%'];
$lists = KefuLang::where(['shop_id' => $shop_id])
->where($condition)
->page($page, $size)
->order('sort')
->select();
$count = KefuLang::where(['shop_id' => $shop_id])->count();
return [
'list' => $lists,
'page' => $page,
'size' => $size,
'count' => $count,
'more' => is_more($count, $page, $size)
];
}
/**
* @notes 用户信息接口
* @param $user_id
* @return array|bool
* @author 段誉
* @date 2021/12/15 15:05
*/
public static function getUserInfo($user_id)
{
try {
$user = User::where(['id' => $user_id, 'del' => 0])->field([
'id', 'sn', 'nickname', 'avatar',
'level', 'mobile', 'total_order_amount',
'birthday', 'client', 'create_time'
])->findOrEmpty()->append(['level_name', 'client_desc'])->toArray();
if (empty($user)) {
throw new \Exception('用户不存在');
}
$user['birthday'] = empty($user['birthday']) ? '-' : $user['birthday'];
$user['avatar'] = empty($user['avatar']) ? '' : UrlServer::getFileUrl($user['avatar']);
$user['mobile'] = empty($user['mobile']) ? '-' : substr_replace($user['mobile'],'****',3,4);
return $user;
} catch (\Exception $e) {
self::$error = $e->getMessage();
return false;
}
}
/**
* @notes 订单列表
* @param $get
* @param $shop_id
* @param $page
* @param $size
* @return array|bool
* @author 段誉
* @date 2021/12/15 16:04
*/
public static function getOrderLists($get, $shop_id, $page, $size)
{
try{
if (empty($get['user_id'])) {
throw new \Exception('参数缺失');
}
$condition[] = ['user_id', '=', $get['user_id']];
$condition[] = ['del', '=', 0];
if ($shop_id > 0) {
$condition[] = ['shop_id', '=', $shop_id];
}
if (isset($get['order_sn']) && $get['order_sn'] != '') {
$condition[] = ['order_sn', 'like', '%' . $get['order_sn'] . '%'];
}
$order = new Order();
$count = $order->with('order_goods')->where($condition)->count();
$lists = $order
->where($condition)
->with('order_goods')
->field(['id', 'order_sn', 'order_type', 'order_status', 'order_amount', 'create_time'])
->append(['order_status_text', 'order_type_text'])
->page($page, $size)
->order('id desc')
->select()->toArray();
return [
'list' => $lists,
'page' => $page,
'size' => $size,
'count' => $count,
'more' => is_more($count, $page, $size)
];
} catch (\Exception $e) {
self::$error = $e->getMessage();
return false;
}
}
/**
* @notes 客服详情
* @param $id
* @return array
* @author 段誉
* @date 2021/12/15 17:34
*/
public static function getKefuInfo($id)
{
$res = Kefu::where(['id' => $id])
->field(['id', 'shop_id', 'nickname', 'avatar'])
->findOrEmpty()
->toArray();
$res['avatar'] = empty($res['avatar']) ? '' : UrlServer::getFileUrl($res['avatar']);
$online = CommonChatLogic::getOnlineKefu($res['shop_id']);
$res['online'] = 0;
if(in_array($res['id'], $online)) {
$res['online'] = 1;
}
return $res;
}
/**
* @notes 上传文件域名
* @return array
* @author 段誉
* @date 2021/12/16 17:05
*/
public static function getConfig()
{
$web_favicon = ConfigServer::get('website', 'web_favicon');
return [
'base_domain' => UrlServer::getFileUrl(),
'web_favicon' => !empty($web_favicon) ? UrlServer::getFileUrl($web_favicon) : $web_favicon,
'company_name' => ConfigServer::get('copyright', 'company_name'),
'ws_domain' => env('project.ws_domain', 'ws:127.0.0.1')
];
}
}

View File

@ -0,0 +1,129 @@
<?php
// +----------------------------------------------------------------------
// | likeshop开源商城系统
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | gitee下载https://gitee.com/likeshop_gitee
// | github下载https://github.com/likeshop-github
// | 访问官网https://www.likeshop.cn
// | 访问社区https://home.likeshop.cn
// | 访问手册http://doc.likeshop.cn
// | 微信公众号likeshop技术社区
// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用未经许可不能去除前后端官方版权标识
// | likeshop系列产品收费版本务必购买商业授权购买去版权授权后方可去除前后端官方版权标识
// | 禁止对系统程序代码以任何目的,任何形式的再发布
// | likeshop团队版权所有并拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeshop.cn.team
// +----------------------------------------------------------------------
namespace app\kefuapi\logic;
use app\common\basics\Logic;
use app\common\enum\KefuEnum;
use app\common\model\Admin;
use app\common\model\kefu\KefuSession;
use app\common\model\shop\ShopAdmin;
use app\common\server\UrlServer;
use think\facade\Config;
/**
* 客服登录逻辑
* Class LoginLogic
* @package app\shopapi\logic
*/
class LoginLogic extends Logic
{
/**
* @notes 账号密码登录
* @param $params
* @return mixed
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @author 段誉
* @date 2021/11/9 16:37
*/
public static function accountLogin($params)
{
if (KefuEnum::TYPE_SHOP == $params['type']) {
$kefu = (new ShopAdmin())->alias('a')
->field(['k.id', 'k.nickname', 'k.avatar', 'k.shop_id', 'a.account'])
->join('kefu k', 'a.id = k.admin_id and a.shop_id = k.shop_id')
->where(['a.account' => $params['account'], 'k.del' => 0])
->findOrEmpty()->toArray();
} else {
$kefu = (new Admin())->alias('a')
->field(['k.id', 'k.nickname', 'k.avatar', 'k.shop_id', 'a.account'])
->join('kefu k', 'a.id = k.admin_id')
->where(['a.account' => $params['account'], 'k.shop_id' => 0, 'k.del' => 0])
->findOrEmpty()->toArray();
}
$kefu['avatar'] = !empty($kefu['avatar']) ? UrlServer::getFileUrl($kefu['avatar']) : "";
$kefu['token'] = self::createSession($kefu['id'], $kefu['shop_id'], $params['client']);
return $kefu;
}
/**
* @notes 退出登录
* @param $user_id
* @param $client
* @return KefuSession
* @author 段誉
* @date 2021/11/23 17:14
*/
public static function logout($user_id, $client)
{
return (new KefuSession())
->where(['kefu_id' => $user_id, 'client' => $client])
->update(['update_time' => time(), 'expire_time' => time()]);
}
/**
* @notes 创建会话
* @param $admin_id
* @param $client
* @return string
* @author 段誉
* @date 2021/11/9 16:38
*/
public static function createSession($kefu_id, $shop_id, $client)
{
$result = KefuSession::where(['kefu_id' => $kefu_id, 'client' => $client])->findOrEmpty();
$time = time();
$expire_time = $time + Config::get('project.token_expire_time');
// 新token
$token = md5($kefu_id . $client . $time);
$data = [
'shop_id' => $shop_id,
'kefu_id' => $kefu_id,
'token' => $token,
'client' => $client,
'update_time' => $time,
'expire_time' => $expire_time,
];
if ($result->isEmpty()) {
KefuSession::create($data);
} else {
KefuSession::where([
'kefu_id' => $kefu_id,
'shop_id' => $shop_id,
'client' => $client,
])->update($data);
}
return $token;
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace app\kefuapi\validate;
use app\common\basics\Validate;
use app\common\model\user\User;
class ChatValidate extends Validate
{
protected $rule = [
'user_id' => 'require|checkUser',
];
protected $message = [
'user_id.require' => '参数缺失',
];
protected function checkUser($value, $rule, $data=[])
{
$user = User::where(['id' => $value])->findOrEmpty();
if ($user->isEmpty() || $user['del']) {
return '用户不存在';
}
return true;
}
}

View File

@ -0,0 +1,122 @@
<?php
namespace app\kefuapi\validate;
use app\common\basics\Validate;
use app\common\enum\KefuEnum;
use app\common\logic\ChatLogic;
use app\common\model\Admin;
use app\common\model\shop\ShopAdmin;
use think\facade\Cache;
/**
* 客服登录验证
* Class LoginValidate
* @package app\shopapi\validate
*/
class LoginValidate extends Validate
{
protected $rule = [
'type' => 'require',
'client' => 'require',
'account' => 'require',
'password' => 'require|checkPassword',
];
protected $message = [
'type.require' => '参数缺失',
'account.require' => '请输入账号',
'password.require' => '请输入密码',
'password.checkPassword' => '账号或密码错误',
'client.require' => '请输入客户端'
];
/**
* @notes 校验密码
* @param $password
* @param $other
* @param $data
* @return bool|string
* @author 段誉
* @date 2021/11/9 16:02
*/
protected function checkPassword($password, $other, $data)
{
$field = 'k.id, k.shop_id, a.account, a.salt, a.password,
k.disable as kefu_disable, a.disable as admin_disable';
$condition = ['a.account' => $data['account'], 'k.del' => 0, 'a.del' => 0];
if (KefuEnum::TYPE_SHOP == $data['type']) {
$isPlatform = false;
$chat = (new ShopAdmin())->alias('a')
->field($field)
->join('kefu k', 'a.id = k.admin_id and a.shop_id = k.shop_id')
->where($condition)
->findOrEmpty();
} else {
$isPlatform = true;
$chat = (new Admin())->alias('a')
->field($field)
->join('kefu k', 'k.admin_id = a.id')
->where(['k.shop_id' => 0])
->where($condition)
->findOrEmpty();
}
if (false === $this->safe(false, $isPlatform)) {
$this->message['password.password'] .= ':多次输入错误';
return false;
}
if ($chat->isEmpty()) {
$this->safe(true, $isPlatform);
return '账号不存在';
}
if ($chat['kefu_disable'] || $chat['admin_disable']) {
return '账号被禁用';
}
$password = generatePassword($password, $chat['salt']);
if ($password != $chat['password']) {
$this->safe(true, $isPlatform);
return false;
}
// 检查后台配置是否开启当前缓存驱动是否redis
if (false === ChatLogic::checkConfig($chat['shop_id'])) {
return ChatLogic::getError() ?: '请联系管理员设置后台配置';
}
return true;
}
/**
* 连续30分钟内15次输错密码无法登录
* @param bool $add
* @return bool
*/
protected function safe($status = false, $isPlatform = false)
{
if ($isPlatform) {
$errorCount = 'platform_kefu_error_count' . request()->ip();
} else {
$errorCount = 'shop_kefu_error_count' . request()->ip();
}
if ($status) {
$loginErrorCount = Cache::get($errorCount);
$loginErrorCount++;
Cache::tag('kefu_login_error_count')->set($errorCount, $loginErrorCount, 1800);
}
$count = Cache::get($errorCount);
if (!empty($count) && $count >= 15) {
return false;
}
return true;
}
}

View File

@ -0,0 +1,106 @@
<?php
// +----------------------------------------------------------------------
// | likeshop开源商城系统
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | gitee下载https://gitee.com/likeshop_gitee
// | github下载https://github.com/likeshop-github
// | 访问官网https://www.likeshop.cn
// | 访问社区https://home.likeshop.cn
// | 访问手册http://doc.likeshop.cn
// | 微信公众号likeshop技术社区
// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用未经许可不能去除前后端官方版权标识
// | likeshop系列产品收费版本务必购买商业授权购买去版权授权后方可去除前后端官方版权标识
// | 禁止对系统程序代码以任何目的,任何形式的再发布
// | likeshop团队版权所有并拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeshop.cn.team
// +----------------------------------------------------------------------
namespace app\kefuapi\validate;
use app\common\basics\Validate;
use app\common\model\Admin;
use app\common\model\kefu\Kefu;
use app\common\model\kefu\KefuSession;
use app\common\model\shop\ShopAdmin;
/**
* 客服登录验证
* Class TokenValidate
* @package app\kefu\validate
*/
class TokenValidate extends Validate
{
protected $rule = [
'token' => 'require|valid|chat',
];
/**
* @notes token验证
* @param $token
* @param $other
* @param $data
* @return bool|string
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author 段誉
* @date 2021/11/23 10:55
*/
protected function valid($token, $other, $data)
{
$session = KefuSession::where(['token' => $token])->find();
if (empty($session)) {
return '会话失效,请重新登录';
}
if ($session['expire_time'] <= time()) {
return '登录超时,请重新登录';
}
return true;
}
/**
* @notes 用户验证
* @param $token
* @param $other
* @param $data
* @return bool|string
* @author 段誉
* @date 2021/11/23 17:29
*/
protected function chat($token, $other, $data)
{
$kefu = (new Kefu())->alias('k')
->join('kefu_session ks', 'k.id = ks.kefu_id')
->where(['ks.token' => $token, 'k.del' => 0])
->field('k.*,ks.token,ks.client')
->hidden(['password'])
->findOrEmpty();
if ($kefu->isEmpty()) {
return '用户不存在';
}
// 获取客服对应的管理员信息
if ($kefu['shop_id'] > 0) {
$kefu_admin = (new ShopAdmin())->where(['id' => $kefu['admin_id']])->findOrEmpty();
} else {
$kefu_admin = (new Admin())->where(['id' => $kefu['admin_id']])->findOrEmpty();
}
if ($kefu_admin->isEmpty()) {
return '关联管理员不存在';
}
if ($kefu['disable'] == 1 || $kefu_admin['disable'] == 1) {
return '用户被禁用';
}
return true;
}
}