Plantask.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace app\index\controller;
  3. use think\Controller;
  4. use getusersig\getusersig;
  5. use tencentim\tencentim;
  6. use think\Db;
  7. use think\Cache;
  8. class Plantask extends Controller
  9. {
  10. //定时跑用户活跃,改成离线
  11. public function user_active(){
  12. $actitime = time() - 600;
  13. //$map = ['requesttime' < $actitime];
  14. $sql = 'update `mt_user` set is_active = 0 where id in (select user_id from mt_user_active where requesttime < '.$actitime.')';
  15. //echo $sql;
  16. db()->query($sql);
  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. 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. $this->sendMessageToUser($ghuser_one,$oneuser,$oneword_one);
  50. }
  51. $cache->set('plantask_first_word_'.$oneuser, 5);
  52. }
  53. }
  54. //二期之后,废弃
  55. //代替公会的人发出第一句话
  56. public function firstword_send()
  57. {
  58. //找出24小时内注册的男人
  59. $map = [
  60. 'jointime' => ['gt',time()-86400],
  61. 'gender' => 1,
  62. 'gh_id' => 0,
  63. ];
  64. $oneuser = Db::name('user')->where($map)->orderRaw('rand()')->value('id');
  65. //dump($oneuser);
  66. //找出公会的人
  67. $map = [
  68. 'gh_id' => ['gt',0],
  69. ];
  70. $ghuser = Db::name('user')->where($map)->orderRaw('rand()')->value('id');
  71. //dump($ghuser);
  72. //随机取出一句话
  73. $oneword = Db::name('plantask_accost')->orderRaw('rand()')->value('title');
  74. //dump($oneword);
  75. //发送出去
  76. $cache = Cache::connect(['type'=>'Redis']);
  77. $times = $cache->get('plantask_first_word_'.$oneuser);
  78. //dump($times);
  79. if($times === false){
  80. $times = 0;
  81. }
  82. if($times < 5){
  83. $this->sendMessageToUser($ghuser,$oneuser,$oneword);
  84. $cache->set('plantask_first_word_'.$oneuser, $times + 1);
  85. }
  86. }
  87. //清空没用的redis
  88. public function firstword_clear(){
  89. $map = [
  90. 'jointime' => ['between',[time()-172800,time()-86400]],
  91. 'gender' => 1,
  92. 'gh_id' => 0,
  93. ];
  94. $map = [];
  95. $userlist = Db::name('user')->where($map)->order('id asc')->column('id');
  96. if(empty($userlist)){
  97. echo 'empty';
  98. exit;
  99. }
  100. //清除
  101. $cache = Cache::connect(['type'=>'Redis']);
  102. foreach($userlist as $key => $val){
  103. $cache->rm('plantask_first_word_'.$val);
  104. }
  105. }
  106. //清空redis
  107. public function clear_redis(){
  108. $val = input('uid');
  109. $cache = Cache::connect(['type'=>'Redis']);
  110. $cache->rm('plantask_first_word_'.$val);
  111. }
  112. /**
  113. * 发送消息给某人-接口调用
  114. */
  115. public function sendToUser() {
  116. $from_user = '26';// 发送者
  117. $to_user = '32';// 接收者
  118. $message = 'hello许犇';// 接收者
  119. if(!$from_user || !$to_user || !$message) $this->error("参数缺失!");
  120. $this->sendMessageToUser($from_user,$to_user,$message);
  121. }
  122. /**
  123. * 发送消息给某人
  124. */
  125. //https://console.tim.qq.com/v4/openim/sendmsg?sdkappid=88888888&identifier=admin&usersig=xxx&random=99999999&contenttype=json
  126. public function sendMessageToUser($from_user,$to_user,$message) {
  127. // $from_user = 54;
  128. // $to_user = 6;
  129. // $message = "sdsd";
  130. $random = rand(10000000,99999999);
  131. $usersig = $this->usersig("administrator");
  132. // 获取配置信息
  133. $config = config("tencent_im");
  134. $url = "https://console.tim.qq.com/v4/openim/sendmsg";
  135. $url .= "?sdkappid=".$config["sdkappid"];
  136. $url .= "&identifier=administrator";
  137. $url .= "&usersig=".$usersig;
  138. $url .= "&random=".$random;
  139. $url .= "&contenttype=json";
  140. $tencentObj = new tencentim($url);
  141. $data = [];
  142. $data["SyncOtherMachine"] = 1;
  143. $data["From_Account"] = (string)$from_user;
  144. $data["To_Account"] = (string)$to_user;
  145. $data["MsgRandom"] = rand(1000000,9999999);
  146. $data["MsgTimeStamp"] = time();
  147. $data["MsgBody"][] = [
  148. "MsgType" => "TIMTextElem",
  149. "MsgContent" => [
  150. "Text"=> $message
  151. ],
  152. ];
  153. $tencentObj->toSend($data);
  154. }
  155. /**
  156. * 获取usersig签名-具体操作
  157. */
  158. private function usersig($user_id) {
  159. // 获取配置信息
  160. $config = config("tencent_im");
  161. $usersigObj = new getusersig($config["sdkappid"],$config["key"]);
  162. $usersig = $usersigObj->genUserSig($user_id);
  163. return $usersig;
  164. }
  165. }