双重预防项目-国泰新华二开定制版
SZH
2022-08-20 f9f0687195e0fe349185437d22c495d74c8d4a20
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
package com.ruoyi.project.mobile.controller;
 
 
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.mobile.domain.ApiRequestHeader;
import com.ruoyi.project.mobile.domain.ApiResult;
import com.ruoyi.project.mobile.service.ApiService;
import com.ruoyi.project.mobile.service.ApiSystemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
 
/**
 * APP使用的统一接口
 */
@Api("安卓总控制器")
@Controller
@RequestMapping(value = "/mobile/api")
public class ApiController extends BaseMobileController {
 
    private static Logger logger = LoggerFactory.getLogger(ApiController.class);
 
    @Autowired
    ApiService service;
 
    @Autowired
    private ServerConfig serverConfig;
 
    @Autowired
    ApiSystemService apiSystemService;
 
    @ApiOperation("登录Index")
    @RequestMapping(value = "/index", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult index(@RequestBody String str, HttpServletRequest request) {
        ApiResult apiResult = null;
        ApiRequestHeader header = parseHeader(request);
        try {
            logger.debug("╔════════════════════════════════════════════════════════════════════════════════════════");
            logger.debug("║ " + header.code);
            logger.debug("╟────────────────────────────────────────────────────────────────────────────────────────");
            logger.debug("║ token:" + header.token);
            logger.debug("║ deviceType:" + header.deviceType);
            logger.debug("║ deviceId:" + header.deviceId);
            logger.debug("║ appType:" + header.appType);
            logger.debug("║ loginName:" + header.loginName);
            logger.debug("║ userId:" + header.userId);
            logger.debug("╟────────────────────────────────────────────────────────────────────────────────────────");
            logger.debug("║ " + str);
            logger.debug("╚════════════════════════════════════════════════════════════════════════════════════════");
 
            if (StringUtils.isEmpty(header.deviceType)) {
                return ApiResult.error("设备类型不能为空");
            }
            if (StringUtils.isEmpty(header.deviceId)) {
                return ApiResult.error("设备id不能为空");
            }
            if (StringUtils.isEmpty(header.appType)) {
                return ApiResult.error("app类型不能为空");
            }
            if (StringUtils.isEmpty(header.loginName)) {
                return ApiResult.error("用户登录帐号不能为空");
            }else{
                String loginName  = java.net.URLDecoder.decode(header.loginName,"utf-8");
                header.loginName = loginName;
            }
            if (StringUtils.isEmpty(header.code)) {
                return ApiResult.error("请求类型不能为空");
            }
            apiResult = processByReflect(str, header);
        } catch (Exception ex) {
            logger.error(ex.getLocalizedMessage());
            ApiResult.error("异常");
        }
        return apiResult;
 
    }
 
 
    private ApiResult processByReflect(String str, ApiRequestHeader header) {
        Class clazz = service.getClass();
        Method mehtod;
        try {
            mehtod = clazz.getDeclaredMethod(header.code, String.class, ApiRequestHeader.class);
        } catch (Exception ex) {
            logger.error(ex.getLocalizedMessage());
            return ApiResult.error("业务[" + header.code + "]不存在!");
        }
        ApiResult apiResult;
        //分发
        try {
            apiResult = (ApiResult) mehtod.invoke(service, str, header);
        } catch (IllegalAccessException ex) {
            logger.error(ex.getLocalizedMessage());
            //TODO:正式部署,把后面的异常信息移除
            apiResult = ApiResult.error("业务[" + header.code + "]process1执行异常!" + ex.getLocalizedMessage());
        } catch (InvocationTargetException ex) {
            logger.error(ex.getLocalizedMessage());
            String msg = ex.getTargetException().getLocalizedMessage();
            logger.error("targetexception: " + msg);
            //TODO:正式部署,把后面的异常信息移除
            apiResult = ApiResult.error("业务[" + header.code + "]process2执行异常!" + msg);
        }
        return apiResult;
    }
 
 
    /**
     * 手机用上传方法
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/upload")
    @ResponseBody
    public AjaxResult uploadImg(MultipartFile file) throws Exception {
        try {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.put("imageName", fileName);
            ajax.put("imageUrl", url);
 
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
 
}