1、打開https://github.com/Wechat-Group/WxJava鏈接下載weixin-java-miniapp代碼至本地渐逃,用idea打開滔吠。
2、打開WxMaQrcodeService钱慢,WxMaQrcodeServiceImpl,WxMaQrcodeServiceImplTest類可以看到對(duì)應(yīng)的生成小程序碼的api接口卿堂。
3束莫、找到對(duì)應(yīng)場(chǎng)景的api接口
通過appid查詢:final WxMaService wxService = WxMaConfiguration.getMaService(appid);
通過wxService得到小程序碼:
final File file=wxService.getQrcodeService().createWxaCodeUnlimit(customerVo.getId().toString(),"pages/index/index");
上傳至阿里云服務(wù)器:
String path=AliyunOss.upload(file);
4懒棉、上傳阿里云服務(wù)器的api接口,具體可參考https://blog.csdn.net/weixin_43570367/article/details/103863717
我這里傳的直接是file文件览绿,因此在上傳時(shí)將File轉(zhuǎn)化成MultipartFile
package cn.jsbingo.volunteer.mini.utils;
import cn.hutool.core.date.DateTime;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import lombok.experimental.UtilityClass;
import org.apache.commons.io.IOUtils;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.UUID;
@UtilityClass
public class AliyunOss {
public Stringupload(File file)throws Exception {
//File轉(zhuǎn)化成MultipartFile
? ? ? ? FileInputStream input =new FileInputStream(file);
? ? ? ? MultipartFile multipartFile =new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray(input));
? ? ? ? // 地域節(jié)點(diǎn)
? ? ? ? String endpoint = AliyunProperties.ENDPOINT;
//以下4個(gè)參數(shù)需要在配置文件中配置好
// 云賬號(hào)AccessKey有所有API訪問權(quán)限策严,建議遵循阿里云安全最佳實(shí)踐,創(chuàng)建并使用RAM子賬號(hào)進(jìn)行API訪問或日常運(yùn)維挟裂,請(qǐng)登錄 https://ram.console.aliyun.com 創(chuàng)建享钞。
? ? ? ? String accessKeyId = AliyunProperties.KEYID;
? ? ? ? String accessKeySecret = AliyunProperties.KEYSECRET;
? ? ? ? // BucketName
? ? ? ? String yourBucketName = AliyunProperties.BUCKETNAME;
? ? ? ? try {
// 1.獲取上傳文件 MultipartFile file
// @RequestParam("file") file 與表單輸入項(xiàng)的name值保持一致
// 2.獲取上傳文件名稱揍诽,獲取上傳文件輸入流
? ? ? ? ? ? String fileName = multipartFile.getOriginalFilename();
? ? ? ? ? ? // 在文件名稱之前添加uuid值诀蓉,保證文件名稱不重復(fù)
? ? ? ? ? ? String uuid = UUID.randomUUID().toString();
? ? ? ? ? ? fileName = uuid + fileName;
? ? ? ? ? ? // 獲取當(dāng)前日期 2020/01/03
? ? ? ? ? ? String filePath =new DateTime().toString("yyyy/MM/dd");
? ? ? ? ? ? // 拼接文件完整路徑 2020/01/03/parker.jpg
? ? ? ? ? ? fileName = filePath +"/" + fileName;
? ? ? ? ? ? // 獲取上傳文件輸入流
? ? ? ? ? ? InputStream in = multipartFile.getInputStream();
? ? ? ? ? ? // 3.把上傳文件存儲(chǔ)到阿里云oss里面
// 創(chuàng)建OSSClient實(shí)例。
? ? ? ? ? ? OSS ossClient =new OSSClient(endpoint, accessKeyId, accessKeySecret);
? ? ? ? ? ? // 上傳文件流
// 第一個(gè)參數(shù)時(shí)BucketName暑脆,第二個(gè)參數(shù)是文件名稱渠啤,第三個(gè)參數(shù)是輸入流
? ? ? ? ? ? ossClient.putObject(yourBucketName, fileName, in);
? ? ? ? ? ? // 關(guān)閉OSSClient。
? ? ? ? ? ? ossClient.shutdown();
? ? ? ? ? ? // 返回上傳之后的oss存儲(chǔ)路徑
? ? ? ? ? ? String path ="https://" + yourBucketName +"." + endpoint +"/" + fileName;
? ? ? ? ? ? if(null==path){
return "小程序碼路徑為空";
? ? ? ? ? ? }else {
return path;
? ? ? ? ? ? }
}catch (Exception e) {
e.printStackTrace();
? ? ? ? ? ? return "數(shù)據(jù)異常";
? ? ? ? }
}
}
5添吗、需要從配置文件中讀取常量
package cn.jsbingo.volunteer.mini.utils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
@Component
public class AliyunPropertiesimplements InitializingBean {
@Value("${ali.oss.endPoint}")
private StringendPoint;
? ? @Value("${ali.oss.accessKeyId}")
private StringkeyId;
? ? @Value("${ali.oss.accessKeySecret}")
private StringkeySecret;
? ? @Value("${ali.oss.bucketName}")
private StringbucketName;
? ? // 定義常量沥曹,為了能夠使用
? ? public static StringENDPOINT;
? ? public static StringKEYID;
? ? public static StringKEYSECRET;
? ? public static StringBUCKETNAME;
? ? /**
* 服務(wù)器啟動(dòng)的時(shí)候,ConstantPropertiesUtil初始化碟联,調(diào)用里面的afterPropertiesSet方法妓美,讀取配置文件的內(nèi)容
? ? * @throws Exception
*/
? ? @Override
? ? public void afterPropertiesSet()throws Exception {
ENDPOINT =endPoint;
? ? ? ? KEYID =keyId;
? ? ? ? KEYSECRET =keySecret;
? ? ? ? BUCKETNAME =bucketName;
? ? }
}
6、在上傳至阿里云服務(wù)器接口中的path 則為阿里云的地址鲤孵,可拿取到直接存入數(shù)據(jù)庫