BackendCommonController.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package com.fuint.module.backendApi.controller;
  2. import com.fuint.common.dto.AccountInfo;
  3. import com.fuint.common.enums.QrCodeEnum;
  4. import com.fuint.common.service.CouponService;
  5. import com.fuint.common.service.SettingService;
  6. import com.fuint.common.service.StoreService;
  7. import com.fuint.common.service.WeixinService;
  8. import com.fuint.common.util.Base64Util;
  9. import com.fuint.common.util.QRCodeUtil;
  10. import com.fuint.common.util.TokenUtil;
  11. import com.fuint.framework.exception.BusinessCheckException;
  12. import com.fuint.framework.web.BaseController;
  13. import com.fuint.framework.web.ResponseObject;
  14. import com.fuint.repository.model.MtCoupon;
  15. import com.fuint.repository.model.MtStore;
  16. import io.swagger.annotations.Api;
  17. import io.swagger.annotations.ApiOperation;
  18. import lombok.AllArgsConstructor;
  19. import org.slf4j.Logger;
  20. import org.slf4j.LoggerFactory;
  21. import org.springframework.core.env.Environment;
  22. import org.springframework.web.bind.annotation.*;
  23. import javax.servlet.http.HttpServletRequest;
  24. import java.io.ByteArrayOutputStream;
  25. import java.util.HashMap;
  26. import java.util.Map;
  27. /**
  28. * 后台公共接口控制器
  29. *
  30. * Created by FSQ
  31. * CopyRight https://www.fuint.cn
  32. */
  33. @Api(tags="管理端-公共接口")
  34. @RestController
  35. @AllArgsConstructor
  36. @RequestMapping(value = "/backendApi/common")
  37. public class BackendCommonController extends BaseController {
  38. private static final Logger logger = LoggerFactory.getLogger(BackendCommonController.class);
  39. private Environment env;
  40. /**
  41. * 微信服务接口
  42. * */
  43. private WeixinService weixinService;
  44. /**
  45. * 系统设置服务接口
  46. * */
  47. private SettingService settingService;
  48. /**
  49. * 店铺服务接口
  50. * */
  51. private StoreService storeService;
  52. /**
  53. * 卡券服务接口
  54. */
  55. private CouponService couponService;
  56. /**
  57. * 生成二维码
  58. *
  59. * @return
  60. */
  61. @ApiOperation(value = "生成二维码")
  62. @RequestMapping(value = "/createQrCode", method = RequestMethod.POST)
  63. @CrossOrigin
  64. public ResponseObject createQrCode(HttpServletRequest request, @RequestBody Map<String, Object> params) throws BusinessCheckException {
  65. String token = request.getHeader("Access-Token");
  66. String type = params.get("type") != null ? params.get("type").toString() : "";
  67. Integer id = params.get("id") == null ? 0 : Integer.parseInt(params.get("id").toString());
  68. AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
  69. if (accountInfo == null) {
  70. return getFailureResult(1001, "请先登录");
  71. }
  72. Integer merchantId = 0;
  73. String page = QrCodeEnum.STORE.getPage() + "?" + QrCodeEnum.STORE.getKey() + "Id=" + id;
  74. if (type.equals(QrCodeEnum.TABLE.getKey())) {
  75. page = QrCodeEnum.TABLE.getPage() + "?" + QrCodeEnum.TABLE.getKey() + "Id=" + id;
  76. }
  77. if (type.equals(QrCodeEnum.COUPON.getKey())) {
  78. page = QrCodeEnum.COUPON.getPage() + "?" + QrCodeEnum.COUPON.getKey() + "Id=" + id;
  79. }
  80. if (type.equals(QrCodeEnum.STORE.getKey())) {
  81. MtStore mtStore = storeService.queryStoreById(id);
  82. if (mtStore != null) {
  83. merchantId = mtStore.getMerchantId();
  84. }
  85. }
  86. if (type.equals(QrCodeEnum.COUPON.getKey())) {
  87. MtCoupon mtCoupon = couponService.queryCouponById(id);
  88. if (mtCoupon != null) {
  89. merchantId = mtCoupon.getMerchantId();
  90. }
  91. }
  92. String h5QrCode = "";
  93. try {
  94. String h5Page = env.getProperty("website.url") + "#" + page;
  95. ByteArrayOutputStream out = new ByteArrayOutputStream();
  96. QRCodeUtil.createQrCode(out, h5Page, 800, 800, "png", "");
  97. h5QrCode = new String(Base64Util.baseEncode(out.toByteArray()), "UTF-8");
  98. h5QrCode = "data:image/jpg;base64," + h5QrCode;
  99. } catch (Exception e) {
  100. logger.error(e.getMessage(), e);
  101. }
  102. String imagePath = settingService.getUploadBasePath();
  103. String minAppQrCode = weixinService.createQrCode(merchantId, type, id, page, 320);
  104. minAppQrCode = imagePath + minAppQrCode;
  105. Map<String, Object> result = new HashMap<>();
  106. result.put("minAppQrCode", minAppQrCode);
  107. result.put("h5QrCode", h5QrCode);
  108. return getSuccessResult(result);
  109. }
  110. }