|
- <?php
- /**
- * Created by PhpStorm.
- * User: Steven
- * Date: 2017/7/6
- * Time: 17:45
- */
-
- namespace common\components;
-
- use backend\common\Utils;
- use common\models\AccessToken;
- use common\models\OfficeWechatDep;
- use common\models\OfficeWechatUser;
-
-
- class zOfficeWechat
- {
- const SEND_HOTEL = 1000002;
- const SEND_BUS = 1000003;
- const SEND_TICKET = 1000004;
-
- /**
- * User:Steven
- * Desc:获取所有部门
- */
- public function GetDepartment()
- {
- $accessToken = new AccessToken('txl');
- $token = $accessToken->getAccessToken();
- $url = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=$token&id=";
- $rs = Utils::http_post($url, []);
- $result_array = json_decode($rs['content'], true);
- return $result_array;
- }
-
-
- /**
- * User:Steven
- * Desc:据部门ID查询用户信息
- * @param int $depId 查询的部门ID
- * @param int $fetchChild 是否遍历子部门
- * @param int $simple 是否只查询用户的基本信息
- * @return string
- */
- public function GetUserByDepartment($depId, $fetchChild = 1, $simple = 1)
- {
- if ($depId > 0) {
- $interface = $simple == 1 ? "simplelist" : "list";
- $accessToken = new AccessToken('txl');
- $token = $accessToken->getAccessToken();
- $url = "https://qyapi.weixin.qq.com/cgi-bin/user/$interface?access_token=$token&department_id=$depId&fetch_child=$fetchChild";
- $rs = Utils::http_post($url, []);
- $result_array = json_decode($rs['content'], true);
- return $result_array;
- } else {
- return false;
- }
-
- }
-
- /**
- * User:Steven
- * Desc:获取所有部门的所有员工
- */
- public function actionGetAllUser()
- {
- $department_list = $this->GetDepartment();
- if ($department_list['errcode'] == 0) {
- foreach ($department_list['department'] as $item) {
- $dep = new OfficeWechatDep();
- $dep->load($item);
- $dep->save();
- $rs = $this->GetUserByDepartment($item['id']);
- if ($rs['errcode'] == 0) {
- foreach ($rs['userlist'] as $val) {
- $user = new OfficeWechatUser();
- $user->load($val);
- $res = $user->save();
- }
- }
- }
- }
- return true;
- }
-
- /**
- * User:Steven
- * Desc:发送消息到企业微信
- * @param $agentid 企业应用ID
- * @param $title 标题
- * @param $msg 内容
- * @param $touser 用户ID
- * @return string
- */
- public static function sendMsg($arr)
- {
- if (\Yii::$app->params['sendRTX']) {
- $accessToken = new AccessToken($arr['agentid']);
- $token = $accessToken->getAccessToken();
- $url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$token";
- $param = array(
- "touser" => $arr['touser'],
- "toparty" => '',
- "totag" => '',
- "msgtype" => 'text',
- "agentid" => $arr['agentid'],
- "text" => array(
- "title" => "Title",
- "content" => urlencode($arr['title'] . "\n" . mb_substr($arr['msg'], 0, 300))
- ),
- "safe" => 0
- );
- $rs = Utils::http_post($url, $param);
- return json_encode($rs);
- } else {
- return true;
- }
- }
- }
|