util.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2018/8/7
  6. * Time: 下午6:12
  7. */
  8. namespace room;
  9. use Exception;
  10. use member_info;
  11. use errcode;
  12. use uniquer;
  13. use relation;
  14. use schema_helper;
  15. class sorter
  16. {
  17. static public function msgid_desc($left,$right)
  18. {
  19. $t_l = intval($left['msg_id']);
  20. $t_r = intval($right['msg_id']);
  21. if($t_l > $t_r)
  22. return -1;
  23. elseif($t_l < $t_r)
  24. return 1;
  25. else
  26. return 0;
  27. }
  28. static public function msgid_asc($left,$right)
  29. {
  30. $t_l = intval($left['msg_id']);
  31. $t_r = intval($right['msg_id']);
  32. if($t_l > $t_r)
  33. return 1;
  34. elseif($t_l < $t_r)
  35. return -1;
  36. else
  37. return 0;
  38. }
  39. static public function steps_desc($left,$right)
  40. {
  41. $t_l = intval($left['steps']);
  42. $t_r = intval($right['steps']);
  43. if($t_l > $t_r)
  44. return -1;
  45. elseif($t_l < $t_r)
  46. return 1;
  47. else
  48. return 0;
  49. }
  50. }
  51. class talks_helper
  52. {
  53. const max_msgid = 1 << 32;
  54. private $mod_room;
  55. public function __construct()
  56. {
  57. $this->mod_room = Model('room');
  58. }
  59. public function friends($user)
  60. {
  61. $relations = $this->relations($user);
  62. $invitees = $this->invitees($user);
  63. $friends = array_merge($relations,$invitees);
  64. $friends[] = $user;
  65. $friends = array_unique($friends);
  66. return $friends;
  67. }
  68. private function relations($user)
  69. {
  70. try
  71. {
  72. $relation = new relation\mem_relation($user);
  73. $follows = $relation->follower();//我关注的
  74. $fans = $relation->subscriber();//关注我的
  75. return array_merge($follows,$fans);
  76. }
  77. catch (Exception $ex) {
  78. return [];
  79. }
  80. }
  81. private function invitees($user)
  82. {
  83. $mod_member = Model('member');
  84. $items = $mod_member->getMemberList(['inviter_id' => $user],'member_id', 0, '');
  85. $users = [];
  86. foreach ($items as $item) {
  87. $users[] = intval($item['member_id']);
  88. }
  89. return $users;
  90. }
  91. public function talks($user,$input)
  92. {
  93. $params = $this->talk_params($input);
  94. $room_descs = $this->chatrooms($user,$roomids);
  95. $room_msgs = $this->lastmsgs($roomids,$room_senders,0);
  96. $room_list = $this->room_list($roomids,$room_msgs,$params['rooms']);
  97. $chatwo_uniquer = new uniquer();
  98. $chatid_others = $this->chatwos($user,$chatwo_uniquer);
  99. $chatwo_msgs = $this->lastmsgs(array_keys($chatid_others),$chatwo_senders,1);
  100. $chatwo_list = $this->chatwo_list($chatid_others,$chatwo_msgs,$params['chatwos']);
  101. $push_descs = $this->pushs($pushids);
  102. $push_msgs = $this->push_lastmsg($user,$pushids);
  103. $push_list = $this->push_list($user,$pushids,$push_msgs,$params['pushs']);
  104. $pushmsgs = [];
  105. foreach ($push_msgs as $msg) {
  106. $pushmsgs[] = $msg;
  107. }
  108. $talks = array_merge($room_list,$chatwo_list);
  109. usort($talks,['room\sorter','msgid_desc']);
  110. $talk_list = array_merge($push_list,$talks);
  111. $friends = $this->friends($user);
  112. foreach ($friends as $friend)
  113. {
  114. if(!$chatwo_uniquer->find($friend)) {
  115. $talk_list[] = ['talk_type' => 'chatwo','talk_id' => $friend,'count' => 0,'msg_id' => 0];
  116. }
  117. }
  118. $other = array_values($chatid_others);
  119. $uids = array_merge($other,$room_senders,$chatwo_senders,$friends);
  120. $uids = array_unique($uids);
  121. $members = member_info::get_members($uids);
  122. return ["talk_list" => $talk_list,'messages' => array_merge($room_msgs,$chatwo_msgs,$pushmsgs),'push_descs' => $push_descs,'room_descs' => $room_descs,'members' => $members];
  123. }
  124. //pushid = 1; 命令消息,界面不限时
  125. //pushid = 2; 系统消息 -》 type=51 and room_id = 0 AND member_id IN (0,user)
  126. //pushid = 3; 新的好友 -》 type=52 and room_id = 0 AND member_id IN (0,user)
  127. private function pushs(&$pushids)
  128. {
  129. $pushids = [2,3];
  130. $result = [];
  131. { //pushid = 2;
  132. $item = [];
  133. $item['pushid'] = 2;
  134. $item['name'] = '系统消息';
  135. $item['avatar'] = RESOURCE_SITE_URL . '/mobile/room/msg_notice.png';
  136. $url = BASE_SITE_URL . "/mobile/index.php?act=member_talk&op=sys_notice";
  137. $item['schema'] = schema_helper::openurl('系统消息',$url);
  138. }
  139. $result[] = $item;
  140. { //pushid = 3;
  141. $item = [];
  142. $item['pushid'] = 3;
  143. $item['name'] = '新的好友';
  144. $item['avatar'] = RESOURCE_SITE_URL . '/mobile/room/apply_notice.png';
  145. $url = BASE_SITE_URL . "/mobile/index.php?act=member_talk&op=new_applys";
  146. $item['schema'] = schema_helper::openurl('系统消息',$url);
  147. }
  148. $result[] = $item;
  149. return $result;
  150. }
  151. private function push_lastmsg($user,$pushids)
  152. {
  153. $cond = ['room_id' => 0,'member_id' => ['in',[0,$user]]];
  154. $result = [];
  155. foreach ($pushids as $pushid)
  156. {
  157. if($pushid == 2) {
  158. $cond['type'] = proto_type::msg_type_nofity;
  159. }
  160. elseif($pushid == 3) {
  161. $cond['type'] = proto_type::msg_type_apply;
  162. }
  163. else {
  164. continue;
  165. }
  166. $msgs = $this->mod_room->getRoomsgList($cond,1);
  167. if(!empty($msgs))
  168. {
  169. $item = $msgs[0];
  170. $info = [
  171. "msg_id" => intval($item['msg_id']),
  172. "room_id" => intval($item['room_id']),
  173. "member_id" => intval($item['member_id']),
  174. "type" => proto_type::msg_stype_text,
  175. "add_time" => intval($item['add_time']),
  176. 'content' => $item['msg']
  177. ];
  178. $result[$pushid] = $info;
  179. }
  180. }
  181. return $result;
  182. }
  183. private function talk_params($json)
  184. {
  185. if(empty(trim($json))) return ["rooms" => false,"chatwos" => false,'pushs' => false];
  186. $input = json_decode($json,true);
  187. if($input == false) return ["rooms" => false,"chatwos" => false,'pushs' => false];
  188. $rooms = [];
  189. $chats = [];
  190. $pushs = [];
  191. foreach ($input as $item)
  192. {
  193. $msgid = intval($item['msg_id']);
  194. if($msgid < 0) $msgid = self::max_msgid;
  195. if($item['talk_type'] == 'room')
  196. {
  197. $room = intval($item['talk_id']);
  198. if($room <= 0) continue;
  199. $rooms[$room] = $msgid;
  200. }
  201. elseif($item['talk_type'] == 'chatwo')
  202. {
  203. $user = intval($item['talk_id']);
  204. if($user <= 0) continue;
  205. $chats[$user] = $msgid;
  206. }
  207. elseif($item['talk_type'] == 'push')
  208. {
  209. $push = intval($item['talk_id']);
  210. if($push <= 0) continue;
  211. $pushs[$push] = $msgid;
  212. }
  213. else {
  214. continue;
  215. }
  216. }
  217. return ["rooms" => $rooms,"chatwos" => $chats,'pushs' => $pushs];
  218. }
  219. private function chatrooms($user, &$roomids)
  220. {
  221. $roomids = [];
  222. $items = $this->mod_room->getRoomParts(['member_id' => $user,'state' => 0],'room_id');
  223. if(empty($items)) return [];
  224. $rooms = [];
  225. foreach ($items as $room) {
  226. $rooms[] = intval($room['room_id']);
  227. }
  228. $items = $this->mod_room->getRooms(['type' => proto_type::room_chat, 'room_id'=>['in',$rooms]]);
  229. if(empty($items)) return [];
  230. $result = [];
  231. foreach ($items as $item)
  232. {
  233. $roomids[] = intval($item['room_id']);
  234. $room_info = $this->room_info($item);
  235. $result[] =$room_info;
  236. }
  237. return $result;
  238. }
  239. //$msg_type == 0 表示 房间消息,1 表示单聊消息
  240. private function lastmsgs($roomids,&$senders,$msg_type = 0)
  241. {
  242. $senders = [];
  243. if(empty($roomids)) return [];
  244. $msgs = $this->mod_room->getLastRoomMsgs(['room_id' => ['in',$roomids],"msg_type" => $msg_type]);
  245. if(empty($msgs)) return [];
  246. $result = [];
  247. foreach ($msgs as $key => $item)
  248. {
  249. $sender = intval($item['member_id']);
  250. $senders[] = $sender;
  251. $info = [
  252. "msg_id" => intval($item['msg_id']),
  253. "room_id" => intval($item['room_id']),
  254. "member_id" => $sender,
  255. "type" => proto_type::from_msgtype(intval($item['type'])),
  256. "add_time" => intval($item['add_time']),
  257. 'content' => $item['msg']
  258. ];
  259. $result[] = $info;
  260. }
  261. return $result;
  262. }
  263. private function push_list($user,$pushids,$pushmsgs,$input)
  264. {
  265. $r_msg = [];
  266. foreach ($pushmsgs as $pushid => $msg) {
  267. $msg_id = intval($msg['msg_id']);
  268. $r_msg[$pushid] = $msg_id;
  269. }
  270. $pairs = [];
  271. foreach ($pushids as $pushid)
  272. {
  273. if(array_key_exists($pushid,$r_msg)) {
  274. $pairs[$pushid] = $r_msg[$pushid];
  275. } else {
  276. $pairs[$pushid] = 0;
  277. }
  278. }
  279. if($input === false)
  280. {
  281. $result = [];
  282. foreach ($pairs as $pushid => $msgid) {
  283. $result[] = ['talk_type' => 'push','talk_id' => $pushid,'count' => 0,'msg_id' => $msgid];
  284. }
  285. return $result;
  286. }
  287. else
  288. {
  289. $result = [];
  290. foreach ($pushids as $pushid)
  291. {
  292. $cond = ['room_id' => 0,'member_id' => ['in',[0,$user]]];
  293. if($pushid == 2) {
  294. $cond['type'] = proto_type::msg_type_nofity;
  295. }
  296. elseif($pushid == 3) {
  297. $cond['type'] = proto_type::msg_type_apply;
  298. }
  299. else {
  300. continue;
  301. }
  302. $msgid = $pairs[$pushid];
  303. if ($msgid == 0) {
  304. $count = 0;
  305. }
  306. else
  307. {
  308. if (array_key_exists($pushid, $input))
  309. {
  310. if ($input[$pushid] >= 0 && $input[$pushid] < $msgid) {
  311. $cond['msg_id'] = [['gt', $input[$pushid]], ['elt', $msgid]];
  312. $count = $this->mod_room->getRoomMsgsCount($cond);
  313. } else {
  314. $count = 0;
  315. }
  316. }
  317. else {
  318. $cond['msg_id'] = [['gt', 0], ['elt', $msgid]];
  319. $count = $this->mod_room->getRoomMsgsCount($cond);
  320. }
  321. }
  322. $result[] = ['talk_type' => 'push', 'talk_id' => $pushid, 'count' => intval($count), 'msg_id' => $msgid];
  323. }
  324. return $result;
  325. }
  326. }
  327. private function chatwo_list($chatid_others, $msgs, $input)
  328. {
  329. $r_msg = [];
  330. foreach ($msgs as $msg) {
  331. $room = intval($msg['room_id']);
  332. $msg_id = intval($msg['msg_id']);
  333. $r_msg[$room] = $msg_id;
  334. }
  335. $pairs = [];
  336. foreach ($chatid_others as $room => $other)
  337. {
  338. if(array_key_exists($room,$r_msg)) {
  339. $pairs[$other] = $r_msg[$room];
  340. } else {
  341. $pairs[$other] = 0;
  342. }
  343. }
  344. if($input === false)
  345. {
  346. $result = [];
  347. foreach ($pairs as $user => $msgid) {
  348. $result[] = ['talk_type' => 'chatwo','talk_id' => $user,'count' => 0,'msg_id' => $msgid];
  349. }
  350. return $result;
  351. }
  352. else
  353. {
  354. $result = [];
  355. foreach ($chatid_others as $room => $other)
  356. {
  357. $msgid = $pairs[$other];
  358. if($msgid == 0) {
  359. $count = 0;
  360. }
  361. else
  362. {
  363. if(array_key_exists($other,$input))
  364. {
  365. if($input[$room] >= 0 && $input[$other] < $msgid) {
  366. $count = $this->mod_room->getRoomMsgsCount(['room_id' => $room,"msg_id"=>array(['gt',$input[$other]],['elt',$msgid]),"msg_type" => 1]);
  367. } else {
  368. $count = 0;
  369. }
  370. }
  371. else {
  372. $count = $this->mod_room->getRoomMsgsCount(['room_id' => $room,"msg_id"=>[['gt',0],['elt',$msgid]],"msg_type" => 1]);
  373. }
  374. }
  375. $result[] = ['talk_type' => 'chatwo','talk_id' => $other,'count' => intval($count),'msg_id' => $msgid];
  376. }
  377. return $result;
  378. }
  379. }
  380. private function room_list($rooms, $msgs, $input)
  381. {
  382. $r_msg = [];
  383. foreach ($msgs as $msg) {
  384. $room = intval($msg['room_id']);
  385. $msg_id = intval($msg['msg_id']);
  386. $r_msg[$room] = $msg_id;
  387. }
  388. $pairs = [];
  389. foreach ($rooms as $room)
  390. {
  391. if(array_key_exists($room,$r_msg)) {
  392. $pairs[$room] = $r_msg[$room];
  393. } else {
  394. $pairs[$room] = 0;
  395. }
  396. }
  397. if($input === false)
  398. {
  399. $result = [];
  400. foreach ($pairs as $room => $msgid) {
  401. $result[] = ['talk_type' => 'room','talk_id' => $room,'count' => 0,'msg_id' => $msgid];
  402. }
  403. return $result;
  404. }
  405. else
  406. {
  407. $result = [];
  408. foreach ($pairs as $room => $msgid)
  409. {
  410. if($msgid == 0) {
  411. $count = 0;
  412. }
  413. else
  414. {
  415. if(array_key_exists($room,$input))
  416. {
  417. if($input[$room] >= 0 && $input[$room] < $msgid) {
  418. $count = $this->mod_room->getRoomMsgsCount(['room_id' => $room,"msg_id"=>array(['gt',$input[$room]],['elt',$msgid]), "msg_type" => 0]);
  419. } else {
  420. $count = 0;
  421. }
  422. }
  423. else
  424. {
  425. $count = $this->mod_room->getRoomMsgsCount(['room_id' => $room, "msg_id"=>[['gt',0],['elt',$msgid]], "msg_type" => 0]);
  426. }
  427. }
  428. $result[] = ['talk_type' => 'room','talk_id' => $room,'count' => intval($count),'msg_id' => $msgid];
  429. }
  430. return $result;
  431. }
  432. }
  433. private function chatwos($user,uniquer $uniquer)
  434. {
  435. $items = $this->mod_room->getChatwoRooms(['user_left' => $user,'user_right' => $user,'_op' => 'OR']);
  436. if(empty($items)) return [];
  437. $result = [];
  438. foreach ($items as $item) {
  439. $id = intval($item['chat_id']);
  440. $left = intval($item['user_left']);
  441. $right = intval($item['user_right']);
  442. $friend = $left == $user ? $right : $left;
  443. $result[$id] = $friend;
  444. $uniquer->add_value($friend);
  445. }
  446. return $result;
  447. }
  448. private function room_info($room)
  449. {
  450. if(empty($room)) return false;
  451. $info = new room_info($room);
  452. $room_info = $info->format();
  453. return $room_info;
  454. }
  455. }
  456. class uploader
  457. {
  458. public function upfile($upload_dir,&$upload_info)
  459. {
  460. if(empty($upload_dir)) {
  461. $upload_info = ['code' => errcode::ErrUpfile, 'msg' => "目标目录不能为空"];
  462. return false;
  463. }
  464. $roomid = intval($_POST['room_id']);
  465. $file_path = $this->file_path();
  466. $upload = new \FileUploader();
  467. if($upload->init_files($file_path,$roomid,$upload_info,'room') == false) {
  468. return false;
  469. }
  470. $upload->set('default_dir',$upload_dir);
  471. $thumb_width = '480,1024';
  472. $thumb_height = '480,1024';
  473. $upload->set('max_size',C('image_max_filesize'));
  474. $upload->set('thumb_width', $thumb_width);
  475. $upload->set('thumb_height',$thumb_height);
  476. $upload->set('fprefix',$roomid);
  477. $upload->set('thumb_ext', '_240,_1024');
  478. $result = $upload->upfile('file');
  479. if($result == true) {
  480. $upload_info = ['code' => 200, 'msg' => $upload->file_name];
  481. return true;
  482. } else {
  483. $upload_info = ['code' => errcode::ErrUpfile, 'msg' => $upload->error];
  484. return false;
  485. }
  486. }
  487. private function file_path()
  488. {
  489. $file_path = $_POST["file_path"];
  490. if(file_exists($file_path) == false) {
  491. $file_path = BASE_ROOT_PATH . '/data/upload/upfile' . $file_path;
  492. }
  493. return $file_path;
  494. }
  495. }