123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?php
- 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 firstword_send()
- {
-
- $map = [
- 'jointime' => ['gt',time()-86400],
- 'gender' => 1,
- 'gh_id' => 0,
- ];
- $oneuser = Db::name('user')->where($map)->orderRaw('rand()')->value('id');
-
-
- $map = [
- 'gh_id' => ['gt',0],
- ];
- $ghuser = Db::name('user')->where($map)->orderRaw('rand()')->value('id');
-
-
- $oneword = Db::name('plantask_accost')->orderRaw('rand()')->value('title');
-
-
- $cache = Cache::connect(['type'=>'Redis']);
- $times = $cache->get('plantask_first_word_'.$oneuser);
-
- if($times === false){
- $times = 0;
- }
- if($times < 5){
- $this->sendMessageToUser('test'.$ghuser,'test'.$oneuser,$oneword);
- $cache->set('plantask_first_word_'.$oneuser, $times + 1);
- }
- }
-
- 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);
- }
-
- 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);
- }
-
- private function usersig($user_id) {
-
- $config = config("tencent_im");
- $usersigObj = new getusersig($config["sdkappid"],$config["key"]);
- $usersig = $usersigObj->genUserSig($user_id);
- return $usersig;
- }
- }
|