Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加“关联企业”的相关API接口 #34

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
composer.lock

runtime/*

public/docs

public/uploads

gitc

.idea/
.env

.DS_Store
.project

config/*_dev.php
config/*_test.php

plugins/*.zip

node_modules
package-lock.json

23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
# About
此项目是Fork自https://github.com/sbzhu/weworkapi_php。原项目基本无后续开发,因此自己加了一些新功能进来。
主要增加的有:

1. 互联企业相关(注:小程序暂不支持互联企业用户,因为无法解code):参考 api\examples\testLink.php
* 互联企业消息推送
* 互联企业的通讯录信息获取(成员、部门等)
1. 消息发送:增加了新进企业微信消息发送功能:
* MarkDown消息发送;
* 小程序消息发送;
* 任务卡片消息发送(需要企业微信应用有回调功能支持);

## 其他新增功能:
请进入 https://github.com/logmecn/weworkapi 查看,除以上功能外还有:
1. 使用Redis缓存。在 config 文件中修改配置。
1. 使用composer加载

后续会继续增加丰富其功能。
如果有需要新增功能,请发 issue 或 PR,会第一时间回复。

# 原说明
weworkapi_php 是为了简化开发者对企业微信API接口的使用而设计的,API调用库系列之php版本
包括企业API接口、消息回调处理方法、第三方开放接口等  
本库仅做示范用,并不保证完全无bug;
Expand All @@ -12,7 +32,7 @@ golang : https://github.com/sbzhu/weworkapi_golang ryanjelin@tencent.com(企业
golang : https://github.com/doubliekill/EnterpriseWechatSDK 1006401052yh@gmail.com(个人开发者)

# Requirement
经测试,PHP 5.3.3 ~ 7.2.0 版本均可使用
PHP 5.4 ~ 7.4 版本均可使用,PHP8.0未测试,理论上应该也ok。

# Director

Expand Down Expand Up @@ -75,7 +95,6 @@ $api->dosomething()
当然,如果要更严格的做的话,建议自行修改,```全局缓存token,比如存redis、存文件等```,失效周期设置为2小时。

# Contact us
abelzhu@tencent.com
xiqunpan@tencent.com

#
48 changes: 48 additions & 0 deletions api/datastructure/Link_Message.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

include_once(__DIR__."/../../utils/error.inc.php");
include_once(__DIR__."/../../utils/Utils.class.php");

class Link_Message
{
public $sendToAll = false; // bool, 是否全员发送, 即文档所谓 @all
public $touser = array(); // string array
public $toparty = array(); // uint array
public $totag = array(); // uint array
public $agentid = null; // uint
public $safe = null; // uint, 表示是否是保密消息,0表示否,1表示是,默认0
public $messageContent = null; // xxxMessageContent

public function CheckMessageSendArgs()
{
if (count($this->touser) > 1000) throw new QyApiError("touser should be no more than 1000");
if (count($this->toparty) > 100) throw new QyApiError("toparty should be no more than 100");
if (count($this->totag) > 100) throw new QyApiError("toparty should be no more than 100");

if (is_null($this->messageContent)) throw new QyApiError("messageContent is empty");
$this->messageContent->CheckMessageSendArgs();
}

public function Message2Array()
{
$args = array();
Utils::setIfNotNull($this->touser, "touser", $args);
Utils::setIfNotNull($this->toparty, "toparty", $args);
Utils::setIfNotNull($this->totag, "totag", $args);

//Utils::setIfNotNull($this->toall, "toall", $args);
Utils::setIfNotNull($this->agentid, "agentid", $args);
Utils::setIfNotNull($this->safe, "safe", $args);

$this->messageContent->MessageContent2Array($args);

return $args;
}

private function setIfNotNull2array($var, $name, &$args)
{
if (!is_null($var)) {
$args[$name] = $var;
}else $args[$name] = [];
}
}
7 changes: 7 additions & 0 deletions api/datastructure/Message.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class Message
public $agentid = null; // uint
public $safe = null; // uint, 表示是否是保密消息,0表示否,1表示是,默认0
public $messageContent = null; // xxxMessageContent
public $enable_id_trans = 0; // 表示是否开启id转译,0表示否,1表示是,默认0
public $enable_duplicate_check = 0; // 表示是否开启重复消息检查,0表示否,1表示是,默认0
public $duplicate_check_interval = 1800; //表示是否重复消息检查的时间间隔,默认1800s,最大不超过4小时

public function CheckMessageSendArgs()
{
Expand Down Expand Up @@ -55,6 +58,10 @@ public function Message2Array()
Utils::setIfNotNull($this->agentid, "agentid", $args);
Utils::setIfNotNull($this->safe, "safe", $args);

Utils::setIfNotNull($this->enable_id_trans, "enable_id_trans", $args);
Utils::setIfNotNull($this->enable_duplicate_check, "enable_duplicate_check", $args);
Utils::setIfNotNull($this->duplicate_check_interval, "duplicate_check_interval", $args);

$this->messageContent->MessageContent2Array($args);

return $args;
Expand Down
168 changes: 168 additions & 0 deletions api/datastructure/MessageExt.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<?php
/*
* 新增几个新加企业微信发送消息的功能,原基础库中没有。
* "msgtype" : "interactive_taskcard", 任务卡片消息
* "msgtype" : "miniprogram_notice", 小程序通知消息
* "msgtype": "markdown",markdown消息
* author ywq
* date: 2021/07/15
*/
include_once(__DIR__."/../../utils/error.inc.php");
include_once(__DIR__."/../../utils/Utils.class.php");

/*
* MD文档消息类型
*/
class MDMessageContent
{
public $msgtype = "markdown";
private $content = null; // string

public function __construct($content=null)
{
$this->content = $content;
}

public function CheckMessageSendArgs()
{
if (mb_detect_encoding($this->content, 'UTF-8') != 'UTF-8') {
throw new QyApiError("invalid MarkDown is not UTF-8");
}
$len = strlen($this->content);
if ($len == 0 || $len > 2048) {
throw new QyApiError("invalid MarkDown content length " . $len);
}
}

public function MessageContent2Array(&$arr)
{
Utils::setIfNotNull($this->msgtype, "msgtype", $arr);

$contentArr = array("content" => $this->content);
Utils::setIfNotNull($contentArr, $this->msgtype, $arr);
}
}

/*
* 小程序消息类型
*/
class MinaMessageContent
{
public $msgtype = "miniprogram_notice";

public function __construct($appid=null, $page=null, $title=null,
$description=null, bool $emphasis_first_item=true, array $content_item=[])
{
$this->appid = $appid;
$this->page = $page;
$this->title = $title;
$this->description = $description;
$this->emphasis_first_item = $emphasis_first_item; //是否放大第一个content_item(default true)
$this->content_item = $content_item;
}

public function CheckMessageSendArgs()
{
$len_title = strlen($this->title);
$len_desc = strlen($this->description);
if (($len_title<4 || $len_title>24) || ($len_desc<4 ||$len_desc>24)) {
throw new QyApiError("Mina Title or Desc len is not allowed!");
}
if(count($this->content_item)>10){
throw new QyApiError("Mina content_item is big than 10");
}
foreach ($this->content_item as $k => $v){
if (strlen($k)>10 || strlen($v)>30){
throw new QyApiError("Mina key or value more than 10 or 30". $k);
}
}
}

public function MessageContent2Array(&$arr)
{
Utils::setIfNotNull($this->msgtype, "msgtype", $arr);
$contentArr = array();
{
Utils::setIfNotNull($this->appid, "appid", $contentArr);
Utils::setIfNotNull($this->page,"page",$contentArr);
Utils::setIfNotNull($this->title, "title", $contentArr);
Utils::setIfNotNull($this->description, "description", $contentArr);
Utils::setIfNotNull($this->emphasis_first_item, "emphasis_first_item", $contentArr);
Utils::setIfNotNull($this->content_item, "content_item", $contentArr);
}
Utils::setIfNotNull($contentArr, $this->msgtype, $arr);
}
}

/*
* 任务卡片消息类型,其中的消息类型示例:
* "interactive_taskcard" : {
"title" : "赵明登的礼物申请",
"description" : "礼品:A31茶具套装\n用途:赠与小黑科技张总经理",
"url" : "URL",
"task_id" : "taskid123",
"btn":[
{
"key": "key111",
"name": "批准",
"color":"red",
"is_bold": true
},
{
"key": "key222",
"name": "驳回"
}
]
},
其中:
btn:key 是 按钮key值,用户点击后,会产生任务卡片回调事件,回调事件会带上该key值,只能由数字、字母和“_-@”组成,最长支持128字节
btn:name 是 按钮名称,最长支持18个字节,超过则截断
btn:color 否 按钮字体颜色,可选“red”或者“blue”,默认为“blue”
btn:is_bold 否 按钮字体是否加粗,默认false
enable_id_trans 否 表示是否开启id转译,0表示否,1表示是,默认0
*/
class TaskCardMessageContent
{
public $msgtype = "interactive_taskcard";

public function __construct($title=null, $description=null,$url=null,
$task_id=null, array $btn=[])
{
$this->title = $title; // 标题,不超过128个字节,超过会自动截断(支持id转译)
$this->description = $description; //描述,不超过512个字节,超过会自动截断(支持id转译)
$this->url = $url; //点击后跳转的链接。最长2048字节,请确保包含了协议头(http/https)
$this->task_id = $task_id; //任务id,同一个应用发送的任务卡片消息的任务id不能重复,只能由数字、字母和“_-@”组成,最长支持128字节
$this->btn = $btn; //按钮列表,按钮个数为1~2个。
}

public function CheckMessageSendArgs()
{
$len_title = strlen($this->title);
$len_desc = strlen($this->description);
if ( $len_title>128 || $len_desc>512 || strlen($this->url)>2048) {
throw new QyApiError("TaskCard Title or Desc or url len is not allowed!");
}
if(count($this->btn)>2){
throw new QyApiError("TaskCard content_item is big than 2");
}
foreach ($this->btn as $k => $v){
if (strlen($k)>128 || strlen($v)>30){
throw new QyApiError("TaskCard key or value more than 10 or 30". $k);
}
}
}

public function MessageContent2Array(&$arr)
{
Utils::setIfNotNull($this->msgtype, "msgtype", $arr);
$contentArr = array();
{
Utils::setIfNotNull($this->title, "title", $contentArr);
Utils::setIfNotNull($this->description, "description", $contentArr);
Utils::setIfNotNull($this->url, "url", $contentArr);
Utils::setIfNotNull($this->task_id,"task_id",$contentArr);
Utils::setIfNotNull($this->btn, "btn", $contentArr);
}
Utils::setIfNotNull($contentArr, $this->msgtype, $arr);
}
}
5 changes: 5 additions & 0 deletions api/examples/config_link.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
return [
'corpid' => 'wx17a22c6916657c82e',
'sec' => '1Gjahd***3UY4Y4', // AGENT_SECRET
];
75 changes: 75 additions & 0 deletions api/examples/testLink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/**
* 注意使用时, linked_id 是关联企业的id,不是 关联企业的corpid.
* 在获取部门相关信息时,用 linked_id 。如 linked_id/departmentid
* 在用户信息的userid 时,用 关联企业的corpid。如 corpid/userid
* 以上两个id容易混乱,请区分。查看方式:
* 在企业微信管理后台,通讯录、互联企业,点其中一个互联企业,
*/

include_once (__DIR__."/../src/LinkAPI.class.php");
$config = require (__DIR__."/config_link.php");
//注意这里的配置文件,请改成自己的配置信息
//use LinkAPI;
//$config = require('./config.php');
$api = new LinkAPI($config['corpid'], $config['sec']);

try {

echo "获取应用的可见范围:";
$LinkGetPermList = $api->LinkGetPermList();
var_dump($LinkGetPermList);

echo '\n获取用户成员详细信息:';
$use_get = $api->Link_UserGet('ww5614ccf1c02e6d99/7086');
var_dump($use_get);

echo '\n获取互联企业部门成员:(需要在“可见范围加该部门可见,否则会提示
//Warning: wrong json format. user not in app perm!
';
$simplelist = $api->Link_UserSimpleList('wh205582b532e12e3f/307');
var_dump($simplelist);

echo '\n获取互联企业部门成员详情:';
$userList = $api->Link_UserList('wh205582b532e12e3f/307');
var_dump($userList);

echo '\n获取互联企业部门列表:';
$dep_list = $api->Link_DepartmentList('wh205582b532e12e3f/307');
var_dump($dep_list);

echo '\n发送图文消息示例:';
$message = new Link_Message();
{
//$message->sendToAll = false;
$message->touser = array('ww5614ccf1c02e6d99/7086', );
// $message->toparty = array(1, 2, 1111, 3333);
// $message->totag= array(3, 4, 22233332, 33334444);
$message->agentid = $wxconfig['agentid'];
$message->safe = 0;

$message->messageContent = new NewsMessageContent(
array(
new NewsArticle(
$title = "testing Got you !",
$description = "from auto.pw error! qywxMsg Warnning!",
$url = "https://work.weixin.qq.com/",
$picurl = "https://p.qpic.cn/pic_wework/167386225/f9ffc8f0a34f301580daaf05f225723ff571679f07e69f91/0",
$btntxt = "btntxt"
),
)
);
}

$invalidUserIdList = null;
$invalidPartyIdList = null;
$invalidTagIdList = null;

$api->Link_MessageSend($message, $invalidUserIdList, $invalidPartyIdList, $invalidTagIdList);



} catch (Exception $e) {
echo $e->getMessage() . "\n";
}
Loading