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