Files
liche/common/libraries/Wx_qyapi.php
T
2022-06-29 14:20:40 +08:00

252 lines
12 KiB
PHP

<?php
/**
* Created by Vim
* User: Lcc
* Date: 2020-12-25
* Time: 10:23
* Desc: 企业微信api
*/
class Wx_qyapi
{
const BASE_URL = 'https://qyapi.weixin.qq.com/';
const TOKEN_API = 'cgi-bin/gettoken?corpid=%s&corpsecret=%s'; //获取access_token
const MOBILE_HASHCODE_API = 'cgi-bin/user/get_mobile_hashcode?access_token=%s'; //获取手机号随机串
private $redis;
private $corpid;
private $corpsecret;
private $ci;
private $access_token;
private $log_file = 'wx_qyapi.log';
private $env;
private $config;
public function __construct($params = array())
{
//获取环境
if (false !== strpos($_SERVER['HTTP_HOST'], 'dev')) { //dev 测试
$this->env = 'd';
} elseif (false !== strpos($_SERVER['HTTP_HOST'], 'test')) {//test 测试
$this->env = 't';
} else { // 正式
$this->env = 'p';
}
$this->redis = &load_cache('redis');
$this->ci = &get_instance();
$this->ci->load->library('mycurl');
$this->init($params);
}
public function init($params)
{
$configs = array(
//狸车新能源
'lichene' => array(
'corpid' => 'wwff2d727ce47d6852',//企业ID
'corpsecret' => 'wXAdJr6WgMnN6myPC73gXyxyHWpqwiI7dDaN21OAWV0',//企业密钥
),
//狸车
'liche' => array(
'corpid' => 'wwc2caba960d202087',
'corpsecret' => 'aQ2yhOBTXZnM0iwFtBzYIWbtyq4wFaIXBYTKg3xFxas',
),
//异业店_凯利之星
'diff_133' => array(
'corpid' => 'ww1493c3e4fb56ef29',
'corpsecret' => 'wbQ6zvc8vdJmpuWYAHH2yr4_izBQlVTByWIWPoGNyZw',
'token' => 'wEa15o5kmUXOutqg',//接收事件使用
'encodingAesKey' => 'qeHFP89LQAGK1LzLcsxBZ2nrqpTMWfkQ1WYd4mz5atX',//接收事件使用
),
);
$params['corpid'] && $this->corpid = $params['corpid'];
$params['corpsecret'] && $this->corpsecret = $params['corpsecret'];
$app = $params['app'] ? $params['app'] : 'liche';
if ($configs[$app]) {
$this->config = $configs[$app];
!$this->corpid && $this->config['corpid'] && $this->corpid = $this->config['corpid'];
!$this->corpsecret && $this->config['corpsecret'] && $this->corpsecret = $this->config['corpsecret'];
}
$this->corpid && $this->log_file = "wx_qyapi_{$this->corpid}.log";
}
public function getConfig()
{
$config = ['corpid' => $this->corpid, 'corpsecret' => $this->corpsecret];
$this->config && $config = $this->config;
return $config;
}
public function access_token()
{
$access_token = $this->redis->get($this->corpsecret);
if (!$access_token) {
if ($this->env == 'p') {
$url = self::BASE_URL . sprintf(self::TOKEN_API, $this->corpid, $this->corpsecret);
$res = $this->ci->mycurl->httpGet($url);
$result = json_decode($res);
if ($result->errcode) {
debug_log('error:__FUNCTION__:' . $res, $this->log_file);
} else {
$access_token = $result->access_token;
$this->redis->save($this->corpsecret, $result->access_token, $result->expires_in);
}
} else {
$url = "https://api.liche.cn/weixin/qy_access_token?corpid={$this->corpid}&corpsecret={$this->corpsecret}";
$res = $this->ci->mycurl->httpGet($url);
$result = json_decode($res);
$access_token = $result->access_token;
}
}
return $access_token;
}
public function mobile_hashcode($mobile, $state = '')
{
$access_token = $this->access_token();
if (!$access_token) return ['code' => 0, 'msg' => '获取access_token失败'];
$url = self::BASE_URL . sprintf(self::MOBILE_HASHCODE_API, $access_token);
$data = [
'mobile' => $mobile
];
$state && $data['state'] = $state;
$res = $this->ci->mycurl->httpPost($url, $data, 'is_json');
$result = json_decode($res);
if ($result->errcode) {
return ['code' => 0, 'msg' => $result->errmsg];
} else {
return ['code' => 1, 'msg' => '', 'hashcode' => $result->hashcode];
}
}
public function token()
{
$url = self::BASE_URL . sprintf(self::TOKEN_API, $this->corpid, $this->corpsecret);
$res = $this->ci->mycurl->httpGet($url);
$result = json_decode($res);
return $result;
}
public function get_external_contact($param)
{
$access_token = $this->access_token();
//https://api.haodian.cn/weixin/qy_access_token?corpid=wwecac3c2b60c31b1b&corpsecret=EFIev4j-0iv4Uy2vrDATzsL3aIW2IT_kRO4zx73I31g
//$access_token = 'v8weWaaKpFILsLvyAoN1YAZbOV6vQcsAaOIT4TKuGURq6UAV3CIfxqNfUCNnyOYRJYaRo5b8AVbruuBx81KmPXdY1GpQpuvnCDkMf992ONGLJIjT30-YzAdvH4ltbdV0SSkzqehY81rev_tnPMa2oIHZsoOW-0X7mwwdc3fGiYsnYBK8Ti1bd164b5uiTa610D5Lx9jrZmjpppLcqENc3Q';
if (!$access_token) return ['code' => 0, 'msg' => '获取access_token失败'];
$res = array();
if ($param['url'] == 'get_follow_user_list') {//获取配置了客户联系功能的成员列表
$url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_follow_user_list?access_token={$access_token}";//&debug=1
$res = $this->ci->mycurl->httpGet($url);
} else if ($param['url'] == 'user_get') {//读取成员 https://developer.work.weixin.qq.com/document/path/90196
$url = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token={$access_token}&userid={$param['userid']}";
$res = $this->ci->mycurl->httpGet($url);
} else if ($param['url'] == 'list') {//获取客户列表
$url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/list?access_token={$access_token}&userid={$param['userid']}";
$res = $this->ci->mycurl->httpGet($url);
} else if ($param['url'] == 'get') {//获取客户详情
$url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get?access_token={$access_token}&external_userid={$param['external_userid']}";
$res = $this->ci->mycurl->httpGet($url);
} else if ($param['url'] == 'get_by_user') {//批量获取客户详情
$url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/batch/get_by_user?access_token={$access_token}";
$limit = $param['limit'] ? $param['limit'] : 100;
$res = $this->ci->mycurl->httpPost($url, array('userid' => $param['userid'], 'cursor' => $param['next_cursor'], 'limit' => $limit), 'is_json');
} else if ($param['url'] == 'get_corp_tag_list') {//获取企业标签库 https://work.weixin.qq.com/api/doc/90001/90143/92696#获取企业标签库
$url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_corp_tag_list?access_token={$access_token}";
$res = $this->ci->mycurl->httpPost($url, array('group_id' => $param['group_id']), 'is_json');
} else if ($param['url'] == 'add_corp_tag') {//添加企业客户标签 https://developer.work.weixin.qq.com/document/path/92117
$url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/add_corp_tag?access_token={$access_token}";
$params = ['group_id' => $param['group_id'], 'group_name' => $param['group_name'], 'order' => $param['order']];
$param['tag'] && $params['tag'] = $param['tag'];
$res = $this->ci->mycurl->httpPost($url, $params, 'is_json');
} else if ($param['url'] == 'edit_corp_tag') {//编辑企业客户标签 https://developer.work.weixin.qq.com/document/path/92117
$url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/edit_corp_tag?access_token={$access_token}";
$res = $this->ci->mycurl->httpPost($url, ['id' => $param['id'], 'name' => $param['name'], 'order' => $param['order']], 'is_json');
} else if ($param['url'] == 'del_corp_tag') {//删除企业客户标签 https://developer.work.weixin.qq.com/document/path/92117
$url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/del_corp_tag?access_token={$access_token}";
$params = [];
$param['tag_id'] && $params['tag_id'] = $param['tag_id'];
$param['group_id'] && $params['group_id'] = $param['group_id'];
$res = $this->ci->mycurl->httpPost($url, $params, 'is_json');
} else if ($param['url'] == 'mark_tag') {//编辑客户企业标签 https://developer.work.weixin.qq.com/document/path/92118
$url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/mark_tag?access_token={$access_token}";
$params = ['userid' => $param['userid'], 'external_userid' => $param['external_userid']];
$param['add_tag'] && $params['add_tag'] = $param['add_tag'];
$param['remove_tag'] && $params['remove_tag'] = $param['remove_tag'];
$res = $this->ci->mycurl->httpPost($url, $params, 'is_json');
} else if ($param['url'] == 'remark') {//修改客户备注信息 https://developer.work.weixin.qq.com/document/path/92115
$url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/remark?access_token={$access_token}";
$res = $this->ci->mycurl->httpPost($url, array('userid' => $param['userid'], 'external_userid' => $param['external_userid']
, 'remark_mobiles' => $param['remark_mobiles'], 'description' => $param['description']), 'is_json');
} else if ($param['url'] == 'get_user_behavior_data') {//联系客户统计 https://developer.work.weixin.qq.com/document/path/92132
$url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_user_behavior_data?access_token={$access_token}";
$params = ['userid' => $param['userid'], 'start_time' => $param['start_time'], 'end_time' => $param['end_time']];
$res = $this->ci->mycurl->httpPost($url, $params, 'is_json');
}
$data = trim($res, chr(239) . chr(187) . chr(191));
$result = json_decode($data, true);
return $result;
}
public function getSignPackage()
{
$jsapiTicket = $this->jsapi_ticket();
$url = "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$timestamp = time();
$nonceStr = $this->createNonceStr();
// 这里参数的顺序要按照 key 值 ASCII 码升序排序
$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
$signature = sha1($string);
$signPackage = array(
"appId" => $this->corpid,
"nonceStr" => $nonceStr,
"timestamp" => $timestamp,
"url" => $url,
"signature" => $signature,
"rawString" => $string,
//"access_token" => $this->access_token(),
'jsapi_ticket' => $jsapiTicket
);
return $signPackage;
}
public function jsapi_ticket()
{
$key = 'ticket_' . $this->corpsecret;
$ticket = $this->redis->get($key);
if (!$ticket) {
if ($this->env == 'p') {
//获取企业的jsapi_ticket https://developer.work.weixin.qq.com/document/path/90506#14924
$access_token = $this->access_token();
$url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token={$access_token}";
$res = $this->ci->mycurl->httpGet($url);
$result = json_decode($res);
if ($result->errcode) {
debug_log('error:__FUNCTION__:' . $res, $this->log_file);
} else {
$ticket = $result->ticket;
$this->redis->save($key, $result->ticket, $result->expires_in);
}
} else {
$url = "https://api.liche.cn/weixin/qy_jsapi_ticket?corpid={$this->corpid}&corpsecret={$this->corpsecret}";
$res = $this->ci->mycurl->httpGet($url);
$result = json_decode($res);
$ticket = $result->ticket;
}
}
return $ticket;
}
private function createNonceStr($length = 16)
{
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
}
?>