User.php 31 KB

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