log_file = "wechat_{$target_class}.log"; } public function index() { echo 'index'; } /** * Notes: * https://liche-api-dev.xiaoyu.com/wechat/lichene/temp * https://api.liche.cn/wechat/lichene/temp * Created on: 2022/3/25 17:37 * Created by: dengbw */ public function temp() { $this->load->library('wx_qyapi', ['app' => 'lichene']); $re_wx = $this->wx_qyapi->getSignPackage(); echo json_encode($re_wx, JSON_UNESCAPED_UNICODE); } /** * 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('
--->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 = ""; //$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 debug_log('qymsg_sMsg:' . json_encode($re_msg, JSON_UNESCAPED_UNICODE), $this->log_file); exit; $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' || $ChangeType == 'edit_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']; $remark_mobiles = $re['follow_user'][0]['remark_mobiles']; $tags = $mobile = ''; $tag_ids = []; if ($tags_ary && $tags_ary != '[]' && is_array($tags_ary)) { $tags = implode(',', array_column($tags_ary, 'tag_name')); $tag_ids = array_column($tags_ary, 'tag_id'); } if ($remark_mobiles && $remark_mobiles != '[]' && is_array($remark_mobiles)) { $mobile = $remark_mobiles[0];//取第一个手机号 } $addData = array( 'external_userid' => $re['external_contact']['external_userid'], 'name' => $re['external_contact']['name'] ? $re['external_contact']['name'] : '', 'avatar' => $re['external_contact']['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'] : '', 'mobile' => $mobile, '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('id' => $re_qy['id'])); } //debug_log('qymsg_get2:' . json_encode($re, JSON_UNESCAPED_UNICODE), $this->log_file); $this->set_customers(['name' => $addData['name'], 'mobile' => $addData['mobile'], 'unionid' => $addData['unionid'] , 'userid' => $addData['userid'], 'tag' => $tag_ids]); //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 == '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'])); } } } 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 == 'delete') { debug_log('qymsg_sMsg:' . json_encode($re_msg, JSON_UNESCAPED_UNICODE), $this->log_file); } else if ($ChangeType == 'shuffle') { debug_log('qymsg_sMsg:' . json_encode($re_msg, JSON_UNESCAPED_UNICODE), $this->log_file); } //debug_log('qymsg_sMsg:' . json_encode($re_msg, JSON_UNESCAPED_UNICODE), $this->log_file); } } } /** * Notes:新增/修改门店客户 * Created on: 2022/3/25 10:06 * Created by: dengbw * @param $param */ private function set_customers($param) { $this->load->model('receiver/receiver_customers_model', 'mdCustomers'); $this->load->model('receiver/receiver_customer_tag_model', 'mdCustomerTag'); $this->load->model('receiver/receiver_customer_tagdata_model', 'mdCustomerTagdata'); $re_cus = $this->mdCustomers->get(['unionid' => $param['unionid'], 'status<>' => -1]); if ($re_cus) {//修改 $c_id = $re_cus['id']; if ($param['mobile']) { if (!$re_cus['mobile']) {//更新手机 $re_cus2 = $this->mdCustomers->get(['biz_id' => $re_cus['biz_id'], 'mobile' => $param['mobile'], 'status<>' => -1]); if ($re_cus2) {//合并用户 $this->mdCustomers->update(['status' => -1], ['id' => $re_cus['id']]);//删除原有数据 $c_id = $re_cus2['id']; $this->mdCustomers->update(['unionid' => $param['unionid']], ['id' => $re_cus2['id']]);//合并到现有手机 } else { $this->mdCustomers->update(['mobile' => $param['mobile']], ['id' => $re_cus['id']]);//更新手机 } } else if ($re_cus['mobile'] != $param['mobile']) {//更改手机了 暂不处理 } } } else { $biz_id = $city_id = $county_id = 0; if ($param['userid']) {//读取成员 $this->load->library('wx_qyapi', ['app' => 'lichene']); $re_wx = $this->wx_qyapi->get_external_contact(array('url' => 'user_get', 'userid' => $param['userid'])); if ($re_wx['errcode'] == 0 && $re_wx['mobile']) { $this->load->model('app/licheb/App_licheb_users_model', 'mdUsers'); $this->load->model("biz/biz_model", 'mdBiz'); $re_u = $this->mdUsers->get(array('mobile' => $re_wx['mobile'])); if ($re_u['biz_id']) { $biz_id = $re_u['biz_id']; $re_biz = $this->mdBiz->get(['id' => $re_u['biz_id'], 'status' => 1]); if ($re_biz) { $city_id = $re_biz['city_id']; $county_id = $re_biz['county_id']; } } } } $add_data = [ 'name' => $param['name'], 'mobile' => $param['mobile'], 'biz_id' => $biz_id, 'city_id' => $city_id, 'county_id' => $county_id, 'unionid' => $param['unionid'], 'cf_title' => '自有资源', 'p_time' => date('Y-m-d H:i:s'), 'c_time' => time() ]; if (!$param['mobile']) { $c_id = $this->mdCustomers->add($add_data); } else { $re_cus2 = $this->mdCustomers->get(['biz_id' => $biz_id, 'mobile' => $add_data['mobile'], 'status<>' => -1]); if ($re_cus2) {//合并用户 $c_id = $re_cus2['id']; $this->mdCustomers->update(['unionid' => $param['unionid']], ['id' => $re_cus2['id']]);//合并到现有手机 } else { $c_id = $this->mdCustomers->add($add_data); } } } if ($param['tag'] && count($param['tag'])) {//加标签 //查找已加入标签 $res_td = $this->mdCustomerTagdata->select(['c_id' => $c_id], 'id desc', 0, 0, 't_id'); $tag_data = $res_td ? array_unique(array_column($res_td, 't_id')) : ''; $add_tag = $tag_data_new = []; //选择现有标签 foreach ($param['tag'] as $key => $val) { $re_tag = $this->mdCustomerTag->get(['qy_id' => $val, 'status' => 1], 'id'); if ($re_tag) { $tag_data_new[] = $re_tag['id']; if (!$tag_data || !in_array($re_tag['id'], $tag_data)) {//未加标签,新增 $add_tag[] = ['c_id' => $c_id, 't_id' => $re_tag['id'], 'c_time' => time()]; } } } if ($tag_data) {//删除标签 foreach ($tag_data as $key => $val) { if (!in_array($val, $tag_data_new)) {//未选择,删除标签 $this->mdCustomerTagdata->delete(['c_id' => $c_id, 't_id' => $val]); } } } if ($add_tag && count($add_tag)) { $this->mdCustomerTagdata->add_batch($add_tag); } } } /** * Notes:显示客户标签 * Created on: 2022/3/24 15:01 * Created by: dengbw * https://liche-api-dev.xiaoyu.com/wechat/lichene/show_corp_tag?sd=1 * https://api.liche.cn/wechat/lichene/show_corp_tag */ public function show_corp_tag() { $this->load->model('receiver/receiver_customer_tag_model', 'mdCustomerTag'); $param = $this->input->get(); if ($param['sd']) { $where = ['pid' => 0]; $select = 'id,name,qy_id,sort,status'; } else { $where = ["status" => 1, 'pid' => 0]; $select = 'id,name,qy_id,sort'; } $list = []; $res = $this->mdCustomerTag->select($where, "sort desc,id desc", 0, 0, $select); foreach ($res as $key => $value) { if ($param['sd']) { $where2 = ["status" => 1, 'pid' => $value['id']]; } else { $where2 = ['pid' => $value['id']]; } $res_tag = $this->mdCustomerTag->select($where2, "sort desc,id desc", 0, 0, $select); $value['options'] = $res_tag; $list[] = $value; } echo json_encode($list, JSON_UNESCAPED_UNICODE); } /** * Notes:添加客户标签 * Created on: 2022/4/06 15:01 * Created by: dengbw * https://liche-api-dev.xiaoyu.com/wechat/lichene/add_corp_tag?pid=49&name=旅游爱好者&order=50 * https://api.liche.cn/wechat/lichene/add_corp_tag */ public function add_corp_tag() { if (false == strpos($_SERVER['HTTP_HOST'], 'api.liche.cn')) { echo '正试【api.liche.cn】才能修改企业标签'; exit; } $this->load->model('receiver/receiver_customer_tag_model', 'mdCustomerTag'); $this->load->library('wx_qyapi', ['app' => 'lichene']); $param = $this->input->get(); if (!$param['pid']) { echo '无pid'; exit; } if (!$param['name']) { echo '无name'; exit; } $re = $this->mdCustomerTag->get(['id' => $param['pid']]); if (!$re) { echo '客户标签不存在'; exit; } $add_tag[] = ['name' => $param['name'], 'order' => intval($param['order'])]; if (!$re['qy_id']) { $add_corp_tag = ['url' => 'add_corp_tag', 'group_id' => '', 'group_name' => $re['name'] , 'order' => $re['sort'], 'tag' => $add_tag]; } else { $add_corp_tag = ['url' => 'add_corp_tag', 'group_id' => $re['qy_id'], 'tag' => $add_tag]; } $re_yq = $this->wx_qyapi->get_external_contact($add_corp_tag); if ($re_yq['errcode'] == 0) { if (!$re['qy_id'] && $re_yq['tag_group']['group_id']) {//更新企微标签组id $this->mdCustomerTag->update(['qy_id' => $re_yq['tag_group']['group_id']], ['id' => $param['pid']]); } foreach ($re_yq['tag_group']['tag'] as $key => $value) { if ($value['id']) {//更新企微标签id $this->mdCustomerTag->update(['qy_id' => $value['id']], ['pid' => $param['pid'], 'name' => $value['name']]); } } } echo json_encode($re_yq, JSON_UNESCAPED_UNICODE); } /** * Notes:删除客户标签 * Created on: 2022/4/06 15:01 * Created by: dengbw * https://liche-api-dev.xiaoyu.com/wechat/lichene/del_tag?ids=1,2,3 * https://api.liche.cn/wechat/lichene/del_tag */ public function del_tag() { $param = $this->input->get(); if (!$param['ids']) { echo '无ids'; exit; } $this->load->model('receiver/receiver_customer_tag_model', 'mdCustomerTag'); $ret = $this->mdCustomerTag->delete(['status' => -1, "id in({$param['ids']})" => null]); print_r($ret); } /** * Notes:批量添加企业客户标签 * Created on: 2022/3/24 15:01 * Created by: dengbw * https://liche-api-dev.xiaoyu.com/wechat/lichene/add_corp_tags * https://api.liche.cn/wechat/lichene/add_corp_tags */ public function add_corp_tags() { $this->load->model('receiver/receiver_customer_tag_model', 'mdCustomerTag'); $this->load->library('wx_qyapi', ['app' => 'lichene']); $res = $this->mdCustomerTag->select(["status" => 1, 'pid' => 0], "sort desc,id desc", 0, 0); $addTag = []; foreach ($res as $key => $value) { $add_corp_tag = ['url' => 'add_corp_tag', 'group_id' => '', 'group_name' => $value['name'], 'order' => $value['sort']]; $res_tag = $this->mdCustomerTag->select(["status" => 1, 'pid' => $value['id']], "sort desc,id desc", 0, 0, 'name,sort'); $tag = []; foreach ($res_tag as $key2 => $value2) { $tag[] = ['name' => $value2['name'], 'order' => $value2['sort']]; } $add_corp_tag['tag'] = count($tag) > 0 ? $tag : ''; $re_yq = $this->wx_qyapi->get_external_contact($add_corp_tag); if ($re_yq['errcode'] == 0 && $re_yq['tag_group']['group_id']) {//更新企微id $this->mdCustomerTag->update(['qy_id' => $re_yq['tag_group']['group_id']], ['id' => $value['id']]); foreach ($re_yq['tag_group']['tag'] as $key3 => $value3) { if ($value3['id']) {//更新企微id $this->mdCustomerTag->update(['qy_id' => $value3['id']], ['pid' => $value['id'], 'name' => $value3['name']]); } } $addTag[] = $re_yq; } } echo json_encode($addTag, JSON_UNESCAPED_UNICODE); } /** * 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); //读取成员 $re = $this->wx_qyapi->get_external_contact(array('url' => 'user_get', 'userid' => 'DengBingWen')); 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)); $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 '
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)); } }