BackendCommonController.java 3.7 KB

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