User.php 32 KB

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