fix: 美团核销相关

This commit is contained in:
张松 2024-11-21 18:27:22 +08:00
parent b618db850a
commit 7e413ad469
3 changed files with 54 additions and 13 deletions

View File

@ -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<String> getBindUrl() {
return ResponseEntity.ok(thirdPartyCouponService.getBindUrl());
public ResponseEntity<String> getBindUrl(@RequestParam Integer shopId) {
return ResponseEntity.ok(thirdPartyCouponService.getBindUrl(shopId));
}
}

View File

@ -5,5 +5,5 @@ import org.springframework.web.bind.annotation.RestController;
public interface ThirdPartyCouponService {
String getBindUrl();
String getBindUrl(Integer shopId);
}

View File

@ -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> T getData(String url, Object data) {
PhpCommonResp<?> resp = restTemplate.getForEntity(phpServerUrl + url, PhpCommonResp.class).getBody();
private <T> 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<Object> entity;
if (data != null) {
Map<String, Object> map = BeanUtil.beanToMap(data);
map.put("title", shopInfo.getShopName());
entity = new HttpEntity<>(map, headers);
} else {
entity = new HttpEntity<>(new HashMap<String, Object>(){{
put("title", shopInfo.getShopName());
}},headers);
}
// 发起请求
ResponseEntity<PhpCommonResp> 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);
}
}