| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475 |
- <?php
- namespace WebIM;
- class Storage
- {
- /**
- * @var \redis
- */
- protected $redis;
- const PREFIX = 'webim';
- const CUSTOMER = 'customer';
- const MERCHANT = 'merchant';
- function __construct($config)
- {
- $this->redis = \Swoole::getInstance()->redis;
- //清除操作
- $this->clearCache();
- $this->config = $config;
- }
- /**
- * redis设置密码
- */
- function storageAuth(){
- $this->redis->auth('byt6gc1w0aed2q7h');
- }
- /**
- * 清除缓存
- */
- function clearCache(){
- $this->redis->delete(self::PREFIX.':online');//在线聊天的clientId
- $keys = [
- self::CUSTOMER . ':customer_*:merchant_*',//在线客户存入缓存 -- 客户 to 商家
- self::MERCHANT . ':customer_*:merchant_*',//在线商家存入缓存 -- 商家 to 客户
- self::CUSTOMER . ':chatListClient:*',//客户信息
- self::MERCHANT . ':chatListClient:*',//商家信息
- self::CUSTOMER . ':chatListOnline:uid_*',//客户打开的所有clientId
- self::MERCHANT . ':chatListOnline:mid_*'//商家户打开的所有clientId
- ];
- foreach ($keys as $key){
- $allKeys = $this->redis->keys($key);
- foreach ($allKeys as $redisKey){
- $this->redis->delete($redisKey);
- }
- }
- }
- /**
- * 会话保存,保存客户端-用户信息
- * @param $client_id
- * @param $info
- * @param $uid
- */
- function login($client_id, $info, $uid=0, $chatUid=0, $isCustomer=false)
- {
- if($uid > 0){
- if($isCustomer){
- $this->setCustomerC2M($client_id, $uid, $chatUid);
- }else{
- $this->setMerchantM2C($client_id, $chatUid, $uid);
- }
- }
- $this->redis->set(self::PREFIX . ':client:' . $client_id, json_encode($info));
- $this->redis->sAdd(self::PREFIX . ':online', $client_id);
- }
- /**
- * 聊天界面退出
- * @param $client_id
- */
- function logout($client_id)
- {
- $data = $this->redis->get(self::PREFIX . ':client:' . $client_id);
- $userInfo = json_decode($data, true);
- $chatUid = $userInfo['chatUid'];
- if($userInfo['isCustomer'] == 1){
- $this->delCustomerC2M($userInfo['uid'], $chatUid, $client_id);
- }else{
- $this->delMerchantM2C($chatUid, $userInfo['uid'], $client_id);
- }
- $this->redis->del(self::PREFIX.':client:'.$client_id);
- $this->redis->sRemove(self::PREFIX.':online', $client_id);
- }
- /**
- * 聊天对象列表页登录
- * @param $client_id
- * @param $info
- * @param int $uid
- * @param bool $isCustomer
- */
- function chatListLogin($client_id, $info, $uid=0, $isCustomer=false){
- if($isCustomer){
- $this->addCustomerChatList($client_id, $uid, $info);
- }else{
- $this->addMerchantChatList($client_id, $uid, $info);
- }
- }
- /**
- * 添加客户聊天clientId
- * @param $clientId
- * @param $customerId
- * @param $info
- */
- function addCustomerChatList($clientId, $customerId, $info){
- $this->redis->set(self::CUSTOMER . ':chatListClient:'.$clientId, json_encode($info));//客户信息
- $this->redis->sAdd(self::CUSTOMER . ':chatListOnline:uid_'.$customerId, $clientId);//此客户打开的所有clientId
- }
- /**
- * 获取客户所有聊天clientId
- * @param $clientId
- * @param $customerId
- * @param $info
- */
- function getCustomerChatListStatus($clientId, $customerId = 0){
- if($customerId == 0){
- $customer = $this->redis->get(self::CUSTOMER . ':chatListClient:'.$clientId);//获取客户信息
- $customer = json_decode($customer, true);
- $customerId = $customer['uid'];
- }
- return $this->redis->sMembers(self::CUSTOMER . ':chatListOnline:uid_'.$customerId);//此客户打开的所有clientId
- }
- /**
- * 添加商家聊天clientId
- * @param $clientId
- * @param $sjId
- * @param $info
- */
- function addMerchantChatList($clientId, $sjId, $info){
- $this->redis->set(self::MERCHANT . ':chatListClient:'.$clientId, json_encode($info));//商家信息
- $this->redis->sAdd(self::MERCHANT . ':chatListOnline:mid_'.$sjId, $clientId);//商家户打开的所有clientId
- }
- /**
- * 获取商家信息
- * @param $clientId
- * @return bool|string
- */
- function getMerchant($clientId){
- $merchant = $this->redis->get(self::MERCHANT . ':chatListClient:'.$clientId);//获取商家信息
- return json_decode($merchant, true);
- }
- /**
- * 获取商家所有聊天clientId
- * @param $clientId
- * @param $sjId
- * @param $info
- */
- function getMerchantChatList($clientId, $sjId = 0){
- if($sjId == 0){
- $merchant = $this->redis->get(self::MERCHANT . ':chatListClient:'.$clientId);//获取商家信息
- $merchant = json_decode($merchant, true);
- $sjId = $merchant['uid'];
- }
- return $this->redis->sMembers(self::MERCHANT . ':chatListOnline:mid_'.$sjId);//商家户打开的所有clientId
- }
- /**
- * 商家聊天对象列表页退出
- * @param $client_id
- */
- function merchantChatListLogout($clientId){
- $data = $this->redis->get(self::MERCHANT . ':chatListClient:' . $clientId);
- $userInfo = json_decode($data, true);
- $sjId = $userInfo['uid'];
- $this->redis->del(self::MERCHANT . ':chatListClient:' . $clientId);
- $this->redis->sRemove(self::MERCHANT . ':chatListOnline:mid_' . $sjId, $clientId);
- }
- /**
- * 用户在线用户列表
- * @return array
- */
- function getOnlineUsers()
- {
- return $this->redis->sMembers(self::PREFIX . ':online');
- }
- /**
- * 批量获取用户信息
- * @param $users
- * @return array
- */
- function getUsers($users)
- {
- $keys = array();
- $ret = array();
- foreach ($users as $v)
- {
- $keys[] = self::PREFIX . ':client:' . $v;
- }
- $info = $this->redis->mget($keys);
- foreach ($info as $v)
- {
- $ret[] = json_decode($v, true);
- }
- return $ret;
- }
- /**
- * 获取单个用户信息
- * @param $clientId
- * @return bool|mixed
- */
- function getUser($clientId)
- {
- $ret = $this->redis->get(self::PREFIX . ':client:' . $clientId);
- $info = json_decode($ret, true);
- return $info;
- }
- function exists($clientId)
- {
- return $this->redis->exists(self::PREFIX . ':client:' . $clientId);
- }
- function keysExists($key){
- return $this->redis->exists($key);
- }
- function getClient($clientId)
- {
- $data = $this->redis->get(self::PREFIX . ':client:' . $clientId);
- return json_decode($data, true);
- }
- /**
- * 在线客户存入缓存 -- 客户 to 商家
- * @param $clientId
- * @param $customerId
- * @param $sjId
- */
- function setCustomerC2M($clientId, $customerId, $sjId){
- //$this->redis->set(self::CUSTOMER . ':customer_'.$customerId.':merchant_' . $sjId, $clientId);
- $this->redis->sAdd(self::CUSTOMER . ':customer_'.$customerId.':merchant_' . $sjId, $clientId);
- }
- /**
- * 获取在线客户的clientIds -- 客户 to 商家
- * @param $clientId
- * @param $customerId
- * @param $sjId
- */
- function getCustomerC2M($customerId, $sjId){
- //return $this->redis->get(self::CUSTOMER . ':customer_'.$customerId.':merchant_' . $sjId);
- return $this->redis->sMembers(self::CUSTOMER . ':customer_'.$customerId.':merchant_' . $sjId);
- }
- /**
- * 删除客户缓存 -- 客户 to 商家
- * @param $customerId
- * @param $sjId
- */
- function delCustomerC2M($customerId, $sjId, $clientId){
- //$this->redis->del(self::CUSTOMER . ':customer_'.$customerId.':merchant_' . $sjId);
- $this->redis->sRemove(self::CUSTOMER . ':customer_'.$customerId.':merchant_' . $sjId, $clientId);
- }
- /**
- * 客户在线的客户端数
- * @param $customerId
- * @param $sjId
- * @return int
- */
- function existCustomerClientC2M($customerId, $sjId){
- return $this->redis->sCard(self::CUSTOMER . ':customer_'.$customerId.':merchant_' . $sjId);
- }
- /**
- * 在线商家存入缓存 -- 商家 to 客户
- * @param $clientId
- * @param $customerId
- * @param $sjId
- */
- function setMerchantM2C($clientId, $customerId, $sjId){
- //$this->redis->set(self::MERCHANT . ':customer_'.$customerId.':merchant_' . $sjId, $clientId);
- $this->redis->sAdd(self::MERCHANT . ':customer_'.$customerId.':merchant_' . $sjId, $clientId);
- }
- /**
- * 获取在线商家的clientIds -- 商家 to 客户
- * @param $clientId
- * @param $customerId
- * @param $sjId
- */
- function getMerchantM2C($customerId, $sjId){
- //return $this->redis->get(self::MERCHANT . ':customer_'.$customerId.':merchant_' . $sjId);
- return $this->redis->sMembers(self::MERCHANT . ':customer_'.$customerId.':merchant_' . $sjId);
- }
- /**
- * 删除商家缓存 -- 商家 to 客户
- * @param $customerId
- * @param $sjId
- */
- function delMerchantM2C($customerId, $sjId, $clientId){
- //$this->redis->del(self::MERCHANT . ':customer_'.$customerId.':merchant_' . $sjId);
- $this->redis->sRemove(self::MERCHANT . ':customer_'.$customerId.':merchant_' . $sjId, $clientId);
- }
- /**
- * 商家在线的客户端数
- * @param $customerId
- * @param $sjId
- * @return int
- */
- function existMerchantClientC2M($customerId, $sjId){
- return $this->redis->sCard(self::MERCHANT . ':customer_'.$customerId.':merchant_' . $sjId);
- }
- /**
- * 记录消息
- * @param $userid
- * @param $msg
- */
- function addHistory($userid, $msg)
- {
- $info = $this->getUser($userid);
- $log['user'] = $info;
- $log['msg'] = $msg;
- $log['time'] = time();
- $log['type'] = empty($msg['type']) ? '' : $msg['type'];
- $chatLogKey = $info['chatLogKey'];
- $isCustomer = $info['isCustomer'];
- $role = $isCustomer ? 'customer' : 'merchant';
- $now = time();
- $this->redis->zAdd($chatLogKey, $now, $now.'{~CHAT~}'.$role.'{~CHAT~}'.$msg);
- }
- /**
- * 直接从参数中取key,记录消息
- * @param $data 缓存相关数据
- * @param $msg
- */
- function addHistoryByData($data, $msg)
- {
- $info = $data;
- $chatLogKey = $info['chatLogKey'];
- $isCustomer = $info['isCustomer'];
- $role = $isCustomer ? 'customer' : 'merchant';
- $now = time();
- $this->redis->zAdd($chatLogKey, $now, $now.'{~CHAT~}'.$role.'{~CHAT~}'.$msg);
- }
- function getHistory($clientId, $offset = 0, $num = 100)
- {
- $userInfo = $this->getUser($clientId);
- $chatLogKey = $userInfo['chatLogKey'];
- $return = $this->redis->zRange($chatLogKey, 0, -1, 'WITHSCORES');
- $conList = [];
- if(!empty($return)){
- foreach($return as $key => $val){
- $time = $val;
- $arr = explode('{~CHAT~}', $key);
- if(count($arr) == 3){
- $createTime = $arr[0];
- $msgBelongTo = $arr[1];
- $msg = $arr[2];
- $conArr = ['msgBelongTo' => $msgBelongTo,'content' => $msg,'createTime' =>$createTime];
- $conList[] = $conArr;
- }
- }
- return $conList;
- }
- return [];
- }
- /**
- * 记录发来新消息的客户
- * @param $customerId
- * @param $sjId
- * @param $time
- */
- function addMerchantNewChater($customerId, $sjId, $time){
- $key = "merchant_{$sjId}_new_msg";
- $this->redis->zAdd($key, $time, $customerId);//记录商家的新消息用户有谁
- }
- /**
- * 记录商家聊过天的客户
- * @param $customerId
- * @param $sjId
- * @param $time
- */
- function addMerchantChaterLog($customerId, $sjId, $time){
- $key = "merchant_{$sjId}_history_chat_user";
- $this->redis->zAdd($key, $time, $customerId);//记录商家的最近联系人(1商家 对 n客户)
- }
- /**
- * 获取商家与客户的聊天缓存记录
- * @param $customerId
- * @param $sjId
- * @param $time
- */
- function getMerchantChaterLog($sjId){
- $key = "merchant_{$sjId}_new_msg";
- $newMsgCustomers = $this->redis->zRange($key, 0, -1, 'WITHSCORES');//商家的新消息用户有谁
- $newMsgCustomers = $this->cacheToArray($newMsgCustomers);
- $key_2 = "merchant_{$sjId}_history_chat_user";
- $historyCustomers = $this->redis->zRange($key_2, 0, -1, 'WITHSCORES');//商家的最近联系人(1商家 对 n客户)
- $historyCustomers = $this->cacheToArray($historyCustomers);
- return ['newMsgCustomers'=>$newMsgCustomers, 'historyCustomers'=>$historyCustomers];
- }
- /**
- * 移除商家的新消息用户
- * @param $customerId
- * @param $sjId
- */
- function remMerchantChater($customerId, $sjId){
- $key = "merchant_{$sjId}_new_msg";
- $this->redis->zRem($key, $customerId);//移除商家的新消息用户
- }
- /**
- * 获取客户的聊天缓存记录
- * @param $customerId
- * @param $sjId
- * @param $time
- */
- function addCustomerChaterLog($customerId, $sjId, $time){
- }
- /**
- * 有序集合 转 数组
- * @param $cacheData
- * @return array
- */
- function cacheToArray($cacheData){
- $array = [];
- if(!empty($cacheData) && is_array($cacheData)){
- foreach($cacheData as $key => $val){
- $array[$val] = $key;
- }
- }
- return $array;
- }
- /**
- * 获取客户用户信息
- * @param $key 缓存键
- * @return array
- */
- function getCustomer($key){
- $user = $this->redis->hGetAll($key);
- return $user;
- }
- }
|