一、获取微信AccessToken
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$result['appid']."&secret=".$result['secret'];
$access_token = WxBase::curlGet($url);
public static function curlGet($url = '', $options = array())
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
if (!empty($options)) {
curl_setopt_array($ch, $options);
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$data = curl_exec($ch);
curl_close($ch);
return json_decode($data,true);
}
二、获取微信小程序二维码
$page = [
'scene' => 'acode=1',
'page' => 'pages/detail/detail',
];
$fileName = 'business/qrcode/'.md5(microtime()).'.png';
$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$access_token;
$res = WxBase::curlPost($url, json_encode($page));
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
$result = $ossClient->putObject($bucket, $fileName, $res);
三、前端打包下载
var content = {
title: '二维码',
data: [
{
name: '图片名称',
url: '图片完整路径'
}
]
};
<templa
te>
<div></div>
</template>
<script>
import JSZip from 'jszip';
import FileSaver from "file-saver";
export default {
name: "oss-pack-download",
data() {
return {
}
},
methods: {
downloadOss(e){
var blogTitle = e.title;
var zip = new JSZip();
var imgs = zip.folder(blogTitle);
var baseList = [];
var that = this;
for (var i = 0; i < e.data.length; i++) {
let image = new Image();
image.setAttribute('crossOrigin', 'anonymous');
image.onload = function () {
let canvas = document.createElement('canvas');
canvas.width = image.width;
canvas.height = image.height;
let context = canvas.getContext('2d');
context.drawImage(image, 0, 0, image.width, image.height);
let url = canvas.toDataURL();
canvas.toDataURL('image/png');
baseList.push(url.substring(22));
if (baseList.length === e.data.length && baseList.length > 0) {
for (let k = 0; k < baseList.length; k++) {
imgs.file(e.data[k].name + '.png', baseList[k], {base64: true})
}
zip.generateAsync({type: 'blob'}).then(function (content) {
FileSaver.saveAs(content, blogTitle + '.zip');
});
}
};
image.src = e.data[i].url;
}
},
}
}
</script>
参考地址