|
|
|
@@ -0,0 +1,273 @@
|
|
|
|
|
<?php
|
|
|
|
|
ini_set('display_errors', 'on');
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Notes:狸车新能源企微
|
|
|
|
|
* Created on: 2022/3/23 16:56
|
|
|
|
|
* Created by: dengbw
|
|
|
|
|
*/
|
|
|
|
|
Class Lichene extends HD_Controller
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
parent::__construct();
|
|
|
|
|
$target_class = lcfirst(get_class($this));//调用类的名称,子类或者当前类名称
|
|
|
|
|
$this->log_file = "wechat_{$target_class}.log";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function index()
|
|
|
|
|
{
|
|
|
|
|
echo 'index';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Notes:企业微信接收事件
|
|
|
|
|
* https://liche-api-dev.xiaoyu.com/wechat/lichene/get_qymsg
|
|
|
|
|
* https://api.liche.cn/wechat/lichene/get_qymsg
|
|
|
|
|
* https://open.work.weixin.qq.com/wwopen/devtool/interface?doc_id=14961 测试回调模式
|
|
|
|
|
* Created on: 2022/3/23 17:23
|
|
|
|
|
* Created by: dengbw
|
|
|
|
|
*/
|
|
|
|
|
public function get_qymsg()
|
|
|
|
|
{
|
|
|
|
|
$param = $this->input->get();
|
|
|
|
|
debug_log('<br>--->qymsg_param:' . json_encode($param), $this->log_file);
|
|
|
|
|
require_once(COMMPATH . '/third_party/WXqy/WXBizMsgCrypt.php');
|
|
|
|
|
$encodingAesKey = "CXUVlU9C7KsxzglxRiMlMsG2WSqM4EkFufDvgjkFciU";
|
|
|
|
|
$token = "CEoJkwUFP58KfZJ";
|
|
|
|
|
$corpId = "wwff2d727ce47d6852";
|
|
|
|
|
|
|
|
|
|
$sVerifyMsgSig = $param['msg_signature'];
|
|
|
|
|
$sVerifyTimeStamp = $param['timestamp'];
|
|
|
|
|
$sVerifyNonce = $param['nonce'];
|
|
|
|
|
$sVerifyEchoStr = $param['echostr'];
|
|
|
|
|
// 需要返回的明文
|
|
|
|
|
$sEchoStr = "";
|
|
|
|
|
$wxcpt = new WXBizMsgCrypt($token, $encodingAesKey, $corpId);
|
|
|
|
|
if ($sVerifyEchoStr) {//验证回调URL
|
|
|
|
|
$errCode = $wxcpt->VerifyURL($sVerifyMsgSig, $sVerifyTimeStamp, $sVerifyNonce, $sVerifyEchoStr, $sEchoStr);
|
|
|
|
|
debug_log('qymsg_errCode:' . $errCode, $this->log_file);
|
|
|
|
|
if ($errCode == 0) {
|
|
|
|
|
// 验证URL成功,将sEchoStr返回
|
|
|
|
|
debug_log('qymsg_sEchoStr:' . $sEchoStr . '&sEchoStrurldecode=' . urldecode($sEchoStr), $this->log_file);
|
|
|
|
|
echo urldecode($sEchoStr);
|
|
|
|
|
} else {
|
|
|
|
|
echo $errCode;
|
|
|
|
|
}
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
// $sVerifyMsgSig = '08eb02c5d7fefaa29e2bd6a63acb8dfcc902ccf4';
|
|
|
|
|
// $sVerifyTimeStamp = '1620713245';
|
|
|
|
|
// $sVerifyNonce = '1620719758';
|
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] == "POST") {
|
|
|
|
|
$sReqData = file_get_contents("php://input");
|
|
|
|
|
//$sReqData = "<xml><ToUserName><![CDATA[wwecac3c2b60c31b1b]]></ToUserName><Encrypt><![CDATA[1zKOzPaVNdUO6ytHYfs1IDaNL3E8RKFmh7bjtyyOHraGnVBnSNCo5q/InAY+GJzh1lyhh7L/Ch94uKSu2nf+zvPiKSOZD3xKv2pgbWXLqOcYAv8pvCYatQJx40a4zEq5qk/Ue+Fmt0AeOGBHmi59Y397t3vyLtpNdcePLcUrbfYCmbHUSWAybaNMmCVW//YJYGFmVJOjKmudTjD7nG1Ok7mpDyw7oETCpP8FY91EqC0DNeF4D5G1/OV4Fh+GNl++/kPpOTxTlsMM0HkjqmWKVE3nXG7elWXVjjRrabURvyDzs7MBAkjw53I7dud5y6ao53oXEsvw9wD+utISca4iZqL+0/mjP+JUylZbQB74jya5kQHR/zN6FaxcxImWLRFWY16SC8losLjkwdDfhke+3BCoa7JavKAIzjOTlLvc0CCEGSQMen9At6ktVFJhPBMAmKO1hF0nrT044zia82PQQa9T6Z9lDck4qttFiW5817uZwweHZ5enjwDaeGf+I8Kq5I4/EAFq1UeYYkl+LBVtATMbAZGKu2VSrO75DgQYPW7tP7XE/A1Eamnyb+PAarvD2PxzSoEtIGv/tI/Jv4I/gw==]]></Encrypt><AgentID><![CDATA[2000003]]></AgentID></xml>";
|
|
|
|
|
//$log = '---';
|
|
|
|
|
//if (is_array($sReqData)) $log = print_r($sReqData, true);
|
|
|
|
|
//debug_log('qymsg_sReqData:' . json_encode($log, JSON_UNESCAPED_UNICODE), $this->log_file);
|
|
|
|
|
$sMsg = ""; // 解析之后的明文
|
|
|
|
|
$errCode = $wxcpt->DecryptMsg($sVerifyMsgSig, $sVerifyTimeStamp, $sVerifyNonce, $sReqData, $sMsg);
|
|
|
|
|
debug_log('qymsg_errCode:' . $errCode, $this->log_file);
|
|
|
|
|
if ($errCode == 0) {
|
|
|
|
|
$this->load->model('app/app_lichene_qy_model', 'mdWechatqy');
|
|
|
|
|
$re_msg = (array)simplexml_load_string($sMsg, 'SimpleXMLElement', LIBXML_NOCDATA);//xml转json
|
|
|
|
|
$ExternalUserID = $re_msg['ExternalUserID'];
|
|
|
|
|
$ChangeType = $re_msg['ChangeType'];
|
|
|
|
|
debug_log('qymsg_ChangeType:' . $ChangeType, $this->log_file);
|
|
|
|
|
$this->load->library('wx_qyapi', ['app' => 'lichene']);
|
|
|
|
|
if ($ChangeType == 'add_external_contact') {//添加企业客户事件
|
|
|
|
|
$re = $this->wx_qyapi->get_external_contact(array('url' => 'get', 'external_userid' => $ExternalUserID));
|
|
|
|
|
//debug_log('qymsg_external_errcode:' . $re['errcode'] . '&external_userid=' . $re['external_contact']['external_userid'], $this->log_file);
|
|
|
|
|
if ($re['errcode'] == 0) {
|
|
|
|
|
$external_userid = $re['external_contact']['external_userid'];
|
|
|
|
|
if ($external_userid) {
|
|
|
|
|
$re_qy = $this->mdWechatqy->get(array('external_userid' => $external_userid));
|
|
|
|
|
$jsondata['external_contact'] = $re['external_contact'] ? $re['external_contact'] : '';
|
|
|
|
|
$jsondata['follow_user'] = $re['follow_user'] ? $re['follow_user'] : '';
|
|
|
|
|
$tags_ary = $re['follow_user'][0]['tags'];
|
|
|
|
|
$tags = '';
|
|
|
|
|
if ($tags_ary && $tags_ary != '[]' && is_array($tags_ary)) {
|
|
|
|
|
$tags = implode(',', array_column($tags_ary, 'tag_id'));
|
|
|
|
|
}
|
|
|
|
|
$addData = array(
|
|
|
|
|
'external_userid' => $re['external_contact']['external_userid'],
|
|
|
|
|
'name' => $re['external_contact']['name'],
|
|
|
|
|
'avatar' => $re['external_contact']['avatar'],
|
|
|
|
|
'unionid' => $re['external_contact']['unionid'] ? $re['external_contact']['unionid'] : '',
|
|
|
|
|
'userid' => $re['follow_user'][0]['userid'] ? $re['follow_user'][0]['userid'] : '',
|
|
|
|
|
'remark' => $re['follow_user'][0]['remark'] ? $re['follow_user'][0]['remark'] : '',
|
|
|
|
|
'tags' => $tags,
|
|
|
|
|
'c_time' => $re['follow_user'][0]['createtime'] ? $re['follow_user'][0]['createtime'] : time(),
|
|
|
|
|
'jsondata' => json_encode($jsondata, JSON_UNESCAPED_UNICODE),
|
|
|
|
|
'status' => 1,
|
|
|
|
|
);
|
|
|
|
|
if (!$re_qy) {
|
|
|
|
|
$this->mdWechatqy->add($addData);
|
|
|
|
|
} else {
|
|
|
|
|
$this->mdWechatqy->update($addData, array('external_userid' => $external_userid));
|
|
|
|
|
}
|
|
|
|
|
//debug_log('qymsg_addData:' . json_encode($addData, JSON_UNESCAPED_UNICODE), $this->log_file);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
debug_log('qymsg_get:' . json_encode($re, JSON_UNESCAPED_UNICODE), $this->log_file);
|
|
|
|
|
} else if ($ChangeType == 'update') {//修改企业客户事件
|
|
|
|
|
debug_log('qymsg_sMsg:' . json_encode($re_msg, JSON_UNESCAPED_UNICODE), $this->log_file);
|
|
|
|
|
// $re = $this->wx_qyapi->get_external_contact(array('url' => 'get', 'external_userid' => $ExternalUserID));
|
|
|
|
|
// debug_log('qymsg_external_errcode:' . $re['errcode'] . '&external_userid=' . $re['external_contact']['external_userid'], $this->log_file);
|
|
|
|
|
// debug_log('qymsg_get:' . json_encode($re, JSON_UNESCAPED_UNICODE), $this->log_file);
|
|
|
|
|
} else if ($ChangeType == 'del_follow_user') {//删除企业客户事件
|
|
|
|
|
if ($ExternalUserID) {
|
|
|
|
|
$re_qy = $this->mdWechatqy->get(array('external_userid' => $ExternalUserID));
|
|
|
|
|
if ($re_qy) {
|
|
|
|
|
$this->mdWechatqy->update(array('status' => -1), array('id' => $re_qy['id']));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//debug_log('qymsg_sMsg:' . json_encode($re_msg, JSON_UNESCAPED_UNICODE), $this->log_file);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Notes:临时更新用户客户id
|
|
|
|
|
* Created on: 2021/03/01 17:02
|
|
|
|
|
* Created by: dengbw
|
|
|
|
|
* https://liche-api-dev.xiaoyu.com/wechat/lichene/tem_external_contact
|
|
|
|
|
* https://api.liche.cn/wechat/lichene/tem_external_contact
|
|
|
|
|
*/
|
|
|
|
|
public function tem_external_contact()
|
|
|
|
|
{
|
|
|
|
|
$this->load->model('app/app_lichene_qy_model', 'mdWechatqy');
|
|
|
|
|
$this->load->library('wx_qyapi', ['app' => 'lichene']);
|
|
|
|
|
$param = $this->input->get();
|
|
|
|
|
$param['page'] = intval($param['page']);
|
|
|
|
|
//获取配置了客户联系功能的成员列表
|
|
|
|
|
$re = $this->wx_qyapi->get_external_contact(array('url' => 'get_follow_user_list'));
|
|
|
|
|
echo json_encode($re, JSON_UNESCAPED_UNICODE);
|
|
|
|
|
exit;
|
|
|
|
|
//用户分页分别获取避免超时
|
|
|
|
|
// $userid = "DianCheDaRenXiaoLi";
|
|
|
|
|
// if ($param['next_cursor'] || !$param['page']) {
|
|
|
|
|
// $next_cursor = $this->get_by_user($userid, $param['next_cursor']);
|
|
|
|
|
// header('refresh:3;url=/wechat/liche/tem_external_contact?next_cursor=' . $next_cursor . '&page=' . ($param['page'] + 1));
|
|
|
|
|
// } else {
|
|
|
|
|
// echo '跑数据结束=' . $userid;
|
|
|
|
|
// }
|
|
|
|
|
// exit;
|
|
|
|
|
// ob_start(); //打开缓冲区
|
|
|
|
|
$follow_user = ["LinZhenZhen", "18950116819", "ShengHuaXiangXueTieLongLuPeng", "ChenHuiCan", "13600972070", "0fun",
|
|
|
|
|
"WuHuanZheng", "ChenWeiHua", "13313999284", "FuZhouShengHuaTaiQiCheXiaoShouYo", "DianCheDaRenXiaoLi", "YeJiaChen",
|
|
|
|
|
"LuoJinYan", "QianJianLin", "18005933510", "wudz", "huangy", "LiZhengHui", "YeRongZhang", "LiZhiWei", "CaiYaFen",
|
|
|
|
|
"TaoQianQian", "FanHaiYu", "XuDanPing", "ChenWenZhang", "HongXinYi", "CaiZhiMin", "JiangYeLei", "ZengLingYan", "18906027570",
|
|
|
|
|
"jean", "fuyy", "taric", "jimmy", "HongSiZe", "huangjq", "xugy", "wuhl", "HuangZhiJie", "ZhangShanBang", "zhangtf", "fudw",
|
|
|
|
|
"HuJianPing", "13666060496", "LinXiaoLong", "LinCongCong", "liuch", "RenLin", "18876491999", "KangPengHui", "liusb", "luohj",
|
|
|
|
|
"YangYaQi", "WangJiangFu", "SuiYuErAn", "Xia", "MuZi2020", "a.bing", "ChenZhiShen", "ChenCuiYun", "YuTing", "WenYan", "YueYouYin",
|
|
|
|
|
"QuanZhouLiCheChuNa"];
|
|
|
|
|
$count = count($follow_user);
|
|
|
|
|
if ($param['page'] >= $count) {
|
|
|
|
|
echo '<br>本次更新企业成员:';
|
|
|
|
|
echo '<br><br>成功更新 <span style="color: red;">' . $count . '</span> 人';
|
|
|
|
|
echo '<br><br><a href="/wechat/liche/tem_external_contact">点击将再次更新企业成员>>></a>';
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
$userid = $follow_user[$param['page']];
|
|
|
|
|
$next_cursor = $this->get_by_user($userid, '');
|
|
|
|
|
if ($next_cursor) {
|
|
|
|
|
$ii = 0;
|
|
|
|
|
while ($next_cursor) {
|
|
|
|
|
$next_cursor = $this->get_by_user($userid, $next_cursor);
|
|
|
|
|
$ii++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
echo '<br>成功更新企业成员:' . $userid;
|
|
|
|
|
header('refresh:3;url=/wechat/liche/tem_external_contact?page=' . ($param['page'] + 1));
|
|
|
|
|
ob_end_flush();//输出全部内容到浏览器
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function get_by_user($userid = '', $next_cursor = '')
|
|
|
|
|
{
|
|
|
|
|
$re = $this->wx_qyapi->get_external_contact(array('url' => 'get_by_user', 'userid' => $userid, 'next_cursor' => $next_cursor, 'limit' => '100'));
|
|
|
|
|
$next_cursor = '';
|
|
|
|
|
if ($re['errcode'] == 0) {
|
|
|
|
|
$next_cursor = $re['next_cursor'];
|
|
|
|
|
foreach ($re['external_contact_list'] as $key => $value) {
|
|
|
|
|
$external_userid = $value['external_contact']['external_userid'];
|
|
|
|
|
if ($external_userid) {
|
|
|
|
|
$re_qy = $this->mdWechatqy->get(array('external_userid' => $external_userid));
|
|
|
|
|
$jsondata = array();
|
|
|
|
|
$jsondata['external_contact'] = $value['external_contact'] ? $value['external_contact'] : '';
|
|
|
|
|
$jsondata['follow_info'] = $value['follow_info'] ? $value['follow_info'] : '';
|
|
|
|
|
$tags = $value['follow_info']['tag_id'];
|
|
|
|
|
$addData = array(
|
|
|
|
|
'external_userid' => $value['external_contact']['external_userid'],
|
|
|
|
|
'name' => $value['external_contact']['name'],
|
|
|
|
|
'avatar' => $value['external_contact']['avatar'],
|
|
|
|
|
'unionid' => $value['external_contact']['unionid'] ? $value['external_contact']['unionid'] : '',
|
|
|
|
|
'userid' => $value['follow_info']['userid'] ? $value['follow_info']['userid'] : '',
|
|
|
|
|
'remark' => $value['follow_info']['remark'] ? $value['follow_info']['remark'] : '',
|
|
|
|
|
'tags' => $tags && $tags != '[]' ? implode(',', $tags) : '',
|
|
|
|
|
'c_time' => $value['follow_info']['createtime'] ? $value['follow_info']['createtime'] : time(),
|
|
|
|
|
'jsondata' => json_encode($jsondata, JSON_UNESCAPED_UNICODE),
|
|
|
|
|
'status' => 1,
|
|
|
|
|
);
|
|
|
|
|
if (!$re_qy) {
|
|
|
|
|
$this->mdWechatqy->add($addData);
|
|
|
|
|
} else {
|
|
|
|
|
$this->mdWechatqy->update($addData, array('external_userid' => $external_userid));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
echo '<br>next_cursor=' . $next_cursor . '&external_contact_list=' . count($re['external_contact_list']);
|
|
|
|
|
}
|
|
|
|
|
return $next_cursor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Notes:添加企业客户事件
|
|
|
|
|
* https://liche-api-dev.xiaoyu.com/wechat/lichene/add_wechatqy
|
|
|
|
|
* https://api.liche.cn/wechat/lichene/add_wechatqy
|
|
|
|
|
* Created on: 2021/5/14 17:23
|
|
|
|
|
* Created by: dengbw
|
|
|
|
|
*/
|
|
|
|
|
public function add_wechatqy()
|
|
|
|
|
{
|
|
|
|
|
$param = $this->input->get();
|
|
|
|
|
$external_userid = $param['external_userid'];
|
|
|
|
|
if (!$external_userid) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$this->load->model('app/app_lichene_qy_model', 'mdWechatqy');
|
|
|
|
|
$this->load->library('wx_qyapi', ['app' => 'lichene']);
|
|
|
|
|
$re = $this->wx_qyapi->get_external_contact(array('url' => 'get', 'external_userid' => $external_userid));
|
|
|
|
|
print_r('qymsg_external_errcode:' . $re['errcode'] . '=' . $re['external_contact']['external_userid']);
|
|
|
|
|
if ($re['errcode'] == 0) {
|
|
|
|
|
$external_userid = $re['external_contact']['external_userid'];
|
|
|
|
|
if ($external_userid) {
|
|
|
|
|
$re_qy = $this->mdWechatqy->get(array('external_userid' => $external_userid));
|
|
|
|
|
$jsondata['external_contact'] = $re['external_contact'] ? $re['external_contact'] : '';
|
|
|
|
|
$jsondata['follow_user'] = $re['follow_user'] ? $re['follow_user'] : '';
|
|
|
|
|
$addData = array(
|
|
|
|
|
'external_userid' => $re['external_contact']['external_userid'],
|
|
|
|
|
'name' => $re['external_contact']['name'],
|
|
|
|
|
'avatar' => $re['external_contact']['avatar'],
|
|
|
|
|
'unionid' => $re['external_contact']['unionid'] ? $re['external_contact']['unionid'] : '',
|
|
|
|
|
'userid' => $re['follow_user'][0]['userid'] ? $re['follow_user'][0]['userid'] : '',
|
|
|
|
|
'remark' => $re['follow_user'][0]['remark'] ? $re['follow_user'][0]['remark'] : '',
|
|
|
|
|
'tags' => $re['follow_user'][0]['tags'] ? $re['follow_user'][0]['tags'] : '',
|
|
|
|
|
'c_time' => $re['follow_user'][0]['createtime'] ? $re['follow_user'][0]['createtime'] : time(),
|
|
|
|
|
'jsondata' => json_encode($jsondata, JSON_UNESCAPED_UNICODE),
|
|
|
|
|
'status' => 1,
|
|
|
|
|
);
|
|
|
|
|
if (!$re_qy) {
|
|
|
|
|
$this->mdWechatqy->add($addData);
|
|
|
|
|
} else {
|
|
|
|
|
$this->mdWechatqy->update($addData, array('external_userid' => $external_userid));
|
|
|
|
|
}
|
|
|
|
|
print_r('qymsg_addData:' . json_encode($addData, JSON_UNESCAPED_UNICODE));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
print_r('qymsg_get:' . json_encode($re, JSON_UNESCAPED_UNICODE));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|