Просмотр исходного кода

公会用户发送一句话给新注册用户

lizhen_gitee 3 лет назад
Родитель
Сommit
8d15aa950b

+ 8 - 0
application/config.php

@@ -298,4 +298,12 @@ return [
         //API接口地址
         'api_url'               => 'https://api.fastadmin.net',
     ],
+    // 腾讯im配置
+    'tencent_im' => [
+        'sdkappid' => '1400589001',
+        'identifier' => 'administrator',
+        'key' => '0e8cd35d0938514fb03d99f9ab14498b2615d6713f43436dfd31c21adc49b96a',
+        'SecretId' => 'AKIDd070nmKyAFSP6WHQTEJ1UfeGgquspZ1C',
+        'SecretKey' => '9qLbjaDcvn0yg6HE0JRhjU6TcV4V5QUI',
+    ],
 ];

+ 18 - 0
application/index/controller/Index.php

@@ -11,6 +11,24 @@ class Index extends Frontend
     protected $noNeedRight = '*';
     protected $layout = '';
 
+
+    public function test1(){
+        // 文本加一个回车
+        $buffer1 = 'abcdefghijklmn';
+// 在php中双引号中的\n代表一个换行符,例如"\n"
+        $buffer2 = '{"type":"say", "content":"hello", "content":"hello", "content":"hello", "content":"hello", "content":"hello", "content":"hello", "content":"hello", "content":"hello", "content":"hello", "content":"hello", "content":"hello", "content":"hello", "content":"hello", "content":"hello", "content":"hello", "content":"hello", "content":"hello", "content":"hello", "content":"hello", "content":"hello", "content":"hello", "content":"hello", "content":"hello", "content":"hello", "content":"hello", "content":"hello", "content":"hello", "content":"hello", "content":"hello", "content":"hello", "content":"hello", "content":"hello", "content":"hello", "content":"hello", "content":"hello"}'."";
+
+        $buffer2 = 'abcdefghijklmn'."\n";
+
+// 与服务端建立socket连接
+        $client = stream_socket_client('tcp://1.14.197.70:2349');
+// 以text协议发送buffer1数据
+        //fwrite($client, $buffer1);
+// 以text协议发送buffer2数据
+        fwrite($client, $buffer2);
+
+    }
+
     public function index()
     {
         return $this->view->fetch();

+ 121 - 1
application/index/controller/Plantask.php

@@ -3,14 +3,134 @@
 namespace app\index\controller;
 
 use think\Controller;
+use getusersig\getusersig;
+use tencentim\tencentim;
+use think\Db;
+use think\Cache;
 
 class Plantask extends Controller
 {
 
     //代替公会的人发出第一句话
-    public function index()
+    public function firstword_send()
     {
+        //找出24小时内注册的男人
+        $map = [
+            'jointime' => ['gt',time()-86400],
+            'gender' => 1,
+            'gh_id'  => 0,
+        ];
+        $oneuser = Db::name('user')->where($map)->orderRaw('rand()')->value('id');
+        //dump($oneuser);
 
+        //找出公会的人
+        $map = [
+            'gh_id'  => ['gt',0],
+        ];
+        $ghuser = Db::name('user')->where($map)->orderRaw('rand()')->value('id');
+        //dump($ghuser);
+
+        //随机取出一句话
+        $oneword = Db::name('plantask_accost')->orderRaw('rand()')->value('title');
+        //dump($oneword);
+
+        //发送出去
+        $cache = Cache::connect(['type'=>'Redis']);
+        $times = $cache->get('plantask_first_word_'.$oneuser);
+        //dump($times);
+
+        if($times === false){
+            $times = 0;
+        }
+        if($times < 5){
+            $this->sendMessageToUser('test'.$ghuser,'test'.$oneuser,$oneword);
+            $cache->set('plantask_first_word_'.$oneuser, $times + 1);
+        }
+
+    }
+
+    //清空没用的redis
+    public function firstword_clear(){
+        $map = [
+            'jointime' => ['between',[time()-172800,time()-86400]],
+            'gender' => 1,
+            'gh_id'  => 0,
+        ];
+        $map = [];
+
+        $userlist = Db::name('user')->where($map)->order('id asc')->column('id');
+
+        if(empty($userlist)){
+            echo 'empty';
+            exit;
+        }
+
+        //清除
+        $cache = Cache::connect(['type'=>'Redis']);
+        foreach($userlist as $key => $val){
+            $cache->rm('plantask_first_word_'.$val);
+        }
+    }
+
+
+
+    /**
+     * 发送消息给某人-接口调用
+     */
+    public function sendToUser() {
+        $from_user = 'test8';// 发送者
+        $to_user = 'test13';// 接收者
+        $message = 'hello许犇';// 接收者
+        if(!$from_user || !$to_user || !$message) $this->error("参数缺失!");
+        $this->sendMessageToUser($from_user,$to_user,$message);
+    }
+
+    /**
+     * 发送消息给某人
+     */
+//https://console.tim.qq.com/v4/openim/sendmsg?sdkappid=88888888&identifier=admin&usersig=xxx&random=99999999&contenttype=json
+    public function sendMessageToUser($from_user,$to_user,$message) {
+//        $from_user = 54;
+//        $to_user = 6;
+//        $message = "sdsd";
+        $random = rand(10000000,99999999);
+        $usersig = $this->usersig("administrator");
+        // 获取配置信息
+        $config = config("tencent_im");
+        $url = "https://console.tim.qq.com/v4/openim/sendmsg";
+        $url .= "?sdkappid=".$config["sdkappid"];
+        $url .= "&identifier=administrator";
+        $url .= "&usersig=".$usersig;
+        $url .= "&random=".$random;
+        $url .= "&contenttype=json";
+        $tencentObj = new tencentim($url);
+
+        $data = [];
+        $data["SyncOtherMachine"] = 1;
+        $data["From_Account"] = (string)$from_user;
+        $data["To_Account"] = (string)$to_user;
+        $data["MsgRandom"] = rand(1000000,9999999);
+        $data["MsgTimeStamp"] = time();
+
+        $data["MsgBody"][] = [
+            "MsgType" => "TIMTextElem",
+            "MsgContent" => [
+                "Text"=> $message
+            ],
+        ];
+        $tencentObj->toSend($data);
+
+    }
+
+    /**
+     * 获取usersig签名-具体操作
+     */
+    private function usersig($user_id) {
+        // 获取配置信息
+        $config = config("tencent_im");
+        $usersigObj = new getusersig($config["sdkappid"],$config["key"]);
+        $usersig = $usersigObj->genUserSig($user_id);
+        return $usersig;
     }
 
 }

+ 26 - 1
extend/tencentim/tencentim.php

@@ -38,7 +38,7 @@ class Tencentim
      * @param $jsonStr
      * @return array
      */
-    function http_post_json($url, $jsonStr)
+    function http_post_json_old($url, $jsonStr)
     {
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_POST, 1);
@@ -56,6 +56,31 @@ class Tencentim
         return array($httpCode, $response);
     }
 
+    function http_post_json($url, $data, $header = '', $timeOut = 0)
+    {
+        //初始化curl
+        $ch = curl_init();
+        //参数设置
+        curl_setopt($ch, CURLOPT_URL, $url);
+        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
+        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
+        curl_setopt($ch, CURLOPT_TIMEOUT, $timeOut);
+        curl_setopt($ch, CURLOPT_HEADER, 0);
+        curl_setopt($ch, CURLOPT_POST, 1);
+        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+        if($header != '') {
+            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
+        }
+        $result = curl_exec($ch);
+        //连接失败
+        if($result == FALSE) {
+            //\think\Log::record('[ CURL ] ERROR ' . curl_error($ch)."\n".var_export(debug_backtrace(), true)."\n", 'error');
+        }
+        curl_close($ch);
+        return $result;
+    }
+
     /**
      * $params 请求参数
      */