Plantask.php 3.8 KB

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