wxapp_settings = cmf_get_option('wxapp_settings');
$this->default_wxapp_settings = $this->wxapp_settings['default'];
$this->appId = $this->default_wxapp_settings['app_id'];
$this->appSecret = $this->default_wxapp_settings['app_secret'];
$access_token = \think\Cache::get('access_token');
if (!$access_token) {
$target_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $this->appId . "&secret=" . $this->appSecret;
$res = $this->http_get($target_url);
$res = json_decode($res, true);
if (isset($res['errcode']) && $res['errcode'] != 0) {
$content = '';
switch (!$res || $res['errcode']) {
case 40001:
$content = "AppSecret错误或者AppSecret不属于这个小程序,请开发者确认AppSecret的正确性";
break;
case 40164:
$content = "调用接口的IP地址不在白名单中,请在接口IP白名单中进行设置";
break;
}
$this->log($content);
}
$access_token = $res['access_token'];
$expire = $res['expires_in'] ? intval($res['expires_in']) - 100 : 7200;
\think\Cache::set('access_token', $res['access_token'], $expire);
}
$this->access_token = $access_token;
}
//菊花码,适用于大量生成二维码
public function getWxQrcode($page = 'pages/index/index',$scene = '', $width = 430, $filepath = null)
{
$width = $width ? $width : 430;
$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" . $this->access_token;
$param = json_encode(array(
"scene" => $scene,
"page" => $page,
"width" => $width
));
$res = $this->http_post($url, $param);
//40130 page要发布
if ($filepath) {
if (!file_exists($filepath)) {
mkdirs($filepath);
}
$file = rtrim($filepath, DS) . DS . uniqid() . '.jpg';
$res = file_put_contents($file, $res);
if ($res === false) {
return false;
}
return $file;
} else {
header('content-type:image/jpeg');
die($res);
}
}
/**
* 获取小程序二维码
* @param $page 带参数的小程序跳转链接
* @param $width 二维码宽度
*/
public function getQrcode($page = 'pages/index/index', $width = '150', $filepath = null)
{
$width = $width ? $width : 150;
$url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=" . $this->access_token;
$param = json_encode(array("path" => $page, "width" => $width));
$res = $this->http_post($url, $param);
if ($filepath) {
if (!file_exists($filepath)) {
mkdirs($filepath);
}
$file = rtrim($filepath, DS) . DS . uniqid() . '.jpg';
$res = file_put_contents($file, $res);
if ($res === false) {
return false;
}
return $file;
} else {
header('content-type:image/jpeg');
die($res);
}
}
public function getTemplateList()
{
$api = "https://api.weixin.qq.com/cgi-bin/wxopen/template/list?access_token=" . $this->access_token;
$data['offset'] = 0;
$data['count'] = 10;
$res = $this->http_post($api, $data);
$res = json_decode($res, true);
if ($res['errcode'] == 0) {
return $res['list'];
}
$this->log($res['errmsg']);
return false;
}
/**
* GET 请求
* @param string $url
*/
private function http_get($url)
{
$oCurl = curl_init();
if (stripos($url, "https://") !== FALSE) {
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
}
curl_setopt($oCurl, CURLOPT_URL, $url);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
$sContent = curl_exec($oCurl);
$aStatus = curl_getinfo($oCurl);
curl_close($oCurl);
if (intval($aStatus["http_code"]) == 200) {
return $sContent;
} else {
return false;
}
}
/**
* POST 请求
* @param string $url
* @param array $param
* @param boolean $post_file 是否文件上传
* @return string content
*/
private function http_post($url, $param, $post_file = false)
{
$oCurl = curl_init();
if (stripos($url, "https://") !== FALSE) {
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
}
if (is_string($param) || $post_file) {
$strPOST = $param;
} else {
$aPOST = array();
foreach ($param as $key => $val) {
$aPOST[] = $key . "=" . urlencode($val);
}
$strPOST = join("&", $aPOST);
}
curl_setopt($oCurl, CURLOPT_URL, $url);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($oCurl, CURLOPT_POST, true);
curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
$sContent = curl_exec($oCurl);
$aStatus = curl_getinfo($oCurl);
curl_close($oCurl);
if (intval($aStatus["http_code"]) == 200) {
return $sContent;
} else {
return false;
}
}
protected function log($content, $title = '', $path = '')
{
if (empty($path)) {
$path = $this->logpath;
}
if (!file_exists($path)) {
mkdirs($path);
}
$filepath = $path . '/' . date('Y-m-d', time()) . '.txt';
$str = '';
if (!empty($title)) {
$str .= $title . PHP_EOL;
}
$str .= '收到请求:' . date('Y-m-d H:i:s') . PHP_EOL . json_encode($this->request->param(), JSON_UNESCAPED_UNICODE) . PHP_EOL;
$str .= '日志内容:' . $content . PHP_EOL;
file_put_contents($filepath, $str, FILE_APPEND);
}
}