enwechat.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * enWeChat.php
  4. * Created by: Owen
  5. * Created on: 2019/11/26 20:24
  6. *
  7. */
  8. // 开发文档:https://work.weixin.qq.com/api/doc/90000/90135/90664
  9. class ModelEnWechat{
  10. public $access_token = '';
  11. public $corpid = "wwab4cd61391c809f9";
  12. public $corpsecret = "dFohBcprezGR1qcWURjOgTIYuAm2jmLtgnYkhckgA0I";
  13. public $agentid = 1000008;
  14. public $uidList = ['luokunpeng'];
  15. public $sendUrl = "https://qyapi.weixin.qq.com/cgi-bin/message/send";
  16. public $accessTokenUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
  17. public $departmentUrl = "https://qyapi.weixin.qq.com/cgi-bin/department/list";
  18. public $userUrl = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist";
  19. public $tagUrl = "https://qyapi.weixin.qq.com/cgi-bin/tag/list";
  20. public $tagUserUrl = "https://qyapi.weixin.qq.com/cgi-bin/tag/get";
  21. public function curlJson($url, $dataJson){
  22. $ch = curl_init($url); //请求的URL地址
  23. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  24. curl_setopt($ch, CURLOPT_POSTFIELDS, $dataJson);//$data JSON类型字符串
  25. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  26. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($dataJson)));
  27. $data = curl_exec($ch);
  28. return $data;
  29. }
  30. /**
  31. * 企业微信获取 accessToken
  32. * Created by: Owen
  33. * Created on: 2019/11/25 20:23
  34. */
  35. public function getAccessToken(){
  36. $key = okeys::enWeChatAccessToken();
  37. $access_token = oo::commonOprRedis('common')->get($key);
  38. if(!empty($access_token)){
  39. return $access_token;
  40. }
  41. $url = "{$this->accessTokenUrl}?corpid={$this->corpid}&corpsecret={$this->corpsecret}";
  42. $data = file_get_contents($url);
  43. $data = json_decode($data,true);
  44. $access_token = $data['access_token'];
  45. oo::commonOprRedis('common')->setex($key,$access_token,7000);
  46. return $access_token;
  47. }
  48. /**
  49. * 获取标签列表
  50. * @return mixed
  51. * Created by: Owen
  52. * Created on: 2021/1/29 16:28
  53. */
  54. public function getTagList(){
  55. $accessToken = self::getAccessToken();
  56. $url = "{$this->tagUrl}?access_token={$accessToken}";
  57. $ret = file_get_contents($url);
  58. $ret = json_decode($ret,true);
  59. if($ret['errcode'] == 0){
  60. return $ret['taglist'];
  61. }
  62. }
  63. /**
  64. * 根据标签获取用户
  65. * @param $tagId
  66. * @return mixed
  67. * Created by: Owen
  68. * Created on: 2021/1/29 16:34
  69. */
  70. public function getUserListByTagId($tagId){
  71. $accessToken = self::getAccessToken();
  72. $userList = [];
  73. $url = "{$this->tagUserUrl}?access_token={$accessToken}&tagid={$tagId}";
  74. $ret = file_get_contents($url);
  75. $ret = json_decode($ret,true);
  76. if($ret['errcode'] == 0){
  77. foreach ($ret['userlist'] as $u){
  78. $userList[] = $u;
  79. }
  80. }
  81. return $userList;
  82. }
  83. /**
  84. * 拉取部门信息
  85. * Created by: Owen
  86. * Created on: 2019/11/27 17:57
  87. */
  88. public function getDepartmentList($departmentId = []){
  89. $accessToken = self::getAccessToken();
  90. $url = "{$this->departmentUrl}?access_token={$accessToken}";
  91. $ret = file_get_contents($url);
  92. $ret = json_decode($ret,true);
  93. if($ret['errcode'] == 0){
  94. if(empty($departmentId)){
  95. return $ret['department'];
  96. }else{
  97. $out = [];
  98. foreach ($ret['department'] as $row){
  99. if(in_array($row['id'],$departmentId)){
  100. $out[] = $row;
  101. }
  102. }
  103. return $out;
  104. }
  105. }
  106. }
  107. /**
  108. * 获取部门用户
  109. * @param array $department
  110. * @return array
  111. * Created by: Owen
  112. * Created on: 2021/1/29 16:26
  113. */
  114. public function getUserListByDepartment($department = []){
  115. $accessToken = self::getAccessToken();
  116. $userList = [];
  117. $department = self::getDepartmentList($department);
  118. foreach ($department as $row){
  119. $url = "{$this->userUrl}?access_token={$accessToken}&department_id={$row['id']}";
  120. $ret = file_get_contents($url);
  121. $ret = json_decode($ret,true);
  122. if($ret['errcode'] == 0){
  123. foreach ($ret['userlist'] as $u){
  124. $userList[] = $u;
  125. }
  126. }
  127. }
  128. return $userList;
  129. }
  130. /**
  131. * 如不限制用户列表或者部门列表就全公司推送
  132. * @param $message
  133. * @param array $uidList
  134. * @param array $department
  135. * @return bool
  136. * Created by: Owen
  137. * Created on: 2019/11/27 18:30
  138. */
  139. public function send_wx($message,$uidList = [],$department = [],$tagId = 0){
  140. $accessToken = self::getAccessToken();
  141. if(empty($uidList)){
  142. if(!empty($department)){
  143. $uidList = array_column(self::getUserListByDepartment($department),'userid');
  144. }else if($tagId){
  145. $uidList = array_column(self::getUserListByTagId($tagId),'userid');
  146. }
  147. $uidList = array_unique($uidList);
  148. }
  149. if(!empty($accessToken) && !empty($uidList)) {
  150. $message = [
  151. 'touser' =>implode('|',$uidList),
  152. 'msgtype'=>"text",
  153. 'agentid'=>$this->agentid,
  154. 'text' => [
  155. 'content' =>$message
  156. ],
  157. 'safe' => 0
  158. ];
  159. $sendUrl = "{$this->sendUrl}?access_token={$accessToken}";
  160. $response = $this->curlJson($sendUrl, json_encode($message));
  161. $response = json_decode($response,true);
  162. if($response['errcode'] == 0){
  163. return true;
  164. }else{
  165. return false;
  166. }
  167. }
  168. return false;
  169. }
  170. //飞书
  171. public function send($message,$uidList = [],$department = [],$tagId = 0){
  172. // $url = "https://open.feishu.cn/open-apis/bot/v2/hook/f7d734e8-af47-4367-86d1-307124169fc0";
  173. $url = "https://open.feishu.cn/open-apis/bot/v2/hook/ede82ff8-1f18-40d8-87c9-4fd4169089c6";
  174. $data = [
  175. "msg_type"=>"text",
  176. "content"=>["text"=>"[CC-PHP服务预警]:".$message.'<at user_id="all">所有人</at>']
  177. ];
  178. $ret = $this->curlJson($url,json_encode($data));
  179. var_dump($ret);
  180. }
  181. }