log_file = "wechat_{$target_class}.log"; } public function index() { echo 'index'; } /** * Notes:企业微信接收事件 * https://liche-api-dev.xiaoyu.com/wechat/liche/get_qymsg * https://api.liche.cn/wechat/liche/get_qymsg * https://open.work.weixin.qq.com/wwopen/devtool/interface?doc_id=14961 测试回调模式 * Created on: 2021/5/7 17:23 * Created by: dengbw */ public function get_qymsg() { $param = $this->input->get(); debug_log('
--->qymsg_param:' . json_encode($param), $this->log_file); require_once(COMMPATH . '/third_party/WXqy/WXBizMsgCrypt.php'); $encodingAesKey = "VISnPl8WvttxHkAhrENTnADEjeWBc7cvU8qiCj9jH4S"; $token = "PMJbt5kIoE7LTf0kWkwTf"; $corpId = "wwc2caba960d202087"; $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 = ""; //$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_wechatqy_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', array()); 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, 'app_id' => self::$app_id)); $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( 'app_id' => self::$app_id, '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)); } //修改企业微信审请通过 $this->load->service("app/user_service", array("app_id" => self::$app_id)); $re_u = $this->user_service->get(array('unionid' => $addData['unionid'])); if ($re_u['id']) { $this->user_service->update(array('wxqy' => 1), array('id' => $re_u['id'])); //添加企业微信加分 $this->load->library('bobing/bo'); $this->bo->uid = $re_u['id']; $result = $this->bo->wxqy_car_gold(); debug_log('wxqy:' . json_encode($result, JSON_UNESCAPED_UNICODE), $this->log_file); } //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, 'app_id' => self::$app_id)); if ($re_qy) { $this->mdWechatqy->update(array('status' => -1), array('id' => $re_qy['id'])); $this->load->service("app/user_service", array("app_id" => self::$app_id)); $re_u = $this->user_service->get(array('unionid' => $re_qy['unionid'])); if ($re_u['id']) { $this->user_service->update(array('wxqy' => -1), array('id' => $re_u['id'])); $this->load->model('bobing/bobing_user_model', 'mdBobingUser'); $this->mdBobingUser->update(array('wxqy' => -1), array('uid' => $re_u['id'], 'app_id' => self::$app_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/liche/tem_external_contact * https://api.liche.cn/wechat/liche/tem_external_contact */ public function tem_external_contact() { $this->load->model('app/app_wechatqy_model', 'mdWechatqy'); $this->load->library('wx_qyapi', array()); $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 '
本次更新企业成员:'; echo '

成功更新 ' . $count . ' 人'; echo '

点击将再次更新企业成员>>>'; 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 '
成功更新企业成员:' . $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, 'app_id' => self::$app_id)); $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( 'app_id' => self::$app_id, '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 '
next_cursor=' . $next_cursor . '&external_contact_list=' . count($re['external_contact_list']); } return $next_cursor; } /** * Notes:添加企业客户事件 * https://hd-api-dev.xiaoyu.com/wechat/xmcard/add_wechatqy * https://api.haodian.cn/wechat/xmcard/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_wechatqy_model', 'mdWechatqy'); $this->load->library('wx_qyapi', array()); $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( 'app_id' => self::$app_id, '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)); } }