Plantask.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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)->orderRaw('rand()')->value('id');
  28. //dump($oneuser);
  29. //找出公会的人
  30. $map = [
  31. 'gh_id' => ['gt',0],
  32. ];
  33. $ghuser = Db::name('user')->where($map)->orderRaw('rand()')->limit(5)->column('id');
  34. //dump($ghuser);
  35. //随机取出一句话
  36. $oneword = Db::name('plantask_accost')->orderRaw('rand()')->limit(5)->column('title');
  37. //dump($oneword);
  38. //发送出去
  39. $cache = Cache::connect(['type'=>'Redis']);
  40. $times = $cache->get('plantask_first_word_'.$oneuser);
  41. //dump($times);
  42. if($times === false){
  43. $times = 0;
  44. }
  45. if($times < 5){
  46. $tenim = new \app\common\library\Tenim;
  47. for($i = 0;$i < 5;$i++){
  48. $ghuser_one = isset($ghuser[$i]) ? $ghuser[$i] : $ghuser[array_rand($ghuser)];
  49. $oneword_one = isset($oneword[$i]) ? $oneword[$i] : $oneword[array_rand($oneword)];
  50. $tenim->sendMessageToUser($ghuser_one,$oneuser,$oneword_one);
  51. }
  52. $cache->set('plantask_first_word_'.$oneuser, 5);
  53. }
  54. }
  55. //计划任务,二期之后,废弃
  56. //代替公会的人发出第一句话
  57. public function firstword_send()
  58. {
  59. //找出24小时内注册的男人
  60. $map = [
  61. 'jointime' => ['gt',time()-86400],
  62. 'gender' => 1,
  63. 'gh_id' => 0,
  64. ];
  65. $oneuser = Db::name('user')->where($map)->orderRaw('rand()')->value('id');
  66. //dump($oneuser);
  67. //找出公会的人
  68. $map = [
  69. 'gh_id' => ['gt',0],
  70. ];
  71. $ghuser = Db::name('user')->where($map)->orderRaw('rand()')->value('id');
  72. //dump($ghuser);
  73. //随机取出一句话
  74. $oneword = Db::name('plantask_accost')->orderRaw('rand()')->value('title');
  75. //dump($oneword);
  76. //发送出去
  77. $cache = Cache::connect(['type'=>'Redis']);
  78. $times = $cache->get('plantask_first_word_'.$oneuser);
  79. //dump($times);
  80. if($times === false){
  81. $times = 0;
  82. }
  83. if($times < 5){
  84. $tenim = new \app\common\library\Tenim;
  85. $tenim->sendMessageToUser($ghuser,$oneuser,$oneword);
  86. $cache->set('plantask_first_word_'.$oneuser, $times + 1);
  87. }
  88. }
  89. //计划任务
  90. //清空没用的redis
  91. public function firstword_clear(){
  92. $map = [
  93. 'jointime' => ['between',[time()-172800,time()-86400]],
  94. 'gender' => 1,
  95. 'gh_id' => 0,
  96. ];
  97. $map = [];
  98. $userlist = Db::name('user')->where($map)->order('id asc')->column('id');
  99. if(empty($userlist)){
  100. echo 'empty';
  101. exit;
  102. }
  103. //清除
  104. $cache = Cache::connect(['type'=>'Redis']);
  105. foreach($userlist as $key => $val){
  106. $cache->rm('plantask_first_word_'.$val);
  107. }
  108. }
  109. //测试用
  110. //清空redis
  111. public function clear_redis(){
  112. $val = input('uid');
  113. $cache = Cache::connect(['type'=>'Redis']);
  114. $cache->rm('plantask_first_word_'.$val);
  115. }
  116. }