REAPI/app/robot/controller/WxWorkBot.php

128 lines
2.7 KiB
PHP
Raw Normal View History

<?php
namespace app\robot\controller;
use app\gateway\service\CurlService;
use think\admin\Controller;
#微信机器人 #
class WxWorkBot extends Controller
{
protected array $status_map=array(
-1=>'暂无订单',
-2=>'暂无卡密',
1=>'成功'
);
protected string $host = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send';
public function initialize(): void
{
#47609e509f946ecbfdba27f16db341c4
parent::initialize();
#获取wechat信息
$this->key='47609e509f946ecbfdba27f16db341c4';
}
#发送文本
public function send_text($content,$key = false,$receiver = false)
{
$text = [
'content' =>$content,
];
if($receiver){
$text['mentioned_mobile_list'] = $receiver;
}
$request = [
'msgtype'=>'text',
'text'=>$text
];
$url = $this->host.'?key='.$this->key;
if($key){
$url = $this->host.'?key='.$key;
}
return $this->curl('post', $url,$request,true);
}
#发送markdown
public function send_markdown($content,$key = false)
{
$markdown = [
'content' =>$content,
];
$request = [
'msgtype'=>'markdown',
'markdown'=>$markdown
];
$url = $this->host.'?key='.$this->key;
if($key){
$url = $this->host.'?key='.$key;
}
// var_dump($request);die;
return $this->send_post($url,$request,'POST');
}
public function send_post($notify_url, $post_data, $type): mixed
{
$postdate = json_encode($post_data);
// $postdate = http_build_query($post_data);
$options = array(
'http' => array(
'method' => $type,
'header' => 'Content-type:application/json',
'content' => $postdate,
'timeout' => 15 * 60 // 超时时间(单位:s
)
);
$context = stream_context_create($options);
return file_get_contents($notify_url, false, $context);
}
protected function curl($method, $url, $param = array(), $json = false, $header = false):mixed
{
if ($param) {
$log['type'] = 'request';
$log['url'] = $url;
$log['param'] = $param;
$this->log($log);
}
$curl = CurlService::getInstance($url, $param, $method, $json, $header);
$curl->setTimeOut(3600);
return $curl->result();
}
protected static function log($data, $type = 'request'):void
{
\dever\Log::write('WxWork', $type, $data);
// \dever\Log::write('jingdong', $type, $data);
}
}