xiaogang
2021-07-17 1024666b99a24bd7d5e4dc442d6b0c0319fa2f29
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package com.java110.api.listener.menu;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.api.listener.AbstractServiceApiDataFlowListener;
import com.java110.core.annotation.Java110Listener;
import com.java110.core.context.DataFlowContext;
import com.java110.core.event.service.api.ServiceDataFlowEvent;
import com.java110.core.factory.DataFlowFactory;
import com.java110.entity.center.AppService;
import com.java110.utils.constant.CommonConstant;
import com.java110.utils.constant.ServiceCodeConstant;
import com.java110.utils.util.Assert;
import com.java110.utils.util.StringUtil;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
 
import java.util.Map;
 
/**
 * 根据用户ID查询菜单信息
 * Created by Administrator on 2019/4/1.
 */
@Java110Listener("queryMenuServiceListener")
public class QueryMenuServiceListener extends AbstractServiceApiDataFlowListener {
    @Override
    public int getOrder() {
        return 0;
    }
 
    @Override
    public String getServiceCode() {
        return ServiceCodeConstant.SERVICE_CODE_QUERY_MENU_INFO;
    }
 
    @Override
    public HttpMethod getHttpMethod() {
        return HttpMethod.GET;
    }
 
    /**
     * 业务处理
     *
     * @param event
     */
    @Override
    public void soService(ServiceDataFlowEvent event) {
        DataFlowContext dataFlowContext = event.getDataFlowContext();
        AppService service = event.getAppService();
        //get 方式下 请求参数会转化到header 中
        Map<String, String> requestHeaders = dataFlowContext.getRequestHeaders();
        Assert.hasKey(requestHeaders, "userId", "请求信息中未包含userId信息");
        Assert.hasKey(requestHeaders, "groupType", "请求信息中未包含组类型");
 
        String userId = requestHeaders.get("userId");
        String groupType = requestHeaders.get("groupType");
        //根据用户查询 商户类
        String domain = queryStoreTypeCd(dataFlowContext, userId);
        domain = StringUtil.isEmpty(domain) ? "-1" : domain;
        ResponseEntity responseEntity = null;
        String requestUrl = service.getUrl() + "?userId=" + userId + "&domain=" + domain+"&groupType="+groupType;
        dataFlowContext.getRequestHeaders().put("REQUEST_URL", requestUrl);
        HttpHeaders header = new HttpHeaders();
        header.add(CommonConstant.HTTP_SERVICE.toLowerCase(), ServiceCodeConstant.SERVICE_CODE_QUERY_MENU_INFO);
        HttpEntity<String> httpEntity = new HttpEntity<String>("", header);
        doRequest(dataFlowContext, service, httpEntity);
        responseEntity = dataFlowContext.getResponseEntity();
        //如果调用后端失败了 则直接返回
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            return;
        }
 
        JSONObject resultObj = JSONObject.parseObject(responseEntity.getBody().toString());
 
        if (!resultObj.containsKey("menus")) {
            return;
        }
 
        dataFlowContext.setResponseEntity(refreshMenusInfo(resultObj.getJSONArray("menus")));
 
    }
 
    /**
     * 根据用户ID查询商户信息
     *
     * @param dataFlowContext
     * @param userId
     * @return
     */
    private String queryStoreTypeCd(DataFlowContext dataFlowContext, String userId) {
        ResponseEntity responseEntity = null;
        AppService appService = DataFlowFactory.getService(dataFlowContext.getAppId(), ServiceCodeConstant.SERVICE_CODE_QUERY_STORE_BYUSER);
        if (appService == null) {
            responseEntity = new ResponseEntity<String>("当前没有权限访问" + ServiceCodeConstant.SERVICE_CODE_QUERY_USER_LOGIN, HttpStatus.UNAUTHORIZED);
            dataFlowContext.setResponseEntity(responseEntity);
            return "";
        }
        String requestUrl = appService.getUrl() + "?userId=" + userId;
        HttpHeaders header = new HttpHeaders();
        header.add(CommonConstant.HTTP_SERVICE.toLowerCase(), ServiceCodeConstant.SERVICE_CODE_QUERY_STORE_BYUSER);
        HttpEntity<String> httpEntity = new HttpEntity<String>("", header);
        doRequest(dataFlowContext, appService, httpEntity);
        responseEntity = dataFlowContext.getResponseEntity();
 
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            dataFlowContext.setResponseEntity(responseEntity);
        }
 
        return JSONObject.parseObject(responseEntity.getBody().toString()).getString("storeTypeCd");
    }
 
    /**
     * 刷新菜单信息
     * 将 数据 [{
     * "gId": "800201904001",
     * "menuDescription": "添加员工",
     * "menuGroupSeq": 1,
     * "menuSeq": 1,
     * "icon": "fa-desktop",
     * "mId": "700201904001",
     * "menuName": "添加员工",
     * "pId": "500201904001",
     * "menuGroupName": "员工管理",
     * "label": "",
     * "menuGroupDescription": "员工管理",
     * "url": "/"
     * }],
     * 转为:
     * "[{'id':1,'icon':'fa-desktop','name':'我的菜单','label':'HOT','childs':[" +
     * "{'name':'子菜单','href':'http://www.baidu.com'}]}," +
     * "{'id':2,'icon':'fa-flask','name':'我的菜单','childs':[],'href':'/doc'}," +
     * "{'id':3,'icon':'fa-globe','name':'我的菜单','childs':[{'name':'子菜单','href':'http://www.baidu.com'}]}" +
     * "]";
     *
     * @param menusList 菜单列表
     * @return
     */
    private ResponseEntity<String> refreshMenusInfo(JSONArray menusList) {
        JSONArray tempMenus = new JSONArray();
        JSONObject tempMenu = null;
        for (int menuIndex = 0; menuIndex < menusList.size(); menuIndex++) {
            JSONObject tMenu = menusList.getJSONObject(menuIndex);
            tempMenu = this.getMenuFromMenus(tempMenus, tMenu.getString("gId"));
            if (tempMenu == null) {
                tempMenu = new JSONObject();
                tempMenu.put("id", tMenu.getString("gId"));
                tempMenu.put("icon", tMenu.getString("icon"));
                tempMenu.put("name", tMenu.getString("menuGroupName"));
                tempMenu.put("label", tMenu.getString("label"));
                tempMenu.put("seq", tMenu.getString("menuGroupSeq"));
                tempMenu.put("childs", new JSONArray());
                tempMenus.add(tempMenu);
            }
            //获取孩子菜单
            JSONArray childs = tempMenu.getJSONArray("childs");
            JSONObject childMenu = new JSONObject();
            childMenu.put("name", tMenu.getString("menuName"));
            childMenu.put("href", tMenu.getString("url"));
            childMenu.put("seq", tMenu.getString("menuSeq"));
            childMenu.put("isShow", tMenu.getString("isShow"));
            childMenu.put("description", tMenu.getString("description"));
            childs.add(childMenu);
        }
        return new ResponseEntity<String>(tempMenus.toJSONString(), HttpStatus.OK);
    }
 
    /**
     * 在菜单列表查询菜单
     *
     * @param gId
     * @return
     */
    private JSONObject getMenuFromMenus(JSONArray tempMenus, String gId) {
        for (int tempIndex = 0; tempIndex < tempMenus.size(); tempIndex++) {
            if (tempMenus.getJSONObject(tempIndex).getString("id").equals(gId)) {
                return tempMenus.getJSONObject(tempIndex);
            }
        }
 
        return null;
    }
}