package com.zy.asrs.openapi.controller; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.zy.asrs.common.openapi.entity.HostKey; import com.zy.asrs.common.openapi.entity.dto.OrderListDto; import com.zy.asrs.common.openapi.entity.param.GenerateOrderPakInParam; import com.zy.asrs.common.openapi.entity.param.OrderListParam; import com.zy.asrs.common.openapi.service.ApiService; import com.zy.asrs.common.openapi.service.HostKeyService; import com.zy.asrs.common.web.BaseController; import com.zy.asrs.common.wms.entity.DocType; import com.zy.asrs.framework.annotations.AppAuth; import com.zy.asrs.framework.common.BaseRes; import com.zy.asrs.framework.common.Cools; import com.zy.asrs.framework.common.R; import com.zy.asrs.framework.exception.CoolException; import lombok.extern.slf4j.Slf4j; import org.apache.commons.codec.digest.DigestUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; import java.util.Map; @Slf4j @RestController @RequestMapping("/api") public class ApiController extends BaseController { @Autowired private HostKeyService hostKeyService; @Autowired private ApiService apiService; /** * 生成入库订单 */ @PostMapping("/generateOrderPakIn") @AppAuth(memo = "生成入库订单") public synchronized R generateOrderPakIn(@RequestHeader(required = true) String appkey, @RequestBody GenerateOrderPakInParam param) { HostKey hostKey = auth(appkey, param, true); if (Cools.isEmpty(param)) { return R.parse(BaseRes.PARAM); } if (Cools.isEmpty(param.getOrderNo())) { return R.error("单据编号[orderNo]不能为空"); } if (Cools.isEmpty(param.getOrderType())) { return R.error("单据类型[orderType]不能为空"); } if (Cools.isEmpty(param.getOrderDetails())) { return R.error("单据明细[orderDetails]不能为空"); } param.setHostId(hostKey.getHostId()); apiService.generateOrderPakIn(param); return R.ok(); } /** * 获取订单类型 */ @PostMapping("/getOrderType") public synchronized R getOrderType(@RequestHeader(required = true) String appkey) { auth(appkey, null, false); List list = apiService.getOrderType(); return R.ok().add(list); } /** * 获取订单列表 */ @PostMapping("/getOrderList") public synchronized R getOrderList(@RequestHeader(required = true) String appkey, @RequestBody(required = false) OrderListParam param) { HostKey hostKey = auth(appkey, null, false); List orderList = apiService.getOrderList(param, hostKey.getHostId()); return R.ok().add(orderList); } private HostKey auth(String appkey, Object obj, boolean signCheck) { request.setAttribute("cache", obj); if (Cools.isEmpty(appkey)) { throw new CoolException("认证失败,请确认appKey无误!"); } HostKey hostKey = hostKeyService.getOne(new LambdaQueryWrapper().eq(HostKey::getAppKey, appkey)); if (hostKey == null) { throw new CoolException("认证失败,请确认appKey无误!"); } if (hostKey.getSignStatus() == 1 && signCheck) { //需要进行签名校验 //创建自然排序map JSONObject param = new JSONObject(true); JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(obj)); for (Map.Entry entry : jsonObject.entrySet()) { if (!entry.getKey().equals("sign")) {//剔除sign param.put(entry.getKey(), entry.getValue()); } } param.put("signKey", hostKey.getSignKey()); String md5Hex = DigestUtils.md5Hex(param.toJSONString()); if (!jsonObject.get("sign").toString().equals(md5Hex)) { throw new CoolException("签名校验失败"); } } return hostKey; } }