企业微信开发 企业微信接口 授权 模板推送 获取用户信息获取部门 获取部门成员 获取通讯录 获取访问用户身份 获取单部门列表 创建直播(课程)

 固定参数有存数据库表 和  存文件  两种  ,下面这是存数据库表形式,下面是相关参数

1.`app_secret`:'应用秘钥',

2.`app_id` : '应用id',

3.`app_name` : '应用名称',

4.`access_token` :'凭证',

5.`over_time` : '过期时间',

6.`corpid` :'企业id',

7.`ticket` :'企业js_sdk凭证',

8.`ticket_over_time` : '企业js_sdk过期时间',

9.`app_ticket` : '应用js_sdk凭证',

10.`app_ticket_over_time` : '引用js_sdk过期时间',

消息推送参解释参数解释

1.touser 否 指定接收消息的成员,成员ID列表(多个接收者用‘|’分隔,最多支持1000个)。

特殊情况:指定为"@all",则向该企业应用的全部成员发送

2.toparty 否 指定接收消息的部门,部门ID列表,多个接收者用‘|’分隔,最多支持100个。

当touser为"@all"时忽略本参数

3.totag 否 指定接收消息的标签,标签ID列表,多个接收者用‘|’分隔,最多支持100个。

当touser为"@all"时忽略本参数

4.msgtype 是 消息类型,此时固定为:text

5.agentid 是 企业应用的id,整型。企业内部开发,可在应用的设置页面查看;第三方服务商,可通过接口 获取企业授权信息 获取该参数值

6.title 是 标题,不超过128个字节,超过会自动截断(支持id转译)

7.description 是 描述,不超过512个字节,超过会自动截断(支持id转译)

8.url 是 点击后跳转的链接。最长2048字节,请确保包含了协议头(http/https)

9.safe 否 表示是否是保密消息,0表示可对外分享,1表示不能分享且内容显示水印,默认为0

10.enable_duplicate_check 否 表示是否开启重复消息检查,0表示否,1表示是,默认0

11.duplicate_check_interval 否 表示是否重复消息检查的时间间隔,默认1800s,最大不超过4小时

//初始化请求地址

static public $api_url = 'https://qyapi.weixin.qq.com/cgi-bin'; 

public function __construct() {

        parent::__construct ();

}

//定义日志方法

private function writeLog($text,$type='ERROR') {

file_put_contents (  "日志地址:自定义服务器存放地址", FILE_APPEND );

}

//获取 token(获取表数据   或者  读取文件获取数据)

private  function gettoken($app_id){

        $row = "获取表数据   或者  读取文件获取数据";

        //判断过期时间

        if($row['over_time'] && $row['over_time'] > date("Y-m-d H:i:s")){

                return $row['access_token'];

        }else{//刷新秘钥

        $time = time();

 $res=json_decode(@file_get_contents(self::$api_url."/gettoken?corpid={$row['corpid']}&corpsecret={$row['app_secret']}"),true);

        if($res['errcode'] == 0){

                //todo  修改表数据   或者  更新存储文件信息

                return $res['access_token'];

        }else{

                 //todo 记录错误日志

                $this->writeLog($res['errcode'].' '.$res['errmsg']);

                return false;

        }

   }

}

//获取 企业js_sdk凭证

private  function get_jsapi_ticket($app_id){

        $row =  "获取表数据   或者  读取文件获取数据";

        //判断过期时间

        if($row['ticket_over_time'] && $row['ticket_over_time'] > date("Y-m-d H:i:s")){

                return $row['ticket'];

        }else{//刷新企业js_sdk凭证

        //获取秘钥

        $access_token = $this->gettoken($app_id);

        $res=json_decode(@file_get_contents(self::$api_url."/get_jsapi_ticket?access_token=$access_token"),true);

        if($res['errcode'] == 0){

                //todo  修改表数据   或者  更新存储文件信息

                return $res['ticket'];

        }else{

                //todo 记录错误日志

                $this->writeLog($res['errcode'].' '.$res['errmsg']);

                return false;

        }

    }

}

//获取   应用js_sdk凭证

private  function get_appjsapi_ticket($app_id){

        $row =  "获取表数据   或者  读取文件获取数据";

        if($row['app_ticket_over_time'] && $row['app_ticket_over_time'] > date("Y-m-d H:i:s")){

                return $row['app_ticket'];

        }else{

                //判断过期时间

                $access_token = $this->gettoken($app_id);

                $time = time();

        $res=json_decode(@file_get_contents(self::$api_url."/ticket/get?access_token=$access_token&type=agent_config"),true);

                if($res['errcode'] == 0){

                        //todo  修改表数据   或者  更新存储文件信息

                        return $res['ticket'];

                }else{

                        //todo 记录错误日志

                        $this->writeLog($res['errcode'].' '.$res['errmsg']);

                        return false;

                  }

         }

}

//获取 随机字符串

public function GetRandStr($length){

        //字符组合

        $str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

        $len = strlen($str)-1;

        $randstr = '';

        for ($i=0;$i<$length;$i++) {

                $num=mt_rand(0,$len);

                $randstr .= $str[$num];

        }

        return $randstr;

}

//生成jssdk签名

public function get_wxConfig($app_id,$url){

        $row =  "获取表数据   或者  读取文件获取数据";

        $time = time();

        $nonceStr = $this->GetRandStr(8);

        $jsapi_ticket = $this->get_jsapi_ticket($app_id);

        $str1 = "jsapi_ticket=$jsapi_ticket&noncestr=$nonceStr×tamp=$time&url=$url";

        $signature = sha1 ($str1); //todo 加密参数

        $return_arr  =[];

        $return_arr['corpid'] = $row['corpid'];

        $return_arr['timestamp'] = $time;

        $return_arr['nonceStr'] = $nonceStr;

        $return_arr['signature'] = $signature;

        return $return_arr;

}

// 获取访问用户身份

public function qy_getuserinfo($app_id,$code) {

        // 获取秘钥

        $access_token = $this->gettoken ($app_id);

//注意: code:通过成员授权获取到的code,最大为512字节。每次成员授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期。

        $json = @file_get_contents(self::$api_url."/user/getuserinfo?access_token=$access_token&code=$code");

        $res =  json_decode($json,true);

        if($res['errcode'] == 0){

                $this->writeLog($json,'SUCCESS');

                if($res['UserId']){//企业用户

                        //todo 企业用户操作逻辑

                }elseif($res['OpenId']){//非企业用户

                        //todo 非企业用户操作逻辑

                }

        }else{

                //todo 记录错误日志

                $this->writeLog($res['errcode'].' '.$res['errmsg']);

                return false;

        }

}

//获取部门列表

public function qy_getdepartment($app_id){

        $access_token = $this->gettoken ($app_id);

        $res=json_decode(@file_get_contents(self::$api_url."/department/list?        access_token=$access_token"),true);

        return $res;

}

//获取单部门列表

public function get_department_byid($app_id,$id){

        $access_token = $this->gettoken ($app_id);

        $res=json_decode(@file_get_contents(self::$api_url."/department/get?        access_token=$access_token&id=$id"),true);

        return $res;

}

/*获取部门成员

 access_token 是 调用接口凭证

 department_id 是 获取的部门id

 fetch_child 否 是否递归获取子部门下面的成员:1-递归获取,0-只获取本部门

 */

public function qy_getuser($app_id,$department_id,$fetch_child=0){

        $access_token = $this->gettoken ($app_id);

        $res=json_decode(@file_get_contents(self::$api_url."/user/simplelist?        access_token=$access_token&department_id=$department_id&fetch_child=$fetch_child"),true);

        return $res;

}

//读取企业通讯录的所有成员的信息

public function qy_getuser_desc($app_id,$userid,$data1=[]){

        $access_token = $this->gettoken ($app_id);

        $res=json_decode(@file_get_contents(self::$api_url."/user/get?        access_token=$access_token&userid=$userid"),true);

        if($res['errcode'] == 0){

            //todo 成员信息操作逻辑

        }else{

                $this->writeLog($res['errcode'].' '.$res['errmsg']);

        }

}

//消息推送

public function         push_text_message($app_id,$contant,$push_user='',$push_department='',$push_tag=''){

        $access_token = $this->gettoken ($app_id);

        $url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$access_token";

        $data = [

                "touser" =>$push_user,

                "toparty" => $push_department,

                "totag" => $push_tag,

                "msgtype" => "text",

                "agentid" => $app_id,

                "text" => ["content"=>$contant],

                "safe" =>0,

                'enable_id_trans' => 0,

                'enable_duplicate_check' =>1,

                'duplicate_check_interval' => '1800',

        ];

        $res =curlRequest($url,json_encode($data));

        return $res;

}

    //消息推送

    public function push_textcard_message($app_id,$title,$description,$message_url,$mobile,$push_department='',$push_tag=''){

        $push_user = $this->getuserid($app_id,$mobile);

        if($push_user['code'] == 0){

            //todo 推送完成信息

        }else{

            return $push_user;

        }

    }

    //创建直播

    public function create_living($app_id,$info,$type=0){

             $access_token = $this->gettoken ($app_id);

             $url = self::$api_url."/living/create?access_token=$access_token";

             $data = [

                     "anchor_userid"=>$info['anchor_userid'],

                     "theme"=> $info['theme'],

                     "living_start"=> $info['living_start'],

                     "living_duration"=> $info['living_duration'],

                     "description"=> $info['description'],

                     "type"=> $type,

                     "remind_time"=> $info['remind_time']];

                     $res =curlRequest($url,json_encode($data));

                      if($res['errcode'] == 0){

                               return ['code'=>0,'msg'=>'ok','livingid'=>$res['livingid']];

                      }else{

                               $this->writeLog($res['errcode'].' '.$res['errmsg']);

                               return ['code'=>$res['errcode'] ,'msg'=>$res['errmsg']];

                  }

            }

    //手机号获取userid

    private  function getuserid($app_id,$mobile){

         if(!$this->isMobile($mobile)){

                    return ['code'=>1,'msg'=>'手机号格式错误'];

        }

        $access_token = $this->gettoken ($app_id);

        $url = self::$api_url."/user/getuserid?access_token=$access_token";

        $data = ["mobile" =>"$mobile"];

        $res =curlRequest($url,json_encode($data));

        if($res['errcode'] == 0){

            return ['code'=>0,'userid'=>$res['userid']];

        }else{

            $this->writeLog($res['errcode'].' '.$res['errmsg']);

            return ['code'=>$res['errcode'] ,'msg'=>$res['errmsg']];

        }

    }

    //判断手机号码是否合法

    private function isMobile($mobile) {

        if (!is_numeric($mobile)) {

            return false;

        }

        return preg_match('#1[3456789]{1}\d{9}$#', $mobile) ? true : false;

    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值