Plantask.php 4.0 KB

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