User.php 32 KB

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