Plantask.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 firstword_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('test'.$ghuser,'test'.$oneuser,$oneword);
  39. $cache->set('plantask_first_word_'.$oneuser, $times + 1);
  40. }
  41. }
  42. //清空没用的redis
  43. public function firstword_clear(){
  44. $map = [
  45. 'jointime' => ['between',[time()-172800,time()-86400]],
  46. 'gender' => 1,
  47. 'gh_id' => 0,
  48. ];
  49. $map = [];
  50. $userlist = Db::name('user')->where($map)->order('id asc')->column('id');
  51. if(empty($userlist)){
  52. echo 'empty';
  53. exit;
  54. }
  55. //清除
  56. $cache = Cache::connect(['type'=>'Redis']);
  57. foreach($userlist as $key => $val){
  58. $cache->rm('plantask_first_word_'.$val);
  59. }
  60. }
  61. /**
  62. * 发送消息给某人-接口调用
  63. */
  64. public function sendToUser() {
  65. $from_user = 'test8';// 发送者
  66. $to_user = 'test13';// 接收者
  67. $message = 'hello许犇';// 接收者
  68. if(!$from_user || !$to_user || !$message) $this->error("参数缺失!");
  69. $this->sendMessageToUser($from_user,$to_user,$message);
  70. }
  71. /**
  72. * 发送消息给某人
  73. */
  74. //https://console.tim.qq.com/v4/openim/sendmsg?sdkappid=88888888&identifier=admin&usersig=xxx&random=99999999&contenttype=json
  75. public function sendMessageToUser($from_user,$to_user,$message) {
  76. // $from_user = 54;
  77. // $to_user = 6;
  78. // $message = "sdsd";
  79. $random = rand(10000000,99999999);
  80. $usersig = $this->usersig("administrator");
  81. // 获取配置信息
  82. $config = config("tencent_im");
  83. $url = "https://console.tim.qq.com/v4/openim/sendmsg";
  84. $url .= "?sdkappid=".$config["sdkappid"];
  85. $url .= "&identifier=administrator";
  86. $url .= "&usersig=".$usersig;
  87. $url .= "&random=".$random;
  88. $url .= "&contenttype=json";
  89. $tencentObj = new tencentim($url);
  90. $data = [];
  91. $data["SyncOtherMachine"] = 1;
  92. $data["From_Account"] = (string)$from_user;
  93. $data["To_Account"] = (string)$to_user;
  94. $data["MsgRandom"] = rand(1000000,9999999);
  95. $data["MsgTimeStamp"] = time();
  96. $data["MsgBody"][] = [
  97. "MsgType" => "TIMTextElem",
  98. "MsgContent" => [
  99. "Text"=> $message
  100. ],
  101. ];
  102. $tencentObj->toSend($data);
  103. }
  104. /**
  105. * 获取usersig签名-具体操作
  106. */
  107. private function usersig($user_id) {
  108. // 获取配置信息
  109. $config = config("tencent_im");
  110. $usersigObj = new getusersig($config["sdkappid"],$config["key"]);
  111. $usersig = $usersigObj->genUserSig($user_id);
  112. return $usersig;
  113. }
  114. }