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); } function init($params) { $configs = array( //狸车 'liche' => array( 'corpid' => 'wwc2caba960d202087',//企业ID 'corpsecret' => 'aQ2yhOBTXZnM0iwFtBzYIWbtyq4wFaIXBYTKg3xFxas',//企业密钥 R9fj_ocdb5p4Vr_qaQY54A4Z5SbyZBRWk4nfTMiPxgo ), //星选家 'xxj' => array( 'corpid' => 'wwecac3c2b60c31b1b',//企业ID 'corpsecret' => 'EFIev4j-0iv4Uy2vrDATzsL3aIW2IT_kRO4zx73I31g',//企业密钥 R9fj_ocdb5p4Vr_qaQY54A4Z5SbyZBRWk4nfTMiPxgo ), //福建小鱼 'fjxy' => array( 'corpid' => 'wweeb9c2f4fbb2227b',//企业ID 'corpsecret' => 'SWgWz7TAyLWm6SKvI3uPZ5c5-ZjS0GIxS7kf92rhIwA',//企业密钥 'coragent' => array('1000012'),//企业应用:号码隐藏功能 ), ); $params['corpid'] && $this->corpid = $params['corpid']; $params['corpsecret'] && $this->corpsecret = $params['corpsecret']; $app = $params['app'] ? $params['app'] : 'liche'; if ($configs[$app]) { $config = $configs[$app]; !$this->corpid && $config['corpid'] && $this->corpid = $config['corpid']; !$this->corpsecret && $config['corpsecret'] && $this->corpsecret = $config['corpsecret']; } $this->corpid && $this->log_file = "wx_qyapi_{$this->corpid}.log"; } 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.haodian.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}"; $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'] == 'mark_tag') {//编辑客户企业标签 https://work.weixin.qq.com/api/doc/90001/90143/92697 $url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/mark_tag?access_token={$access_token}"; $res = $this->ci->mycurl->httpPost($url, array('userid' => $param['userid'], 'external_userid' => $param['external_userid'] , 'add_tag' => $param['add_tag']), 'is_json'); } else if ($param['url'] == 'add_corp_tag') {//添加企业客户标签 https://work.weixin.qq.com/api/doc/90001/90143/92696#添加企业客户标签 $url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/add_corp_tag?access_token={$access_token}&debug=1"; $res = $this->ci->mycurl->httpPost($url, array('group_id' => $param['group_id'], 'tag' => array(array('name' => $param['name']))), 'is_json'); } else if ($param['url'] == 'remark') {//修改客户备注信息 https://work.weixin.qq.com/api/doc/90001/90143/92694 $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'] , 'description' => $param['description']), 'is_json'); } $data = trim($res, chr(239) . chr(187) . chr(191)); $result = json_decode($data, true); return $result; } } ?>