User.php 32 KB

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