diff --git a/eladmin-system/src/main/java/cn/ysk/cashier/controller/ThirdPartyCouponController.java b/eladmin-system/src/main/java/cn/ysk/cashier/controller/ThirdPartyCouponController.java index a7579669..0a69f072 100644 --- a/eladmin-system/src/main/java/cn/ysk/cashier/controller/ThirdPartyCouponController.java +++ b/eladmin-system/src/main/java/cn/ysk/cashier/controller/ThirdPartyCouponController.java @@ -2,10 +2,7 @@ package cn.ysk.cashier.controller; import cn.ysk.cashier.service.ThirdPartyCouponService; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestHeader; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; /** * 三方团购券 @@ -25,7 +22,7 @@ public class ThirdPartyCouponController { * @return 美团绑定链接 */ @GetMapping - public ResponseEntity getBindUrl() { - return ResponseEntity.ok(thirdPartyCouponService.getBindUrl()); + public ResponseEntity getBindUrl(@RequestParam Integer shopId) { + return ResponseEntity.ok(thirdPartyCouponService.getBindUrl(shopId)); } } diff --git a/eladmin-system/src/main/java/cn/ysk/cashier/service/ThirdPartyCouponService.java b/eladmin-system/src/main/java/cn/ysk/cashier/service/ThirdPartyCouponService.java index 9fc82d46..3da26911 100644 --- a/eladmin-system/src/main/java/cn/ysk/cashier/service/ThirdPartyCouponService.java +++ b/eladmin-system/src/main/java/cn/ysk/cashier/service/ThirdPartyCouponService.java @@ -5,5 +5,5 @@ import org.springframework.web.bind.annotation.RestController; public interface ThirdPartyCouponService { - String getBindUrl(); + String getBindUrl(Integer shopId); } diff --git a/eladmin-system/src/main/java/cn/ysk/cashier/service/impl/ThirdPartyCouponServiceImpl.java b/eladmin-system/src/main/java/cn/ysk/cashier/service/impl/ThirdPartyCouponServiceImpl.java index 50f50a69..5082747c 100644 --- a/eladmin-system/src/main/java/cn/ysk/cashier/service/impl/ThirdPartyCouponServiceImpl.java +++ b/eladmin-system/src/main/java/cn/ysk/cashier/service/impl/ThirdPartyCouponServiceImpl.java @@ -1,38 +1,82 @@ package cn.ysk.cashier.service.impl; +import cn.hutool.core.bean.BeanUtil; import cn.ysk.cashier.exception.BadRequestException; +import cn.ysk.cashier.mybatis.mapper.MpShopInfoMapper; +import cn.ysk.cashier.pojo.shop.TbShopInfo; import cn.ysk.cashier.resp.PhpCommonResp; import cn.ysk.cashier.service.ThirdPartyCouponService; import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; +import java.util.HashMap; +import java.util.Map; + @Service public class ThirdPartyCouponServiceImpl implements ThirdPartyCouponService { + private final MpShopInfoMapper mpShopInfoMapper; @Value("${phpServer}") private String phpServerUrl; - public ThirdPartyCouponServiceImpl(RestTemplate restTemplate) { + public ThirdPartyCouponServiceImpl(RestTemplate restTemplate, MpShopInfoMapper mpShopInfoMapper) { this.restTemplate = restTemplate; + this.mpShopInfoMapper = mpShopInfoMapper; } private final RestTemplate restTemplate; - private T getData(String url, Object data) { - PhpCommonResp resp = restTemplate.getForEntity(phpServerUrl + url, PhpCommonResp.class).getBody(); + private T getData(String url, Integer shopId, Object data) { + // 获取店铺信息 + TbShopInfo shopInfo = mpShopInfoMapper.selectById(shopId); + if (shopInfo == null) { + throw new BadRequestException("店铺信息不存在"); + } + + // 设置请求头 + HttpHeaders headers = new HttpHeaders(); + headers.set("account", shopId.toString()); + + // 构造请求实体,根据 data 是否为空设置请求体 + HttpEntity entity; + if (data != null) { + Map map = BeanUtil.beanToMap(data); + map.put("title", shopInfo.getShopName()); + entity = new HttpEntity<>(map, headers); + } else { + entity = new HttpEntity<>(new HashMap(){{ + put("title", shopInfo.getShopName()); + }},headers); + } + + // 发起请求 + ResponseEntity response = restTemplate.exchange( + phpServerUrl + url, + HttpMethod.POST, // 使用 POST 请求发送 body 数据 + entity, + PhpCommonResp.class + ); + + // 处理响应 + PhpCommonResp resp = response.getBody(); if (resp == null) { throw new BadRequestException("请求php服务器失败"); } - if (!"1".equals(resp.getCode())) { throw new BadRequestException(resp.getMsg()); } + // 返回数据 return (T) resp.getData(); } + @Override - public String getBindUrl() { - return getData("/meituan/getuisdkurl", null); + public String getBindUrl(Integer shopId) { + return getData("/meituan/getuisdkurl", shopId, null); } }