notice.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. <?php
  2. class ModelNotice{
  3. /**
  4. * Notes:定时公告推送
  5. * User: wsc
  6. * Time: 2020/7/13 16:29
  7. * @param int $type
  8. * @param bool $do 是否直接推送
  9. * @return array
  10. */
  11. public function cronToSendNotice($type=1,$uid='',$do=false){
  12. $time =time();
  13. $data = $this->getNotice($type);
  14. if($type=1&&!empty($data)){//停服公告
  15. $sendTime = [30,10,5,1];
  16. $shortTime = $data['stime']-$time;
  17. $shortMinute = $shortTime>0?intval($shortTime/60):-1;
  18. $ret['content'] =funs::dejson($data['content']);
  19. if($shortMinute==0||$do==2){//到点
  20. $ret['stime']= $data['stime'];
  21. $ret['etime']= $data['etime'];
  22. if(!empty($uid)){
  23. oo::commonOprModel('Workerman')->push($uid,ocmd::$send['Announcement'],$ret);
  24. }else{
  25. oo::commonOprModel('Workerman')->pushAll(ocmd::$send['Announcement'],$ret);
  26. }
  27. }
  28. if(in_array($shortMinute,$sendTime)||$do==1){
  29. $ret['etime']= $data['stime'];
  30. $ret['type']=1;
  31. $ret['systemTips']=0;//持续显示
  32. if(!empty($uid)){
  33. oo::commonOprModel('Workerman')->push($uid,ocmd::$SVR_NOTICE_INFO,$ret);
  34. }else{
  35. oo::commonOprModel('Workerman')->pushAll(ocmd::$SVR_NOTICE_INFO,$ret);
  36. }
  37. return true;
  38. }else{
  39. return false;
  40. }
  41. }
  42. }
  43. /**
  44. * Notes:客户端获取公告
  45. * User: wsc
  46. * Time: 2020/7/7 15:49
  47. * @param $type 1停服公告
  48. * @return array
  49. */
  50. public function clientGetNotice($uid,$type=1){
  51. $time =time();
  52. $data = $this->getNotice($type);
  53. $title = !empty($data['title'])?funs::dejson($data['title']):[];
  54. $content = !empty($data['content'])?funs::dejson($data['content']):[];
  55. $lang = oo::getDefinedLang($uid);
  56. $lang = strtoupper($lang);
  57. $ret['title'] = isset($title[$lang])?$title[$lang]:$title['EN'];
  58. $ret['content'] = isset($content[$lang])?$content[$lang]:$content['EN'];
  59. $ret['stime'] = $data['stime'];
  60. $ret['etime'] = $data['etime'];
  61. $wList = explode(',',$data['wlist']);
  62. if(empty($data)||$data['stime']-60>$time||$time>$data['etime']||$data['isStop']||in_array($uid,$wList)){
  63. return [];
  64. }
  65. return $ret;
  66. }
  67. /**
  68. * Notes:获取公告
  69. * User: wsc
  70. * Time: 2020/7/7 15:49
  71. * @param string $type
  72. * @param bool $db
  73. * @return array
  74. */
  75. public function getNotice($type="",$db=false){
  76. $key = okeys::serverNotice();
  77. $tb = otable::notice();
  78. $list = oo::commonOprRedis('config')->hGetAll($key);
  79. $data = funs::getArrFromJsonArr($list);
  80. if($db){
  81. $where = '';
  82. if($type){
  83. $where = " WHERE type={$type}";
  84. }
  85. $sql = "SELECT * FROM {$tb} {$where} ORDER BY stime DESC";
  86. $result = oo::commonOprDb('config')->getAll($sql,1);
  87. if(!empty($result)){
  88. $data = array_column($result,null,'type');
  89. foreach ($data as $k=>$v){
  90. $d = json_encode($v,256);
  91. oo::commonOprRedis('config')->hSet($key,$v['type'],$d);
  92. }
  93. }
  94. }
  95. if(!empty($type)&&!empty($data[$type])){
  96. $data = $data[$type];
  97. }
  98. return $data;
  99. }
  100. /**
  101. * Notes:设置公告
  102. * User: wsc
  103. * Time: 2020/7/7 15:49
  104. * @param $data
  105. * @return bool
  106. */
  107. public function setNotice($data){
  108. $type = $data['type'];
  109. $tb = otable::notice();
  110. $data['content'] = isset($data['content'])?json_encode($data['content'],256):"[]";
  111. $data['title'] = isset($data['title'])?json_encode($data['title']):"[]";
  112. if(!empty($this->getNotice($type))){
  113. unset($data['type']);
  114. unset($data['id']);
  115. $w = " type={$type}";
  116. $sql =funs::db_updateSQL($tb,$data,$w);
  117. }else{
  118. $sql = funs::db_insertSQL($tb,$data);
  119. }
  120. $res = oo::commonOprDb('config')->query($sql);
  121. if($res){
  122. $this->getNotice($type,true);
  123. return true;
  124. }
  125. return false;
  126. }
  127. //==============系统公告====================
  128. public function getNowAnnouncement($uid){
  129. $ret = $data=[];
  130. $list = $this->getAnnouncemant();
  131. $now = time();
  132. foreach ($list as $v){
  133. if(!empty($v['stime'])&&!empty($v['etime'])){//过滤时间
  134. if($now>=$v['stime']-30&&$now<=$v['etime']){
  135. $data = $v;
  136. break;
  137. }
  138. }
  139. }
  140. if(!empty($data)){
  141. $slInfo = oo::commonOprRedis('userinfo')->hGetAll(okeys::userDefinedInfo($uid));
  142. $blist = !empty($data['blist'])?explode(',',$data['blist']):[];
  143. if(in_array($uid,$blist)){//黑名单只要有系统公告就停服
  144. $data['type']=1;
  145. }else{
  146. if($data['device']!=0&&!empty($slInfo['sid'])&&$data['device']!=$slInfo['sid']){//过滤渠道
  147. $data =[];
  148. }
  149. if($data['usertype']!=0&&!empty($slInfo['lid'])&&$data['usertype']!=$slInfo['lid']){//过滤用户类型
  150. $data =[];
  151. }
  152. $wlist = !empty($data['wlist'])?explode(',',$data['wlist']):[];
  153. if(!empty($wlist)&&is_array($wlist)&&in_array($uid,$wlist)){//过滤白名单
  154. $data =[];
  155. }
  156. }
  157. if($data){
  158. $lang = oo::getDefinedLang($uid)??'EN';
  159. $lang = strtoupper($lang);
  160. unset($data['id'],$data['wlist'],$data['blist'],$data['device'],$data['usertype']);
  161. $data['content'] = $data['content'][$lang]?$data['content'][$lang]:$data['content']['EN'];
  162. }
  163. }
  164. return $data;
  165. }
  166. /**
  167. * 1.11新版公告
  168. * @param int $uid
  169. * @return array
  170. */
  171. public function getNowAnnouncementNew($uid) {
  172. $key = okeys::announcementReward($uid);
  173. $ret = oo::commonOprRedis('userinfo')->get($key);
  174. //if(!empty($ret)){
  175. // return (array)json_decode($ret, true);
  176. //}
  177. $list = $this->getAnnouncemant(2);
  178. $now = time();
  179. $data = [];
  180. foreach ($list as $v){
  181. if(!empty($v['stime'])&&!empty($v['etime'])){//过滤时间
  182. if($now>=$v['stime']-30&&$now<=$v['etime']){
  183. $data = $v;
  184. break;
  185. }
  186. }
  187. }
  188. if(empty($data)){
  189. return [];
  190. }
  191. $slInfo = oo::commonOprRedis('userinfo')->hGetAll(okeys::userDefinedInfo($uid));
  192. if($data['device']!=0&&!empty($slInfo['sid'])&&$data['device']!=$slInfo['sid']){//过滤渠道
  193. $data =[];
  194. }
  195. if(empty($data)){
  196. return [];
  197. }
  198. $version = oo::commonOprModel('user')->getUserVersion($uid);
  199. //过滤版本
  200. $dversion =implode('.',array_slice(explode('.',$version),0,3));
  201. $versions = [];
  202. if(!empty($data['version']) && is_array($data['version'])){
  203. foreach ($data['version'] as $v){
  204. $str = implode('.',array_slice(explode('.',$v),0,3));
  205. $versions[] = $str;
  206. }
  207. }
  208. if(!empty($versions) && !in_array($dversion,$versions)){
  209. return [];
  210. }
  211. //if($data){
  212. //$lang = oo::getDefinedLang($uid)??'EN';
  213. //unset($data['id'], $data['usertype'], $data['admin_user'], $data['version']);
  214. //$data['content'] = $data['content'][$lang]?$data['content'][$lang]:$data['content']['EN'];
  215. //}
  216. $ret = $ret ? (array)json_decode($ret, true) : [];
  217. $got = !empty($ret['got']) ? (int)$ret['got'] : 0;
  218. $newVersion = !empty($data['newVersion']) ? $data['newVersion'] : '1.11.1';
  219. $rewardInfo = [
  220. 'title' => $data['title'],
  221. 'content' => $data['content'],
  222. 'version' => $newVersion,
  223. 'reward' => $data['reward'],
  224. 'stime' => $data['stime'],
  225. 'etime' => $data['etime'],
  226. 'got' => !empty($ret['got']) ? (int)$ret['got'] : 0,
  227. 'range_desc' => $data['range_desc'],
  228. 'time_desc' => $data['time_desc'],
  229. ];
  230. if(empty($ret)){ //第一次设置值
  231. //没有领过的新版本都要可领取
  232. $ret = ['got' => 0, 'version' => $version];
  233. $expire = $data['etime'] - $now + 3*24*60*60;
  234. oo::commonOprRedis('userinfo')->setex($key,json_encode($ret),oo::redisRandomExpire($expire));
  235. }
  236. return $rewardInfo;
  237. }
  238. /**
  239. * 添加1.11新版公告
  240. * @param int $flag
  241. * @return array
  242. */
  243. public function getAnnouncemant($flag = 0){
  244. $key = okeys::announcement($flag);
  245. $list = oo::commonOprRedis('config')->hGetAll($key);
  246. if(empty($list)){
  247. $tb = otable::announcement($flag);
  248. $sql = "SELECT * FROM {$tb}";
  249. if($flag == 2){
  250. $sql .= ' WHERE plat IN(0, 1)'; //过滤fox公告
  251. }
  252. $retData = oo::commonOprDb('config')->getAll($sql,MYSQLI_ASSOC);
  253. foreach ($retData as $k=>$v){
  254. if($flag){
  255. $v['version'] = empty($v['version']) ? [] : (array)json_decode($v['version'], true);
  256. $v['title'] = empty($v['title']) ? [] : (array)json_decode($v['title'], true);
  257. $v['content'] = empty($v['content']) ? [] : (array)json_decode($v['content'], true);
  258. $v['reward'] = empty($v['reward']) ? [] : (array)json_decode($v['reward'], true);
  259. $v['time_desc'] = empty($v['time_desc']) ? [] : (array)json_decode($v['time_desc'], true);
  260. $v['range_desc'] = empty($v['range_desc']) ? [] : (array)json_decode($v['range_desc'], true);
  261. }
  262. $list[$v['id']] = json_encode($v,256);
  263. }
  264. oo::commonOprRedis('config')->hMset($key,$list);
  265. }
  266. if($list){
  267. $list = funs::getArrFromJsonArr($list);
  268. }else {
  269. $list = [];
  270. }
  271. return $list;
  272. }
  273. //==============消息类跑马灯管理===========
  274. /**
  275. * Notes:跑马灯样板
  276. * User: wsc
  277. * Time: 2021/1/20 20:55
  278. * @param $type
  279. * @return mixed
  280. */
  281. public function msgModel($type){
  282. $arr['buyVip'] = [
  283. "type"=>"buyVip",
  284. "content"=>[
  285. 'ZH'=> '恭喜! <color=#fff220>{0}</color>成为了尊贵的<color=#5afffe>VIP</color>',
  286. 'TW'=> '恭喜! <color=#fff220>{0}</color> 成為了尊貴的<color=#5afffe>VIP</color>',
  287. 'EN'=> 'Congratulations! <color=#fff220>{0}</color> became the <color=#5afffe>VIP</color>!',
  288. 'FR'=> 'Félicitations ! <color=#fff220>{0}</color> a devenu <color=#5afffe>VIP</color>!',
  289. 'DE'=> 'Glückwunsch! <color=#fff220>{0}</color> wurde der <color=#5afffe>VIP</color>!',
  290. ],
  291. 'conf'=>[]
  292. ];
  293. $arr['getCard'] = [
  294. "type"=>"getCard",
  295. "content"=>[
  296. 'ZH'=> '哇!<color=#fff220>{0}</color>获得了一张<color=#5afffe>小丑卡</color>!',
  297. 'TW'=> '哇!<color=#fff220>{0}</color>獲得了一張<color=#5afffe>小丑卡</color>!',
  298. 'EN'=> 'Amazing! <color=#fff220>{0}</color> got a <color=#5afffe>Joker Card</color>!',
  299. 'FR'=> 'Parfait ! <color=#fff220>{0}</color> gagne une <color=#5afffe>Carte Joker</color> !',
  300. 'DE'=> 'Toll! <color=#fff220>{0}</color> hat eine <color=#5afffe>Joker-Karte</color> bekommen!',
  301. ],
  302. 'conf'=>[]
  303. ];
  304. $arr['fulCardGroup'] = [
  305. "type"=>"fulCardGroup",
  306. "content"=>[
  307. 'ZH'=> '恭喜!<color=#fff220>{0}</color>集齐了<color=#5afffe>{1}</color>卡组!',
  308. 'TW'=> '恭喜!<color=#fff220>{0}</color>集齊了<color=#5afffe>YYY</color>卡組!',
  309. 'EN'=> 'Congratulations! <color=#fff220>{0}</color> completed the <color=#5afffe>{1}</color> set!',
  310. 'FR'=> 'Félicitations ! <color=#fff220>{0}</color> complète <color=#5afffe>{1}</color> set !',
  311. 'DE'=> 'Glückwunsch! <color=#fff220>{0}</color> hat das Set <color=#5afffe>{1}</color> vervollständigt!',
  312. ],
  313. 'conf'=>[]
  314. ];
  315. $arr['getMaxSpins'] = [
  316. "type"=>"getMaxSpins",
  317. "content"=>[
  318. 'ZH'=> '太棒了!<color=#fff220>{0}</color>在<color=#1fff2a>{2}</color>中获得了<color=#5afffe>{1}</color>体力!',
  319. 'TW'=> '太棒了!<color=#fff220>{0}</color>在<color=#1fff2a>{2}</color>中獲得了<color=#5afffe>{1}</color>體力!',
  320. 'EN'=> 'Awesome! <color=#fff220>{0}</color> won <color=#5afffe>{1}</color> spins in <color=#1fff2a>{2}</color>!',
  321. 'FR'=> 'Bravo ! <color=#fff220>{0}</color> gagne <color=#5afffe>{1}</color> tours sur <color=#1fff2a>{2}</color>!',
  322. 'DE'=> 'Bravo! <color=#fff220>{0}</color> hat <color=#5afffe>{1}</color> Drehungen im <color=#1fff2a>{2}</color> gewonnen!',
  323. ],
  324. 'conf'=>["maxSpins"=>1000]
  325. ];
  326. $arr['buyCrazyPass']=[
  327. "type"=>"buyCrazyPass",
  328. "content"=>[
  329. 'ZH'=> '<color=#fff220>{0}</color> 购买了<color=#5afffe>疯狂通行证</color>',
  330. 'TW'=> '<color=#fff220>{0}</color> 購買了<color=#5afffe>瘋狂通行證</color>',
  331. 'EN'=> '<color=#fff220>{0}</color> purchased <color=#5afffe>Crazy Pass</color>',
  332. 'FR'=> '<color=#fff220>{0}</color> a acheté le <color=#5afffe>Crazy Pass</color>',
  333. 'DE'=> '<color=#fff220>{0}</color> hat <color=#5afffe>Verrückten Pass</color> gekauft',
  334. ],
  335. 'conf'=>[]
  336. ];
  337. return $arr[$type];
  338. }
  339. /**
  340. * Notes:通过类型获取跑马灯配置
  341. * User: wsc
  342. * Time: 2021/1/20 15:28
  343. * @param $type
  344. * @param bool $db
  345. * @return array|mixed
  346. */
  347. public function getNoticeMsgByType($type){
  348. $key=okeys::noticeMsg();
  349. $info = oo::commonOprRedis('config')->hGet($key,$type);
  350. if(empty($info)){
  351. $tb = otable::noticeMsg();
  352. $sql = "SELECT * FROM {$tb} where type ='{$type}'";
  353. $data = oo::commonOprDb('config')->getOne($sql,1);
  354. if(!empty($data)){
  355. $arr['type'] = $data['type'];
  356. $arr['conf']=json_decode($data['conf'],true);
  357. $arr['content']=json_decode($data['content'],true);
  358. $info = str_replace("\/", "/", json_encode($arr,256));
  359. oo::commonOprRedis('config')->hSet($key,$type,$info);
  360. }
  361. }
  362. return !empty($info)?json_decode($info,true):[];
  363. }
  364. /**
  365. * Notes:
  366. * User: wsc
  367. * Time: 2021/1/20 21:58
  368. * @param $uid
  369. * @param $type
  370. * @param array $ext buyVip=[],getCard=[],fulCardGroup=["卡组英文名"],getMaxSpins=["体力值","场景英文名"],buyCrazyPass=[]
  371. */
  372. public function sendNoticeMsg($uid,$type,$ext=[],$filter=[]){
  373. $msgCfg = $this->getAgesCfg($type);
  374. if($msgCfg['status']!=1){//开关
  375. return true;
  376. }
  377. $info = oo::commonOprModel('member')->getUserInfo($uid);
  378. if(in_array($type,["buyCrazyPass"])){//循环播放
  379. $data = [
  380. "stime"=>time(),
  381. "etime"=>time()+20,
  382. "systemTips"=>1,
  383. ];
  384. }else{//一次性播放
  385. $data = ["systemTips"=>0];
  386. }
  387. $data["type"]=$type;
  388. $data['args'] = $ext;
  389. $nick=oo::commonOprModel('member')->getUserFirstName($info['nick']);
  390. array_unshift($data['args'],$nick);
  391. if(funs::concurrentLock(1,'sendNoticeMsg_'.$type,2)){//群播,2秒最多一条 //oo::commonOprRedis('statistics')->exists(okeys::noticeMsgList($type))||
  392. if($filter){//过滤群播
  393. $this->filterUserSend($filter,$data);
  394. }else{
  395. oo::commonOprModel('Workerman')->pushAll(ocmd::$send['NOTICE_MSG_REC'],$data);//群播所有人
  396. }
  397. }else{//单播给自己
  398. oo::commonOprModel('Workerman')->push($uid,ocmd::$send['NOTICE_MSG_REC'],$data);
  399. }
  400. }
  401. /**
  402. * Notes:过滤型群播
  403. * User: wsc
  404. * Time: 2021/1/21 17:11
  405. * @param $filter array 过滤群播条件["minLevel"=>"最小等级"]
  406. * @param $data array 群播数据
  407. */
  408. public function filterUserSend($filter,$data){
  409. $allUid = oo::commonOprModel("workerman")->getAllUidList();//当前机器所有uid
  410. foreach($allUid as $k => $uid){
  411. $property = oo::commonOprModel('member')->getUserAssetsInfo($uid);
  412. $levelId = intval($property['levelId']);
  413. if($levelId >= $filter['minLevel']){
  414. oo::commonOprModel('Workerman')->push($uid,ocmd::$send['NOTICE_MSG_REC'],$data);
  415. }
  416. }
  417. }
  418. /**
  419. * Notes:获取跑马灯配置
  420. * User: wsc
  421. * Time: 2021/1/21 17:01
  422. * @param $type
  423. * @return mixed
  424. */
  425. public function getAgesCfg($type){
  426. $info = $this->getNoticeMsgByType($type);
  427. return $info['conf'];
  428. }
  429. }