Plantask.php 4.8 KB

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