Browse Source

非首次注册男性用户每次打开app,系统自动推送女性(公会)打招呼消息3人次。触发点是广告图

lizhen_gitee 2 years ago
parent
commit
cb36211431
2 changed files with 98 additions and 0 deletions
  1. 30 0
      application/api/controller/Baseconfig.php
  2. 68 0
      application/common/library/Tenim.php

+ 30 - 0
application/api/controller/Baseconfig.php

@@ -31,12 +31,42 @@ class Baseconfig extends Api
 
     //启动广告图
     public function start_advert(){
+
+        if($this->auth->isLogin()){
+            if($this->auth->gender == 1 && $this->auth->gh_id == 0){
+                $this->firstopen_send($this->auth->id);
+            }
+        }
+
         $info = Db::name('start_advert')->where('is_show',1)->order('id desc')->find();
 
         $info = info_domain_image($info,['image']);
         $this->success_find('success',$info);
     }
 
+    //非首次注册男性用户每次打开app,系统自动推送女性(公会)打招呼消息3人次。
+    private function firstopen_send($oneuser){
+        //找出公会的人
+        $map = [
+            'gh_id'  => ['gt',0],
+        ];
+        $ghuser = Db::name('user')->where($map)->orderRaw('rand()')->limit(3)->column('id');
+        //dump($ghuser);
+
+        //随机取出一句话
+        $oneword = Db::name('plantask_accost')->orderRaw('rand()')->limit(3)->column('title');
+        //dump($oneword);
+
+        $tenim = new \app\common\library\Tenim;
+
+        for($i = 0;$i < 3;$i++){
+            $ghuser_one  = isset($ghuser[$i])  ? $ghuser[$i]  : $ghuser[array_rand($ghuser)];
+            $oneword_one = isset($oneword[$i]) ? $oneword[$i] : $oneword[array_rand($oneword)];
+            $tenim->sendMessageToUser($ghuser_one,$oneuser,$oneword_one);
+        }
+    }
+
+
     //个人资料的一下枚举
     public function userinfo_enum(){
         $enum_hobby = Db::name('enum_hobby')->field('id,name')->order('weight desc,id desc')->select();

+ 68 - 0
application/common/library/Tenim.php

@@ -0,0 +1,68 @@
+<?php
+
+namespace app\common\library;
+
+use getusersig\getusersig;
+use tencentim\tencentim;
+
+class Tenim
+{
+
+    /**
+     * 发送消息给某人-接口调用
+     */
+    public function sendToUser() {
+        $from_user = '2';// 发送者
+        $to_user = '294';// 接收者
+        $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) {
+        $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签名-具体操作
+     */
+    public function usersig($user_id) {
+        // 获取配置信息
+        $config = config("tencent_im");
+        $usersigObj = new getusersig($config["sdkappid"],$config["key"]);
+        $usersig = $usersigObj->genUserSig($user_id);
+        return $usersig;
+    }
+}