corpid = $this->corpid; $this->corpsecret = $this->corpsecret; $this->accessToken = $this->setAccessToken($corpid, $corpsecret); } /** * 获取普通token * @param $corpid 企业ID * @param $corpsecret 应用的凭证密钥 * @return mixed */ public function setAccessToken($corpid, $corpsecret) { if (Cache::has($corpid . '_' . $corpsecret)) { return Cache::get($corpid . '_' . $corpsecret); } $url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' . $corpid . '&corpsecret=' . $corpsecret; $res = $this->curl($url); $res = json_decode($res, true); if (isset($res['errcode']) && $res['errcode'] != 0) { trace('access_token获取失败'.json_encode($res), 'error'); return null; } Cache::set($corpid . '_' . $corpsecret, $res['access_token'], '7200'); return $res['access_token']; } /** * 获取企业应用的jsapi_ticket */ public function getAgentJsapiTicket() { $url = 'https://qyapi.weixin.qq.com/cgi-bin/ticket/get?access_token=' . $this->accessToken . '&type=agent_config'; $res = $this->curl($url); $res = json_decode($res, true); if (isset($res['errcode']) && $res['errcode'] != 0) { trace('jsapi_ticket获取失败:'.json_encode($res), 'error'); return null; } Cache::set($this->corpid . '_' . $this->corpsecret . '_agent_jsapi_ticket', $res['ticket']); return $res['ticket']; } /** * 获取企业的jsapi_ticket */ public function getJsapiTicket() { $url = 'https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=' . $this->accessToken; $res = $this->curl($url); $res = json_decode($res, true); if (isset($res['errcode']) && $res['errcode'] != 0) { return null; } Cache::set($this->corpid . '_' . $this->corpsecret . '_jsapi_ticket', $res['ticket']); return $res['ticket']; } /** * 签名 */ public function sign($data) { $str = 'jsapi_ticket='.$data['jsapi_ticket'].'&noncestr='.$data['noncestr'].'×tamp='.$data['timestamp'].'&url='.$data['url']; return sha1($str); } /* * 获取企业微信用户UserId */ public function getUserId($code) { $getUserId = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=".$this->accessToken."&code=".$code; $res = $this->curl($getUserId); $res = json_decode($res, true); if (isset($res['errcode']) && $res['errcode'] != 0) { trace('UserId获取失败'.json_encode($res), 'error'); return null; } if (!isset($res['UserId'])) { trace('非企业成员获取失败'.json_encode($res), 'error'); return null; } return $res['UserId']; } /* * 获取企业微信用户信息 */ public function getUserInfo($UserId) { $getUserInfo = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=".$this->accessToken."&userid=".$UserId; $res = $this->curl($getUserInfo); $res = json_decode($res, true); if (isset($res['errcode']) && $res['errcode'] != 0) { trace('企业微信信息获取失败'.json_encode($res), 'error'); return null; } return $res; } /** * 请求接口 * @param $url * @param null $data * @return mixed|null */ protected function curl($url, $data = null) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-type: application/json; encoding=utf-8']); if (!empty($data)) { curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_HEADER, false); $data = curl_exec($curl); curl_close($curl); return $data; } }