携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第11天,点击查看活动详情
太阳码也叫小程序码,是微信小程序推出的一个二维码,通过扫描可以直接跳转进入到对应的小程序中。相比普通的正方形二维码来讲,它圆圆的形状就像一个太阳,因此得名太阳码。
业务场景
之前写的文章中有一篇是介绍通过分享来推广小程序的,这次的生成太阳码同样也是推广需求,用户可以生成一张自己专属的小程序码,并将小程序码以图片的方式保存到本地,如果有其他用户扫描该小程序码将会绑定。
开发思路
获取小程序的方式有三种,这里我使用的是获取不限制数量的小程序码的接口。文档入口
第一步:获取access_token,获取到token后才可以调用生成小程序码的接口。
第二步:调用微信提供的生成程序码的接口,进行生成,然后将接口返回的图片保存到服务器或者本地。这里注意的是可以将用户参数参入生成程序码接口,这样就可以生成该用户专属的小程序码了。
public static String getWechatQrCode(Long userId, String domain, String rootPath) {
String wxAccessToken = getWxAccessToken();
RestTemplate rest = new RestTemplate();
InputStream inputStream = null;
OutputStream outputStream = null;
String resultPath = "";
/** 请求结果 */
String url = "https://api.weixin.qq.com//wxa/getwxacodeunlimit?access_token=" + wxAccessToken;
try {
Map<String,Object> param = new HashMap<>();
param.put("scene", userId);
param.put("page", "pages/index/index");
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
HttpEntity requestEntity = new HttpEntity(param, headers);
ResponseEntity<byte[]> entity = rest.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]);
System.out.println("调用小程序生成微信永久小程序码URL接口返回结果:" + entity.getBody());
byte[] result = entity.getBody();
inputStream = new ByteArrayInputStream(result);
// 将文件保存到服务器
//先判断文件是否存在
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String fileAdd = sdf.format(new Date());
//获取文件夹路径
File file1 =new File(rootPath+"/"+fileAdd);
//如果文件夹不存在则创建
if(!file1 .exists() && !file1 .isDirectory()){
file1 .mkdir();
}
String fileNam2 = UUID.randomUUID().toString();
String fileTyle = ".png";
String imagePath = rootPath+"/"+fileAdd+"/"+fileNam2+fileTyle;
resultPath = domain+"/upload/"+fileAdd+"/"+fileNam2+fileTyle;
// File file = new File("E:/1.png");
File file = new File(imagePath);
if (!file.exists()){
file.createNewFile();
}
outputStream = new FileOutputStream(file);
int len = 0;
byte[] buf = new byte[1024];
while ((len = inputStream.read(buf, 0, 1024)) != -1) {
outputStream.write(buf, 0, len);
}
outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
}finally {
if(inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(outputStream != null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return resultPath;
}
复制代码
保存图片
通过上面的接口已经把小程序码的图片获取到了,这时候用户可以在小程序里点击保存,将二维码图片保存到本地相册。使用到了uniapp的uni.downloadFile,不过这里如果没有授权访问相册的话,首次保存是需要进行用户授权的。
<u-button type="warning" :custom-style="btnStyle" shape="circle" @click="saveEwm">保存邀请码</u-button>
saveEwm(){
//获取相册授权
uni.getSetting({
success(res) {
if (!res.authSetting['scope.writePhotosAlbum']) {
uni.authorize({
scope: 'scope.writePhotosAlbum',
success() {
//这里是用户同意授权后的回调
_self.saveImgToLocal();
},
fail() {//这里是用户拒绝授权后的回调
_self.openSettingBtnHidden=false
}
})
} else {//用户已经授权过了
_self.saveImgToLocal();
}
}
})
},
saveImgToLocal(){
uni.showModal({
title: '提示',
content: '确定保存到相册吗',
success: function (res) {
if (res.confirm) {
uni.downloadFile({
url: _self.qrPath,//图片地址
success: (res) =>{
if (res.statusCode === 200){
uni.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success: function() {
uni.showToast({
title: "保存成功",
icon: "none"
});
},
fail: function() {
uni.showToast({
title: "保存失败",
icon: "none"
});
}
});
}
}
})
} else if (res.cancel) {
}
}
});
}
复制代码
最后
小程序码生成和分享都是用于业务推广的一种方式,一个是直接分享给微信好友或者群里,另一个是可以生成本地图片进行打印范围更大一些。