package zs.payment.controller; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.alibaba.fastjson.JSONObject; import zs.payment.dao.OrderCallbackDao; import zs.payment.entity.OrderCallback; import zs.payment.req.*; import zs.payment.entity.Orders; import zs.payment.req.aftersales.AfterSalesOrderReq; import zs.payment.resp.Result; import zs.payment.service.OrdersService; import zs.payment.utils.HttpUtil; import zs.payment.utils.RedisUtils; import javax.validation.Valid; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 芸信 供应链平台 */ @Slf4j @RestController @RequestMapping("/supply") public class YxSupplyChannelManualController { @Value("${supply.demain}") private String prefixUrl; @Value("${supply.appKey}") private String appKey; @Value("${supply.appSecret}") private String appSecret; @Autowired private RedisUtils redisUtils; @Autowired private OrdersService ordersService; @Autowired private OrderCallbackDao callbackDao; public static final String SUPPLY_TOKEN = "supply:token"; /** * 获取token */ @PostMapping("/getToken") public Result getToken() { return fetchAndCacheToken(); } /** * 请求上游获取token并缓存到Redis * 成功返回 Result.success(token),失败返回 Result.fail(msg) */ private Result fetchAndCacheToken() { Map params = new HashMap<>(); params.put("app_key", appKey); params.put("app_secret", appSecret); String resp = HttpUtil.restTemplatePost(prefixUrl + "/supplyapi/app/application/getToken", params); JSONObject respJson = JSONObject.parseObject(resp); if (respJson.getIntValue("code") != 0) { return Result.fail(respJson.getString("msg")); } JSONObject data = respJson.getJSONObject("data"); String token = data.getString("token"); long expiresAt = data.getLongValue("expiresAt"); long ttlSeconds = (expiresAt - System.currentTimeMillis()) / 1000; if (ttlSeconds <= 0) ttlSeconds = 60; redisUtils.set(SUPPLY_TOKEN, token, (int) ttlSeconds); return Result.success(token); } /** * 选品列表API */ @PostMapping("/cursorList") public Result cursorList(@Valid @RequestBody ProductPageReq req) { String token = redisUtils.get(SUPPLY_TOKEN); if (token == null || token.isEmpty()) { Result tokenResult = fetchAndCacheToken(); if (!tokenResult.isSuccess()) { return tokenResult; } token = (String) tokenResult.getBody().get("token"); } Map params = new HashMap<>(); params.put("pageSize", req.getPageSize()); if (req.getCursor() != null) { params.put("cursor", req.getCursor()); } Map headers = new HashMap<>(); headers.put("x-token", token); String resp = HttpUtil.restTemplatePost(prefixUrl + "/supplyapi/app/product/storage/cursorList", params, headers); JSONObject respJson = JSONObject.parseObject(resp); if (respJson.getIntValue("code") != 0) { return Result.fail(respJson.getString("msg")); } return Result.success(respJson.getJSONObject("data")); } //支持多个商品详情查看 @PostMapping("/detailList") public Result detailList(@Valid @RequestBody ProductDetailReq req) { String token = redisUtils.get(SUPPLY_TOKEN); if (token == null || token.isEmpty()) { Result tokenResult = fetchAndCacheToken(); if (!tokenResult.isSuccess()) { return tokenResult; } token = (String) tokenResult.getBody().get("token"); } Map params = new HashMap<>(); params.put("ids", req.getIds()); Map headers = new HashMap<>(); headers.put("x-token", token); String resp = HttpUtil.restTemplatePost(prefixUrl + "/supplyapi/app/product/storage/detailList", params, headers); JSONObject respJson = JSONObject.parseObject(resp); if (respJson.getIntValue("code") != 0) { return Result.fail(respJson.getString("msg")); } return Result.success(respJson.getJSONObject("data")); } // 订单验证 @PostMapping("/beforeCheck") public Result beforeCheck(@Valid @RequestBody OrderCheckReq req) { String token = redisUtils.get(SUPPLY_TOKEN); if (token == null || token.isEmpty()) { Result tokenResult = fetchAndCacheToken(); if (!tokenResult.isSuccess()) { return tokenResult; } token = (String) tokenResult.getBody().get("token"); } Map params = new HashMap<>(); params.put("spu", req.getSpu()); params.put("address", req.getAddress()); Map headers = new HashMap<>(); headers.put("x-token", token); String resp = HttpUtil.restTemplatePost(prefixUrl + "/supplyapi/app/order/beforeCheck", params, headers); JSONObject respJson = JSONObject.parseObject(resp); if (respJson.getIntValue("code") != 0) { return Result.fail(respJson.getString("msg")); } return Result.success(respJson.getJSONObject("data")); } //仅下单 @PostMapping("/orderConfirmOnly") public Result orderConfirmOnly(@Valid @RequestBody OrderReq req){ String token = redisUtils.get(SUPPLY_TOKEN); if (token == null || token.isEmpty()) { Result tokenResult = fetchAndCacheToken(); if (!tokenResult.isSuccess()) { return tokenResult; } token = (String) tokenResult.getBody().get("token"); } Map params = new HashMap<>(); params.put("spu", req.getSpu()); params.put("address", req.getAddress()); params.put("order_sn", req.getOrderSn()); Map headers = new HashMap<>(); headers.put("x-token", token); String resp = HttpUtil.restTemplatePost(prefixUrl + "/supplyapi/app/order/orderConfirmOnly", params, headers); JSONObject respJson = JSONObject.parseObject(resp); if (respJson.getIntValue("code") != 0) { return Result.fail(respJson.getString("msg")); } List orders = JSONObject.parseArray( respJson.getJSONObject("data").getJSONArray("Orders").toJSONString(), Orders.class ); log.info("仅下单,存储订单"); ordersService.addBatchOrder(orders); return Result.success(orders); } //订单支付(单独下单之后使用这个支付) @PostMapping("/orderPayOnly") public Result orderPayOnly(@Valid @RequestBody OrderPayReq req){ String token = redisUtils.get(SUPPLY_TOKEN); if (token == null || token.isEmpty()) { Result tokenResult = fetchAndCacheToken(); if (!tokenResult.isSuccess()) { return tokenResult; } token = (String) tokenResult.getBody().get("token"); } Map params = new HashMap<>(); params.put("order_sn", req.getOrderSn()); Map headers = new HashMap<>(); headers.put("x-token", token); String resp = HttpUtil.restTemplatePost(prefixUrl + "/supplyapi/app/order/orderPayOnly", params, headers); JSONObject respJson = JSONObject.parseObject(resp); if (respJson.getIntValue("code") != 0) { return Result.fail(respJson.getString("msg")); } List orders = JSONObject.parseArray( respJson.getJSONObject("data").getJSONArray("Orders").toJSONString(), Orders.class ); return Result.success(orders); } // 生成订单同时支付,如果已生成订单则是直接支付 @PostMapping("/order") public Result order(@Valid @RequestBody OrderReq req) { String token = redisUtils.get(SUPPLY_TOKEN); if (token == null || token.isEmpty()) { Result tokenResult = fetchAndCacheToken(); if (!tokenResult.isSuccess()) { return tokenResult; } token = (String) tokenResult.getBody().get("token"); } Map params = new HashMap<>(); params.put("spu", req.getSpu()); params.put("address", req.getAddress()); params.put("order_sn", req.getOrderSn()); Map headers = new HashMap<>(); headers.put("x-token", token); String resp = HttpUtil.restTemplatePost(prefixUrl + "/supplyapi/app/order", params, headers); JSONObject respJson = JSONObject.parseObject(resp); if (respJson.getIntValue("code") != 0) { return Result.fail(respJson.getString("msg")); } List orders = JSONObject.parseArray( respJson.getJSONObject("data").getJSONArray("Orders").toJSONString(), Orders.class ); log.info("下单并支付,存储订单"); ordersService.addBatchOrder(orders); return Result.success(orders); } // 取消订单 @PostMapping("/appCloseOrder") public Result appCloseOrder(@Valid @RequestBody CloseOrderReq req){ String token = redisUtils.get(SUPPLY_TOKEN); if (token == null || token.isEmpty()) { Result tokenResult = fetchAndCacheToken(); if (!tokenResult.isSuccess()) { return tokenResult; } token = (String) tokenResult.getBody().get("token"); } Map params = new HashMap<>(); params.put("order_id", req.getOrderId()); Map headers = new HashMap<>(); headers.put("x-token", token); String resp = HttpUtil.restTemplatePost(prefixUrl + "/supplyapi/app/order/appCloseOrder", params, headers); JSONObject respJson = JSONObject.parseObject(resp); if (respJson.getIntValue("code") != 0) { return Result.fail(respJson.getString("msg")); } //{ // "body": { // "MSG": "SUCCESS", // "CODE": 200, // "data": { // "errorOrdersSNs": null, // "successOrdersSNs": [ // 195319850638 // ] // } // }, // "success": true //} return Result.success(respJson.getJSONObject("data")); } // =======================售后========================================== /** * 获取订单支持的售后方式 * @param req * @return */ @PostMapping("/getAfterSalesTypeNameMap") public Result getAfterSalesTypeNameMap(@Valid @RequestBody AfterSalesOrderReq req){ String token = redisUtils.get(SUPPLY_TOKEN); if (token == null || token.isEmpty()) { Result tokenResult = fetchAndCacheToken(); if (!tokenResult.isSuccess()) { return tokenResult; } token = (String) tokenResult.getBody().get("token"); } Map params = new HashMap<>(); params.put("order_id", req.getOrderId()); Map headers = new HashMap<>(); headers.put("x-token", token); String resp = HttpUtil.restTemplateGet(prefixUrl + "/supplyapi/app/afterSales/getAfterSalesTypeNameMap", params, headers); JSONObject respJson = JSONObject.parseObject(resp); if (respJson.getIntValue("code") != 0) { return Result.fail(respJson.getString("msg")); } return Result.success(respJson.getJSONObject("data")); } //申请售后 @PostMapping("/afterSalesCreate") public Result afterSalesCreate(@Valid @RequestBody AfterSalesOrderReq req){ String token = redisUtils.get(SUPPLY_TOKEN); if (token == null || token.isEmpty()) { Result tokenResult = fetchAndCacheToken(); if (!tokenResult.isSuccess()) { return tokenResult; } token = (String) tokenResult.getBody().get("token"); } Map params = new HashMap<>(); params.put("order_id", req.getOrderId()); params.put("is_received",req.getIsReceived()); params.put("order_item_id",req.getOrderItemId()); Map headers = new HashMap<>(); headers.put("x-token", token); String resp = HttpUtil.restTemplatePost(prefixUrl + "/supplyapi/app/afterSales/create", params, headers); JSONObject respJson = JSONObject.parseObject(resp); if (respJson.getIntValue("code") != 0) { return Result.fail(respJson.getString("msg")); } return Result.success(respJson.getJSONObject("data")); } //回调接口,拿到回调数据后,再对数据做处理 @PostMapping("/callback") public Map callback(@Valid @RequestBody Object req) { Map result = new HashMap<>(); result.put("code", 0); OrderCallback callback=new OrderCallback(); callback.setMessage(req); callbackDao.save(callback); return result; } }