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); } public function init($params) { $configs = array( //狸车新能源_客户画像 'lichene_1000005' => array( 'corpid' => 'wwff2d727ce47d6852', 'corpsecret' => 'QBkmK9hm1WgrWI9RUE5-Al10qVtHCM2eEoa8NQzTKkI', 'agentid' => 1000005 ), //狸车_客户画像 'liche_1000024' => array( 'corpid' => 'wwc2caba960d202087', 'corpsecret' => 'OD30gLu3BHTYnTPDre4z2tuPr3rocMcMpIBLZaZG_0c', 'agentid' => 1000024 ), //异业店_凯利之星_客户画像 'diff_133_1000002' => array( 'corpid' => 'ww1493c3e4fb56ef29', 'corpsecret' => 'W6iKxOYqNLidbN4-7mYUYbiLKaMZmm7UHFF_TfpOk8M', 'agentid' => 1000002 ), ); $params['corpid'] && $this->corpid = $params['corpid']; $params['corpsecret'] && $this->corpsecret = $params['corpsecret']; $params['agentid'] && $this->agentid = $params['agentid']; $app = $params['app'] ? $params['app'] : 'lichene_1000005'; if ($configs[$app]) { $config = $configs[$app]; !$this->corpid && $config['corpid'] && $this->corpid = $config['corpid']; !$this->corpsecret && $config['corpsecret'] && $this->corpsecret = $config['corpsecret']; !$this->agentid && $config['agentid'] && $this->agentid = $config['agentid']; } $this->corpid && $this->log_file = "wx_qyapi_agent_{$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.liche.cn/weixin/qy_agent_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 getConfig() { return ['corpid' => $this->corpid, 'corpsecret' => $this->corpsecret, 'agentid' => $this->agentid]; } public function getSignPackage() { $jsapiTicket = $this->jsapi_ticket(); $url = "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; $timestamp = time(); $nonceStr = $this->createNonceStr(); // 这里参数的顺序要按照 key 值 ASCII 码升序排序 $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; $signature = sha1($string); $signPackage = array( "appId" => $this->corpid, "nonceStr" => $nonceStr, "timestamp" => $timestamp, "url" => $url, "signature" => $signature, "rawString" => $string, "access_token" => $this->access_token(), 'jsapi_ticket' => $jsapiTicket, "agentid" => $this->agentid ); return $signPackage; } public function jsapi_ticket() { $key = 'ticket_' . $this->corpsecret; $ticket = $this->redis->get($key); if (!$ticket) { if ($this->env == 'p') { //获取企业应用jsapi_ticket 的https://developer.work.weixin.qq.com/document/10029#%E8%8E%B7%E5%8F%96%E5%BA%94%E7%94%A8%E7%9A%84jsapi_ticket $access_token = $this->access_token(); $url = "https://qyapi.weixin.qq.com/cgi-bin/ticket/get?access_token={$access_token}&type=agent_config"; $res = $this->ci->mycurl->httpGet($url); $result = json_decode($res); if ($result->errcode) { debug_log('error:__FUNCTION__:' . $res, $this->log_file); } else { $ticket = $result->ticket; $this->redis->save($key, $result->ticket, $result->expires_in); } } else { $url = "https://api.liche.cn/weixin/qy_agent_jsapi_ticket?corpid={$this->corpid}&corpsecret={$this->corpsecret}"; $res = $this->ci->mycurl->httpGet($url); $result = json_decode($res); $ticket = $result->ticket; } } return $ticket; } private function createNonceStr($length = 16) { $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $str = ""; for ($i = 0; $i < $length; $i++) { $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); } return $str; } } ?>