#
Junjie
2024-01-05 7ed7b800442bcac4689a0f445664073b7afcf785
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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<DocType> 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<OrderListDto> 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<HostKey>().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<String, Object> 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;
    }
 
}