Browse Source

代码评审,微信代码优化

liuam 7 years ago
parent
commit
3eb59b4f23

+ 13 - 29
src/main/java/com/uas/platform/b2c/common/weixin/contoller/WeChatController.java

@@ -10,7 +10,6 @@ import org.apache.commons.lang3.ArrayUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.ui.Model;
 import org.springframework.ui.ModelMap;
 import org.springframework.util.StringUtils;
 import org.springframework.web.bind.annotation.RequestBody;
@@ -18,10 +17,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RestController;
 
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import java.io.IOException;
-import java.io.PrintWriter;
 import java.util.List;
 
 /**
@@ -52,32 +47,20 @@ public class WeChatController {
     /**
      * 与微信服务器接口配置
      *
-     * @param model
-     * @param request
-     * @param response
+     * @param signature 与微信服务器接口配置
+     * @param timestamp 时间戳
+     * @param nonce 随机数
+     * @param echostr 随机字符串
      */
     @RequestMapping(value = "/check", method = RequestMethod.GET)
-    public void wenxinCheck(Model model, HttpServletRequest request, HttpServletResponse response) {
-        // 微信加密签名
-        String signature = request.getParameter("signature");
-        // 时间戳
-        String timestamp = request.getParameter("timestamp");
-        // 随机数
-        String nonce = request.getParameter("nonce");
-        // 随机字符串
-        String echostr = request.getParameter("echostr");
+    public String wenxinCheck(String signature, String timestamp, String nonce, String echostr) {
         logger.info("与微信服务器接口配置 with signature: {} and timestamp: {} and nonce: {} and echostr: {}", signature, timestamp, nonce, echostr);
-        PrintWriter print;
+
         // 通过检验signature对请求进行校验,若校验成功则原样返回echostr,表示接入成功,否则接入失败
         if (signature != null && CheckoutUtil.checkSignature(signature, timestamp, nonce)) {
-            try {
-                print = response.getWriter();
-                print.write(echostr);
-                print.flush();
-            } catch (IOException e) {
-                e.printStackTrace();
-            }
+            return echostr;
         }
+        return null;
     }
 
     /**
@@ -96,7 +79,8 @@ public class WeChatController {
     /**
      * 发送模板消息
      *
-     * @return
+     * @param messages 需要发送模板的实体,包括uu号
+     * @return 提示信息
      */
     @RequestMapping(value = "/sendTemplateMessage", method = RequestMethod.POST)
     public ModelMap sendTemplateMessage(@RequestBody List<MessageModel> messages) {
@@ -107,8 +91,8 @@ public class WeChatController {
     /**
      * 绑定用户
      *
-     * @param user
-     * @return
+     * @param user 用户包装类型。传入 code 或密码
+     * @return 正确信息或者错误信息,前端使用
      */
     @RequestMapping(value = "/bindUser", method = RequestMethod.POST)
     public ModelMap bindUser(@RequestBody UserVo user) {
@@ -119,7 +103,7 @@ public class WeChatController {
     /**
      * 发送手机验证码
      * @param mobile 手机号
-     * @return
+     * @return 前端使用的提示信息
      */
     @RequestMapping(value = "/sendSmsCode", method = RequestMethod.GET)
     public ModelMap sendSmsCode(String mobile) {

+ 3 - 1
src/main/java/com/uas/platform/b2c/common/weixin/model/MessageModel.java

@@ -1,7 +1,9 @@
 package com.uas.platform.b2c.common.weixin.model;
 
 /**
- * 消息
+ * 模板消息实体类
+ * @author liuam
+ * @date 2018年9月25日17:11:32
  */
 public class MessageModel {
     /**

+ 10 - 4
src/main/java/com/uas/platform/b2c/common/weixin/service/impl/WeChatServiceImpl.java

@@ -38,6 +38,7 @@ import java.util.TreeMap;
 import java.util.concurrent.TimeUnit;
 
 /**
+ * 微信服务类
  * @author liusw
  * @date 2018-05-30 9:24
  */
@@ -55,12 +56,12 @@ public class WeChatServiceImpl implements WeChatService{
     /**
      * 保存到 redis 里的 ACCESS_TOKEN 过期时间(second)
      */
-    private static final Integer ACCESS_TOKEN_EXPIRES_IN = 3600;
+    private static final int ACCESS_TOKEN_EXPIRES_IN = 3600;
 
     /**
      * 保存到 redis 里的验证码过期时间(second)
      */
-    private static final Integer SMS_EXPIRES_IN = 10 * 60;
+    private static final int SMS_EXPIRES_IN = 10 * 60;
 
     @Override
     public ModelMap getWxUserInfo(String code, String state, String openId) {
@@ -100,6 +101,11 @@ public class WeChatServiceImpl implements WeChatService{
         return result;
     }
 
+    /**
+     * User 类型转换成 UserAccout 类型,账户中心使用
+     * @param user 商城 user 实体
+     * @return
+     */
     private UserAccount convertUserAccount(User user) {
         UserAccount userAccount = new UserAccount();
         userAccount.setAppId("mall");
@@ -257,7 +263,7 @@ public class WeChatServiceImpl implements WeChatService{
      * 通过code获取用户openId
      * @param code
      */
-    private AuthUserInfo getAccessTokenByCode(String code) throws Exception {
+    private AuthUserInfo getAccessTokenByCode(String code) throws WeChatException {
         logger.info("通过code获取用户openId, code: {}", code);
         long start = System.currentTimeMillis();
         AuthTokenParams authTokenParams = new AuthTokenParams(WeChatUtil.APPID, WeChatUtil.APPSECRET, code, "authorization_code");
@@ -281,7 +287,7 @@ public class WeChatServiceImpl implements WeChatService{
      * @param openId
      * @return
      */
-    private AuthUserInfo getUserInfo(String accessToken, String openId) throws Exception {
+    private AuthUserInfo getUserInfo(String accessToken, String openId) throws WeChatException {
         logger.info("获取用户信息 accessToken: {}, openId: {}", accessToken, openId);
         // 返回国家地区语言版本,zh_CN 简体,zh_TW 繁体,en 英语
         AuthUserParams authUserParams = new AuthUserParams(accessToken, openId, "zh_CN");

+ 4 - 5
src/main/java/com/uas/platform/b2c/common/weixin/util/CheckoutUtil.java

@@ -25,7 +25,6 @@ public class CheckoutUtil {
     public static boolean checkSignature(String signature, String timestamp, String nonce) {
         String[] arr = new String[] { token, timestamp, nonce };
         // 将token、timestamp、nonce三个参数进行字典序排序
-        // Arrays.sort(arr);
         sort(arr);
         StringBuilder content = new StringBuilder();
         for (int i = 0; i < arr.length; i++) {
@@ -69,14 +68,14 @@ public class CheckoutUtil {
      * @return
      */
     private static String byteToHexStr(byte mByte) {
-        char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
+        char[] digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
         char[] tempArr = new char[2];
-        tempArr[0] = Digit[(mByte >>> 4) & 0X0F];
-        tempArr[1] = Digit[mByte & 0X0F];
+        tempArr[0] = digit[(mByte >>> 4) & 0X0F];
+        tempArr[1] = digit[mByte & 0X0F];
         String s = new String(tempArr);
         return s;
     }
-    public static void sort(String a[]) {
+    public static void sort(String[] a) {
         for (int i = 0; i < a.length - 1; i++) {
             for (int j = i + 1; j < a.length; j++) {
                 if (a[j].compareTo(a[i]) < 0) {

+ 0 - 1
src/main/java/com/uas/platform/b2c/common/weixin/util/HttpReqUtil.java

@@ -82,7 +82,6 @@ public class HttpReqUtil {
                         hasParams = true;
                     }
                     if (charset != null && !"".equals(charset)) {
-                        // builder.append(key).append("=").append(URLDecoder.(value,charset));
                         builder.append(key).append("=").append(urlEncode(value, charset));
                     } else {
                         builder.append(key).append("=").append(value);