User.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. <?php
  2. /**
  3. * raingad IM [ThinkPHP6]
  4. * @author xiekunyu <raingad@foxmail.com>
  5. */
  6. namespace app\enterprise\model;
  7. use GatewayClient\Gateway;
  8. use app\BaseModel;
  9. use think\facade\Db;
  10. use think\model\concern\SoftDelete;
  11. use app\manage\model\Config;
  12. use thans\jwt\facade\JWTAuth;
  13. use app\admin\model\Sign;
  14. use app\admin\model\KefuWork;
  15. use app\admin\model\KefuTime;
  16. use app\admin\model\Department;
  17. class User extends BaseModel
  18. {
  19. use SoftDelete;
  20. protected $pk = "user_id";
  21. public static $defaultField = 'user_id,realname,realname as displayName,account,avatar,name_py,email,last_login_ip,service_status,cs_uid,timeout_type,remark,language_code,role,last_login_time,offline_time';
  22. protected $json = ['setting'];
  23. protected $jsonAssoc = true;
  24. // 系统人员或者其他静态人员
  25. public static function staticUser(){
  26. return [
  27. 'adminNotice'=>[
  28. 'id'=>'admin_notice',
  29. 'displayName'=>lang('system.notice'),
  30. 'avatar'=>getMainHost().'/static/common/img/notice.png',
  31. 'name_py'=>'xitongtongzhi',
  32. ],
  33. 'fileTransfer'=>[
  34. 'id'=>-1,
  35. 'displayName'=>lang('system.favor'),
  36. 'avatar'=>getMainHost().'/static/common/img/file_transfer.png',
  37. 'name_py'=>'wodeshoucang',
  38. ]
  39. ];
  40. }
  41. public function getUid()
  42. {
  43. return self::$uid;
  44. }
  45. //查询用户信息
  46. public static function getUserInfo($map=[])
  47. {
  48. if(!$map){
  49. return self::$userInfo;
  50. }
  51. $data = self::where($map)->find();
  52. if ($data) {
  53. $data = $data->toArray();
  54. }
  55. return $data;
  56. }
  57. /**
  58. * 刷新用户token 之前token将被拉黑
  59. * 修改用户数据后 调用该方法 并返回前台更新token
  60. * @param array $info 用户信息
  61. * @param string $terminal 客户端标识
  62. * @return string
  63. * @throws \think\db\exception\DataNotFoundException
  64. * @throws \think\db\exception\DbException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. */
  67. public static function refreshToken($info,$terminal)
  68. {
  69. $info = str_encipher(json_encode($info),true, config('app.aes_token_key'));
  70. $authToken = 'bearer '.JWTAuth::builder(['info' => $info, 'terminal' => $terminal]);
  71. return $authToken;
  72. }
  73. // 获取所有用户列表
  74. public static function getAllUser($map, $user_ids = [],$user_id,$group_id = 0)
  75. {
  76. $field = self::$defaultField;
  77. $list=[];
  78. if($group_id){
  79. $groupUser=GroupUser::where([['group_id','=',$group_id],['role','<>',1],['status','=',1]])->column('user_id');
  80. if($groupUser){
  81. $list=User::where([['user_id','in',$groupUser]])->field($field)->select()->toArray();
  82. }
  83. }else{
  84. $config=Config::getSystemInfo();
  85. // 如果是社区模式,就只查询自己的好友,如果是企业模式,就查询所有用户
  86. if($config['sysInfo']['runMode']==1){
  87. $list = self::where($map)->field($field)->select()->toArray();
  88. }else{
  89. $friendList = Friend::getFriend(['create_user' => $user_id,'status'=>1]);
  90. $userList = array_keys($friendList);
  91. $list = self::where($map)->where('user_id', 'in', $userList)->field($field)->select()->toArray();
  92. }
  93. }
  94. foreach ($list as $k => $v) {
  95. $list[$k]['disabled'] = false;
  96. $list[$k]['avatar'] = avatarUrl($v['avatar'], $v['realname'], $v['user_id']);
  97. if ($user_ids) {
  98. if (in_array($v['user_id'], $user_ids)) {
  99. $list[$k]['disabled'] = true;
  100. }
  101. }
  102. }
  103. return $list;
  104. }
  105. //查询用户列表
  106. public static function getUserList($userInfo, $map, $user_id, $field = "", $role = 0)
  107. {
  108. if (!$field) {
  109. $field = self::$defaultField;
  110. }
  111. $config=Config::getSystemInfo();
  112. // 如果是社区模式,就只查询自己的好友,如果是企业模式,就查询所有用户
  113. if($config['sysInfo']['runMode']==1){
  114. $friendList = Friend::getFriend(['create_user' => $user_id]);
  115. if ($role > 0) {
  116. $list = self::where($map)->whereOr('cs_uid', 0)->field($field)->select();
  117. } else {
  118. $list = self::where($map)->field($field)->select();
  119. }
  120. }else{
  121. $userList = [];
  122. // 将专属客服设置为好友
  123. $csUid=$userInfo['cs_uid'] ?? 0;
  124. if($csUid){
  125. $userList[]=$csUid;
  126. }
  127. // 如果是管理员,就查询整个部门的客服好友
  128. if ($userInfo['role'] > 0) {
  129. $friendList = Friend::getFriend(['create_user' => $user_id,'status'=>1]);//管理员自己的好友
  130. $userList = array_merge($userList, array_keys($friendList));
  131. $admin_id = $userInfo['uid'];
  132. $department_cs_uids = Department::getDepartmentCsUids($admin_id);//整个部门的用户
  133. $department_cs_uids[] = $user_id;
  134. $cus=self::whereIn('cs_uid',$department_cs_uids)->column('user_id');
  135. if($cus){
  136. $userList=array_merge($userList,$cus);
  137. }
  138. }
  139. $userList = array_unique($userList);
  140. $map[] = ['user_id','in',$userList];
  141. $list = self::where($map)->whereOr(function ($query) use ($role) {
  142. if ($role > 0) {
  143. $query->where('role', 0)->where('service_status', 0)->where('is_online', 1);
  144. }
  145. })->field($field)->select();
  146. }
  147. $list_chart = chartSort($list, 'realname', false, 'index');
  148. // 查询未读消息
  149. $unread = Db::name('message')
  150. ->field('from_user,count(msg_id) as unread')
  151. ->where([['to_user', '=', $user_id], ['is_read', '=', 0], ['is_group', '=', 0]])
  152. ->group('from_user')
  153. ->select();
  154. // 查询最近的联系人
  155. $map1 = [['to_user', '=', $user_id], ['is_last', '=', 1], ['is_group', '=', 0]];
  156. $map2 = [['from_user', '=', $user_id], ['is_last', '=', 1], ['is_group', '=', 0]];
  157. $msgField = 'from_user,to_user,content as lastContent,create_time as lastSendTime,chat_identify,type,del_user';
  158. $lasMsgList = Db::name('message')
  159. ->field($msgField)
  160. ->whereOr([$map1, $map2])
  161. ->order('create_time desc')
  162. ->select();
  163. // 查询群聊
  164. $group = Group::getMyGroup(['gu.user_id' => $user_id, 'gu.status' => 1]);
  165. if ($group) {
  166. $group = $group->toArray();
  167. $group_ids = arrayToString($group, 'group_id');
  168. $getGroupLastMsg = Db::name('message')->field($msgField)->where([['to_user', 'in', $group_ids], ['is_group', '=', 1], ['is_last', '=', 1]])->select();
  169. $getAtMsg=Db::name('message')->field($msgField)->where([['to_user', 'in', $group_ids], ['is_group', '=', 1]])->whereFindInSet('at',$user_id)->select();
  170. // halt($getAtMsg);
  171. foreach ($group as $k => $v) {
  172. $setting = $v['setting'] ? json_decode($v['setting'], true) : ['manage' => 0, 'invite' => 1, 'nospeak' => 0];
  173. $group_id = 'group-' . $v['group_id'];
  174. $group[$k]['id'] = $group_id;
  175. $group[$k]['account'] = $group_id;
  176. $group[$k]['avatar'] = avatarUrl($v['avatar'], $v['displayName'], $v['group_id'], 120,1);
  177. $group[$k]['name_py'] = $v['name_py'];
  178. $group[$k]['owner_id'] = $v['owner_id'];
  179. $group[$k]['role'] = $v['role'];
  180. $group[$k]['is_group'] = 1;
  181. $group[$k]['setting'] = $setting;
  182. $group[$k]['index'] = "[2]群聊";
  183. $group[$k]['realname'] = $v['displayName'] . " [群聊]";
  184. $group[$k]['is_notice'] = $v['is_notice'];
  185. $group[$k]['is_top'] = $v['is_top'];
  186. $group[$k]['is_online'] = 1;
  187. $group[$k]['is_at'] = 0;
  188. if ($getGroupLastMsg) {
  189. foreach ($getGroupLastMsg as $key=>$val) {
  190. if ($val['to_user'] == $v['group_id']) {
  191. $group[$k]['type'] =$val['type'];
  192. $group[$k]['lastContent'] = str_encipher($val['lastContent'],false);
  193. $group[$k]['lastSendTime'] = $val['lastSendTime'] * 1000;
  194. // 已经赋值了删除掉提升下次循环的性能
  195. unset($getGroupLastMsg[$key]);
  196. break;
  197. }
  198. }
  199. }
  200. if($getAtMsg){
  201. foreach ($getAtMsg as $key=> $val) {
  202. if ($val['to_user'] == $v['group_id']) {
  203. ++$group[$k]['is_at'];
  204. // 已经赋值了删除掉提升下次循环的性能
  205. unset($getAtMsg[$key]);
  206. }
  207. }
  208. }
  209. }
  210. }
  211. try{
  212. Gateway::$registerAddress = config('gateway.registerAddress');
  213. $onlineList=Gateway::getAllUidList();
  214. }catch(\Exception $e){
  215. $onlineList=[];
  216. }
  217. $isRegion=$config['sysInfo']['ipregion'] ?? 0;
  218. $auto_cs_uid = getAutoCsUid();//获取机器人ID
  219. foreach ($list_chart as $k => $v) {
  220. $last_login_time = $v['last_login_time'];
  221. $is_auto = $v['role'] == 0 && $v['cs_uid'] == $auto_cs_uid ? 1 : 0; //挂在机器人名下的用户
  222. if ($is_auto == 1 && $last_login_time > 0 && $last_login_time < time() - 7*24*60*60) {
  223. unset($list_chart[$k]);//过滤一周前的用户
  224. continue;
  225. }
  226. $lastSendTime = $list_chart[$k]['lastSendTime'];
  227. if ($is_auto && $lastSendTime == 0) {
  228. $map1 = [['to_user', '=', $auto_cs_uid], ['is_last', '=', 1], ['is_group', '=', 0]];
  229. $map2 = [['from_user', '=', $auto_cs_uid], ['is_last', '=', 1], ['is_group', '=', 0]];
  230. $lastSendTime = Db::name('message')
  231. ->field($msgField)
  232. ->whereOr([$map1, $map2])
  233. ->order('create_time desc')
  234. ->value('create_time');
  235. }
  236. // 是否有消息通知或者置顶聊天
  237. $friend = isset($friendList[$v['user_id']]) ? $friendList[$v['user_id']] : [];
  238. $list_chart[$k]['id'] = $v['user_id'];
  239. $list_chart[$k]['displayName'] = ($friend['nickname'] ?? '') ? : $v['realname'];
  240. $list_chart[$k]['name_py'] = $v['name_py'];
  241. $list_chart[$k]['avatar'] = avatarUrl($v['avatar'], $v['realname'], $v['user_id'], 120);
  242. $list_chart[$k]['lastContent'] = '';
  243. $list_chart[$k]['unread'] = 0;
  244. $list_chart[$k]['lastSendTime'] = $lastSendTime * 1000;
  245. $list_chart[$k]['offline_time'] = date('Y-m-d H:i:s', $v['offline_time']);
  246. $list_chart[$k]['is_group'] = 0;
  247. $list_chart[$k]['setting'] = [];
  248. $list_chart[$k]['is_at'] = 0;
  249. $list_chart[$k]['last_login_ip'] = '';
  250. $list_chart[$k]['location'] ="";
  251. $list_chart[$k]['cs_uid'] = $v['cs_uid'];
  252. $list_chart[$k]['is_auto'] = $is_auto;
  253. $list_chart[$k]['role'] = $v['role'];
  254. $list_chart[$k]['service_status'] = $v['service_status'];
  255. $list_chart[$k]['language_code'] = $v['language_code'];
  256. if($isRegion){
  257. $list_chart[$k]['last_login_ip'] = $v['last_login_ip'];
  258. $list_chart[$k]['location'] =$v['last_login_ip'] ? implode(" ", \Ip::find($v['last_login_ip'])) : "未知";
  259. }
  260. $is_online=0;
  261. if($v['is_online'] == 0 && isset($onlineList[$v['user_id']])){
  262. $is_online=1;
  263. }
  264. $list_chart[$k]['is_online'] = $is_online;
  265. $is_top = 0;
  266. $is_notice = 1;
  267. if ($friend) {
  268. $is_top = $friend['is_top'];
  269. $is_notice = $friend['is_notice'];
  270. }
  271. $list_chart[$k]['is_top'] = $is_top;
  272. $list_chart[$k]['is_notice'] = $is_notice;
  273. if ($unread) {
  274. foreach ($unread as $val) {
  275. if ($val['from_user'] == $v['user_id']) {
  276. $list_chart[$k]['unread'] = $val['unread'];
  277. break;
  278. }
  279. }
  280. }
  281. if ($lasMsgList) {
  282. foreach ($lasMsgList as $val) {
  283. if ($val['from_user'] == $v['user_id'] || $val['to_user'] == $v['user_id']) {
  284. $content = str_encipher($val['lastContent'],false);
  285. // 屏蔽已删除的消息
  286. if ($val['del_user']) {
  287. $delUser = explode(',', $val['del_user']);
  288. if (in_array($user_id, $delUser)) {
  289. $content = "";
  290. }
  291. }
  292. $list_chart[$k]['type'] = $val['type'];
  293. $list_chart[$k]['lastContent'] = $content;
  294. $list_chart[$k]['lastSendTime'] = $val['lastSendTime'] * 1000;
  295. break;
  296. }
  297. }
  298. }
  299. }
  300. // 合并群聊和联系人
  301. $data = array_merge($list_chart, $group);
  302. // 合并助手消息和聊天消息
  303. $helper=self::otherChat($user_id);
  304. $data=array_merge($data,$helper);
  305. return $data;
  306. }
  307. //查询用户列表
  308. public static function getChatList($user_id, $field = "")
  309. {
  310. if (!$field) {
  311. $field = self::$defaultField;
  312. }
  313. $list_chart=[];
  314. $config=Config::getSystemInfo();
  315. // 查询未读消息
  316. $unread = Db::name('message')
  317. ->field('from_user,count(msg_id) as unread')
  318. ->where([['to_user', '=', $user_id], ['is_read', '=', 0], ['is_group', '=', 0]])
  319. ->group('from_user')
  320. ->select();
  321. $unread = self::matchListKey($unread,'from_user');
  322. // 查询最近的联系人
  323. $map1 = [['to_user', '=', $user_id], ['is_last', '=', 1], ['is_group', '=', 0]];
  324. $map2 = [['from_user', '=', $user_id], ['is_last', '=', 1], ['is_group', '=', 0]];
  325. $msgField = 'from_user,to_user,content as lastContent,create_time as lastSendTime,chat_identify,type,del_user';
  326. $lasMsgList = Db::name('message')
  327. ->field($msgField)
  328. ->whereOr([$map1, $map2])
  329. ->order('create_time desc')
  330. ->select();
  331. if($lasMsgList){
  332. $list=self::matchChatUser($lasMsgList, $user_id);
  333. $list_chart=$list['list'];
  334. $lasMsgList=$list['lastMsg'];
  335. }
  336. // 查询群聊
  337. $group = Group::getMyGroup(['gu.user_id' => $user_id, 'gu.status' => 1]);
  338. $groupList=[];
  339. if ($group) {
  340. $group = $group->toArray();
  341. $group_ids = arrayToString($group, 'group_id');
  342. $getGroupLastMsg = Db::name('message')->field($msgField)->where([['to_user', 'in', $group_ids], ['is_group', '=', 1], ['is_last', '=', 1]])->select();
  343. $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();
  344. $getGroupLastMsg=self::matchListKey($getGroupLastMsg,'to_user');
  345. $getAtMsg=self::matchListKey($getAtMsg,'to_user');
  346. $groupList=self::recombileGroupList($group,$getGroupLastMsg,$getAtMsg,false,$user_id);
  347. }
  348. try{
  349. Gateway::$registerAddress = config('gateway.registerAddress');
  350. $onlineList=Gateway::getAllUidList();
  351. }catch(\Exception $e){
  352. $onlineList=[];
  353. }
  354. $isRegion=$config['sysInfo']['ipregion'] ?? 0;
  355. $friendList = Friend::getFriend(['create_user' => $user_id,'status'=>1]);
  356. $list_chart=self::recombineUserList($list_chart,$isRegion,$friendList,$unread,$lasMsgList,$onlineList,$user_id);
  357. // 合并群聊和联系人
  358. $data = array_merge($list_chart, $groupList);
  359. // 合并助手消息和聊天消息
  360. $helper=self::otherChat($user_id);
  361. $data=array_merge($data,$helper);
  362. return $data;
  363. }
  364. // 获取好友列表
  365. public static function getFriendList($map,$user_id,$field='')
  366. {
  367. if (!$field) {
  368. $field = self::$defaultField;
  369. }
  370. $config=Config::getSystemInfo();
  371. // 如果是社区模式,就只查询自己的好友,如果是企业模式,就查询所有用户
  372. if($config['sysInfo']['runMode']==1){
  373. $friendList = Friend::getFriend(['create_user' => $user_id]);
  374. $list = self::where($map)->field($field)->select();
  375. }else{
  376. $friendList = Friend::getFriend(['create_user' => $user_id,'status'=>1]);
  377. $userList = array_keys($friendList);
  378. // 将专属客服设置为好友,6.2+已废弃,客服改为双向好友关系
  379. // $csUid=request()->userInfo['cs_uid'] ?? 0;
  380. // if($csUid){
  381. // $userList[]=$csUid;
  382. // }
  383. // 如果我有客服权限,就查询客服的好友
  384. // $cus=self::where(['cs_uid'=>$user_id])->column('user_id');
  385. // if($cus){
  386. // $userList=array_merge($userList,$cus);
  387. // }
  388. // $userList = array_unique($userList);
  389. $list = self::where($map)->where('user_id', 'in', $userList)->field($field)->select();
  390. }
  391. $list_chart=self::recombineUserList($list,$config['sysInfo']['ipregion'] ?? 0,$friendList);
  392. $list=chartSort($list_chart, 'displayName', false, 'index');
  393. // 合并助手消息和聊天消息
  394. $helper=self::otherChat($user_id);
  395. return array_merge($list,$helper);
  396. }
  397. // 获取好友列表
  398. public static function getGroupList($user_id)
  399. {
  400. // 查询群聊
  401. $group = Group::getMyGroup(['gu.user_id' => $user_id, 'gu.status' => 1]);
  402. $groupList=[];
  403. if ($group) {
  404. $group = $group->toArray();
  405. $groupList=self::recombileGroupList($group,[],[],true);
  406. }
  407. return $groupList;
  408. }
  409. //重新组成标准的群聊数据
  410. protected static function recombileGroupList($group,$getGroupLastMsg=[],$getAtMsg=[],$is_all=false,$user_id=0){
  411. $groupList=[];
  412. foreach ($group as $k => $v) {
  413. $val=$v;
  414. $group_id = 'group-' . $v['group_id'];
  415. if($user_id){
  416. $delChat=ChatDelog::getCache($user_id);
  417. $delChat = $delChat['groupList'] ?? [];
  418. if(in_array($group_id,$delChat)){
  419. continue;
  420. }
  421. }
  422. $groupVal=$getGroupLastMsg[$v['group_id']] ?? [];
  423. if($groupVal){
  424. $val['type'] =$groupVal['type'];
  425. $val['lastContent'] = str_encipher($groupVal['lastContent'],false);
  426. $val['lastSendTime'] = $groupVal['lastSendTime'] * 1000;
  427. }else{
  428. if(!$is_all){
  429. continue;
  430. }
  431. }
  432. $setting = $v['setting'] ? json_decode($v['setting'], true) : ['manage' => 0, 'invite' => 1, 'nospeak' => 0, 'profile' => 0, 'history' => 0];
  433. $val['id'] = $group_id;
  434. $val['account'] = $group_id;
  435. $val['avatar'] = avatarUrl($v['avatar'], $v['displayName'], $v['group_id'], 120,1);
  436. $val['name_py'] = $v['name_py'];
  437. $val['owner_id'] = $v['owner_id'];
  438. $val['role'] = $v['role'];
  439. $val['is_group'] = 1;
  440. $val['setting'] = $setting;
  441. $val['index'] = "[2]群聊";
  442. $val['realname'] = $v['displayName'] . " [群聊]";
  443. $val['is_notice'] = $v['is_notice'];
  444. $val['is_top'] = $v['is_top'];
  445. $val['is_online'] = 1;
  446. $val['is_at'] = 0;
  447. $atMsgVal=$getAtMsg[$v['group_id']] ?? [];
  448. if($atMsgVal){
  449. $val['is_at'] =$atMsgVal['count'];
  450. }
  451. $groupList[]=$val;
  452. }
  453. return $groupList;
  454. }
  455. // 重新组成标准的好友数据
  456. protected static function recombineUserList($list_chart,$isRegion,$friendList,$unread=[],$lasMsgList=[],$onlineList=[],$user_id=0){
  457. $isRegion=$config['sysInfo']['ipregion'] ?? 0;
  458. if($list_chart){
  459. foreach ($list_chart as $k => $v) {
  460. if($user_id){
  461. // 过滤已删除的聊天
  462. $delChat=ChatDelog::getCache($user_id);
  463. $delChat = $delChat['userList'] ?? [];
  464. if(in_array($v['user_id'],$delChat)){
  465. continue;
  466. }
  467. }
  468. // 是否有消息通知或者置顶聊天
  469. $friend = $friendList[$v['user_id']] ?? [];
  470. $list_chart[$k]['id'] = $v['user_id'];
  471. $list_chart[$k]['displayName'] = ($friend['nickname'] ?? '') ? : $v['realname'];
  472. $list_chart[$k]['name_py'] = $v['name_py'];
  473. $list_chart[$k]['avatar'] = avatarUrl($v['avatar'], $v['realname'], $v['user_id'], 120);
  474. $list_chart[$k]['lastContent'] = '';
  475. $list_chart[$k]['unread'] = 0;
  476. $list_chart[$k]['lastSendTime'] = time() * 1000;
  477. $list_chart[$k]['is_group'] = 0;
  478. $list_chart[$k]['setting'] = [];
  479. $list_chart[$k]['is_at'] = 0;
  480. $list_chart[$k]['last_login_ip'] = '';
  481. $list_chart[$k]['location'] ="";
  482. if($isRegion){
  483. $list_chart[$k]['last_login_ip'] = $v['last_login_ip'];
  484. $list_chart[$k]['location'] =$v['last_login_ip'] ? implode(" ", \Ip::find($v['last_login_ip'])) : "未知";
  485. }
  486. $list_chart[$k]['is_online'] = ($onlineList[$v['user_id']] ?? 0) ? 1 : 0;
  487. $is_top = 0;
  488. $is_notice = 1;
  489. if ($friend) {
  490. $is_top = $friend['is_top'];
  491. $is_notice = $friend['is_notice'];
  492. }
  493. $list_chart[$k]['is_top'] = $is_top;
  494. $list_chart[$k]['is_notice'] = $is_notice;
  495. $unrreadVal=$unread[$v['user_id']] ?? [];
  496. $list_chart[$k]['unread'] = $unrreadVal['unread'] ?? 0;
  497. $list_chart[$k]['type'] = 'text';
  498. $lastMsgVal=$lasMsgList[$v['user_id']] ?? [];
  499. if($lastMsgVal){
  500. $content = str_encipher($lastMsgVal['lastContent'],false);
  501. $list_chart[$k]['type'] = $lastMsgVal['type'];
  502. $list_chart[$k]['lastContent'] = $content;
  503. $list_chart[$k]['lastSendTime'] = $lastMsgVal['lastSendTime'] * 1000;
  504. }
  505. }
  506. }
  507. return $list_chart;
  508. }
  509. // 获取机器人聊天消息
  510. public static function otherChat($uid){
  511. return [];
  512. $staticList=self::staticUser();
  513. $adminNotice=$staticList['adminNotice'];
  514. $fileTransfer=$staticList['fileTransfer'];
  515. $count=Message::where(['chat_identify'=>$adminNotice['id']])->count();
  516. $createTime=Message::where(['chat_identify'=>$adminNotice['id']])->order('id desc')->value('create_time');
  517. $sendTime=0;
  518. if($createTime){
  519. $sendTime=is_string($createTime) ? strtotime($createTime) : $createTime;
  520. }
  521. $chat_identify=chat_identify($uid,$fileTransfer['id']);
  522. $fileLast=Message::where(['is_last'=>1,'chat_identify'=>$chat_identify])->find();
  523. $fileSendTime=$fileLast['create_time'] ?? '';
  524. $content =$fileLast['content'] ?? '';
  525. $friend=Friend::where(['create_user'=>$uid,'friend_user_id'=>$fileTransfer['id']])->find();
  526. $notice=[
  527. [
  528. 'id'=>$adminNotice['id'],
  529. 'user_id'=>$adminNotice['id'],
  530. 'displayName'=>$adminNotice['displayName'],
  531. 'realname'=>$adminNotice['displayName'],
  532. 'name_py'=>$adminNotice['name_py'],
  533. 'avatar'=>$adminNotice['avatar'],
  534. 'lastContent'=>$sendTime ? lang('system.announce',['num'=>$count]) :'',
  535. 'unread'=>0,
  536. 'lastSendTime'=>$sendTime * 1000,
  537. 'is_group'=>2,
  538. 'setting'=>[],
  539. 'type'=>'text',
  540. 'is_top'=>0,
  541. 'is_notice'=>1,
  542. 'is_online'=>0,
  543. 'index'=>"",
  544. ],
  545. [
  546. 'id'=>$fileTransfer['id'],
  547. 'user_id'=>$fileTransfer['id'],
  548. 'displayName'=>$fileTransfer['displayName'],
  549. 'realname'=>$fileTransfer['displayName'],
  550. 'name_py'=>$fileTransfer['name_py'],
  551. 'avatar'=>$fileTransfer['avatar'],
  552. 'lastContent'=> str_encipher($content,false) ?: lang('system.transFile'),
  553. 'unread'=>0,
  554. 'lastSendTime'=>((is_string($fileSendTime) ? strtotime($fileSendTime) : $fileSendTime) * 1000) ?: time() * 1000,
  555. 'is_group'=>3,
  556. 'setting'=>[],
  557. 'type'=>$fileLast['type'] ?? 'text',
  558. 'is_top'=>$friend['is_top'] ?? 0,
  559. 'is_notice'=>$friend['is_notice'] ?? 1,
  560. 'is_online'=>0,
  561. 'index'=>"",
  562. ],
  563. ];
  564. return $notice;
  565. }
  566. public static function getList($map)
  567. {
  568. return self::field(self::$defaultField)->where($map)->select();
  569. }
  570. // 匹配用户列表信息(返回用户信息)
  571. public static function matchUser($data, $many = false, $field = 'user_id', $cs = 80)
  572. {
  573. if ($many) {
  574. $idr = arrayToString($data, $field, false);
  575. } else {
  576. $idr = [];
  577. if (is_array($field)) {
  578. foreach ($field as $v) {
  579. $idr[] = $data[$v];
  580. }
  581. } else {
  582. $idr = [$data[$field]];
  583. }
  584. }
  585. $key = array_search(0, $idr);
  586. if ($key) {
  587. array_splice($idr, $key, 1);
  588. }
  589. $userList = self::where([['user_id', 'in', $idr]])->field(self::$defaultField)->select()->toArray();
  590. $friend = Friend::where([['friend_user_id', 'in', $idr],['create_user','=',self::$uid]])->field('friend_user_id,nickname')->select()->toArray();
  591. $list = [];
  592. foreach ($userList as $v) {
  593. $v['avatar'] = avatarUrl($v['avatar'], $v['realname'], $v['user_id'], $cs);
  594. $v['id'] = $v['user_id'];
  595. if($friend){
  596. foreach($friend as $key=>$val){
  597. if($val['friend_user_id']==$v['user_id']){
  598. $v['realname']=$val['nickname'] ? : $v['displayName'];
  599. break;
  600. }
  601. }
  602. }
  603. $list[$v['user_id']] = $v;
  604. }
  605. return $list;
  606. }
  607. // 匹配联系人列表
  608. public static function matchChatUser($data,$user_id)
  609. {
  610. $idr=[];
  611. $lastMsg=[];
  612. foreach($data as $k=>$v){
  613. $idr[]=$v['from_user'];
  614. $idr[]=$v['to_user'];
  615. if($v['from_user']==$user_id){
  616. $lastMsg[$v['to_user']]=$v;
  617. continue;
  618. }
  619. if($v['to_user']==$user_id){
  620. $lastMsg[$v['from_user']]=$v;
  621. continue;
  622. }
  623. }
  624. $key = array_search(0, $idr);
  625. if ($key) {
  626. array_splice($idr, $key, 1);
  627. }
  628. $idr = array_diff($idr, [$user_id]);
  629. $list = self::where([['user_id', 'in', $idr]])->field(self::$defaultField)->select()->toArray();
  630. return [
  631. 'list'=>$list,
  632. 'lastMsg'=>$lastMsg
  633. ];
  634. }
  635. // 匹配用户列表信息(返回data)
  636. public static function matchAllUser($data, $many = false, $field = 'user_id', $key = "userInfo", $cs = 80)
  637. {
  638. if ($many) {
  639. $idr = arrayToString($data, $field);
  640. $userList = self::getList([['user_id', 'in', $idr]]);
  641. $userList=self::matchListKey($userList,'user_id');
  642. foreach ($data as $k => $v) {
  643. $vv=$userList[$v[$field]] ?? [];
  644. if($vv){
  645. $data[$k][$key] = [
  646. 'id' => $vv['user_id'],
  647. 'displayName' => $vv['realname'],
  648. 'account' => $vv['account'],
  649. 'name_py' => $vv['name_py'],
  650. 'avatar' => avatarUrl($vv['avatar'], $vv['realname'], $vv['user_id'], $cs),
  651. ];
  652. }else{
  653. $data[$k][$key]=[];
  654. }
  655. }
  656. } else {
  657. $user = self::getUserInfo(['user_id' => $data[$field]]);
  658. $data[$key] = [
  659. 'id' => $user['user_id'],
  660. 'displayName' => $user['realname'],
  661. 'account' => $user['account'],
  662. 'name_py' => $user['name_py'],
  663. 'avatar' => avatarUrl($user['avatar'], $user['realname'], $user['user_id']),
  664. ];
  665. }
  666. return $data;
  667. }
  668. // 将id转换成联系人信息
  669. public function setContact($id,$is_group=0,$type='text',$content='',$contactInfo=null){
  670. $data=[
  671. 'id'=>$id,
  672. 'lastContent'=>$content,
  673. 'unread'=>0,
  674. 'lastSendTime'=> time() * 1000,
  675. 'is_group'=>$is_group,
  676. 'is_top'=>0,
  677. 'is_notice'=>1,
  678. 'is_top'=>0,
  679. 'is_at'=>0,
  680. 'setting'=>[],
  681. 'type'=>$type,
  682. 'location'=>'',
  683. ];
  684. if($is_group==0){
  685. $user=$contactInfo ?: User::where('user_id',$id)->find();
  686. if(!$user){
  687. $this->error=lang('user.exist');
  688. return false;
  689. }
  690. $user->avatar=avatarUrl($user->avatar,$user->realname,$user->user_id,120);
  691. // 查询好友关系
  692. $friend= self::$userInfo ? Friend::where(['friend_user_id'=>$id,'create_user'=>self::$userInfo['user_id']])->find() : [];
  693. $data['displayName'] = ($friend['nickname'] ?? '') ? : $user['realname'];
  694. $data['avatar'] = avatarUrl($user['avatar'], $user['realname'], $user['user_id'], 120);
  695. $data['location'] =$user['last_login_ip'] ? implode(" ", \Ip::find($user['last_login_ip'])) : "未知";
  696. $data['name_py'] = $user['name_py'];
  697. }else{
  698. $group_id=is_numeric($id) ? $id : (explode('-',$id)[1] ?? 0);
  699. $group=$contactInfo ?: Group::where(['group_id'=>$group_id])->find();
  700. if(!$group){
  701. $this->error=lang('group.exist');
  702. return false;
  703. }
  704. $data['id'] = 'group-'.$group_id;
  705. $data['displayName'] = $group['name'];
  706. $data['avatar'] = avatarUrl($group['avatar'], $group['name'], $group['group_id'], 120,1);
  707. $data['name_py'] = $group['name_py'];
  708. $data['setting'] = $group['setting'];
  709. $data['role'] = 3;
  710. }
  711. $data['index'] =getFirstChart($data['displayName']);
  712. return $data;
  713. }
  714. // 验证账号的合法性
  715. public function checkAccount(&$data){
  716. $user_id=$data['user_id'] ?? 0;
  717. if($user_id){
  718. $user=self::find($data['user_id']);
  719. if(!$user){
  720. $this->error='账户不存在';
  721. return false;
  722. }
  723. if($user->user_id==1 && self::$uid!=1){
  724. $this->error='超管账户只有自己才能修改';
  725. return false;
  726. }
  727. $other=self::where([['account','=',$data['account']],['user_id','<>',$data['user_id']]])->find();
  728. if($other){
  729. $this->error='账户已存在';
  730. return false;
  731. }
  732. }else{
  733. $user=self::where('account',$data['account'])->find();
  734. if($user){
  735. $this->error='账户已存在';
  736. return false;
  737. }
  738. }
  739. $config=Config::getSystemInfo();
  740. $regauth=$config['sysInfo']['regauth'] ?? 0;
  741. $acType=\utils\Regular::check_account($data['account']);
  742. switch($regauth){
  743. case 1:
  744. if($acType!=1){
  745. $this->error='当前系统只允许账号为手机号!';
  746. return false;
  747. }
  748. break;
  749. case 2:
  750. if($acType!=2){
  751. $this->error='当前系统只允许账号为邮箱!';
  752. return false;
  753. }
  754. break;
  755. case 3:
  756. // 验证账号是否为手机号或者邮箱
  757. if(!$acType){
  758. $this->error='账户必须为手机号或者邮箱';
  759. return false;
  760. }
  761. break;
  762. default:
  763. break;
  764. }
  765. $data['is_auth'] =$regauth ? 1 : 0;
  766. $email=$data['email'] ?? '';
  767. if($data['is_auth'] && $acType==2 && !$email){
  768. $data['email'] =$data['account'];
  769. }
  770. return true;
  771. }
  772. //设置用户在线状态
  773. public static function setOnline($user_id, $is_online)
  774. {
  775. $user = self::where('user_id',$user_id)->find();
  776. if (!$user) {
  777. return false;
  778. }
  779. //离线
  780. if ($is_online == 0 && $user->role == 3) {
  781. //结束今日的签到
  782. $sign = Sign::where('admin_id', $user->uid)->where('created_at', '>=', date('Y-m-d'))->order('id', 'desc')->find();
  783. if ($sign && $sign->time == 0) {
  784. $sign->time = time() - strtotime($sign->created_at);
  785. $sign->updated_at = date('Y-m-d H:i:s');
  786. $sign->save();
  787. }
  788. //结束客服在线时间
  789. KefuTime::endData($user->uid, 2);
  790. //结束客服接线时间
  791. KefuTime::endData($user->uid, 3, $user->cs_uid);
  792. //结束客服服务时间
  793. KefuTime::endData($user->uid, 4);
  794. }
  795. //上线
  796. if ($user->is_online == 0 && $is_online != 0 && $user->role == 3) {
  797. //添加客服上线次数
  798. KefuWork::addNum($user->uid, 'online_num');
  799. //通知客服签到
  800. wsSendMsg($user->user_id,'sign',['is_sign'=>1]);
  801. }
  802. //忙碌
  803. if ($user->is_online != 2 && $is_online == 2 && $user->role == 3) {
  804. //更新客服忙碌次数
  805. KefuWork::addNum($user->uid, 'busy_num');
  806. }
  807. if ($user->is_online == 2 && $is_online != 2 && $user->role == 3) {
  808. //结束客服忙碌时间
  809. KefuTime::endData($user->uid, 1);
  810. }
  811. $update_data = [
  812. 'is_online' => $is_online,
  813. ];
  814. if ($is_online == 0) {
  815. $update_data['offline_time'] = time();
  816. } else {
  817. $update_data['is_finished'] = 0;
  818. }
  819. return self::where('user_id', $user_id)->update($update_data);
  820. }
  821. public static function isOnline($user_id = null)
  822. {
  823. return $user_id ? Gateway::isUidOnline($user_id) : 0;
  824. }
  825. }