Plantask.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. //代替公会的人10秒内发出五句话
  11. public function firstuser_send()
  12. {
  13. //找出24小时内注册的男人
  14. $map = [
  15. 'jointime' => ['gt',time()-86400],
  16. 'gender' => 1,
  17. 'gh_id' => 0,
  18. ];
  19. $oneuser = Db::name('user')->where($map)->orderRaw('rand()')->value('id');
  20. //dump($oneuser);
  21. //找出公会的人
  22. $map = [
  23. 'gh_id' => ['gt',0],
  24. ];
  25. $ghuser = Db::name('user')->where($map)->orderRaw('rand()')->limit(5)->column('id');
  26. //dump($ghuser);
  27. //随机取出一句话
  28. $oneword = Db::name('plantask_accost')->orderRaw('rand()')->limit(5)->column('title');
  29. //dump($oneword);
  30. //发送出去
  31. $cache = Cache::connect(['type'=>'Redis']);
  32. $times = $cache->get('plantask_first_word_'.$oneuser);
  33. //dump($times);
  34. if($times === false){
  35. $times = 0;
  36. }
  37. if($times < 5){
  38. for($i = 0;$i < 5;$i++){
  39. $ghuser_one = isset($ghuser[$i]) ? $ghuser[$i] : $ghuser[array_rand($ghuser)];
  40. $oneword_one = isset($oneword[$i]) ? $oneword[$i] : $oneword[array_rand($oneword)];
  41. $this->sendMessageToUser($ghuser_one,$oneuser,$oneword_one);
  42. }
  43. $cache->set('plantask_first_word_'.$oneuser, 5);
  44. }
  45. }
  46. //代替公会的人发出第一句话
  47. public function firstword_send()
  48. {
  49. //找出24小时内注册的男人
  50. $map = [
  51. 'jointime' => ['gt',time()-86400],
  52. 'gender' => 1,
  53. 'gh_id' => 0,
  54. ];
  55. $oneuser = Db::name('user')->where($map)->orderRaw('rand()')->value('id');
  56. //dump($oneuser);
  57. //找出公会的人
  58. $map = [
  59. 'gh_id' => ['gt',0],
  60. ];
  61. $ghuser = Db::name('user')->where($map)->orderRaw('rand()')->value('id');
  62. //dump($ghuser);
  63. //随机取出一句话
  64. $oneword = Db::name('plantask_accost')->orderRaw('rand()')->value('title');
  65. //dump($oneword);
  66. //发送出去
  67. $cache = Cache::connect(['type'=>'Redis']);
  68. $times = $cache->get('plantask_first_word_'.$oneuser);
  69. //dump($times);
  70. if($times === false){
  71. $times = 0;
  72. }
  73. if($times < 5){
  74. $this->sendMessageToUser($ghuser,$oneuser,$oneword);
  75. $cache->set('plantask_first_word_'.$oneuser, $times + 1);
  76. }
  77. }
  78. //清空没用的redis
  79. public function firstword_clear(){
  80. $map = [
  81. 'jointime' => ['between',[time()-172800,time()-86400]],
  82. 'gender' => 1,
  83. 'gh_id' => 0,
  84. ];
  85. $map = [];
  86. $userlist = Db::name('user')->where($map)->order('id asc')->column('id');
  87. if(empty($userlist)){
  88. echo 'empty';
  89. exit;
  90. }
  91. //清除
  92. $cache = Cache::connect(['type'=>'Redis']);
  93. foreach($userlist as $key => $val){
  94. $cache->rm('plantask_first_word_'.$val);
  95. }
  96. }
  97. //清空redis
  98. public function clear_redis(){
  99. $val = input('uid');
  100. $cache = Cache::connect(['type'=>'Redis']);
  101. $cache->rm('plantask_first_word_'.$val);
  102. }
  103. /**
  104. * 发送消息给某人-接口调用
  105. */
  106. public function sendToUser() {
  107. $from_user = '26';// 发送者
  108. $to_user = '32';// 接收者
  109. $message = 'hello许犇';// 接收者
  110. if(!$from_user || !$to_user || !$message) $this->error("参数缺失!");
  111. $this->sendMessageToUser($from_user,$to_user,$message);
  112. }
  113. /**
  114. * 发送消息给某人
  115. */
  116. //https://console.tim.qq.com/v4/openim/sendmsg?sdkappid=88888888&identifier=admin&usersig=xxx&random=99999999&contenttype=json
  117. public function sendMessageToUser($from_user,$to_user,$message) {
  118. // $from_user = 54;
  119. // $to_user = 6;
  120. // $message = "sdsd";
  121. $random = rand(10000000,99999999);
  122. $usersig = $this->usersig("administrator");
  123. // 获取配置信息
  124. $config = config("tencent_im");
  125. $url = "https://console.tim.qq.com/v4/openim/sendmsg";
  126. $url .= "?sdkappid=".$config["sdkappid"];
  127. $url .= "&identifier=administrator";
  128. $url .= "&usersig=".$usersig;
  129. $url .= "&random=".$random;
  130. $url .= "&contenttype=json";
  131. $tencentObj = new tencentim($url);
  132. $data = [];
  133. $data["SyncOtherMachine"] = 1;
  134. $data["From_Account"] = (string)$from_user;
  135. $data["To_Account"] = (string)$to_user;
  136. $data["MsgRandom"] = rand(1000000,9999999);
  137. $data["MsgTimeStamp"] = time();
  138. $data["MsgBody"][] = [
  139. "MsgType" => "TIMTextElem",
  140. "MsgContent" => [
  141. "Text"=> $message
  142. ],
  143. ];
  144. $tencentObj->toSend($data);
  145. }
  146. /**
  147. * 获取usersig签名-具体操作
  148. */
  149. private function usersig($user_id) {
  150. // 获取配置信息
  151. $config = config("tencent_im");
  152. $usersigObj = new getusersig($config["sdkappid"],$config["key"]);
  153. $usersig = $usersigObj->genUserSig($user_id);
  154. return $usersig;
  155. }
  156. }