User.php 29 KB

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