Browse Source

初步完成

刘一凡 3 years ago
parent
commit
6b5e5a9a58

+ 2 - 1
src/main/java/com/chuanghai/h3c_reporting/common/exception/BizCodeEnume.java

@@ -16,7 +16,8 @@ public enum BizCodeEnume {
     BODY_IS_EMPTY(11002, "body为空"),
     BODY_IS_EMPTY(11002, "body为空"),
     PARAMETER_ERROR(11003, "参数异常"),
     PARAMETER_ERROR(11003, "参数异常"),
     DATA_IS_EXIST(16000, "数据已存在"),
     DATA_IS_EXIST(16000, "数据已存在"),
-    DATA_NOT_EXIST(16000, "数据不存在");
+    DATA_NOT_EXIST(16000, "数据不存在"),
+    WX_EXCEPTION(17000, "微信相关异常");
 
 
     private int code;
     private int code;
     private String msg;
     private String msg;

+ 41 - 3
src/main/java/com/chuanghai/h3c_reporting/controller/InformationReportingController.java

@@ -9,17 +9,23 @@ import com.chuanghai.h3c_reporting.dto.ReportingDownloadDTO;
 import com.chuanghai.h3c_reporting.entity.InformationReporting;
 import com.chuanghai.h3c_reporting.entity.InformationReporting;
 import com.chuanghai.h3c_reporting.entity.User;
 import com.chuanghai.h3c_reporting.entity.User;
 import com.chuanghai.h3c_reporting.service.InformationReportingService;
 import com.chuanghai.h3c_reporting.service.InformationReportingService;
+import com.chuanghai.h3c_reporting.vo.InformationReportingVO;
 import lombok.extern.slf4j.Slf4j;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.ibatis.annotations.Param;
 import org.apache.ibatis.annotations.Param;
+import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.bind.annotation.*;
 
 
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpServletResponse;
+import javax.validation.Valid;
+import java.time.LocalDateTime;
 import java.util.ArrayList;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Arrays;
 import java.util.List;
 import java.util.List;
 
 
-/**  报备模块
+/**
+ * 报备模块
+ *
  * @author 27951
  * @author 27951
  * @version 1.0
  * @version 1.0
  * @description: TODO
  * @description: TODO
@@ -34,8 +40,40 @@ public class InformationReportingController {
     private InformationReportingService informationReportingService;
     private InformationReportingService informationReportingService;
 
 
     /**
     /**
-    * 信息上传
-    */
+     * 信息上传
+     */
+    @PostMapping("/add")
+    public CommonResult<String> add(@Valid @RequestBody InformationReportingVO staffEntity) {
+//        if (!StringUtils.hasText(staffEntity.getName())) {
+//            throw new RRException(BizCodeEnume.PARAMETER_ERROR, "姓名不能为空");
+//        }
+//        if (!StringUtils.hasText(staffEntity.getPhone())) {
+//            throw new RRException(BizCodeEnume.PARAMETER_ERROR, "手机号不能为空");
+//        }
+//        if (!StringUtils.hasText(staffEntity.getWxPhone())) {
+//            throw new RRException(BizCodeEnume.PARAMETER_ERROR, "微信绑定手机号不能为空");
+//        }
+//        if (!StringUtils.hasText(staffEntity.getCompany())) {
+//            throw new RRException(BizCodeEnume.PARAMETER_ERROR, "单位名称不能为空");
+//        }
+//        if (!StringUtils.hasText(staffEntity.getContent())) {
+//            throw new RRException(BizCodeEnume.PARAMETER_ERROR, "事件登记不能为空");
+//        }
+
+        InformationReporting reporting = new InformationReporting();
+        BeanUtils.copyProperties(staffEntity, reporting);
+        LocalDateTime localDateTime = LocalDateTime.now();
+        reporting.setReportingTime(localDateTime);
+        reporting.setStatus(1);
+        boolean flag = informationReportingService.save(reporting);
+        if (flag) {
+            log.info("上传信息=========>>> 【{}】【微信绑定手机号:{}】成功上传了编号为【{}】的项目信息", reporting.getName(), reporting.getWxPhone(), reporting.getId());
+            return CommonResult.ok();
+        } else {
+            log.info("上传信息失败=========>>> 【{}】【微信绑定手机号:{}】上传编号为【{}】的项目信息失败", reporting.getName(), reporting.getWxPhone(), reporting.getId());
+            return CommonResult.fail();
+        }
+    }
 
 
     /**
     /**
      * 删除信息
      * 删除信息

+ 109 - 0
src/main/java/com/chuanghai/h3c_reporting/controller/WxController.java

@@ -0,0 +1,109 @@
+package com.chuanghai.h3c_reporting.controller;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.chuanghai.h3c_reporting.common.exception.BizCodeEnume;
+import com.chuanghai.h3c_reporting.common.exception.RRException;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.client.RestTemplate;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * 获取微信绑定手机号
+ *
+ * @Author 浮生
+ * @Date 2023/3/12 14:46
+ * @PackageName:com.chuanghai.h3c_reporting.controller
+ * @ClassName: WxController
+ * @Description: TODO
+ * @Version 1.0
+ */
+@Controller
+@RequestMapping("/wx")
+public class WxController {
+
+    @Value("${wx.app_id}")
+    private String appID;
+
+    @Value("${wx.secret}")
+    private String secret;
+
+    private String access_token = "";
+
+    /**
+     * 通过code换取用户手机号
+     */
+    @GetMapping("/getPhone")
+    public String getPhone(String code, String state) {
+        if (access_token.equals("")) {
+            getAccessToken();
+        }
+        try {
+            RestTemplate restTemplate = new RestTemplate();
+            HttpHeaders headers = new HttpHeaders();
+            Map<String, String> paramCardNumber = new HashMap<>();
+//            paramCardNumber.put("access_token", access_token);
+            paramCardNumber.put("code", code);
+            HttpEntity<Map<String, String>> requestM = new HttpEntity<>(paramCardNumber, headers);
+            String userInfoUrl = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + access_token;
+            ResponseEntity<String> responsem = restTemplate.postForEntity(userInfoUrl, requestM, String.class);
+            JSONObject obj = JSON.parseObject(responsem.getBody());
+
+            String phoneNumber = "";
+            ObjectMapper objectMapper = new ObjectMapper();
+            if (obj != null) {
+                try {
+                    Map<String, Object> jsonMap = objectMapper.readValue(obj.toString(), new TypeReference<Map<String, Object>>() {
+                    });
+                    phoneNumber = jsonMap.get("phoneNumber").toString();
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+            }
+            return phoneNumber;
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new RRException(BizCodeEnume.WX_EXCEPTION, "获取用户绑定的手机号失败");
+        }
+    }
+
+    // 获取access_token
+    @Scheduled(cron = "0 59 0/1 * * ?")   // 每1小时59分,执行一次刷新access_token
+    public void getAccessToken() {
+        Map<String, String> token = new HashMap<>();
+        token.put("appid", appID);
+        token.put("secret", secret);
+        token.put("grant_type", "client_credentials");
+        String url = "https://api.weixin.qq.com/cgi-bin/token";
+
+        RestTemplate restTemplate = new RestTemplate();
+        HttpHeaders headers = new HttpHeaders();
+        headers.setContentType(MediaType.APPLICATION_JSON);
+        HttpEntity<Map<String, String>> request = new HttpEntity<>(token, headers);
+        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, request, String.class);
+        JSONObject objParam = JSON.parseObject(responseEntity.getBody());
+        for (Map.Entry<String, Object> entry : objParam.entrySet()) {
+            Object o = entry.getValue();
+            if (o instanceof String) {
+                if (entry.getKey().equals("access_token")) {
+                    access_token = (String) entry.getValue();
+                }
+            }
+        }
+//        System.out.println(access_token);
+//        return access_token;
+    }
+
+}

+ 2 - 2
src/main/java/com/chuanghai/h3c_reporting/entity/InformationReporting.java

@@ -42,10 +42,10 @@ public class InformationReporting implements Serializable {
     private String phone;
     private String phone;
 
 
     /**
     /**
-     * 微信绑定电话
+     * 微信绑定手机号
      */
      */
     @ExcelIgnore
     @ExcelIgnore
-    @ExcelProperty("微信绑定电话")
+    @ExcelProperty("微信绑定手机号")
     @ColumnWidth(15)
     @ColumnWidth(15)
     private String wxPhone;
     private String wxPhone;
 
 

+ 87 - 0
src/main/java/com/chuanghai/h3c_reporting/util/WxUtils.java

@@ -0,0 +1,87 @@
+//package com.chuanghai.h3c_reporting.util;
+//
+//import com.alibaba.excel.util.StringUtils;
+//import com.alibaba.fastjson.JSON;
+//import com.alibaba.fastjson.JSONObject;
+//import com.chuanghai.h3c_reporting.common.exception.BizCodeEnume;
+//import com.chuanghai.h3c_reporting.common.exception.RRException;
+//import lombok.extern.slf4j.Slf4j;
+//import org.springframework.http.HttpStatus;
+//import org.springframework.http.client.ClientHttpResponse;
+//import org.springframework.stereotype.Component;
+//import org.springframework.web.client.ResponseErrorHandler;
+//import org.springframework.web.client.RestTemplate;
+//
+//import java.io.IOException;
+//import java.util.HashMap;
+//import java.util.Map;
+//
+///**
+// * @Author 浮生
+// * @Date 2023/3/12 14:30
+// * @PackageName:com.chuanghai.h3c_reporting.util
+// * @ClassName: WxUtils
+// * @Description: TODO
+// * @Version 1.0
+// */
+//@Component
+//@Slf4j
+//public class WxUtils {
+//    private final RestTemplate restTemplate;
+//
+//    {
+//        restTemplate = new RestTemplate();
+//        ResponseErrorHandler responseErrorHandler = new ResponseErrorHandler() {
+//            @Override
+//            public boolean hasError(ClientHttpResponse response) throws IOException {
+//                return false;
+//            }
+//
+//            @Override
+//            public void handleError(ClientHttpResponse response) throws IOException {
+//                HttpStatus statusCode = response.getStatusCode();
+//                String statusText = response.getStatusText();
+//                log.info("【微信接口请求失败】 errorCode={},errorMsg={}", statusCode, statusText);
+//                throw new RRException(BizCodeEnume.WX_EXCEPTION, "微信网络繁忙,请稍后再试。");
+//            }
+//        };
+//
+//        restTemplate.setErrorHandler(responseErrorHandler);
+//    }
+//
+//
+//
+//    /**
+//     * @params: []
+//     * @return: java.lang.String
+//     * @Description: 获取用户openId
+//     */
+//    public String code2Session(String appId, String secret, String code) {
+//
+//
+//        //组装参数
+//        Map<String, String> params = new HashMap<>();
+//        params.put("appid", appId);
+//        params.put("secret", secret);
+//        params.put("code", code);
+//        params.put("grant_type", "authorization_code");
+//
+//
+//        StringBuilder sb = new StringBuilder(UrlConstants.WX_CODE_2_SESSION);
+//        sb.append("?")
+//                .append("appid=").append(appId).append("&")
+//                .append("secret=").append(secret).append("&")
+//                .append("js_code=").append(code).append("&")
+//                .append("grant_type=").append("authorization_code");
+//
+//        String result = restTemplate.getForObject(sb.toString(), String.class);
+//
+//        JSONObject jsonResult = JSON.parseObject(result);
+//        String openid = jsonResult.getString("openid");
+//        if (StringUtils.isEmpty(openid)) {
+//            log.error("【微信code2Session失败!】url={},Params:{},wxResult={}", sb.toString(), JSON.toJSONString(params), result);
+//            throw new RRException(BizCodeEnume.WX_EXCEPTION, "获取用户信息失败,请稍后再试。");
+//        }
+//        return jsonResult.getString("openid");
+//    }
+//}

+ 49 - 0
src/main/java/com/chuanghai/h3c_reporting/vo/InformationReportingVO.java

@@ -0,0 +1,49 @@
+package com.chuanghai.h3c_reporting.vo;
+
+import lombok.Data;
+
+import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Pattern;
+
+/**
+ * @Author 浮生
+ * @Date 2023/3/12 13:36
+ * @PackageName:com.chuanghai.h3c_reporting.vo
+ * @ClassName: InformationReportingVO
+ * @Description: TODO
+ * @Version 1.0
+ */
+@Data
+public class InformationReportingVO {
+
+    /**
+     * 姓名
+     */
+    @NotNull(message = "姓名不能为空")
+    private String name;
+
+    /**
+     * 手机号
+     */
+    @NotNull(message = "手机号不能为空")
+    @Pattern(regexp = "^[1][3,4,5,6,7,8,9][0-9]{9}$", message = "手机号格式有误")
+    private String phone;
+
+    /**
+     * 微信绑定手机号
+     */
+    @NotNull(message = "微信绑定手机号不能为空")
+    private String wxPhone;
+
+    /**
+     * 单位名称
+     */
+    @NotNull(message = "单位名称不能为空")
+    private String company;
+
+    /**
+     * 事件登记
+     */
+    @NotNull(message = "事件登记不能为空")
+    private String content;
+}

File diff suppressed because it is too large
+ 4 - 0
src/main/resources/application.yml