Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

120 řádky
3.5 KiB

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Steven
  5. * Date: 2017/7/6
  6. * Time: 17:45
  7. */
  8. namespace common\components;
  9. use backend\common\Utils;
  10. use common\models\AccessToken;
  11. use common\models\OfficeWechatDep;
  12. use common\models\OfficeWechatUser;
  13. class zOfficeWechat
  14. {
  15. const SEND_HOTEL = 1000002;
  16. const SEND_BUS = 1000003;
  17. const SEND_TICKET = 1000004;
  18. /**
  19. * User:Steven
  20. * Desc:获取所有部门
  21. */
  22. public function GetDepartment()
  23. {
  24. $accessToken = new AccessToken('txl');
  25. $token = $accessToken->getAccessToken();
  26. $url = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=$token&id=";
  27. $rs = Utils::http_post($url, []);
  28. $result_array = json_decode($rs['content'], true);
  29. return $result_array;
  30. }
  31. /**
  32. * User:Steven
  33. * Desc:据部门ID查询用户信息
  34. * @param int $depId 查询的部门ID
  35. * @param int $fetchChild 是否遍历子部门
  36. * @param int $simple 是否只查询用户的基本信息
  37. * @return string
  38. */
  39. public function GetUserByDepartment($depId, $fetchChild = 1, $simple = 1)
  40. {
  41. if ($depId > 0) {
  42. $interface = $simple == 1 ? "simplelist" : "list";
  43. $accessToken = new AccessToken('txl');
  44. $token = $accessToken->getAccessToken();
  45. $url = "https://qyapi.weixin.qq.com/cgi-bin/user/$interface?access_token=$token&department_id=$depId&fetch_child=$fetchChild";
  46. $rs = Utils::http_post($url, []);
  47. $result_array = json_decode($rs['content'], true);
  48. return $result_array;
  49. } else {
  50. return false;
  51. }
  52. }
  53. /**
  54. * User:Steven
  55. * Desc:获取所有部门的所有员工
  56. */
  57. public function actionGetAllUser()
  58. {
  59. $department_list = $this->GetDepartment();
  60. if ($department_list['errcode'] == 0) {
  61. foreach ($department_list['department'] as $item) {
  62. $dep = new OfficeWechatDep();
  63. $dep->load($item);
  64. $dep->save();
  65. $rs = $this->GetUserByDepartment($item['id']);
  66. if ($rs['errcode'] == 0) {
  67. foreach ($rs['userlist'] as $val) {
  68. $user = new OfficeWechatUser();
  69. $user->load($val);
  70. $res = $user->save();
  71. }
  72. }
  73. }
  74. }
  75. return true;
  76. }
  77. /**
  78. * User:Steven
  79. * Desc:发送消息到企业微信
  80. * @param $agentid 企业应用ID
  81. * @param $title 标题
  82. * @param $msg 内容
  83. * @param $touser 用户ID
  84. * @return string
  85. */
  86. public static function sendMsg($arr)
  87. {
  88. if (\Yii::$app->params['sendRTX']) {
  89. $accessToken = new AccessToken($arr['agentid']);
  90. $token = $accessToken->getAccessToken();
  91. $url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$token";
  92. $param = array(
  93. "touser" => $arr['touser'],
  94. "toparty" => '',
  95. "totag" => '',
  96. "msgtype" => 'text',
  97. "agentid" => $arr['agentid'],
  98. "text" => array(
  99. "title" => "Title",
  100. "content" => urlencode($arr['title'] . "\n" . mb_substr($arr['msg'], 0, 300))
  101. ),
  102. "safe" => 0
  103. );
  104. $rs = Utils::http_post($url, $param);
  105. return json_encode($rs);
  106. } else {
  107. return true;
  108. }
  109. }
  110. }