User.php 31 KB

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