User.php 32 KB

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