Chat.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. <?php
  2. /**
  3. * User: Owen
  4. */
  5. use \GatewayWorker\Lib\Gateway;
  6. require_once "./GatewayWorker/Function/Init.php";
  7. class ChatCrazyWorker extends Init {
  8. public function Init($client_id,$data){
  9. $uid = intval($data['uid']);
  10. $message = $this->redis->hGetAll(okeys::Chat($uid));
  11. $out = [];
  12. foreach ($message as $key=>$row){
  13. $out[] = json_decode($row,true);
  14. }
  15. Gateway::sendToClient($client_id,$this->encryp(json_encode(['cmd'=>array_search('Chat_Init_Rec',ocmd::$Cmd),'code'=>1,'data'=>$out])));
  16. }
  17. public function sendToFriend($client_id,$data){
  18. $uid = intval($data['uid']);
  19. $toUid = intval($data['toUid']);
  20. $type = intval($data['type']);
  21. $content = $data['content'];
  22. if(empty($uid) || empty($toUid) || empty($type) || empty($content)){
  23. Gateway::sendToClient($client_id,$this->encryp(json_encode(['cmd'=>array_search('Chat_sendToFriend_Rec',ocmd::$Cmd),'code'=>-1,'msg'=>'參數錯誤'])));
  24. return false;
  25. }
  26. if($toUid == 1){
  27. $temp = [
  28. 'user_id' => $uid,
  29. 'content' => addslashes($content),
  30. 'orientation' => 1,
  31. 'have_read' => 0,
  32. 'type' => 1,
  33. 'created_at' => time(),
  34. ];
  35. $tb = otable::FeedBack2();
  36. $keys = [];
  37. $insert = [];
  38. foreach ($temp as $key=>$row){
  39. $keys[] = $key;
  40. $insert[] = $row;
  41. }
  42. $keys = implode(',',$keys);
  43. $insert = "'".implode("','",$insert)."'";
  44. $sql = "INSERT INTO {$tb} ({$keys}) VALUES ({$insert})";
  45. $this->db->query($sql,false);
  46. $userInfo = $this->getUserInfo($uid);
  47. $userAssets = $this->getUserAssets($uid);
  48. $message = [
  49. 'uid'=>$uid,
  50. 'toUid'=>$toUid,
  51. 'time'=>time(),
  52. 'type'=>$type,
  53. 'content'=>$content,
  54. 'star'=>$userAssets['star'],
  55. 'levelId'=>$userAssets['levelId'],
  56. 's_picture'=>$userInfo['s_picture'],
  57. 'nick'=>$userInfo['nick'],
  58. 'isFb'=>intval($userInfo['isFb']),
  59. 'u_flag'=>$userInfo['u_flag'],
  60. 'signature'=>$userInfo['signature'],
  61. 'sex'=>$userInfo['sex'],
  62. 'money'=>$userAssets['money'],
  63. 'vip'=>$userInfo['vip'],
  64. 'vipLv'=>$userInfo['vipLv'],
  65. 'vipExpire'=>$userInfo['vip_expire'],
  66. 'avatar'=>intval($userInfo['avatar']),
  67. ];
  68. Gateway::sendToClient($client_id,$this->encryp(json_encode(['cmd'=>array_search('Chat_sendToFriend_message',ocmd::$Cmd),'code'=>1,'data'=>$message])));
  69. return false;
  70. }
  71. $userInfo = $this->getUserInfo($uid);
  72. $userAssets = $this->getUserAssets($uid);
  73. $time = time();
  74. $message = [
  75. 'uid'=>$uid,
  76. 'toUid'=>$toUid,
  77. 'time'=>$time,
  78. 'type'=>$type,
  79. 'content'=>$content,
  80. 'star'=>$userAssets['star'],
  81. 'levelId'=>$userAssets['levelId'],
  82. 's_picture'=>$userInfo['s_picture'],
  83. 'nick'=>$userInfo['nick'],
  84. 'isFb'=>intval($userInfo['isFb']),
  85. 'u_flag'=>$userInfo['u_flag'],
  86. 'signature'=>$userInfo['signature'],
  87. 'sex'=>$userInfo['sex'],
  88. 'money'=>$userAssets['money'],
  89. 'vip'=>$userInfo['vip'],
  90. 'vipLv'=>$userInfo['vipLv'],
  91. 'vipExpire'=>$userInfo['vip_expire'],
  92. 'avatar'=>intval($userInfo['avatar']),
  93. ];
  94. if($type == 2){
  95. $message['voiceTime'] = $data['voiceTime'];
  96. }
  97. $this->redis->hSet(okeys::Chat($toUid),$uid.'#'.$time,json_encode($message));
  98. $this->redis->expire(okeys::Chat($toUid),oo::redisRandomExpire(30*24*60*60));
  99. Gateway::sendToUid($toUid,$this->encryp(json_encode(['cmd'=>array_search('Chat_sendToFriend_message',ocmd::$Cmd),'code'=>1,'data'=>$message])));
  100. Gateway::sendToClient($client_id,$this->encryp(json_encode(['cmd'=>array_search('Chat_sendToFriend_message',ocmd::$Cmd),'code'=>1,'data'=>$message])));
  101. }
  102. public function deleteCache($client_id,$data){
  103. $uid = intval($data['uid']);
  104. if(isset($data['toUid'])){
  105. $toUid = intval($data['toUid']);
  106. $message = $this->redis->hGetAll(okeys::Chat($uid));
  107. foreach ($message as $key=>$row){
  108. $temp = explode('#',$key);
  109. if($temp[0] == $toUid){
  110. $info = json_decode($row,true);
  111. if($info['type'] == 2){
  112. $str = $info['content'];
  113. $index = strpos($str,'storage');
  114. if($index !== false){
  115. $str = substr($str,$index,strlen($str));
  116. @unlink(__DIR__.'/../../'.$str);
  117. }
  118. }
  119. $this->redis->hDel(okeys::Chat($uid),$key);
  120. }
  121. }
  122. }else{
  123. $message = $this->redis->hGetAll(okeys::Chat($uid));
  124. foreach ($message as $key=>$row){
  125. $info = json_decode($row,true);
  126. if($info['type'] == 2){
  127. $str = $info['content'];
  128. $index = strpos($str,'storage');
  129. if($index !== false){
  130. $str = substr($str,$index,strlen($str));
  131. @unlink(__DIR__.'/../../'.$str);
  132. }
  133. }
  134. $this->redis->hDel(okeys::Chat($uid),$key);
  135. }
  136. }
  137. Gateway::sendToClient($client_id,$this->encryp(json_encode(['cmd'=>array_search('Chat_deleteCache_Rec',ocmd::$Cmd),'code'=>1])));
  138. }
  139. public function addToWorld($client_id,$data){
  140. Gateway::joinGroup($client_id,'WORLD');
  141. $out = [];
  142. $worldMessage = $this->redis->lGetRange(okeys::Chat('WORLD'),0,-1);
  143. if(isset($data['id'])){
  144. $id = intval($data['id']);
  145. foreach ($worldMessage as $row){
  146. $temp = json_decode($row,true);
  147. if($temp['id']>$id){
  148. $out[] = $temp;
  149. }
  150. }
  151. }else{
  152. foreach ($worldMessage as $row){
  153. $out[] = json_decode($row,true);
  154. }
  155. }
  156. Gateway::sendToClient($client_id,$this->encryp(json_encode(['cmd'=>array_search('Chat_addToWorld_Rec',ocmd::$Cmd),'code'=>1,'data'=>$out])));
  157. }
  158. public function leaveWorld($client_id){
  159. Gateway::leaveGroup($client_id,'WORLD');
  160. Gateway::sendToClient($client_id,$this->encryp(json_encode(['cmd'=>array_search('Chat_leaveWorld_Rec',ocmd::$Cmd),'code'=>1])));
  161. }
  162. public function sendToWorld($client_id,$data){
  163. $uid = intval($data['uid']);
  164. $type = intval($data['type']);
  165. $content = $data['content'];
  166. if(empty($uid) || empty($type) || empty($content)){
  167. Gateway::sendToClient($client_id,$this->encryp(json_encode(['cmd'=>array_search('Chat_sendToWorld_Rec',ocmd::$Cmd),'code'=>-1,'msg'=>'參數錯誤'])));
  168. return false;
  169. }
  170. if(oo::commonOprRedis('Userinfo')->get(okeys::chatLock($uid))){
  171. Gateway::sendToClient($client_id,$this->encryp(json_encode(['cmd'=>array_search('Chat_sendToWorld_Rec',ocmd::$Cmd),'code'=>-1,'msg'=>'參數錯誤'])));
  172. return;
  173. }
  174. if($type == 3){
  175. $gift = intval($data['gift']);
  176. $sendFlag = $this->redis->get(okeys::ChatGiftSend($uid,$gift));
  177. if($sendFlag){
  178. $ttl = $this->redis->ttl(okeys::ChatGiftSend($uid,$gift));
  179. Gateway::sendToClient($client_id,$this->encryp(json_encode(['cmd'=>array_search('Chat_sendToWorld_Rec',ocmd::$Cmd),'code'=>-2,'msg'=>'礼物时间','gift'=>$gift,'ttl'=>$ttl])));
  180. return false;
  181. }
  182. if(intval($this->redis->hGet(okeys::chatGitfMaxNum(),okeys::chatGitfMaxNum($uid,$gift)))>=2){
  183. Gateway::sendToClient($client_id,$this->encryp(json_encode(['cmd'=>array_search('Chat_sendToWorld_Rec',ocmd::$Cmd),'code'=>-2,'msg'=>'礼物时间','gift'=>$gift,'ttl'=>0])));
  184. return false;
  185. }
  186. }
  187. $chatNum = 0;
  188. if($type == 1){
  189. $chatNum = intval(oo::commonOprRedis('common')->hGet(okeys::chatMaxNum(),$uid));
  190. if($chatNum>=1){
  191. Gateway::sendToClient($client_id,$this->encryp(json_encode(['cmd'=>array_search('Chat_sendToWorld_Rec',ocmd::$Cmd),'code'=>-3,'msg'=>'发言上限','maxNum'=>$chatNum])));
  192. return false;
  193. }
  194. }
  195. $userInfo = $this->getUserInfo($uid);
  196. $userAssets = $this->getUserAssets($uid);
  197. $time = time();
  198. list($msec, $sec) = explode(' ', microtime());
  199. $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
  200. $message = [
  201. 'id'=>date("Ymd").$msectime,
  202. 'uid'=>$uid,
  203. 'time'=>$time,
  204. 'type'=>$type,
  205. 'star'=>$userAssets['star'],
  206. 'levelId'=>$userAssets['levelId'],
  207. 's_picture'=>$userInfo['s_picture'],
  208. 'nick'=>$userInfo['nick'],
  209. 'isFb'=>intval($userInfo['isFb']),
  210. 'u_flag'=>$userInfo['u_flag'],
  211. 'signature'=>$userInfo['signature'],
  212. 'sex'=>$userInfo['sex'],
  213. 'money'=>$userAssets['money'],
  214. 'vip'=>$userInfo['vip'],
  215. 'vipLv'=>$userInfo['vipLv'],
  216. 'vipExpire'=>$userInfo['vip_expire'],
  217. 'avatar'=>intval($userInfo['avatar']),
  218. ];
  219. if($type == 3){
  220. $message['gift'] = $gift;
  221. if($gift != 3){
  222. $num = 0;
  223. $maxNum = 10;
  224. $str = date('YmdHis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
  225. $message['giftId'] = $str;
  226. $message['num'] = $num;
  227. $message['maxNum'] = $maxNum;
  228. $this->redis->setex(okeys::ChatGift($str),json_encode([
  229. 'num'=>$num,
  230. 'maxNum'=>$maxNum,
  231. 'gift'=>$gift,
  232. 'uid'=>$uid,
  233. ]),24*60*60);
  234. }
  235. $this->redis->setex(okeys::ChatGiftSend($uid,$gift),1,10);
  236. $this->redis->hSet(okeys::chatGitfMaxNum(),okeys::chatGitfMaxNum($uid,$gift),intval($this->redis->hGet(okeys::chatGitfMaxNum(),okeys::chatGitfMaxNum($uid,$gift)))+1);
  237. }else{
  238. $message['content'] = $content;
  239. if($type == 2){
  240. $message['voiceTime'] = $data['voiceTime'];
  241. }
  242. if($type == 1){
  243. oo::commonOprRedis('common')->hSet(okeys::chatMaxNum(),$uid,$chatNum+1);
  244. }
  245. }
  246. Gateway::sendToGroup('WORLD',$this->encryp(json_encode(['cmd'=>array_search('Chat_sendToWorld_message',ocmd::$Cmd),'code'=>1,'data'=>$message])));
  247. $this->redis->rPush(okeys::Chat('WORLD'),json_encode($message));
  248. oo::logs()->debug5($message,date('Ymd').'.log','Chat/');
  249. $len = @$this->redis->lSize(okeys::Chat('WORLD'));
  250. if($len>20){
  251. $lPop = $this->redis->lPop(okeys::Chat('WORLD'));
  252. $lPop = json_decode($lPop,true);
  253. if($lPop['type'] == 2){
  254. $str = $lPop['content'];
  255. $index = strpos($str,'storage');
  256. if($index !== false){
  257. $str = substr($str,$index,strlen($str));
  258. @unlink(__DIR__.'/../../'.$str);
  259. }
  260. }
  261. }
  262. }
  263. /**
  264. * 领取礼物
  265. * @param $param
  266. * @return false|mixed|string
  267. * Created by: Owen
  268. * Created on: 2019/8/30 15:11
  269. * Description:
  270. */
  271. public function gift($client_id,$data){
  272. $uid = intval($data['uid']);
  273. $giftId = intval($data['giftId']);
  274. $gift = $this->redis->get(okeys::ChatGift($giftId));
  275. if(empty($gift)){
  276. return json_encode(['code'=>-1,'msg'=>'gift error']);
  277. }
  278. $gift = json_decode($gift,true);
  279. // 判断参与人是否能领 -- 礼物ID限制
  280. $userGiftId = $this->redis->get(okeys::ChatGiftId($uid,$giftId));
  281. if($userGiftId != false){
  282. Gateway::sendToClient($client_id,$this->encryp(json_encode(['cmd'=>array_search('Chat_gift_Rec',ocmd::$Cmd),'code'=>-1,'msg'=>'已领取过礼物'])));
  283. return false;
  284. }
  285. // 判断参与人是否能领 -- 数量限制
  286. $userGift = $this->redis->get(okeys::ChatGiftRec($uid,$gift['gift'].'#participate'));
  287. $money = 5000;
  288. $spins = 1;
  289. if(empty($userGift)){
  290. if($gift['gift'] == 1){
  291. oo::commonOprModel('member')->optProperty($uid,array('money'=>$money),'+',106,'聊天礼物');
  292. }else if($gift['gift'] == 2){
  293. oo::commonOprModel('member')->optProperty($uid,array('spins'=>$spins),'+',106,'聊天礼物');
  294. }
  295. $this->redis->set(okeys::ChatGiftRec($uid,$gift['gift'].'#participate'),1);
  296. $this->redis->expireAt(okeys::ChatGiftRec($uid,$gift['gift'].'#participate'),strtotime('23:59:59'));
  297. }else{
  298. if($gift['gift'] == 1){
  299. if(intval($userGift)>=10){
  300. Gateway::sendToClient($client_id,$this->encryp(json_encode(['cmd'=>array_search('Chat_gift_Rec',ocmd::$Cmd),'code'=>-2,'msg'=>'礼物已领满'])));
  301. return false;
  302. }else{
  303. oo::commonOprModel('member')->optProperty($uid,array('money'=>$money),'+',106,'聊天礼物');
  304. }
  305. }else if($gift['gift'] == 2){
  306. if(intval($userGift)>=10){
  307. Gateway::sendToClient($client_id,$this->encryp(json_encode(['cmd'=>array_search('Chat_gift_Rec',ocmd::$Cmd),'code'=>-2,'msg'=>'礼物已领满'])));
  308. return false;
  309. }else{
  310. oo::commonOprModel('member')->optProperty($uid,array('spins'=>$spins),'+',106,'聊天礼物');
  311. }
  312. }else{
  313. Gateway::sendToClient($client_id,$this->encryp(json_encode(['cmd'=>array_search('Chat_gift_Rec',ocmd::$Cmd),'code'=>-3,'msg'=>'礼物类型异常'])));
  314. return false;
  315. }
  316. $this->redis->set(okeys::ChatGiftRec($uid,$gift['gift'].'#participate'),intval($userGift)+1);
  317. $this->redis->expireAt(okeys::ChatGiftRec($uid,$gift['gift'].'#participate'),strtotime('23:59:59'));
  318. }
  319. // // 判断发起人是否能领
  320. // $userGift = $this->redis->get(okeys::ChatGiftRec($gift['uid'],$gift['gift'].'#initiate'));
  321. // $initiate = false;
  322. // if(empty($userGift)){
  323. // if($gift['gift'] == 1){
  324. // oo::commonOprModel('member')->optProperty($gift['uid'],array('money'=>$money),'+','聊天礼物');
  325. // }else if($gift['gift'] == 2){
  326. // oo::commonOprModel('member')->optProperty($gift['uid'],array('spins'=>$spins),'+','聊天礼物');
  327. // }
  328. // $this->redis->set(okeys::ChatGiftRec($gift['uid'],$gift['gift'].'#initiate'),$spins);
  329. // $this->redis->expireAt(okeys::ChatGiftRec($gift['uid'],$gift['gift'].'#initiate'),strtotime('23:59:59'));
  330. // $initiate = true;
  331. // }else{
  332. // if(intval($userGift)<10){
  333. // if($gift['gift'] == 1){
  334. // oo::commonOprModel('member')->optProperty($gift['uid'],array('money'=>$money),'+','聊天礼物');
  335. // }else if($gift['gift'] == 2){
  336. // oo::commonOprModel('member')->optProperty($gift['uid'],array('spins'=>$spins),'+','聊天礼物');
  337. // }
  338. // $this->redis->set(okeys::ChatGiftRec($gift['uid'],$gift['gift'].'#initiate'),intval($userGift)+1);
  339. // $this->redis->expireAt(okeys::ChatGiftRec($gift['uid'],$gift['gift'].'#initiate'),strtotime('23:59:59'));
  340. // $initiate = true;
  341. // }
  342. // }
  343. $newNum = $gift['num']+1;
  344. $sData = [
  345. 'num'=>$newNum,
  346. 'maxNum'=>$gift['maxNum'],
  347. 'gift'=>$gift['gift'],
  348. 'uid'=>$gift['uid'],
  349. 'giftId'=>$giftId,
  350. 'recUid'=>$uid
  351. ];
  352. // if($initiate){
  353. // $sData['reward'] = [
  354. // 'type'=>$gift['gift'],
  355. // 'num'=>($gift['gift'] == 1)?$money:$spins,
  356. // ];
  357. // }
  358. $worldMessage = $this->redis->lGetRange(okeys::Chat('WORLD'),0,-1);
  359. foreach ($worldMessage as $key=>$row){
  360. $temp = json_decode($row,true);
  361. if($temp['type'] == 3 && $temp['giftId'] == $giftId){
  362. $temp['num'] = $newNum;
  363. $this->redis->lSet(okeys::Chat('WORLD'),$key,json_encode($temp));
  364. }
  365. }
  366. if($newNum>=10){
  367. $this->redis->delete(okeys::ChatGift($giftId));
  368. }else{
  369. $ttl = $this->redis->ttl(okeys::ChatGift($giftId));
  370. $this->redis->setex(okeys::ChatGift($giftId),json_encode($sData),$ttl);
  371. }
  372. $this->redis->setex(okeys::ChatGiftId($uid,$giftId),1,24*60*60);
  373. //聊天的人均金币体力领取次数统计
  374. oo::commonOprModel('statistics')->chatGiftSta($gift['gift']==1?'money':'spins');
  375. Gateway::sendToGroup('WORLD',$this->encryp(json_encode(['cmd'=>array_search('Chat_giftRec_message',ocmd::$Cmd),'code'=>1,'data'=>$sData])));
  376. Gateway::sendToClient($client_id,$this->encryp(json_encode(['cmd'=>array_search('Chat_gift_Rec',ocmd::$Cmd),'code'=>1,'data'=>[
  377. 'type'=>$gift['gift'],
  378. 'num'=>($gift['gift'] == 1)?$money:$spins,
  379. ]])));
  380. }
  381. /**
  382. * 礼物通知
  383. * @param $client_id
  384. * @param $data
  385. * @return bool
  386. * Created by: Owen
  387. * Created on: 2019/9/4 12:26
  388. */
  389. public function giftReced($client_id,$data){
  390. $uid = intval($data['uid']);
  391. $toUid = intval($data['toUid']);
  392. $gift = intval($data['gift']);
  393. $time = intval($data['time']);
  394. if(empty($uid) || empty($toUid) || empty($gift)){
  395. Gateway::sendToClient($client_id,$this->encryp(json_encode(['cmd'=>array_search('Chat_giftReced_Rec',ocmd::$Cmd),'code'=>-1,'msg'=>'param error'])));
  396. return false;
  397. }
  398. Gateway::sendToUid($toUid,$this->encryp(json_encode(['cmd'=>array_search('Chat_giftReced_message',ocmd::$Cmd),'code'=>1,'data'=>['uid'=>$uid,'toUid'=>$toUid,'gift'=>$gift,'time'=>$time]])));
  399. }
  400. /**
  401. * ---------------------------------------数据获取-----------------------------------------------
  402. */
  403. public function getUserInfo($uid){
  404. $userInfo = oo::commonOprModel('member')->getUserInfo($uid);
  405. return $userInfo;
  406. }
  407. public function getUserAssets($uid){
  408. return oo::commonOprModel('member')->getUserAssetsInfo($uid);
  409. }
  410. }