mirror of
https://gitee.com/y_project/RuoYi-Cloud.git
synced 2026-01-26 11:51:55 +08:00
赛会功能代码
This commit is contained in:
@@ -5,6 +5,9 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import com.ruoyi.common.security.annotation.EnableCustomConfig;
|
||||
import com.ruoyi.common.security.annotation.EnableRyFeignClients;
|
||||
import com.ruoyi.common.swagger.annotation.EnableCustomSwagger2;
|
||||
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
* 系统模块
|
||||
@@ -31,4 +34,9 @@ public class RuoYiSystemApplication
|
||||
" | | \\ / \\ / \n" +
|
||||
" ''-' `'-' `-..-' ");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RestTemplate restTemplate(RestTemplateBuilder builder) {
|
||||
// Do any additional configuration here
|
||||
return builder.build(); }
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ package com.ruoyi.system.controller;
|
||||
import java.util.List;
|
||||
import java.io.IOException;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import com.ruoyi.system.api.domain.vo.WxAppletsCodeVo;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
@@ -81,6 +83,14 @@ public class CompetitionController extends BaseController
|
||||
return toAjax(competitionService.insertCompetition(competition));
|
||||
}
|
||||
|
||||
@RequiresPermissions("system:competition:genCompetitionCommonAqrSpread")
|
||||
@Log(title = "生成赛会普通微信推广码", businessType = BusinessType.OTHER)
|
||||
@PostMapping("/genCompetitionCommonAqrSpread")
|
||||
public AjaxResult genCompetitionCommonAqrSpread(@RequestBody WxAppletsCodeVo wxAppletsCodeVo)
|
||||
{
|
||||
return AjaxResult.success(competitionService.genCompetitionCommonAqrSpread(wxAppletsCodeVo));
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改比赛信息
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.ruoyi.system.controller;
|
||||
|
||||
import com.ruoyi.common.core.web.controller.BaseController;
|
||||
import com.ruoyi.common.core.web.domain.AjaxResult;
|
||||
import com.ruoyi.common.core.web.page.TableDataInfo;
|
||||
import com.ruoyi.common.log.annotation.Log;
|
||||
import com.ruoyi.common.log.enums.BusinessType;
|
||||
import com.ruoyi.common.security.annotation.RequiresPermissions;
|
||||
import com.ruoyi.system.api.domain.vo.WxAppletsCodeVo;
|
||||
import com.ruoyi.system.domain.UserWxAqrCode;
|
||||
import com.ruoyi.system.domain.vo.UserWxAqrCodeVo;
|
||||
import com.ruoyi.system.service.IWxUserService;
|
||||
import com.ruoyi.system.service.WxApplesCodeService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author 吴一博
|
||||
* @date 2022年11月17日 17:10
|
||||
* @Description 微信小程序代码控制
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/wxApplesCode")
|
||||
public class WxApplesCodeController extends BaseController {
|
||||
@Resource
|
||||
private WxApplesCodeService wxApplesCodeService;
|
||||
|
||||
@RequiresPermissions("system:wxApplesCode:getWxApplesAccessToken")
|
||||
@GetMapping("/getWxApplesAccessToken")
|
||||
public AjaxResult getWxApplesAccessToken()
|
||||
{
|
||||
return AjaxResult.success(wxApplesCodeService.getWxApplesAccessToken());
|
||||
}
|
||||
@RequiresPermissions("system:wxApplesCode:genWxApplesAqrCode")
|
||||
@Log(title = "生成微信小程序二维码", businessType = BusinessType.OTHER)
|
||||
@PostMapping("/genWxApplesAqrCode")
|
||||
public AjaxResult genWxApplesAqrCode(@RequestBody WxAppletsCodeVo wxAppletsCodeVo)
|
||||
{
|
||||
return AjaxResult.success(wxApplesCodeService.genWxApplesAqrCode(wxAppletsCodeVo));
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.ruoyi.system.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.ruoyi.system.api.domain.vo.WxAppletsCodeVo;
|
||||
import com.ruoyi.system.domain.Competition;
|
||||
|
||||
/**
|
||||
@@ -58,4 +60,6 @@ public interface ICompetitionService
|
||||
* @return 结果
|
||||
*/
|
||||
public int deleteCompetitionById(Long id);
|
||||
|
||||
WxAppletsCodeVo genCompetitionCommonAqrSpread(WxAppletsCodeVo wxAppletsCodeVo);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.ruoyi.system.service;
|
||||
|
||||
import com.ruoyi.system.api.domain.vo.WxAppletsCodeVo;
|
||||
|
||||
/**
|
||||
* @author 吴一博
|
||||
* @date 2022年11月17日 17:11
|
||||
* @Description
|
||||
*/
|
||||
public interface WxApplesCodeService {
|
||||
/**
|
||||
* @description 获取微信小程序AccessToken
|
||||
* @author 吴一博
|
||||
* @date 2022/11/17 17:13
|
||||
* @return java.lang.String
|
||||
*/
|
||||
String getWxApplesAccessToken();
|
||||
|
||||
WxAppletsCodeVo genWxApplesAqrCode(WxAppletsCodeVo wxAppletsCodeVo);
|
||||
}
|
||||
@@ -1,12 +1,21 @@
|
||||
package com.ruoyi.system.service.impl;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import com.ruoyi.common.core.constant.CacheConstants;
|
||||
import com.ruoyi.common.redis.service.RedisService;
|
||||
import com.ruoyi.system.api.domain.vo.WxAppletsCodeVo;
|
||||
import com.ruoyi.system.service.WxApplesCodeService;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import com.ruoyi.system.mapper.CompetitionMapper;
|
||||
import com.ruoyi.system.domain.Competition;
|
||||
import com.ruoyi.system.service.ICompetitionService;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* 比赛信息Service业务层处理
|
||||
*
|
||||
@@ -16,9 +25,12 @@ import com.ruoyi.system.service.ICompetitionService;
|
||||
@Service
|
||||
public class CompetitionServiceImpl implements ICompetitionService
|
||||
{
|
||||
@Autowired
|
||||
@Resource
|
||||
private CompetitionMapper competitionMapper;
|
||||
|
||||
@Resource
|
||||
private WxApplesCodeService wxApplesCodeService;
|
||||
@Resource
|
||||
private RedisService redisService;
|
||||
/**
|
||||
* 查询比赛信息
|
||||
*
|
||||
@@ -90,4 +102,16 @@ public class CompetitionServiceImpl implements ICompetitionService
|
||||
{
|
||||
return competitionMapper.deleteCompetitionById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxAppletsCodeVo genCompetitionCommonAqrSpread(WxAppletsCodeVo wxAppletsCodeVo) {
|
||||
Object key = redisService.getCacheObject(CacheConstants.COMPETITION_SPREAD_AQR_CODE + wxAppletsCodeVo.getScene());
|
||||
if(ObjectUtils.isEmpty(key)){
|
||||
wxAppletsCodeVo = wxApplesCodeService.genWxApplesAqrCode(wxAppletsCodeVo);
|
||||
redisService.setCacheObject(CacheConstants.COMPETITION_SPREAD_AQR_CODE + wxAppletsCodeVo.getScene(),wxAppletsCodeVo.getCodeImgUrl(),30L, TimeUnit.DAYS);
|
||||
}else {
|
||||
wxAppletsCodeVo.setCodeImgUrl((String) key);
|
||||
}
|
||||
return wxAppletsCodeVo;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,157 @@
|
||||
package com.ruoyi.system.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.ruoyi.common.core.constant.CacheConstants;
|
||||
import com.ruoyi.common.redis.service.RedisService;
|
||||
import com.ruoyi.system.api.domain.vo.WxAppletsCodeVo;
|
||||
import com.ruoyi.system.service.WxApplesCodeService;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.*;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author 吴一博
|
||||
* @date 2022年11月17日 17:12
|
||||
* @Description
|
||||
*/
|
||||
@Log4j2
|
||||
@Service
|
||||
public class WxApplesCodeServiceImpl implements WxApplesCodeService {
|
||||
@Autowired
|
||||
private RestTemplate restTemplate;
|
||||
@Resource
|
||||
private RedisService redisService;
|
||||
|
||||
@Value("${image.location.linux}")
|
||||
private String linuxLocation;
|
||||
@Value("${image.domainName}")
|
||||
private String domainName;
|
||||
@Override
|
||||
public String getWxApplesAccessToken() {
|
||||
String accessToken=null;
|
||||
//如果已经获取就不需要再次请求了
|
||||
if(ObjectUtils.isEmpty(redisService.getCacheObject(CacheConstants.WX_APPLETS_REDIS_ACCESS_TOKEN_KEY))) {
|
||||
//获取access_token
|
||||
String appid = CacheConstants.WX_APPLETS_APP_ID;
|
||||
String appsecret = CacheConstants.WX_APPLETS_APP_SERCERT;
|
||||
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" +
|
||||
"&appid=" + appid + "&secret=" + appsecret;
|
||||
if (restTemplate == null) {
|
||||
restTemplate = new RestTemplate();
|
||||
}
|
||||
String json = restTemplate.getForObject(url, String.class);
|
||||
JSONObject myJson = JSONObject.parseObject(json);
|
||||
log.info("获取AccessToken={}", JSON.toJSONString(myJson));
|
||||
//保存到缓存
|
||||
accessToken = myJson.get("access_token").toString();
|
||||
Long expires_in=myJson.getLong("expires_in");
|
||||
redisService.setCacheObject(CacheConstants.WX_APPLETS_REDIS_ACCESS_TOKEN_KEY,accessToken,expires_in-100, TimeUnit.SECONDS);
|
||||
return accessToken;
|
||||
}else {
|
||||
return (String) redisService.getCacheObject(CacheConstants.WX_APPLETS_REDIS_ACCESS_TOKEN_KEY);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxAppletsCodeVo genWxApplesAqrCode(WxAppletsCodeVo wxAppletsCodeVo) {
|
||||
if(restTemplate==null){
|
||||
restTemplate = new RestTemplate();
|
||||
}
|
||||
InputStream inputStream = null;
|
||||
OutputStream outputStream = null;
|
||||
//根据APPid和密钥获取存取令牌
|
||||
try {
|
||||
String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + this.getWxApplesAccessToken();
|
||||
//定义生产二维码所需的参数、样式
|
||||
Map<String, Object> param = new HashMap<>();
|
||||
param.put("scene", wxAppletsCodeVo.getScene());
|
||||
param.put("page", wxAppletsCodeVo.getPage());
|
||||
param.put("width", org.apache.commons.lang3.StringUtils.isEmpty(wxAppletsCodeVo.getPage())?10:wxAppletsCodeVo.getPage());
|
||||
param.put("auto_color", wxAppletsCodeVo.getAutoColor()==null?false:wxAppletsCodeVo.getAutoColor());
|
||||
param.put("is_hyaline",wxAppletsCodeVo.getIsHyaline()==null?false:wxAppletsCodeVo.getIsHyaline());
|
||||
Map<String, Object> line_color = new HashMap<>();
|
||||
line_color.put("r", 0);
|
||||
line_color.put("g", 0);
|
||||
line_color.put("b", 0);
|
||||
param.put("line_color", line_color);
|
||||
System.out.println(param+"调用微信URL传参");
|
||||
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
|
||||
HttpEntity requestEntity = new HttpEntity(param, headers);
|
||||
// System.out.println("协议请求头"+headers+""+requestEntity);
|
||||
ResponseEntity<byte[]> entity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]);
|
||||
// LOG.info("调用小程序生成微信永久小程序码URL接口返回结果:" + entity.getBody());
|
||||
// System.out.println("返回结果"+entity.getBody()+".."+entity);
|
||||
byte[] result = entity.getBody();
|
||||
// LOG.info(Base64.encodeBase64String(result));
|
||||
// System.out.println("不知道是什么:"+Base64.encodeBase64String(result));
|
||||
inputStream = new ByteArrayInputStream(result);
|
||||
// 生成随机数命名图片
|
||||
String filename = UUID.randomUUID().toString();
|
||||
// System.out.println(filename);
|
||||
Date date = new Date();
|
||||
String time = new SimpleDateFormat("yyyy-MM-dd").format(date);
|
||||
String[] str = time.split("-");//根据‘-’进行拆分字符串 拆分出来的日期有,年,日,月,根据年日月创建文件夹
|
||||
String datePath="/"+str[0]+"/"+str[1]+"/"+str[2]+"/";
|
||||
//创建文件夹
|
||||
String xdpath = datePath+filename+".png";
|
||||
String filePath = linuxLocation+datePath+filename+".png";
|
||||
// 服务器存放位置
|
||||
File file = new File(filePath);
|
||||
//文件目录不存在需要先创建
|
||||
if(!file.getParentFile().exists()){
|
||||
file.getParentFile().mkdirs();
|
||||
}
|
||||
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();
|
||||
wxAppletsCodeVo.setCodeImgUrl(domainName+xdpath);
|
||||
return wxAppletsCodeVo;
|
||||
|
||||
} catch (Exception e) {
|
||||
// LOG.error("调用小程序生成微信永久小程序码URL接口异常", e);
|
||||
} finally {
|
||||
if (inputStream != null) {
|
||||
try {
|
||||
inputStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (outputStream != null) {
|
||||
try {
|
||||
outputStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println("获取二维码");
|
||||
return wxAppletsCodeVo;
|
||||
}
|
||||
}
|
||||
@@ -10,3 +10,10 @@ spring:
|
||||
application:
|
||||
# 应用名称
|
||||
name: ruoyi-system
|
||||
|
||||
image:
|
||||
#图片域名
|
||||
domainName: "https://adu.shjmall.cn/liguanghui/image/"
|
||||
location:
|
||||
linux: /bt/pic/
|
||||
windows: D://test//
|
||||
|
||||
Reference in New Issue
Block a user