diff --git a/api/controllers/Weixin.php b/api/controllers/Weixin.php new file mode 100644 index 00000000..ae717605 --- /dev/null +++ b/api/controllers/Weixin.php @@ -0,0 +1,86 @@ +app_id = $this->input->get('app_id'); + $this->reset = $this->input->get('reset'); + $this->appid = $this->input->get('appid'); + $this->secret = $this->input->get('secret'); + $this->corpid = $this->input->get('corpid'); + $this->corpsecret = $this->input->get('corpsecret'); + + $wx_configs = array( + //小鱼冒泡 + "13.1" => array('appid' => "wx13bb4ff11493bc7c", "secret" => "40d6032dece9aba7f9c2faa482abd827"), + ); + + $wx_config = $wx_configs[$this->app_id]; + if($wx_config){ + !$this->appid && $this->appid = $wx_config['appid']; + !$this->secret && $this->secret = $wx_config['secret']; + } + + $param = array('appid' => $this->appid, "secret" => $this->secret); + $this->load->library("hd_wechat", $param); + } + + /** + * 获取access_token + */ + function access_token(){ + $access_token = $this->hd_wechat->access_token($this->reset); + + $data = array("access_token" => $access_token); + + echo json_encode($data, JSON_UNESCAPED_UNICODE); + + return; + } + + /** + * 获取分享凭证 + */ + function api_ticket(){ + $api_ticket = $this->hd_wechat->api_ticket($this->reset); + + $data = array("api_ticket" => $api_ticket); + + echo json_encode($data, JSON_UNESCAPED_UNICODE); + return; + } + /* + * 企业微信access_token + */ + public function qy_access_token(){ + $app = $this->input->get('app'); + $corpid = $this->input->get('corpid'); + $corpsecret = $this->input->get('corpsecret'); + + $params = array(); + $app && $params['app'] = $app; + $corpid && $params['corpid'] = $corpid; + $corpsecret && $params['corpsecret'] = $corpsecret; + + $this->load->library('wx_qyapi', $params); + $access_token = $this->wx_qyapi->access_token(); + + $data['access_token'] = $access_token; + echo json_encode($data, JSON_UNESCAPED_UNICODE); + return; + } +} diff --git a/api/libraries/Hdwechat.php b/api/libraries/Hdwechat.php index 22ca8e24..657628e8 100755 --- a/api/libraries/Hdwechat.php +++ b/api/libraries/Hdwechat.php @@ -97,11 +97,11 @@ class Hdwechat } $access_token = $this->access_token(); + var_dump($access_token);exit; if (!$access_token) { debug_log("[error] " . __FUNCTION__ . ": not access_token", $this->log_file); return array(); } - $pre_url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='; $url = $pre_url . $access_token; $data = array( diff --git a/api/libraries/Hdwechat1.php b/api/libraries/Hdwechat1.php deleted file mode 100755 index 5d760f47..00000000 --- a/api/libraries/Hdwechat1.php +++ /dev/null @@ -1,226 +0,0 @@ -init($config); - } - - /** - * @param $config ('appid', 'secret') - */ - function init($config){ - $this->appid = $config['appid']; - $this->secret = $config['secret']; - $this->token_url = $config['token_url']; - - $CI = & get_instance(); - $CI->load->library("hd_wechat", $config); - $this->hd_wechat = new Hd_wechat($config); - } - - /** - * 获取或者重置access_token - * @param $reset (是否重置) - * @return mixed - */ - function access_token($reset = false){ - $this->access_token = $this->hd_wechat->access_token($reset); - - return $this->access_token; - } - - - /** - * 生成二维码 - * @param $filename (文件名称) - * @param $scene (最大32个可见字符,只支持数字,大小写英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~,其它字符请自行编码为合法字符(因不支持%,中文无法使用 urlencode 处理,请使用其他编码方式) - * @param $page (必须是已经发布的小程序存在的页面(否则报错),例如 pages/index/index, 根路径前不要填加 /,不能携带参数(参数请放在scene字段里),如果不填写这个字段,默认跳主页面) - * @param $width (二维码的宽度,单位 px,最小 280px,最大 1280px,默认430px) - * @return array {'file':'文件路径', 'url':'访问相对路径'} - */ - function wxacode($filename, $scene, $page, $width){ - $file = APPPATH . '../www/home/wx/wxacode/' . $filename . '.png'; - $dir = substr($file, 0, strrpos($file, '/')); - if(!is_dir($dir)){ - $ret = mkdir($dir, 0777, true);// 如果文件夹不存在,将以递归方式创建该文件夹 - if(!$ret){ - debug_log("[error] ". __FUNCTION__ . ": mkdir {$ret}, filename:{$filename}, scene:{$scene}, page:{$page}, width:{$width}", $this->log_file); - return array(); - } - } - - if(file_exists($file)){ - return array('file' => $file, 'url' => 'wx/wxacode/' . $filename . '.png'); - } - - $pre_url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='; - $url = $pre_url . $this->access_token(); - $data = array( - 'scene' => $scene, - 'page' => $page, - 'width' => $width ? $width : 430, - ); - - list($code, $res) = $this->curl_post($url, $data); - $ret = json_decode($res, true); - - if(isset($ret['errcode']) && 40001 == $ret['errcode']){//token过期,重置后请求 - $url = $pre_url . $this->access_token(true); - list($code, $res) = $this->curl_post($url, $data); - $ret = json_decode($res, true); - } - - if(isset($ret['errcode'])){ - debug_log("[error] ". __FUNCTION__ . ": httpcode:{$code}, response:{$res}, filename:{$filename}, scene:{$scene}, page:{$page}, width:{$width}", $this->log_file); - return array(); - } - - $ret = file_put_contents($file, $res); - if(false === $ret){ - debug_log("[error] ". __FUNCTION__ . ": file_put_contents {$ret}, filename:{$filename}, scene:{$scene}, page:{$page}, width:{$width}", $this->log_file); - return array(); - } - - return array('file' => $file, 'url' => 'wx/' . $filename . '.png'); - } - - function qrcode($filename, $scene, $type = 0, $expire = 0){ - $file = APPPATH . '../www/api/wx/qrcode/' . $filename . '.png'; - $file_url = 'wx/qrcode/' . $filename . '.png'; - $dir = substr($file, 0, strrpos($file, '/')); - if(!is_dir($dir)){ - $ret = mkdir($dir, 0777, true);// 如果文件夹不存在,将以递归方式创建该文件夹 - if(!$ret){ - debug_log("[error] ". __FUNCTION__ . ": mkdir {$ret}, filename:{$filename}, scene:{$scene}", $this->log_file); - return array(); - } - } - - if(file_exists($file)){ - return array('file' => $file, 'url' => $file_url); - } - - if(is_numeric($scene)){ - $action_info = array( - 'scene' => array('scene_id' => $scene) - ); - $action_name = $type ? 'QR_LIMIT_SCENE' : 'QR_SCENE'; - } else { - $action_info = array( - 'scene' => array('scene_str' => $scene) - ); - $action_name = $type ? 'QR_LIMIT_STR_SCENE' : 'QR_STR_SCENE'; - } - - $data = array( - 'action_name' => $action_name, - 'action_info' => $action_info - ); - $expire && $data['expire_seconds'] = $expire; - - $qr_url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='; - $url = $qr_url . $this->access_token(); - - //请求获取二维码的ticket - list($code, $res) = $this->curl_post($url, $data); - $ret = json_decode($res, true); - - if(isset($ret['errcode']) && 40001 == $ret['errcode']){//token过期,重置后请求 - $url = $qr_url . $this->access_token(true); - list($code, $res) = $this->curl_post($url, $data); - $ret = json_decode($res, true); - } - - if(isset($ret['errcode'])){ - debug_log("[error] ". __FUNCTION__ . ": httpcode:{$code}, response:{$res}, filename:{$filename}, scene:{$scene}", $this->log_file); - return array(); - } - - if(!$ret['ticket']){ - debug_log("[error] ". __FUNCTION__ . ": not ticket, filename:{$filename}, scene:{$scene}", $this->log_file); - return array(); - } - - //根据ticket获取 - $ticket_url='https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.$ret['ticket']; - - debug_log("[info] ". __FUNCTION__ . ": ticket_url=$ticket_url", $this->log_file); - - $ret = file_put_contents($file, $this->url_get($ticket_url)); - if(false === $ret){ - debug_log("[error] ". __FUNCTION__ . ": file_put_contents {$ret}, filename:{$filename}, scene:{$scene}", $this->log_file); - return array(); - } - - return array('file' => $file, 'url' => $file_url); - } - - /** - * 从其他接口获取token,保证获取token不影响其他平台 - * @param $url - * @return string - */ - private function get_self_token($url){ - $res = trim($this->url_get($url)); - - debug_log("[info] ". __FUNCTION__ . ":url={$url}, res={$res}", $this->log_file); - - return $res; - } - - /** - * @param $url - * @param $data - * @return array - */ - private function curl_post($url, $data){ - debug_log("[info] ". __FUNCTION__ . ":url={$url}, data=".json_encode($data), $this->log_file); - - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_POST, 1); - curl_setopt($ch, CURLOPT_HEADER, 0); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); - $res = curl_exec($ch); - $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); - curl_close($ch); - - $size = strlen($res); - if($size > 5000){ - debug_log("[info] ". __FUNCTION__ . ":httpcode={$code}, response is big only show size={$size}", $this->log_file); - } else { - debug_log("[info] ". __FUNCTION__ . ":httpcode={$code}, response={$res}", $this->log_file); - } - - return array($code, $res); - } - - /** - * @param $url - * @return string - */ - private function url_get($url){ - $res = file_get_contents($url); - debug_log("[info] ". __FUNCTION__ . ":url={$url}, res={$res}", $this->log_file); - return $res; - } -} \ No newline at end of file diff --git a/common/libraries/Hd_wechat.php b/common/libraries/Hd_wechat.php index 455f389a..9953e792 100755 --- a/common/libraries/Hd_wechat.php +++ b/common/libraries/Hd_wechat.php @@ -24,14 +24,14 @@ class Hd_wechat //获取环境 if (false !== strpos($_SERVER['HTTP_HOST'], 'dev')) { //dev 测试 $this->env = 'd'; - } elseif (false !== strpos($_SERVER['HTTP_HOST'], 'test')) {//test 测试 + } elseif (false !== strpos($_SERVER['HTTP_HOST'], 'api.lc.haodian.cn')) {//test 测试 $this->env = 't'; } else { // 正式 $this->env = 'p'; } if ('p' != $this->env) { - $this->url_wechat = "https://api.haodian.cn/weixin"; + $this->url_wechat = "https://api.liche.cn/weixin"; } $this->redis = &load_cache('redis'); @@ -198,4 +198,4 @@ class Hd_wechat return $res; } -} \ No newline at end of file +}