贴代码记录
where('id',$activeId)->fetch();
log::info(var_export($isfile,true));
if(empty($isfile['limitQrcode'])){
//调用类中的生成方法
$data = xcxCode::createLimitCode($activeId,$mid,$mpId);
// log::info('************************'.var_export($data,true));
//将二维码地址存入表中
if($data){
$sql = "update bi_community_active set limitQrcode = ? where id = ?";
DB()->executeSql($sql,array($data,$activeId));
}else{
trigger_error('生成二维码失败,请重试');
}
}else{
$data = $isfile['limitQrcode'];
}
return $data;
}
public static function createLimitCode($activeId,$mid,$mpId){
self::$app_id = G_XCX_APP_ID;//小程序appid
self::$app_secret = G_XCX_APP_SECRET;//小程序appsecret
//获取access_token 注:同一主体下公众号和小程序不是同一个
$access_token = parent::getToken(self::$app_id, self::$app_secret);
// log::info($access_token);
//接口B
$url="https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$access_token;
$post_data = array();
//小程序发布的页面
$post_data['page'] = 'pages/groupActive/groupActive';
//参数
$post_data['scene'] = "$activeId.'-'.$mid.'-'.$mpId";
$post_data = json_encode($post_data);
$data = self::https_post($url,$post_data);
// file_put_contents("qrcode-'.$activeId.'.png", $data);
//二进制流转换为base64格式
$base64_image ="data:image/jpeg;base64,".base64_encode( $data );
//调用保存到本地的方法
$path = OSSUploader::uploadBase64ImgToLocal($base64_image);
if($path['imageUrl']){
//调用传入阿里云的方法
$imgurl = OSSUploader::uploadLocalImgToOSS($path['imageUrl'],$mid,'m');
}
return $imgurl;
}
public static function https_post($url,$data){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
if (curl_errno($curl)) {
return 'Errno'.curl_error($curl);
}
curl_close($curl);
return $result;
}
//base64图片上传到本地
public static function uploadBase64ImgToLocal($base64Img){
$arr=array("result"=>-1);
if(!$base64Img){
$arr['message']='请上传图片';
return $arr;
}
$img = $base64Img;
$path = UPLOAD_SAVE_PATH.'/tempImg/';
$type_limit = array('jpg','jpeg','png');
if(preg_match('/data:\s*image\/(\w+);base64,/iu',$img,$tmp)){
if(!in_array($tmp[1],$type_limit)){
//error('图片格式不正确,只支持jpg,jpeg,png!');
$arr['message']='图片格式不正确,只支持jpg,jpeg,png!';
return $arr;
}
}else{
$arr['message']='抱歉!上传失败,请重新再试!';
return $arr;
}
$img = str_replace(' ','+',$img);
$img = str_replace($tmp[0], '', $img);
$img = base64_decode($img);
$date = date('ymdHis');
$temp = uniqid();
$imgName=strtoupper($date.substr($temp,0,4)).'.'.$tmp[1];
$file = $path.$imgName;
if(!file_put_contents($file,$img)){
$arr['message']='上传图片失败!';
}else{
$arr['result']=0;
$arr['message']='恭喜您!上传成功!';
$arr['imageUrl'] = $path.$imgName ;
$arr['tempImageUrl'] = $path.$imgName ;
$arr['imgType']=$tmp[1];
}
return $arr;
}