| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849 |
- <?php
- /**
- * raingad IM [ThinkPHP6]
- * @author xiekunyu <raingad@foxmail.com>
- */
- namespace app\enterprise\model;
- use GatewayClient\Gateway;
- use app\BaseModel;
- use think\facade\Db;
- use think\model\concern\SoftDelete;
- use app\manage\model\Config;
- use thans\jwt\facade\JWTAuth;
- use app\admin\model\Sign;
- use app\admin\model\KefuWork;
- use app\admin\model\KefuTime;
- use app\admin\model\Department;
- class User extends BaseModel
- {
- use SoftDelete;
- protected $pk = "user_id";
-
- public static $defaultField = 'user_id,realname,realname as displayName,account,avatar,name_py,email,last_login_ip,service_status,cs_uid,timeout_type,remark';
- protected $json = ['setting'];
- protected $jsonAssoc = true;
- // 系统人员或者其他静态人员
- public static function staticUser(){
- return [
- 'adminNotice'=>[
- 'id'=>'admin_notice',
- 'displayName'=>lang('system.notice'),
- 'avatar'=>getMainHost().'/static/common/img/notice.png',
- 'name_py'=>'xitongtongzhi',
- ],
- 'fileTransfer'=>[
- 'id'=>-1,
- 'displayName'=>lang('system.favor'),
- 'avatar'=>getMainHost().'/static/common/img/file_transfer.png',
- 'name_py'=>'wodeshoucang',
- ]
- ];
- }
- public function getUid()
- {
- return self::$uid;
- }
- //查询用户信息
- public static function getUserInfo($map=[])
- {
- if(!$map){
- return self::$userInfo;
- }
- $data = self::where($map)->find();
- if ($data) {
- $data = $data->toArray();
- }
- return $data;
- }
-
- /**
- * 刷新用户token 之前token将被拉黑
- * 修改用户数据后 调用该方法 并返回前台更新token
- * @param array $info 用户信息
- * @param string $terminal 客户端标识
- * @return string
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function refreshToken($info,$terminal)
- {
- $info = str_encipher(json_encode($info),true, config('app.aes_token_key'));
- $authToken = 'bearer '.JWTAuth::builder(['info' => $info, 'terminal' => $terminal]);
- return $authToken;
- }
- // 获取所有用户列表
- public static function getAllUser($map, $user_ids = [],$user_id,$group_id = 0)
- {
- $field = self::$defaultField;
- $list=[];
- if($group_id){
- $groupUser=GroupUser::where([['group_id','=',$group_id],['role','<>',1],['status','=',1]])->column('user_id');
- if($groupUser){
- $list=User::where([['user_id','in',$groupUser]])->field($field)->select()->toArray();
- }
- }else{
- $config=Config::getSystemInfo();
- // 如果是社区模式,就只查询自己的好友,如果是企业模式,就查询所有用户
- if($config['sysInfo']['runMode']==1){
- $list = self::where($map)->field($field)->select()->toArray();
- }else{
- $friendList = Friend::getFriend(['create_user' => $user_id,'status'=>1]);
- $userList = array_keys($friendList);
- $list = self::where($map)->where('user_id', 'in', $userList)->field($field)->select()->toArray();
- }
- }
- foreach ($list as $k => $v) {
- $list[$k]['disabled'] = false;
- $list[$k]['avatar'] = avatarUrl($v['avatar'], $v['realname'], $v['user_id']);
- if ($user_ids) {
- if (in_array($v['user_id'], $user_ids)) {
- $list[$k]['disabled'] = true;
- }
- }
- }
- return $list;
- }
- //查询用户列表
- public static function getUserList($userInfo, $map, $user_id, $field = "", $role = 0)
- {
- if (!$field) {
- $field = self::$defaultField;
- }
-
- $config=Config::getSystemInfo();
- // 如果是社区模式,就只查询自己的好友,如果是企业模式,就查询所有用户
- if($config['sysInfo']['runMode']==1){
- $friendList = Friend::getFriend(['create_user' => $user_id]);
- if ($role > 0) {
- $list = self::where($map)->whereOr('cs_uid', 0)->field($field)->select();
- } else {
- $list = self::where($map)->field($field)->select();
- }
- }else{
- $userList = [];
- // 将专属客服设置为好友
- $csUid=$userInfo['cs_uid'] ?? 0;
- if($csUid){
- $userList[]=$csUid;
- }
- // 如果是管理员,就查询整个部门的客服好友
- if ($userInfo['role'] > 0) {
- $friendList = Friend::getFriend(['create_user' => $user_id,'status'=>1]);//管理员自己的好友
- $userList = array_merge($userList, array_keys($friendList));
-
- $admin_id = $userInfo['uid'];
- $department_cs_uids = Department::getDepartmentCsUids($admin_id);//整个部门的用户
- $department_cs_uids[] = $user_id;
- $cus=self::whereIn('cs_uid',$department_cs_uids)->column('user_id');
- if($cus){
- $userList=array_merge($userList,$cus);
- }
- }
-
- $userList = array_unique($userList);
- $map[] = ['user_id','in',$userList];
- $list = self::where($map)->whereOr(function ($query) use ($role) {
- if ($role > 0) {
- $query->where('role', 0)->where('service_status', 0)->where('is_online', 1);
- }
- })->field($field)->select();
- }
- $list_chart = chartSort($list, 'realname', false, 'index');
- // 查询未读消息
- $unread = Db::name('message')
- ->field('from_user,count(msg_id) as unread')
- ->where([['to_user', '=', $user_id], ['is_read', '=', 0], ['is_group', '=', 0]])
- ->group('from_user')
- ->select();
- // 查询最近的联系人
- $map1 = [['to_user', '=', $user_id], ['is_last', '=', 1], ['is_group', '=', 0]];
- $map2 = [['from_user', '=', $user_id], ['is_last', '=', 1], ['is_group', '=', 0]];
- $msgField = 'from_user,to_user,content as lastContent,create_time as lastSendTime,chat_identify,type,del_user';
- $lasMsgList = Db::name('message')
- ->field($msgField)
- ->whereOr([$map1, $map2])
- ->order('create_time desc')
- ->select();
- // 查询群聊
- $group = Group::getMyGroup(['gu.user_id' => $user_id, 'gu.status' => 1]);
- if ($group) {
- $group = $group->toArray();
- $group_ids = arrayToString($group, 'group_id');
- $getGroupLastMsg = Db::name('message')->field($msgField)->where([['to_user', 'in', $group_ids], ['is_group', '=', 1], ['is_last', '=', 1]])->select();
- $getAtMsg=Db::name('message')->field($msgField)->where([['to_user', 'in', $group_ids], ['is_group', '=', 1]])->whereFindInSet('at',$user_id)->select();
- // halt($getAtMsg);
- foreach ($group as $k => $v) {
- $setting = $v['setting'] ? json_decode($v['setting'], true) : ['manage' => 0, 'invite' => 1, 'nospeak' => 0];
- $group_id = 'group-' . $v['group_id'];
- $group[$k]['id'] = $group_id;
- $group[$k]['account'] = $group_id;
- $group[$k]['avatar'] = avatarUrl($v['avatar'], $v['displayName'], $v['group_id'], 120,1);
- $group[$k]['name_py'] = $v['name_py'];
- $group[$k]['owner_id'] = $v['owner_id'];
- $group[$k]['role'] = $v['role'];
- $group[$k]['is_group'] = 1;
- $group[$k]['setting'] = $setting;
- $group[$k]['index'] = "[2]群聊";
- $group[$k]['realname'] = $v['displayName'] . " [群聊]";
- $group[$k]['is_notice'] = $v['is_notice'];
- $group[$k]['is_top'] = $v['is_top'];
- $group[$k]['is_online'] = 1;
- $group[$k]['is_at'] = 0;
- if ($getGroupLastMsg) {
- foreach ($getGroupLastMsg as $key=>$val) {
- if ($val['to_user'] == $v['group_id']) {
- $group[$k]['type'] =$val['type'];
- $group[$k]['lastContent'] = str_encipher($val['lastContent'],false);
- $group[$k]['lastSendTime'] = $val['lastSendTime'] * 1000;
- // 已经赋值了删除掉提升下次循环的性能
- unset($getGroupLastMsg[$key]);
- break;
- }
- }
- }
- if($getAtMsg){
- foreach ($getAtMsg as $key=> $val) {
- if ($val['to_user'] == $v['group_id']) {
- ++$group[$k]['is_at'];
- // 已经赋值了删除掉提升下次循环的性能
- unset($getAtMsg[$key]);
- }
- }
- }
- }
- }
- try{
- Gateway::$registerAddress = config('gateway.registerAddress');
- $onlineList=Gateway::getAllUidList();
- }catch(\Exception $e){
- $onlineList=[];
- }
- $isRegion=$config['sysInfo']['ipregion'] ?? 0;
- foreach ($list_chart as $k => $v) {
- // 是否有消息通知或者置顶聊天
- $friend = isset($friendList[$v['user_id']]) ? $friendList[$v['user_id']] : [];
- $list_chart[$k]['id'] = $v['user_id'];
- $list_chart[$k]['displayName'] = ($friend['nickname'] ?? '') ? : $v['realname'];
- $list_chart[$k]['name_py'] = $v['name_py'];
- $list_chart[$k]['avatar'] = avatarUrl($v['avatar'], $v['realname'], $v['user_id'], 120);
- $list_chart[$k]['lastContent'] = '';
- $list_chart[$k]['unread'] = 0;
- $list_chart[$k]['lastSendTime'] = $list_chart[$k]['lastSendTime'] * 1000;//time() * 1000;
- $list_chart[$k]['is_group'] = 0;
- $list_chart[$k]['setting'] = [];
- $list_chart[$k]['is_at'] = 0;
- $list_chart[$k]['last_login_ip'] = '';
- $list_chart[$k]['location'] ="";
- $list_chart[$k]['cs_uid'] = $v['cs_uid'];
- $list_chart[$k]['service_status'] = $v['service_status'];
- if($isRegion){
- $list_chart[$k]['last_login_ip'] = $v['last_login_ip'];
- $list_chart[$k]['location'] =$v['last_login_ip'] ? implode(" ", \Ip::find($v['last_login_ip'])) : "未知";
- }
- $is_online=0;
- if($v['is_online'] == 0 && isset($onlineList[$v['user_id']])){
- $is_online=1;
- }
- $list_chart[$k]['is_online'] = $is_online;
-
- $is_top = 0;
- $is_notice = 1;
- if ($friend) {
- $is_top = $friend['is_top'];
- $is_notice = $friend['is_notice'];
- }
- $list_chart[$k]['is_top'] = $is_top;
- $list_chart[$k]['is_notice'] = $is_notice;
- if ($unread) {
- foreach ($unread as $val) {
- if ($val['from_user'] == $v['user_id']) {
- $list_chart[$k]['unread'] = $val['unread'];
- break;
- }
- }
- }
- if ($lasMsgList) {
- foreach ($lasMsgList as $val) {
- if ($val['from_user'] == $v['user_id'] || $val['to_user'] == $v['user_id']) {
- $content = str_encipher($val['lastContent'],false);
- // 屏蔽已删除的消息
- if ($val['del_user']) {
- $delUser = explode(',', $val['del_user']);
- if (in_array($user_id, $delUser)) {
- $content = "";
- }
- }
- $list_chart[$k]['type'] = $val['type'];
- $list_chart[$k]['lastContent'] = $content;
- $list_chart[$k]['lastSendTime'] = $val['lastSendTime'] * 1000;
- break;
- }
- }
- }
- }
- // 合并群聊和联系人
- $data = array_merge($list_chart, $group);
- // 合并助手消息和聊天消息
- $helper=self::otherChat($user_id);
- $data=array_merge($data,$helper);
- return $data;
- }
- //查询用户列表
- public static function getChatList($user_id, $field = "")
- {
- if (!$field) {
- $field = self::$defaultField;
- }
- $list_chart=[];
- $config=Config::getSystemInfo();
- // 查询未读消息
- $unread = Db::name('message')
- ->field('from_user,count(msg_id) as unread')
- ->where([['to_user', '=', $user_id], ['is_read', '=', 0], ['is_group', '=', 0]])
- ->group('from_user')
- ->select();
- $unread = self::matchListKey($unread,'from_user');
- // 查询最近的联系人
- $map1 = [['to_user', '=', $user_id], ['is_last', '=', 1], ['is_group', '=', 0]];
- $map2 = [['from_user', '=', $user_id], ['is_last', '=', 1], ['is_group', '=', 0]];
- $msgField = 'from_user,to_user,content as lastContent,create_time as lastSendTime,chat_identify,type,del_user';
- $lasMsgList = Db::name('message')
- ->field($msgField)
- ->whereOr([$map1, $map2])
- ->order('create_time desc')
- ->select();
- if($lasMsgList){
- $list=self::matchChatUser($lasMsgList, $user_id);
- $list_chart=$list['list'];
- $lasMsgList=$list['lastMsg'];
- }
- // 查询群聊
- $group = Group::getMyGroup(['gu.user_id' => $user_id, 'gu.status' => 1]);
- $groupList=[];
- if ($group) {
- $group = $group->toArray();
- $group_ids = arrayToString($group, 'group_id');
- $getGroupLastMsg = Db::name('message')->field($msgField)->where([['to_user', 'in', $group_ids], ['is_group', '=', 1], ['is_last', '=', 1]])->select();
- $getAtMsg=Db::name('message')->field('to_user,count(msg_id) as count')->where([['to_user', 'in', $group_ids], ['is_group', '=', 1]])->whereFindInSet('at',$user_id)->group('to_user')->select();
- $getGroupLastMsg=self::matchListKey($getGroupLastMsg,'to_user');
- $getAtMsg=self::matchListKey($getAtMsg,'to_user');
- $groupList=self::recombileGroupList($group,$getGroupLastMsg,$getAtMsg,false,$user_id);
- }
- try{
- Gateway::$registerAddress = config('gateway.registerAddress');
- $onlineList=Gateway::getAllUidList();
- }catch(\Exception $e){
- $onlineList=[];
- }
- $isRegion=$config['sysInfo']['ipregion'] ?? 0;
- $friendList = Friend::getFriend(['create_user' => $user_id,'status'=>1]);
- $list_chart=self::recombineUserList($list_chart,$isRegion,$friendList,$unread,$lasMsgList,$onlineList,$user_id);
- // 合并群聊和联系人
- $data = array_merge($list_chart, $groupList);
- // 合并助手消息和聊天消息
- $helper=self::otherChat($user_id);
- $data=array_merge($data,$helper);
- return $data;
- }
- // 获取好友列表
- public static function getFriendList($map,$user_id,$field='')
- {
- if (!$field) {
- $field = self::$defaultField;
- }
- $config=Config::getSystemInfo();
- // 如果是社区模式,就只查询自己的好友,如果是企业模式,就查询所有用户
- if($config['sysInfo']['runMode']==1){
- $friendList = Friend::getFriend(['create_user' => $user_id]);
- $list = self::where($map)->field($field)->select();
- }else{
- $friendList = Friend::getFriend(['create_user' => $user_id,'status'=>1]);
- $userList = array_keys($friendList);
- // 将专属客服设置为好友,6.2+已废弃,客服改为双向好友关系
- // $csUid=request()->userInfo['cs_uid'] ?? 0;
- // if($csUid){
- // $userList[]=$csUid;
- // }
- // 如果我有客服权限,就查询客服的好友
- // $cus=self::where(['cs_uid'=>$user_id])->column('user_id');
- // if($cus){
- // $userList=array_merge($userList,$cus);
- // }
- // $userList = array_unique($userList);
- $list = self::where($map)->where('user_id', 'in', $userList)->field($field)->select();
- }
- $list_chart=self::recombineUserList($list,$config['sysInfo']['ipregion'] ?? 0,$friendList);
- $list=chartSort($list_chart, 'displayName', false, 'index');
- // 合并助手消息和聊天消息
- $helper=self::otherChat($user_id);
- return array_merge($list,$helper);
- }
- // 获取好友列表
- public static function getGroupList($user_id)
- {
- // 查询群聊
- $group = Group::getMyGroup(['gu.user_id' => $user_id, 'gu.status' => 1]);
- $groupList=[];
- if ($group) {
- $group = $group->toArray();
- $groupList=self::recombileGroupList($group,[],[],true);
- }
- return $groupList;
- }
- //重新组成标准的群聊数据
- protected static function recombileGroupList($group,$getGroupLastMsg=[],$getAtMsg=[],$is_all=false,$user_id=0){
- $groupList=[];
- foreach ($group as $k => $v) {
- $val=$v;
- $group_id = 'group-' . $v['group_id'];
- if($user_id){
- $delChat=ChatDelog::getCache($user_id);
- $delChat = $delChat['groupList'] ?? [];
- if(in_array($group_id,$delChat)){
- continue;
- }
- }
- $groupVal=$getGroupLastMsg[$v['group_id']] ?? [];
- if($groupVal){
- $val['type'] =$groupVal['type'];
- $val['lastContent'] = str_encipher($groupVal['lastContent'],false);
- $val['lastSendTime'] = $groupVal['lastSendTime'] * 1000;
- }else{
- if(!$is_all){
- continue;
- }
- }
- $setting = $v['setting'] ? json_decode($v['setting'], true) : ['manage' => 0, 'invite' => 1, 'nospeak' => 0, 'profile' => 0, 'history' => 0];
-
- $val['id'] = $group_id;
- $val['account'] = $group_id;
- $val['avatar'] = avatarUrl($v['avatar'], $v['displayName'], $v['group_id'], 120,1);
- $val['name_py'] = $v['name_py'];
- $val['owner_id'] = $v['owner_id'];
- $val['role'] = $v['role'];
- $val['is_group'] = 1;
- $val['setting'] = $setting;
- $val['index'] = "[2]群聊";
- $val['realname'] = $v['displayName'] . " [群聊]";
- $val['is_notice'] = $v['is_notice'];
- $val['is_top'] = $v['is_top'];
- $val['is_online'] = 1;
- $val['is_at'] = 0;
- $atMsgVal=$getAtMsg[$v['group_id']] ?? [];
- if($atMsgVal){
- $val['is_at'] =$atMsgVal['count'];
- }
- $groupList[]=$val;
- }
- return $groupList;
- }
- // 重新组成标准的好友数据
- protected static function recombineUserList($list_chart,$isRegion,$friendList,$unread=[],$lasMsgList=[],$onlineList=[],$user_id=0){
- $isRegion=$config['sysInfo']['ipregion'] ?? 0;
- if($list_chart){
- foreach ($list_chart as $k => $v) {
- if($user_id){
- // 过滤已删除的聊天
- $delChat=ChatDelog::getCache($user_id);
- $delChat = $delChat['userList'] ?? [];
- if(in_array($v['user_id'],$delChat)){
- continue;
- }
- }
- // 是否有消息通知或者置顶聊天
- $friend = $friendList[$v['user_id']] ?? [];
- $list_chart[$k]['id'] = $v['user_id'];
- $list_chart[$k]['displayName'] = ($friend['nickname'] ?? '') ? : $v['realname'];
- $list_chart[$k]['name_py'] = $v['name_py'];
- $list_chart[$k]['avatar'] = avatarUrl($v['avatar'], $v['realname'], $v['user_id'], 120);
- $list_chart[$k]['lastContent'] = '';
- $list_chart[$k]['unread'] = 0;
- $list_chart[$k]['lastSendTime'] = time() * 1000;
- $list_chart[$k]['is_group'] = 0;
- $list_chart[$k]['setting'] = [];
- $list_chart[$k]['is_at'] = 0;
- $list_chart[$k]['last_login_ip'] = '';
- $list_chart[$k]['location'] ="";
- if($isRegion){
- $list_chart[$k]['last_login_ip'] = $v['last_login_ip'];
- $list_chart[$k]['location'] =$v['last_login_ip'] ? implode(" ", \Ip::find($v['last_login_ip'])) : "未知";
- }
- $list_chart[$k]['is_online'] = ($onlineList[$v['user_id']] ?? 0) ? 1 : 0;
- $is_top = 0;
- $is_notice = 1;
- if ($friend) {
- $is_top = $friend['is_top'];
- $is_notice = $friend['is_notice'];
- }
- $list_chart[$k]['is_top'] = $is_top;
- $list_chart[$k]['is_notice'] = $is_notice;
- $unrreadVal=$unread[$v['user_id']] ?? [];
- $list_chart[$k]['unread'] = $unrreadVal['unread'] ?? 0;
- $list_chart[$k]['type'] = 'text';
- $lastMsgVal=$lasMsgList[$v['user_id']] ?? [];
- if($lastMsgVal){
- $content = str_encipher($lastMsgVal['lastContent'],false);
- $list_chart[$k]['type'] = $lastMsgVal['type'];
- $list_chart[$k]['lastContent'] = $content;
- $list_chart[$k]['lastSendTime'] = $lastMsgVal['lastSendTime'] * 1000;
- }
- }
- }
- return $list_chart;
- }
- // 获取机器人聊天消息
- public static function otherChat($uid){
- return [];
- $staticList=self::staticUser();
- $adminNotice=$staticList['adminNotice'];
- $fileTransfer=$staticList['fileTransfer'];
- $count=Message::where(['chat_identify'=>$adminNotice['id']])->count();
- $createTime=Message::where(['chat_identify'=>$adminNotice['id']])->order('id desc')->value('create_time');
- $sendTime=0;
- if($createTime){
- $sendTime=is_string($createTime) ? strtotime($createTime) : $createTime;
- }
- $chat_identify=chat_identify($uid,$fileTransfer['id']);
- $fileLast=Message::where(['is_last'=>1,'chat_identify'=>$chat_identify])->find();
- $fileSendTime=$fileLast['create_time'] ?? '';
- $content =$fileLast['content'] ?? '';
- $friend=Friend::where(['create_user'=>$uid,'friend_user_id'=>$fileTransfer['id']])->find();
- $notice=[
- [
- 'id'=>$adminNotice['id'],
- 'user_id'=>$adminNotice['id'],
- 'displayName'=>$adminNotice['displayName'],
- 'realname'=>$adminNotice['displayName'],
- 'name_py'=>$adminNotice['name_py'],
- 'avatar'=>$adminNotice['avatar'],
- 'lastContent'=>$sendTime ? lang('system.announce',['num'=>$count]) :'',
- 'unread'=>0,
- 'lastSendTime'=>$sendTime * 1000,
- 'is_group'=>2,
- 'setting'=>[],
- 'type'=>'text',
- 'is_top'=>0,
- 'is_notice'=>1,
- 'is_online'=>0,
- 'index'=>"",
- ],
- [
- 'id'=>$fileTransfer['id'],
- 'user_id'=>$fileTransfer['id'],
- 'displayName'=>$fileTransfer['displayName'],
- 'realname'=>$fileTransfer['displayName'],
- 'name_py'=>$fileTransfer['name_py'],
- 'avatar'=>$fileTransfer['avatar'],
- 'lastContent'=> str_encipher($content,false) ?: lang('system.transFile'),
- 'unread'=>0,
- 'lastSendTime'=>((is_string($fileSendTime) ? strtotime($fileSendTime) : $fileSendTime) * 1000) ?: time() * 1000,
- 'is_group'=>3,
- 'setting'=>[],
- 'type'=>$fileLast['type'] ?? 'text',
- 'is_top'=>$friend['is_top'] ?? 0,
- 'is_notice'=>$friend['is_notice'] ?? 1,
- 'is_online'=>0,
- 'index'=>"",
- ],
- ];
- return $notice;
- }
- public static function getList($map)
- {
- return self::field(self::$defaultField)->where($map)->select();
- }
- // 匹配用户列表信息(返回用户信息)
- public static function matchUser($data, $many = false, $field = 'user_id', $cs = 80)
- {
- if ($many) {
- $idr = arrayToString($data, $field, false);
- } else {
- $idr = [];
- if (is_array($field)) {
- foreach ($field as $v) {
- $idr[] = $data[$v];
- }
- } else {
- $idr = [$data[$field]];
- }
- }
- $key = array_search(0, $idr);
- if ($key) {
- array_splice($idr, $key, 1);
- }
- $userList = self::where([['user_id', 'in', $idr]])->field(self::$defaultField)->select()->toArray();
- $friend = Friend::where([['friend_user_id', 'in', $idr],['create_user','=',self::$uid]])->field('friend_user_id,nickname')->select()->toArray();
- $list = [];
- foreach ($userList as $v) {
- $v['avatar'] = avatarUrl($v['avatar'], $v['realname'], $v['user_id'], $cs);
- $v['id'] = $v['user_id'];
- if($friend){
- foreach($friend as $key=>$val){
- if($val['friend_user_id']==$v['user_id']){
- $v['realname']=$val['nickname'] ? : $v['displayName'];
- break;
- }
- }
- }
- $list[$v['user_id']] = $v;
- }
- return $list;
- }
- // 匹配联系人列表
- public static function matchChatUser($data,$user_id)
- {
- $idr=[];
- $lastMsg=[];
- foreach($data as $k=>$v){
- $idr[]=$v['from_user'];
- $idr[]=$v['to_user'];
- if($v['from_user']==$user_id){
- $lastMsg[$v['to_user']]=$v;
- continue;
- }
- if($v['to_user']==$user_id){
- $lastMsg[$v['from_user']]=$v;
- continue;
- }
- }
- $key = array_search(0, $idr);
- if ($key) {
- array_splice($idr, $key, 1);
- }
- $idr = array_diff($idr, [$user_id]);
- $list = self::where([['user_id', 'in', $idr]])->field(self::$defaultField)->select()->toArray();
- return [
- 'list'=>$list,
- 'lastMsg'=>$lastMsg
- ];
- }
- // 匹配用户列表信息(返回data)
- public static function matchAllUser($data, $many = false, $field = 'user_id', $key = "userInfo", $cs = 80)
- {
- if ($many) {
- $idr = arrayToString($data, $field);
- $userList = self::getList([['user_id', 'in', $idr]]);
- $userList=self::matchListKey($userList,'user_id');
- foreach ($data as $k => $v) {
- $vv=$userList[$v[$field]] ?? [];
- if($vv){
- $data[$k][$key] = [
- 'id' => $vv['user_id'],
- 'displayName' => $vv['realname'],
- 'account' => $vv['account'],
- 'name_py' => $vv['name_py'],
- 'avatar' => avatarUrl($vv['avatar'], $vv['realname'], $vv['user_id'], $cs),
- ];
- }else{
- $data[$k][$key]=[];
- }
- }
- } else {
- $user = self::getUserInfo(['user_id' => $data[$field]]);
- $data[$key] = [
- 'id' => $user['user_id'],
- 'displayName' => $user['realname'],
- 'account' => $user['account'],
- 'name_py' => $user['name_py'],
- 'avatar' => avatarUrl($user['avatar'], $user['realname'], $user['user_id']),
- ];
- }
- return $data;
- }
- // 将id转换成联系人信息
- public function setContact($id,$is_group=0,$type='text',$content='',$contactInfo=null){
- $data=[
- 'id'=>$id,
- 'lastContent'=>$content,
- 'unread'=>0,
- 'lastSendTime'=> time() * 1000,
- 'is_group'=>$is_group,
- 'is_top'=>0,
- 'is_notice'=>1,
- 'is_top'=>0,
- 'is_at'=>0,
- 'setting'=>[],
- 'type'=>$type,
- 'location'=>'',
- ];
- if($is_group==0){
- $user=$contactInfo ?: User::where('user_id',$id)->find();
- if(!$user){
- $this->error=lang('user.exist');
- return false;
- }
- $user->avatar=avatarUrl($user->avatar,$user->realname,$user->user_id,120);
- // 查询好友关系
- $friend= self::$userInfo ? Friend::where(['friend_user_id'=>$id,'create_user'=>self::$userInfo['user_id']])->find() : [];
- $data['displayName'] = ($friend['nickname'] ?? '') ? : $user['realname'];
- $data['avatar'] = avatarUrl($user['avatar'], $user['realname'], $user['user_id'], 120);
- $data['location'] =$user['last_login_ip'] ? implode(" ", \Ip::find($user['last_login_ip'])) : "未知";
- $data['name_py'] = $user['name_py'];
- }else{
- $group_id=is_numeric($id) ? $id : (explode('-',$id)[1] ?? 0);
- $group=$contactInfo ?: Group::where(['group_id'=>$group_id])->find();
- if(!$group){
- $this->error=lang('group.exist');
- return false;
- }
- $data['id'] = 'group-'.$group_id;
- $data['displayName'] = $group['name'];
- $data['avatar'] = avatarUrl($group['avatar'], $group['name'], $group['group_id'], 120,1);
- $data['name_py'] = $group['name_py'];
- $data['setting'] = $group['setting'];
- $data['role'] = 3;
- }
- $data['index'] =getFirstChart($data['displayName']);
- return $data;
- }
- // 验证账号的合法性
- public function checkAccount(&$data){
- $user_id=$data['user_id'] ?? 0;
- if($user_id){
- $user=self::find($data['user_id']);
- if(!$user){
- $this->error='账户不存在';
- return false;
- }
- if($user->user_id==1 && self::$uid!=1){
- $this->error='超管账户只有自己才能修改';
- return false;
- }
- $other=self::where([['account','=',$data['account']],['user_id','<>',$data['user_id']]])->find();
- if($other){
- $this->error='账户已存在';
- return false;
- }
- }else{
- $user=self::where('account',$data['account'])->find();
- if($user){
- $this->error='账户已存在';
- return false;
- }
- }
- $config=Config::getSystemInfo();
- $regauth=$config['sysInfo']['regauth'] ?? 0;
- $acType=\utils\Regular::check_account($data['account']);
- switch($regauth){
- case 1:
- if($acType!=1){
- $this->error='当前系统只允许账号为手机号!';
- return false;
- }
- break;
- case 2:
- if($acType!=2){
- $this->error='当前系统只允许账号为邮箱!';
- return false;
- }
- break;
- case 3:
- // 验证账号是否为手机号或者邮箱
- if(!$acType){
- $this->error='账户必须为手机号或者邮箱';
- return false;
- }
- break;
- default:
- break;
- }
- $data['is_auth'] =$regauth ? 1 : 0;
- $email=$data['email'] ?? '';
- if($data['is_auth'] && $acType==2 && !$email){
- $data['email'] =$data['account'];
- }
- return true;
- }
- //设置用户在线状态
- public static function setOnline($user_id, $is_online)
- {
- $user = self::where('user_id',$user_id)->find();
- if (!$user) {
- return false;
- }
- //离线
- if ($is_online == 0 && $user->role == 3) {
- //结束今日的签到
- $sign = Sign::where('admin_id', $user->uid)->where('created_at', '>=', date('Y-m-d'))->order('id', 'desc')->find();
- if ($sign && $sign->time == 0) {
- $sign->time = time() - strtotime($sign->created_at);
- $sign->updated_at = date('Y-m-d H:i:s');
- $sign->save();
- }
- //结束客服在线时间
- KefuTime::endData($user->uid, 2);
- //结束客服接线时间
- KefuTime::endData($user->uid, 3, $user->cs_uid);
- //结束客服服务时间
- KefuTime::endData($user->uid, 4);
- }
- //上线
- if ($user->is_online == 0 && $is_online != 0 && $user->role == 3) {
- //添加客服上线次数
- KefuWork::addNum($user->uid, 'online_num');
- //通知客服签到
- wsSendMsg($user->user_id,'sign',['is_sign'=>1]);
- }
- //忙碌
- if ($user->is_online != 2 && $is_online == 2 && $user->role == 3) {
- //更新客服忙碌次数
- KefuWork::addNum($user->uid, 'busy_num');
- }
- if ($user->is_online == 2 && $is_online != 2 && $user->role == 3) {
- //结束客服忙碌时间
- KefuTime::endData($user->uid, 1);
- }
- $update_data = [
- 'is_online' => $is_online,
- ];
- if ($is_online == 0) {
- $update_data['offline_time'] = time();
- } else {
- $update_data['is_finished'] = 0;
- }
- return self::where('user_id', $user_id)->update($update_data);
- }
- public static function isOnline($user_id = null)
- {
- return $user_id ? Gateway::isUidOnline($user_id) : 0;
- }
- }
|