363 lines
18 KiB
PHP
363 lines
18 KiB
PHP
<?php
|
|
ini_set('display_errors', 'on');
|
|
|
|
/**
|
|
* Notes:异业店企微
|
|
* Created on: 2022/6/28 16:56
|
|
* Created by: dengbw
|
|
*/
|
|
Class Different extends HD_Controller
|
|
{
|
|
private $bid;
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$bid = $this->input->get('bid') ? $this->input->get('bid') : $this->input->post('bid');
|
|
$this->bid = intval($bid);
|
|
if ($this->bid == 133) {
|
|
$this->load->library('wx_qyapi', ['app' => "diff_{$bid}"]);
|
|
} else {
|
|
echo '参数错误~';
|
|
exit;
|
|
}
|
|
$target_class = lcfirst(get_class($this));//调用类的名称,子类或者当前类名称
|
|
$this->log_file = "wechat_{$target_class}_{$this->bid}.log";
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
echo 'index';
|
|
}
|
|
|
|
/**
|
|
* Notes:
|
|
* https://liche-api-dev.xiaoyu.com/wechat/different/temp?bid=133
|
|
* https://api.liche.cn/wechat/different/temp?bid=133
|
|
* Created on: 2022/3/25 17:37
|
|
* Created by: dengbw
|
|
*/
|
|
public function temp()
|
|
{
|
|
$param = $this->input->get();
|
|
// $re_wx = $this->wx_qyapi->getSignPackage();
|
|
if ($param) {
|
|
$re_wx = $this->wx_qyapi->get_external_contact($param);
|
|
echo json_encode($re_wx, JSON_UNESCAPED_UNICODE);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Notes:企业微信接收事件
|
|
* https://liche-api-dev.xiaoyu.com/wechat/different/get_qymsg?bid=133
|
|
* https://api.liche.cn/wechat/different/get_qymsg?bid=133
|
|
* 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();
|
|
require_once(COMMPATH . '/third_party/WXqy/WXBizMsgCrypt.php');
|
|
$config = $this->wx_qyapi->getConfig();
|
|
$sVerifyMsgSig = $param['msg_signature'];
|
|
$sVerifyTimeStamp = $param['timestamp'];
|
|
$sVerifyNonce = $param['nonce'];
|
|
$sVerifyEchoStr = $param['echostr'];
|
|
// 需要返回的明文
|
|
$sEchoStr = "";
|
|
$wxcpt = new WXBizMsgCrypt($config['token'], $config['encodingAesKey'], $config['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返回
|
|
echo urldecode($sEchoStr);
|
|
} else {
|
|
echo $errCode;
|
|
}
|
|
exit;
|
|
}
|
|
if ($_SERVER['REQUEST_METHOD'] == "POST") {
|
|
$sReqData = file_get_contents("php://input");
|
|
$sMsg = ""; // 解析之后的明文
|
|
$errCode = $wxcpt->DecryptMsg($sVerifyMsgSig, $sVerifyTimeStamp, $sVerifyNonce, $sReqData, $sMsg);
|
|
if ($errCode == 0) {
|
|
$this->load->model('app/app_different_qy_log_model', 'mdWechatqyLog');
|
|
$re_msg = (array)simplexml_load_string($sMsg, 'SimpleXMLElement', LIBXML_NOCDATA);//xml转json
|
|
$qy_user = [];
|
|
if ($re_msg['ChangeType']) {
|
|
$change_type = $this->mdWechatqyLog->change_type($re_msg['ChangeType']);
|
|
$userid = $re_msg['UserID'];
|
|
$external_userid = $re_msg['ExternalUserID'];
|
|
if ($change_type) {//只记录现有的
|
|
if ($userid) {//企业成员
|
|
$re_msg['log_id'] = $this->mdWechatqyLog->add(['brand_id' => $this->bid, 'external_userid' => $external_userid, 'userid' => $userid,
|
|
'change_type' => $re_msg['ChangeType'], 'remark' => $change_type, 'c_time' => $re_msg['CreateTime']]);
|
|
$wxqy = 0;
|
|
if ($re_msg['ChangeType'] == 'add_external_contact') {//新增
|
|
$wxqy = 1;
|
|
} else if ($re_msg['ChangeType'] == 'del_external_contact' || $re_msg['ChangeType'] == 'del_follow_user') {//删除
|
|
$wxqy = -1;
|
|
}
|
|
if ($wxqy && $external_userid) {//查找关连企微客户
|
|
$this->load->model('app/licheb/App_licheb_users_model', 'mdUsers');
|
|
$re_use = $this->mdUsers->get(['userid' => $userid]);
|
|
$biz_id = $re_use ? intval($re_use['biz_id']) : 0;
|
|
if ($biz_id) {
|
|
$re_wx = $this->wx_qyapi->get_external_contact(array('url' => 'get', 'external_userid' => $external_userid));
|
|
if ($re_wx['errcode'] == 0) {
|
|
$qy_user['external_contact'] = $re_wx['external_contact'];
|
|
$remark_mobiles = '';
|
|
foreach ($re_wx['follow_user'] as $key => $val) {
|
|
if ($val['userid'] == $userid) {//找出当前企业员工
|
|
$remark_mobiles = $val['remark_mobiles'];//用户手机
|
|
$qy_user['follow_info'] = $val;
|
|
break;
|
|
}
|
|
}
|
|
if ($remark_mobiles && $remark_mobiles != '[]' && is_array($remark_mobiles)) {
|
|
$mobile = $remark_mobiles[0];//取第一个手机号
|
|
if ($mobile) {//关连客户企微状态
|
|
$this->load->model('receiver/receiver_customers_model', 'mdCustomers');
|
|
$ret = $this->mdCustomers->update(['wxqy' => $wxqy], ['mobile' => $mobile, 'biz_id' => $biz_id]);
|
|
$ret && $re_msg['wxqy'] = $wxqy . '_' . $mobile . '_' . $biz_id;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if ($re_msg['ChangeType'] == 'add_external_contact') {//添加企业客户
|
|
if (!$qy_user) {
|
|
$re_wx = $this->wx_qyapi->get_external_contact(array('url' => 'get', 'external_userid' => $external_userid));
|
|
if ($re_wx['errcode'] == 0) {
|
|
$qy_user['external_contact'] = $re_wx['external_contact'];
|
|
foreach ($re_wx['follow_user'] as $key => $val) {
|
|
if ($val['userid'] == $userid) {//找出当前企业员工
|
|
$qy_user['follow_info'] = $val;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if ($qy_user) {
|
|
$this->load->model('app/app_different_qy_model', 'mdWechatqy');
|
|
$this->set_wechatqy(['userid' => $userid, 'external_userid' => $external_userid], $qy_user);
|
|
}
|
|
}
|
|
}
|
|
debug_log('qymsg_sMsg:' . json_encode($re_msg, JSON_UNESCAPED_UNICODE), $this->log_file);
|
|
exit;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Notes:企业微信接收事件(测试用)
|
|
* https://liche-api-dev.xiaoyu.com/wechat/different/get_qy?bid=133
|
|
* https://api.liche.cn/wechat/different/get_qy?bid=133
|
|
* 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_qy()
|
|
{
|
|
$param = $this->input->get();
|
|
require_once(COMMPATH . '/third_party/WXqy/WXBizMsgCrypt.php');
|
|
$sVerifyMsgSig = $param['msg_signature'];
|
|
$sVerifyTimeStamp = $param['timestamp'];
|
|
$sVerifyNonce = $param['nonce'];
|
|
$sVerifyEchoStr = $param['echostr'];
|
|
$token = 'y1U8zJBQWsHC';
|
|
$encodingAesKey = 'sA52I1PPw4ahdWrJjOKRlDtyyKUMBasTGdl5aCqszpi';
|
|
$corpid = 'ww0fb634daed56031e';
|
|
// 需要返回的明文
|
|
$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返回
|
|
echo urldecode($sEchoStr);
|
|
} else {
|
|
echo $errCode;
|
|
}
|
|
exit;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Notes:临时更新用户客户id
|
|
* Created on: 2021/03/01 17:02
|
|
* Created by: dengbw
|
|
* https://liche-api-dev.xiaoyu.com/wechat/different/tem_external_contact
|
|
* https://api.liche.cn/wechat/different/tem_external_contact
|
|
*/
|
|
public function tem_external_contact()
|
|
{
|
|
$this->load->model('app/app_different_qy_model', 'mdWechatqy');
|
|
$this->load->library('wx_qyapi', ['app' => 'different']);
|
|
echo 'access_token=' . $this->wx_qyapi->access_token() . '<br>';
|
|
$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;
|
|
//读取成员
|
|
$re = $this->wx_qyapi->get_external_contact(array('url' => 'user_get', 'userid' => 'DengBingWen'));
|
|
echo json_encode($re, JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
//获取客户详情
|
|
// $re = $this->wx_qyapi->get_external_contact(array('url' => 'get', 'external_userid' => 'wm2x4YKwAAqdNIMj9eOt1LXroR6cn-NQ'));
|
|
// 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 = ["LiLiXiang", "ChenZongHui", "FangYanPing", "JiangYaLi", "DianCheDaRen", "LinFan", "ChenHuiCan01", "13507568055"
|
|
, "LiuXinXin_2", "caohui", "HuJianPing", "LiuChaHua", "GuoYangYang", "ZhengYongWei", "WangWei", "LiJianJie", "YuWei", "ChenYuYou"
|
|
, "ZengJiaMin", "ZengLingYan", "15859720661", "LiHuaiSong", "LaiDaoAi", "18605086929", "DuXiaoXia", "ChenHuiMin", "WuHaiLin"
|
|
, "LinGuoPu", "WeiJiaXing", "WangZhiJie", "ZhaoZhenRong", "SuYaQin", "LiTingTing", "LiFengCan", "ChenYingMing", "huanghong"
|
|
, "wangwenbin", "FuDeWang", "lxt", "DongPingPing", "LiJiaQunGongYongShouJi", "LiMeiJin", "lanpeifang", "linxiaolong", "hekaihong"
|
|
, "ZhangQingYuan", "LiJiaQun", "ChenLiXin", "ZhangShanBang", "WuHuanZheng", "XuHang", "HuangJian", "YuZhen", "18750080006"
|
|
, "13859842421", "YouYongHuang", "ShaoYunFei", "ChenYingQin", "LuoHuaiJie", "WengQiZhang", "LiuShengHua", "LiuHaiQuan"
|
|
, "DengBingWen", "DongKaiJie", "jimmy", "HuangJunQiang", "LaiLianYing", "ZhangWei", "WuDianZi", "XuGuangYi", "ZhouJinBin"
|
|
, "ShaoBinHua", "FuYangYi", "LiZhengHui", "YeJiaChen", "YangYaQi", "ChenMei", "DianCheLiKe", "ChenCuiYun", "YuTing", "ZhangTengFei"
|
|
, "no.39", "ZhengYun", "HuangChenChen"];
|
|
$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/different/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/different/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'];
|
|
$this->set_wechatqy(['userid' => $userid, 'external_userid' => $external_userid], $value);
|
|
}
|
|
echo '<br>next_cursor=' . $next_cursor . '&external_contact_list=' . count($re['external_contact_list']);
|
|
}
|
|
return $next_cursor;
|
|
}
|
|
|
|
/**
|
|
* Notes:新增/更新企微用户
|
|
* Created on: 2022/5/31 9:54
|
|
* Created by: dengbw
|
|
* @param array $param
|
|
* @param array $user
|
|
*/
|
|
private function set_wechatqy($param = [], $user = [])
|
|
{
|
|
if (!$param['userid'] || !$param['external_userid'] || !$user) {
|
|
return;
|
|
}
|
|
$jsondata['external_contact'] = $user['external_contact'] ? $user['external_contact'] : '';
|
|
$jsondata['follow_info'] = $user['follow_info'] ? $user['follow_info'] : '';
|
|
$remark_mobiles = $user['follow_info']['remark_mobiles'];
|
|
$mobile = '';
|
|
if ($remark_mobiles && $remark_mobiles != '[]' && is_array($remark_mobiles)) {
|
|
$mobile = $remark_mobiles[0];//取第一个手机号
|
|
}
|
|
$addData = array(
|
|
'brand_id' => $this->bid,
|
|
'external_userid' => $user['external_contact']['external_userid'],
|
|
'userid' => $user['follow_info']['userid'] ? $user['follow_info']['userid'] : '',
|
|
'name' => $user['external_contact']['name'],
|
|
'avatar' => $user['external_contact']['avatar'],
|
|
'unionid' => $user['external_contact']['unionid'] ? $user['external_contact']['unionid'] : '',
|
|
'remark' => $user['follow_info']['remark'] ? $user['follow_info']['remark'] : '',
|
|
'mobile' => $mobile,
|
|
'c_time' => $user['follow_info']['createtime'] ? $user['follow_info']['createtime'] : time(),
|
|
'jsondata' => json_encode($jsondata, JSON_UNESCAPED_UNICODE),
|
|
'status' => 1,
|
|
);
|
|
$re_qy = $this->mdWechatqy->get(['brand_id' => $this->bid, 'userid' => $param['userid'], 'external_userid' => $param['external_userid']]);
|
|
if (!$re_qy) {
|
|
$this->mdWechatqy->add($addData);
|
|
} else {
|
|
$this->mdWechatqy->update($addData, ['id' => $re_qy['id']]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Notes:添加企业客户事件
|
|
* https://liche-api-dev.xiaoyu.com/wechat/different/add_wechatqy?bid=133
|
|
* https://api.liche.cn/wechat/different/add_wechatqy?bid=133
|
|
* 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_different_qy_model', 'mdWechatqy');
|
|
$this->load->library('wx_qyapi', ['app' => 'different']);
|
|
$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(
|
|
'brand_id' => $this->bid,
|
|
'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));
|
|
}
|
|
|
|
} |