123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- namespace app\common\logic;
-
-
- use app\common\basics\Logic;
- use app\common\enum\ChatMsgEnum;
- use app\common\model\kefu\ChatRelation;
- use app\common\model\kefu\Kefu;
- use app\common\model\user\User;
- use app\common\server\ConfigServer;
- use app\common\server\UrlServer;
- use app\common\utils\Redis;
-
- class ChatLogic extends Logic
- {
-
-
-
- public static function getOnlineKefu($shop_id)
- {
- $key = self::getChatPrefix() . 'shop_' . $shop_id . '_kefu';
- return (new Redis())->getSmembersArray($key);
- }
-
-
-
-
- public static function getOnlineUser()
- {
- $key = self::getChatPrefix() . 'user';
- return (new Redis())->getSmembersArray($key);
- }
-
-
-
-
- public static function formatChatRecords($records, $count, $page, $size)
- {
- if (empty($records)) {
- return [
- 'list' => $records,
- 'page' => $page,
- 'size' => $size,
- 'count' => $count,
- 'more' => is_more($count, $page, $size)
- ];
- }
-
- $kefu = [];
- $user = [];
-
-
- foreach ($records as $item) {
- if ($item['from_type'] == 'kefu') {
- $kefu[] = $item['from_id'];
- } else {
- $user[] = $item['from_id'];
- }
- }
-
- $kefu = array_unique($kefu);
- $user = array_unique($user);
-
-
- $kefu = Kefu::where('id', 'in', $kefu)->column('nickname, avatar', 'id');
- $user = User::where('id', 'in', $user)->column('nickname, avatar', 'id');
-
-
- foreach ($records as &$item) {
- $item['from_nickname'] = '';
- $item['from_avatar'] = '';
-
- if ($item['from_type'] == 'kefu') {
- $kefu_id = $item['from_id'];
- if (isset($kefu[$kefu_id])) {
- $item['from_nickname'] = $kefu[$kefu_id]['nickname'] ?? '';
- $item['from_avatar'] = $kefu[$kefu_id]['avatar'] ?? '';
- }
- }
-
- if ($item['from_type'] == 'user') {
- $user_id = $item['from_id'];
- if (isset($user[$user_id])) {
- $item['from_nickname'] = $user[$user_id]['nickname'] ?? '';
- $item['from_avatar'] = $user[$user_id]['avatar'] ?? '';
- }
- }
-
-
- $item['goods'] = [];
- if ($item['msg_type'] == ChatMsgEnum::TYPE_GOODS) {
- $item['goods'] = json_decode($item['msg'], true);
- }
-
- $item['create_time_stamp'] = strtotime($item['create_time']);
- }
-
- $records = array_reverse($records);
-
- return [
- 'list' => $records,
- 'page' => $page,
- 'size' => $size,
- 'count' => $count,
- 'more' => is_more($count, $page, $size)
- ];
- }
-
-
-
-
- public static function getConfig($shop_id)
- {
-
- if (self::getConfigSetting($shop_id) == 1) {
- return ['code' => 0, 'msg' => ''];
- }
-
-
- if ('redis' != self::getCacheDrive()) {
- return ['code' => 0, 'msg' => '请参考部署文档配置在线客服'];
- }
-
-
- $online = self::getOnlineKefu($shop_id);
- if (empty($online)) {
- return ['code' => 0, 'msg' => '当前客服不在线,有问题请联系人工客服'];
- }
-
- return ['code' => 1, 'msg' => ''];
- }
-
-
-
-
- public static function checkConfig(int $shop_id = 0)
- {
- try {
- if (self::getConfigSetting($shop_id) == 1) {
- throw new \Exception('请联系管理员开启在线客服');
- }
- if ('redis' != self::getCacheDrive()) {
- throw new \Exception('请参考部署文档配置在线客服');
- }
- return true;
- } catch (\Exception $e) {
- self::$error = $e->getMessage();
- return false;
- }
- }
-
-
-
- public static function bindRelation($user_id, $kefu_id, $shop_id, $data, $is_read = 0)
- {
- $relation = ChatRelation::where(['user_id' => $user_id, 'shop_id' => $shop_id])->findOrEmpty();
-
- $user = User::where(['id' => $user_id])->findOrEmpty();
-
- if ($relation->isEmpty()) {
- $relation = ChatRelation::create([
- 'shop_id' => $shop_id,
- 'user_id' => $user_id,
- 'kefu_id' => $kefu_id,
- 'nickname' => $user['nickname'],
- 'avatar' => $user['avatar'],
- 'client' => $data['client'] ?? 0,
- 'msg' => $data['msg'] ?? '',
- 'msg_type' => $data['msg_type'] ?? ChatMsgEnum::TYPE_TEXT,
- 'is_read' => 1,
- 'create_time' => time(),
- 'update_time' => time(),
- ]);
- } else {
- ChatRelation::update(
- [
- 'kefu_id' => $kefu_id,
- 'nickname' => $user['nickname'],
- 'avatar' => $user['avatar'],
- 'client' => $data['client'] ?? 0,
- 'msg' => $data['msg'] ?? '',
- 'msg_type' => $data['msg_type'] ?? ChatMsgEnum::TYPE_TEXT,
- 'update_time' => time(),
- 'is_read' => $is_read
- ],
- ['id' => $relation['id']]
- );
- }
-
- return $relation['id'];
- }
-
-
-
-
- public static function getConfigSetting($shop_id)
- {
-
- if ($shop_id > 0) {
- $config = ConfigServer::get('shop_customer_service', 'type', 1, $shop_id);
- } else {
- $config = ConfigServer::get('customer_service', 'type', 1);
- }
- return $config;
- }
-
-
-
-
- public static function getCacheDrive()
- {
- return config('cache.default');
- }
-
-
-
- public static function getChatPrefix()
- {
- return config('default.websocket_prefix');
- }
-
-
-
-
- public static function setChatDisable($shop_id, $kefu_id)
- {
- $cache = new Redis();
- $prefix = self::getChatPrefix();
- $key = $prefix . 'shop_' . $shop_id . '_kefu';
-
- $result = $cache->getSmembersArray($key);
- $fds = $cache->getSmembersArray($prefix . 'kefu_' . $kefu_id);
-
- if (in_array($kefu_id, $result) && $fds) {
- $cache->srem($key, $kefu_id);
- foreach ($fds as $fd) {
- $cache->srem($prefix . 'kefu_' . $kefu_id, $fd);
- $cache->del($prefix . 'fd_' . $fd);
- }
- }
- }
-
-
- }
|