TestTalk.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2018/7/16
  6. * Time: 下午4:36
  7. */
  8. define('BASE_ROOT_PATH',str_replace('/test','',dirname(__FILE__)));
  9. require_once (BASE_ROOT_PATH . '/fooder.php');
  10. require_once(BASE_ROOT_PATH . '/helper/search/tcp_client.php');
  11. require_once(BASE_ROOT_PATH . '/helper/room/factory_client.php');
  12. require_once(BASE_ROOT_PATH . '/helper/room/proto_type.php');
  13. require_once(BASE_ROOT_PATH . '/helper/room/author.php');
  14. class TestTalk extends PHPUnit_Framework_TestCase
  15. {
  16. public static function setUpBeforeClass()
  17. {
  18. Base::run_util();
  19. }
  20. public function testGen()
  21. {
  22. global $config;
  23. $room_id = $config['special_rooms']['shake_bonus'];
  24. $webaddr = $config['access_addr'];
  25. $uids = [39625,39653];
  26. $creator = self::shake_creator($room_id);
  27. $ret = room\factory_client::instance()->invite($room_id,$creator,$uids);
  28. foreach ($uids as $uid)
  29. {
  30. $result = [];
  31. if($ret != false) {
  32. $result['addr'] = $webaddr;
  33. $result['token'] = room\author::sign_web($room_id,$uid);
  34. $result['room'] = intval($room_id);
  35. $result['user'] = $uid;
  36. }
  37. }
  38. }
  39. static private function shake_creator($room_id)
  40. {
  41. static $stCreator = 0;
  42. if($stCreator == 0) {
  43. $mod_room = Model('room');
  44. $params = $mod_room->getRoom($room_id);
  45. $rinfo = new room\base_info($params);
  46. $stCreator = $rinfo->creator();
  47. }
  48. return $stCreator;
  49. }
  50. public function testSql()
  51. {
  52. $left['from_user&to_user&msg_id'] = ['_multi' => true,1,2,['lt',100]];
  53. $left['to_user&from_user&msg_id'] = ['_multi' => true,1,2,['lt',100]];
  54. $left['_op'] = 'OR';
  55. // $right['msg_id'] = ['lt',100];
  56. //
  57. // $cond[] = $left;
  58. // $cond[] = $right;
  59. // $cond['_op'] = 'and';
  60. $mod_room = Model('room');
  61. // $msgs = $mod_room->getRoomMsgList($cond,20);
  62. $msgs = $mod_room->getChatwoMsgList($left,20);
  63. }
  64. public function testLastRoomMsg(){
  65. $user = 39623;
  66. $mod_room = Model('room');
  67. $items =$mod_room->table('room_msg')->field("MAX(msg_id) as msg_id,room_id,member_id")->group('room_id')->having('member_id = '.$user)->select();
  68. $last_msg = [];
  69. foreach ($items as $item){
  70. $last_msg[$items['room_id']]= $items['msg_id'];
  71. }
  72. $room_key =array_keys($last_msg);
  73. $msg_key = array_values($last_msg);
  74. $items =$mod_room->table('room_msg')->field("msg_id,room_id,member_id")->group('room_id')->having('member_id = '.$user)->select();
  75. echo 1;
  76. }
  77. public function testTalk_Chats(){
  78. $json = [
  79. "rooms" =>[
  80. "room_id"=> "msg_id",
  81. ],
  82. "friends" =>[
  83. "39679" => 265,
  84. ]
  85. ];
  86. $user = 39623;//自己
  87. $mod_room = Model('room');
  88. $items = $mod_room->getLastChatwoMsg($user);
  89. $ret = $this->talk_format($items,'chatwo',$user,$json['friends']);
  90. if(!empty($json["friends"]))
  91. {
  92. $local_last = $json["friends"];
  93. foreach ($ret as $member_id => $last_msg){
  94. if(isset($local_last[$member_id]) && $local_last[$member_id]<$last_msg["msg_id"]){
  95. $not_read = $mod_room->table("room_chatwo_msg")->where([
  96. "from_user"=>$member_id,
  97. "msg_id"=>[['gt',$local_last[$member_id]],['elt',$last_msg["msg_id"]]]
  98. ])->count();
  99. $ret[$member_id]["not_read"] = $not_read;
  100. }else{
  101. $ret[$member_id]["not_read"] = 0;
  102. }
  103. }
  104. }
  105. echo 0;
  106. }
  107. public function testTalk_Rooms(){
  108. $json = [
  109. "rooms" =>[
  110. 83=> "msg_id",
  111. ],
  112. "friends" =>[
  113. "39679" => 265,
  114. ]
  115. ];
  116. $user = 39623;//自己
  117. $mod_room = Model('room');
  118. $items = $mod_room->getLastRoomMsg($user);
  119. $ret = $this->talk_format($items,'room',$user,$json['rooms']);
  120. echo 0;
  121. }
  122. public function testTalks(){
  123. $arr = [
  124. ["type"=>'room',"room"=>83, "msg_id"=>2550927],
  125. ["type"=>'chat',"user"=>39679,"msg_id"=>265],
  126. ];
  127. $post_data = json_encode($arr);
  128. $user = 39623;
  129. $post = $this->init_talk($post_data);
  130. $chats = $this->talk_chatwos($user,$post['local_chats']);
  131. $rooms = $this->talk_rooms($user,$post['local_rooms']);
  132. echo 0;
  133. }
  134. private function init_talk($post){
  135. $local_rooms = [];
  136. $local_chats = [];
  137. if(empty(trim($post))){
  138. return ["local_rooms"=>$local_rooms,"local_chats"=>$local_chats];
  139. }
  140. $json = json_decode($post,true);
  141. if(!empty($json))
  142. {
  143. foreach ($json as $v)
  144. {
  145. if($v['type'] == 'room'){
  146. $local_rooms[$v['room']] = $v['msg_id'];
  147. }elseif($v['type'] == 'chat'){
  148. $local_chats[$v['user']] = $v['msg_id'];
  149. }else{
  150. continue;
  151. }
  152. }
  153. }
  154. return ["local_rooms"=>$local_rooms,"local_chats"=>$local_chats];
  155. }
  156. private function talk_rooms($user,$local_last)
  157. {
  158. $mod_room = Model('room');
  159. $items = $mod_room->getLastRoomMsg($user);
  160. $ret = $this->talk_format($items,'room',$user,$local_last);
  161. return $ret;
  162. }
  163. private function talk_chatwos($user,$local_last)
  164. {
  165. $mod_room = Model('room');
  166. $items = $mod_room->getLastChatwoMsg($user);
  167. $chats = $this->talk_format($items,'chatwo',$user,$local_last);
  168. return $chats;
  169. }
  170. private function talk_format($talks,$type,$user,$local_last)
  171. {
  172. $items = [];
  173. $mod_room = Model('room');
  174. foreach($talks as $last_msg)
  175. {
  176. if($type == 'room')
  177. {
  178. $id = $last_msg['room_id'];
  179. if(isset($local_last[$id]) && $local_last[$id]<$last_msg["msg_id"]) {
  180. $not_read = $mod_room->getNotReadCnt($type,$local_last[$id],$last_msg["msg_id"],$id);
  181. }else{
  182. $not_read = 0;
  183. }
  184. $item = [
  185. "msg_id" => $last_msg['msg_id'],
  186. "msg" => $last_msg['msg'],
  187. "add_time" => $last_msg['add_time'],
  188. "not_read" => $not_read,
  189. "room_id" => $id
  190. ];
  191. }
  192. elseif($type=='chatwo')
  193. {
  194. $id = $last_msg['from_user'] == $user?$last_msg['to_user']:$last_msg['from_user'];
  195. if(isset($local_last[$id]) && $local_last[$id]<$last_msg["msg_id"]) {
  196. $not_read = $mod_room->getNotReadCnt($type,$local_last[$id],$last_msg["msg_id"],$id);
  197. }else{
  198. $not_read = 0;
  199. }
  200. $item = [
  201. "msg_id" => $last_msg['msg_id'],
  202. "msg" => $last_msg['msg'],
  203. "add_time" => $last_msg['add_time'],
  204. "not_read" => $not_read,
  205. "member_id" => $id
  206. ];
  207. }else{
  208. $id = 0;
  209. $item = [];
  210. }
  211. $items[$id] = $item;
  212. }
  213. return $items;
  214. }
  215. public function testRoom_list(){
  216. $_SESSION['is_login'] =1;
  217. $_SESSION['member_id'] = 39623;
  218. $user = 39623;
  219. $case1 = $this->room_case1($user);
  220. $case2 = $this->room_case2($user);
  221. $case3 = $this->room_case3($user);
  222. echo 0;
  223. }
  224. private function room_case2($user)
  225. {
  226. $mod_room = Model("room");
  227. $chats_rooms = $mod_room->getChatsRoom($user);
  228. $rooms = [];
  229. $uids_total = [];
  230. if(!empty($chats_rooms))
  231. {
  232. foreach ($chats_rooms as $room)
  233. {
  234. $uids = [];
  235. $paticipates = $mod_room->getRoomMember($room['room_id']);
  236. if(!empty($paticipates))
  237. {
  238. foreach ($paticipates as $one)
  239. {
  240. $uids[] = $one['member_id'];
  241. }
  242. $uids_total = array_merge($uids_total,$uids);
  243. }
  244. $rooms[] = [
  245. "room_id"=>$room['room_id'],
  246. "room_name"=>$room['room_name'],
  247. "avatar"=>"",
  248. "room_members"=>$uids
  249. ];
  250. }
  251. $members = [];
  252. if(!empty($uids_total))
  253. {
  254. $uids_total = array_unique($uids_total);
  255. $items = Model('member')->getMemberList(['member_id' => ['in',$uids_total]]);
  256. foreach ($items as $item){
  257. try
  258. {
  259. $info = new member_info($item);
  260. $members[$item['member_id']] = $info->filter();
  261. }
  262. catch (Exception $ex) {
  263. Log::record($ex->getMessage(),Log::ERR);
  264. }
  265. }
  266. }
  267. if(!empty($members))
  268. {
  269. foreach ($rooms as $index => $room){
  270. $room_members = [];
  271. if(!empty($room['room_members']))
  272. {
  273. foreach ($room['room_members'] as $uid)
  274. {
  275. if(isset($members[$uid])){
  276. $room_members[] = $members[$uid];
  277. }
  278. }
  279. }
  280. $rooms[$index]['room_members'] = $room_members;
  281. }
  282. }
  283. }
  284. return $rooms;
  285. }
  286. private function room_case1($user)
  287. {
  288. $mod_room = Model('room');
  289. $chat_rooms = $mod_room->getJoinedChatRoom($user);
  290. $rooms = [];
  291. if(!empty($chat_rooms))
  292. {
  293. foreach ($chat_rooms as $item)
  294. {
  295. $rooms[] = $this->room_info($item);
  296. }
  297. }
  298. return $rooms;
  299. }
  300. private function room_info($room)
  301. {
  302. $mod_room = Model("room");
  303. $paticipates = $mod_room->getRoomMember($room['room_id']);
  304. $uids = [];
  305. if(!empty($paticipates))
  306. {
  307. foreach ($paticipates as $one){
  308. $uids[] = $one['member_id'];
  309. }
  310. }
  311. if(empty($uids)){
  312. $items = [];
  313. }else{
  314. $items = Model('member')->getMemberList(['member_id' => ['in',$uids]]);
  315. }
  316. $members = [];
  317. foreach ($items as $item){
  318. try
  319. {
  320. $info = new member_info($item);
  321. $members[] = $info->filter();
  322. }
  323. catch (Exception $ex) {
  324. Log::record($ex->getMessage(),Log::ERR);
  325. }
  326. }
  327. $ret = [
  328. "room_id"=>$room['room_id'],
  329. "room_name"=>$room["room_name"],
  330. "avatar"=> "", //头像和member查询维度太高
  331. "room_members"=>$members,
  332. ];
  333. return $ret;
  334. }
  335. private function room_case3($user){
  336. $mod_room = Model("room");
  337. $chats_rooms = $mod_room->getChatsRoom($user);
  338. $rooms = [];
  339. if(!empty($chats_rooms))
  340. {
  341. $roomids = [];
  342. foreach ($chats_rooms as $room)
  343. {
  344. $rooms[] = [
  345. "room_id"=>$room['room_id'],
  346. "room_name"=>$room['room_name'],
  347. "avatar"=>"",
  348. "room_members"=>[]
  349. ];
  350. $roomids[] = intval($room['room_id']);
  351. }
  352. $room_members = [];
  353. $uids_total = [];
  354. $roomTouser = $mod_room->table('room_msg')
  355. ->field('room_id,group_concat(DISTINCT member_id) as uidstr')
  356. ->where(['room_id'=>['in',$roomids]])
  357. ->group('room_id')
  358. ->select();
  359. if(!empty($roomTouser))
  360. {
  361. foreach ($roomTouser as $index => $item)
  362. {
  363. $uids = explode(',',$item['uidstr']);
  364. $uids_total = array_merge($uids_total,$uids);
  365. $room_members[$item['room_id']] = $uids;
  366. }
  367. }
  368. $members = [];
  369. if(!empty($uids_total))
  370. {
  371. $uids_total = array_unique($uids_total);
  372. $items = Model('member')->getMemberList(['member_id' => ['in',$uids_total]]);
  373. foreach ($items as $item){
  374. try
  375. {
  376. $info = new member_info($item);
  377. $members[$item['member_id']] = $info->filter();
  378. }
  379. catch (Exception $ex) {
  380. Log::record($ex->getMessage(),Log::ERR);
  381. }
  382. }
  383. }
  384. if(!empty($members))
  385. {
  386. foreach ($rooms as $index => $room)
  387. {
  388. $room_id = $room['room_id'];
  389. if(isset($room_members[$room_id]))
  390. {
  391. $user_list = [];
  392. foreach ($room_members[$room_id] as $i=>$uid)
  393. {
  394. if(isset($members[$uid])) $user_list[$i] = $members[$uid];
  395. }
  396. $rooms[$index]['room_members'] = $user_list;
  397. }
  398. }
  399. }
  400. }
  401. return $rooms;
  402. }
  403. }