<?php
class ControllerAccountQrcode extends Controller {
public function getWechatQrcode() {
if (!$this->customer->isLogged()) {
$this->session->data['redirect'] = $this->url->link('account/account', '', 'SSL');
$this->response->redirect($this->url->link('account/login', '', 'SSL'));
}
$this->load->model('account/customer');
$affiliate_id=$this->model_account_customer->getAffiliateIdByCustomerId();
if(!$affiliate_id){
echo "用户未注册成为代理商,无法生成推广二维码";die;
}
$qrcodeImg=$this->model_account_customer->getQrodeImg($affiliate_id);
if(!empty($qrcodeImg)){
$data['qrcode']=HTTP_SERVER.$qrcodeImg;
}else{
$appid="***********";
$secret="****************************";
$access_token="";
$token_access_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $secret;
$res = $this->curl_get($token_access_url);
$access_token=$res['access_token'];
$url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$access_token;
$scene = array(
"scene_id" => $affiliate_id
);
$action_info = array(
"scene" => $scene
);
$post_data = array(
"action_name" => "QR_LIMIT_SCENE",
"action_info" => $action_info
);
$post_data=json_encode($post_data);
$ticket=$this->curl_post($url,$post_data);
if(isset($ticket['ticket'])){
$url="https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".$ticket['ticket'];
$qrcode_img=$this->DownLoadQr($url,time());
if($qrcode_img){
$this->model_account_customer->addQrodeImg($affiliate_id, $qrcode_img);
}
$data['qrcode']=HTTP_SERVER.$qrcode_img;
}
}
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/account/qrcode.tpl')) {
$this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/account/qrcode.tpl', $data));
} else {
$this->response->setOutput($this->load->view('default/template/account/qrcode.tpl', $data));
}
}
function curl_get($url){
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$res = curl_exec($ch);
if(curl_errno($ch)){
var_dump(curl_error($ch));
}
$resArr = json_decode($res,1);
curl_close($ch);
return $resArr;
}
function curl_post($url, $post_data){
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($ch);
if(curl_errno($ch)){
var_dump(curl_error($ch));
}
$resArr = json_decode($res,1);
curl_close($ch);
return $resArr;
}
function DownLoadQr($url,$filestring){
if($url == ""){
return false;
}
$filename = $filestring.'.jpg';
ob_start();
readfile($url);
$img=ob_get_contents();
ob_end_clean();
$size=strlen($img);
$fp2=fopen('./upload/qrcode/'.$filename,"a");
if(fwrite($fp2,$img) === false){
return '';
}
fclose($fp2);
return 'upload/qrcode/'.$filename;
}
}
转载请注明原文地址: https://www.6miu.com/read-36442.html