Plantask.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace app\index\controller;
  3. use think\Controller;
  4. use think\Db;
  5. use think\Cache;
  6. class Plantask extends Controller
  7. {
  8. //计划任务
  9. //定时跑用户活跃,改成离线
  10. public function user_active(){
  11. $actitime = time() - 600;
  12. //$map = ['requesttime' < $actitime];
  13. $sql = 'update `mt_user` set is_active = 0 where id in (select user_id from mt_user_active where requesttime < '.$actitime.')';
  14. //echo $sql;
  15. db()->query($sql);
  16. }
  17. //计划任务
  18. //代替公会的人10秒内发出五句话
  19. public function firstuser_send()
  20. {
  21. //找出24小时内注册的男人
  22. $map = [
  23. 'jointime' => ['gt',time()-86400],
  24. 'gender' => 1,
  25. 'gh_id' => 0,
  26. ];
  27. $oneuser = Db::name('user')->where($map)->order('id asc')->value('id');
  28. dump($oneuser);
  29. if(!$oneuser){
  30. echo 'empty';
  31. exit;
  32. }
  33. //找出公会的人
  34. $map = [
  35. 'gh_id' => ['gt',0],
  36. 'gender' => 0,
  37. ];
  38. $ghuser = Db::name('user')->where($map)->orderRaw('rand()')->limit(5)->column('id');
  39. //dump($ghuser);
  40. //随机取出一句话
  41. $oneword = Db::name('plantask_accost')->orderRaw('rand()')->limit(5)->column('title');
  42. //dump($oneword);
  43. //发送出去
  44. $cache = Cache::connect(['type'=>'Redis']);
  45. $times = $cache->get('plantask_first_word_'.$oneuser);
  46. //dump($times);
  47. if($times === false){
  48. $times = 0;
  49. }
  50. if($times < 5){
  51. $tenim = new \app\common\library\Tenim;
  52. for($i = 0;$i < 5;$i++){
  53. $ghuser_one = isset($ghuser[$i]) ? $ghuser[$i] : $ghuser[array_rand($ghuser)];
  54. $oneword_one = isset($oneword[$i]) ? $oneword[$i] : $oneword[array_rand($oneword)];
  55. $tenim->sendMessageToUser($ghuser_one,$oneuser,$oneword_one);
  56. sleep(2);
  57. }
  58. $cache->set('plantask_first_word_'.$oneuser, 5);
  59. }
  60. }
  61. //计划任务,二期之后,废弃
  62. //代替公会的人发出第一句话
  63. public function firstword_send()
  64. {
  65. exit;
  66. //找出24小时内注册的男人
  67. $map = [
  68. 'jointime' => ['gt',time()-86400],
  69. 'gender' => 1,
  70. 'gh_id' => 0,
  71. ];
  72. $oneuser = Db::name('user')->where($map)->orderRaw('rand()')->value('id');
  73. //dump($oneuser);
  74. //找出公会的人
  75. $map = [
  76. 'gh_id' => ['gt',0],
  77. 'gender' => 0,
  78. ];
  79. $ghuser = Db::name('user')->where($map)->orderRaw('rand()')->value('id');
  80. //dump($ghuser);
  81. //随机取出一句话
  82. $oneword = Db::name('plantask_accost')->orderRaw('rand()')->value('title');
  83. //dump($oneword);
  84. //发送出去
  85. $cache = Cache::connect(['type'=>'Redis']);
  86. $times = $cache->get('plantask_first_word_'.$oneuser);
  87. //dump($times);
  88. if($times === false){
  89. $times = 0;
  90. }
  91. if($times < 5){
  92. $tenim = new \app\common\library\Tenim;
  93. $tenim->sendMessageToUser($ghuser,$oneuser,$oneword);
  94. $cache->set('plantask_first_word_'.$oneuser, $times + 1);
  95. }
  96. }
  97. //计划任务
  98. //清空没用的redis
  99. public function firstword_clear(){
  100. $map = [
  101. 'jointime' => ['between',[time()-172800,time()-86400]],
  102. 'gender' => 1,
  103. 'gh_id' => 0,
  104. ];
  105. $map = [];
  106. $userlist = Db::name('user')->where($map)->order('id asc')->column('id');
  107. if(empty($userlist)){
  108. echo 'empty';
  109. exit;
  110. }
  111. //清除
  112. $cache = Cache::connect(['type'=>'Redis']);
  113. foreach($userlist as $key => $val){
  114. $cache->rm('plantask_first_word_'.$val);
  115. }
  116. }
  117. //测试用
  118. //清空redis
  119. public function clear_redis(){
  120. $val = input('uid');
  121. $cache = Cache::connect(['type'=>'Redis']);
  122. $cache->rm('plantask_first_word_'.$val);
  123. }
  124. }