From 116852ce307ad5fed43c202a6fd04b78116d0830 Mon Sep 17 00:00:00 2001 From: wangw <1594593906@qq.com> Date: Sat, 14 Sep 2024 09:14:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BA=97=E9=93=BA=E5=8F=82=E6=95=B0=20?= =?UTF-8?q?=E5=B0=8F=E7=A8=8B=E5=BA=8F=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mybatis/rest/StorageController.java | 57 ++----------- .../cn/ysk/cashier/service/WxService.java | 10 +++ .../cashier/service/impl/WxServiceImpl.java | 80 +++++++++++++++++++ .../impl/shopimpl/TbShopInfoServiceImpl.java | 7 ++ 4 files changed, 105 insertions(+), 49 deletions(-) create mode 100644 eladmin-system/src/main/java/cn/ysk/cashier/service/WxService.java create mode 100644 eladmin-system/src/main/java/cn/ysk/cashier/service/impl/WxServiceImpl.java diff --git a/eladmin-system/src/main/java/cn/ysk/cashier/mybatis/rest/StorageController.java b/eladmin-system/src/main/java/cn/ysk/cashier/mybatis/rest/StorageController.java index d42421ca..09d2d775 100644 --- a/eladmin-system/src/main/java/cn/ysk/cashier/mybatis/rest/StorageController.java +++ b/eladmin-system/src/main/java/cn/ysk/cashier/mybatis/rest/StorageController.java @@ -1,29 +1,22 @@ package cn.ysk.cashier.mybatis.rest; -import cn.hutool.core.io.IoUtil; -import cn.hutool.http.HttpUtil; import cn.ysk.cashier.annotation.Log; -import cn.ysk.cashier.domain.QiniuContent; import cn.ysk.cashier.exception.BadRequestException; import cn.ysk.cashier.mybatis.entity.Activate; import cn.ysk.cashier.mybatis.service.ShopService; -import cn.ysk.cashier.service.QiNiuService; +import cn.ysk.cashier.service.WxService; import cn.ysk.cashier.utils.CacheKey; import cn.ysk.cashier.utils.RedisUtils; import cn.ysk.cashier.utils.SecurityUtils; -import com.alibaba.fastjson.JSONObject; -import com.google.gson.JsonObject; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.*; -import java.io.InputStream; import java.util.Map; @Slf4j @@ -32,15 +25,9 @@ import java.util.Map; @Api(tags = "/shop/storage") @RequestMapping("/shop/storage") public class StorageController { - @Autowired - private ShopService shopService; + private final ShopService shopService; private final RedisUtils redisUtils; - - private final QiNiuService qiNiuService; - private static final String APP_ID = "wxd88fffa983758a30"; - private static final String APP_SECRET = "a34a61adc0602118b49400baa8812454"; - private static final String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token"; - private static final String QR_CODE_URL = "https://api.weixin.qq.com/wxa/getwxacode"; + private final WxService wxService; @GetMapping("/findActivate") public ResponseEntity findActivate(@RequestParam String shopId){ @@ -72,38 +59,10 @@ public class StorageController { */ @PostMapping("/getwxacode") public ResponseEntity getwxacode(@RequestBody Map params) { - if (CollectionUtils.isEmpty(params) || !params.containsKey("shopId")) throw new BadRequestException("参数错误"); - if (redisUtils.hasKey(CacheKey.VIPCODE + params.get("shopId"))) return new ResponseEntity<>(redisUtils.get(CacheKey.VIPCODE + params.get("shopId")),HttpStatus.OK); - try { - InputStream qrCodeStream = fetchQRCode(params); - QiniuContent qiniuContent = qiNiuService.uploadByte(IoUtil.readBytes(qrCodeStream),qiNiuService.findCloud()); - redisUtils.set(CacheKey.VIPCODE + params.get("shopId"),qiniuContent.getUrl()); - return new ResponseEntity<>(qiniuContent.getUrl(),HttpStatus.OK); - } catch (Exception e) { - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) - .body("Failed to generate QR code: " + e.getMessage()); - } - } - - private String getAccessToken() { - String url = String.format("%s?grant_type=client_credential&appid=%s&secret=%s", TOKEN_URL, APP_ID, APP_SECRET); - String response = HttpUtil.get(url); - JSONObject jsonResponse = JSONObject.parseObject(response); - if (!jsonResponse.containsKey("access_token")) { - throw new RuntimeException("Failed to retrieve access token: " + response); - } - return jsonResponse.getString("access_token"); - } - private InputStream fetchQRCode(Map params) { - JsonObject jsonObject = new JsonObject(); - jsonObject.addProperty("path", "pages/member/index?shopId="+params.get("shopId"));//路径 - jsonObject.addProperty("is_hyaline", true);//是否需要透明底色,为 true 时,生成透明底色的小程序码 - if (params.containsKey("env_version")) jsonObject.addProperty("env_version", "trial");//正式版为 release,体验版为 trial,开发版为 develop - String accessToken = getAccessToken(); - String url = String.format("%s?access_token=%s", QR_CODE_URL, accessToken); - return HttpUtil.createPost(url) - .body(jsonObject.toString(), "application/json") - .execute() - .bodyStream(); + if (CollectionUtils.isEmpty(params) || !params.containsKey("shopId")) + throw new BadRequestException("参数错误"); + if (redisUtils.hasKey(CacheKey.VIPCODE + params.get("shopId"))) + return new ResponseEntity<>(redisUtils.get(CacheKey.VIPCODE + params.get("shopId")), HttpStatus.OK); + return new ResponseEntity<>(wxService.getFetchQRCode(params), HttpStatus.OK); } } diff --git a/eladmin-system/src/main/java/cn/ysk/cashier/service/WxService.java b/eladmin-system/src/main/java/cn/ysk/cashier/service/WxService.java new file mode 100644 index 00000000..e486007f --- /dev/null +++ b/eladmin-system/src/main/java/cn/ysk/cashier/service/WxService.java @@ -0,0 +1,10 @@ +package cn.ysk.cashier.service; + +import java.util.Map; + +public interface WxService { + + String getFetchQRCode(Map params); + + String getSmallQrcode(String shopId); +} diff --git a/eladmin-system/src/main/java/cn/ysk/cashier/service/impl/WxServiceImpl.java b/eladmin-system/src/main/java/cn/ysk/cashier/service/impl/WxServiceImpl.java new file mode 100644 index 00000000..e1f760ed --- /dev/null +++ b/eladmin-system/src/main/java/cn/ysk/cashier/service/impl/WxServiceImpl.java @@ -0,0 +1,80 @@ +package cn.ysk.cashier.service.impl; + +import cn.hutool.core.io.IoUtil; +import cn.hutool.http.HttpUtil; +import cn.ysk.cashier.domain.QiniuContent; +import cn.ysk.cashier.service.QiNiuService; +import cn.ysk.cashier.service.WxService; +import cn.ysk.cashier.utils.CacheKey; +import cn.ysk.cashier.utils.RedisUtils; +import com.alibaba.fastjson.JSONObject; +import com.google.gson.JsonObject; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.io.InputStream; +import java.util.Map; + +@Service +@RequiredArgsConstructor +public class WxServiceImpl implements WxService { + + private final QiNiuService qiNiuService; + + private final RedisUtils redisUtils; + private static final String APP_ID = "wxd88fffa983758a30"; + private static final String APP_SECRET = "a34a61adc0602118b49400baa8812454"; + private static final String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token"; + private static final String QR_CODE_URL = "https://api.weixin.qq.com/wxa/getwxacode"; + + @Override + public String getFetchQRCode(Map params) { + InputStream qrCodeStream = fetchQRCode(params); + QiniuContent qiniuContent = qiNiuService.uploadByte(IoUtil.readBytes(qrCodeStream),qiNiuService.findCloud()); + redisUtils.set(CacheKey.VIPCODE + params.get("shopId"),qiniuContent.getUrl()); + return qiniuContent.getUrl(); + } + + @Override + public String getSmallQrcode(String shopId) { + InputStream shopQRCodeStream = smallQrcode(shopId); + QiniuContent qiniuContent = qiNiuService.uploadByte(IoUtil.readBytes(shopQRCodeStream),qiNiuService.findCloud()); + return qiniuContent.getUrl(); + } + + + public InputStream fetchQRCode(Map params) { + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("path", "pages/member/index?shopId="+params.get("shopId"));//路径 + jsonObject.addProperty("is_hyaline", true);//是否需要透明底色,为 true 时,生成透明底色的小程序码 + if (params.containsKey("env_version")) jsonObject.addProperty("env_version", "trial");//正式版为 release,体验版为 trial,开发版为 develop + String accessToken = getAccessToken(); + String url = String.format("%s?access_token=%s", QR_CODE_URL, accessToken); + return HttpUtil.createPost(url) + .body(jsonObject.toString(), "application/json") + .execute() + .bodyStream(); + } + + public InputStream smallQrcode(String shopId) { + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("path", "pages/index/index?shopId="+shopId);//路径 + jsonObject.addProperty("is_hyaline", true);//是否需要透明底色,为 true 时,生成透明底色的小程序码 + String accessToken = getAccessToken(); + String url = String.format("%s?access_token=%s", QR_CODE_URL, accessToken); + return HttpUtil.createPost(url) + .body(jsonObject.toString(), "application/json") + .execute() + .bodyStream(); + } + + private String getAccessToken() { + String url = String.format("%s?grant_type=client_credential&appid=%s&secret=%s", TOKEN_URL, APP_ID, APP_SECRET); + String response = HttpUtil.get(url); + JSONObject jsonResponse = JSONObject.parseObject(response); + if (!jsonResponse.containsKey("access_token")) { + throw new RuntimeException("Failed to retrieve access token: " + response); + } + return jsonResponse.getString("access_token"); + } +} diff --git a/eladmin-system/src/main/java/cn/ysk/cashier/service/impl/shopimpl/TbShopInfoServiceImpl.java b/eladmin-system/src/main/java/cn/ysk/cashier/service/impl/shopimpl/TbShopInfoServiceImpl.java index 96200030..a112ff6e 100644 --- a/eladmin-system/src/main/java/cn/ysk/cashier/service/impl/shopimpl/TbShopInfoServiceImpl.java +++ b/eladmin-system/src/main/java/cn/ysk/cashier/service/impl/shopimpl/TbShopInfoServiceImpl.java @@ -28,6 +28,7 @@ import cn.ysk.cashier.pojo.shop.TbPlussShopStaff; import cn.ysk.cashier.pojo.shop.TbShopInfo; import cn.ysk.cashier.repository.TbShopPayTypeRepository; import cn.ysk.cashier.repository.shop.*; +import cn.ysk.cashier.service.WxService; import cn.ysk.cashier.service.shop.TbShopInfoService; import cn.ysk.cashier.system.domain.Dept; import cn.ysk.cashier.system.domain.Job; @@ -84,6 +85,7 @@ public class TbShopInfoServiceImpl implements TbShopInfoService { private final RedisUtils redisUtils; private final TbShopPayTypeRepository tbShopPayTypeRepository; + private final WxService wxService; @Override public Map queryAll(TbShopInfoQueryCriteria criteria){ @@ -102,6 +104,11 @@ public class TbShopInfoServiceImpl implements TbShopInfoService { @Transactional public TbShopInfoDto findById(Integer id) { TbShopInfo tbShopInfo = tbShopInfoRepository.findById(id).orElseGet(TbShopInfo::new); + if(StringUtils.isBlank(tbShopInfo.getSmallQrcode())){ + String smallQrcode = wxService.getSmallQrcode(id.toString()); + tbShopInfo.setSmallQrcode(smallQrcode); + tbShopInfoRepository.save(tbShopInfo); + } ValidationUtil.isNull(tbShopInfo.getId(),"TbShopInfo","id",id); return tbShopInfoMapper.toDto(tbShopInfo); }