Browse Source

微信接口修改

zjm 5 years ago
parent
commit
da7c3334a5

+ 61 - 58
src/main/java/com/yingying/tourist/exception/ErrorCode.java

@@ -1,58 +1,61 @@
1
-package com.yingying.tourist.exception;
2
-
3
-/**
4
- * @Author: XPF
5
- * @Description:
6
- * @Date: Created in 12:02 2018/6/30
7
- * @Modified by:
8
- */
9
-public enum ErrorCode {
10
-
11
-    SERVER_OK(200, "请求成功"),
12
-    /**
13
-     * 服务端异常
14
-     */
15
-    SERVER_SYSERR(1001, "系统错误"),
16
-    SERVER_BIZERR(1002, "业务逻辑异常"),
17
-    PARAMETERS_ERROR(1003, "参数异常"),
18
-    SESSION_TIMEOUT(1004, "登录超时"),
19
-    PARAM_BINDINGERR(1005, "拦截绑定参数异常"),
20
-    DATA_ERROR(1006, "数据异常"),
21
-    DATA_CURDERR(1007, "数据CURD操作失败"),
22
-    CONNECTION_COINERR(1008,"访问coin服务器失败"),
23
-    
24
-    // 2000以上 错误是业务操作问题
25
-    NOT_LOGIN(2000,"请先登录"),
26
-    FAILURE_LOGIN(2000,"登录失效"),
27
-    HAVE_HAPPENED(2001,"已经发生请勿重复操作,如若没有收到验证码,请在五分钟以后再次操作"),
28
-	CHOICE_VALID_COIN(2002,"请输入正确的币种id"),
29
-	REPEAT_ADD_COIN(2003,"请勿重复添加自选"),
30
-	REPEAT_DEL_COIN(2004,"请勿重复删除自选"),
31
-	CHOICE_VALID_COIN_PAIR(2005,"请输入正确的币对id"),
32
-    ;
33
-
34
-    private int code;
35
-    private String msg;
36
-
37
-    ErrorCode(int code, String msg) {
38
-        this.code = code;
39
-        this.msg = msg;
40
-    }
41
-
42
-    public String getMsg() {
43
-        return msg;
44
-    }
45
-
46
-    public int getCode() {
47
-        return code;
48
-    }
49
-
50
-    @Override
51
-    public String toString() {
52
-        final StringBuffer sb = new StringBuffer("ErrorCode{");
53
-        sb.append("code=").append(code);
54
-        sb.append(", msg='").append(msg).append('\'');
55
-        sb.append('}');
56
-        return sb.toString();
57
-    }
58
-}
1
+package com.yingying.tourist.exception;
2
+
3
+/**
4
+ * @Author: XPF
5
+ * @Description:
6
+ * @Date: Created in 12:02 2018/6/30
7
+ * @Modified by:
8
+ */
9
+public enum ErrorCode {
10
+
11
+    SERVER_OK(200, "请求成功"),
12
+    /**
13
+     * 服务端异常
14
+     */
15
+    SERVER_SYSERR(1001, "系统错误"),
16
+    SERVER_BIZERR(1002, "业务逻辑异常"),
17
+    PARAMETERS_ERROR(1003, "参数异常"),
18
+    SESSION_TIMEOUT(1004, "登录超时"),
19
+    PARAM_BINDINGERR(1005, "拦截绑定参数异常"),
20
+    DATA_ERROR(1006, "数据异常"),
21
+    DATA_CURDERR(1007, "数据CURD操作失败"),
22
+    CONNECTION_COINERR(1008,"访问coin服务器失败"),
23
+    
24
+    // 2000以上 错误是业务操作问题
25
+    NOT_LOGIN(2000,"请先登录"),
26
+    FAILURE_LOGIN(2000,"登录失效"),
27
+    HAVE_HAPPENED(2001,"已经发生请勿重复操作,如若没有收到验证码,请在五分钟以后再次操作"),
28
+	CHOICE_VALID_COIN(2002,"请输入正确的币种id"),
29
+	REPEAT_ADD_COIN(2003,"请勿重复添加自选"),
30
+	REPEAT_DEL_COIN(2004,"请勿重复删除自选"),
31
+	CHOICE_VALID_COIN_PAIR(2005,"请输入正确的币对id"),
32
+	
33
+	// 3000-3100微信端错误
34
+	JS_SIGN_NULL(3000, "未获取到JS授权验证"),
35
+	OPENID_NULL(3001,"未获取到OPENID");
36
+
37
+    private int code;
38
+    private String msg;
39
+
40
+    ErrorCode(int code, String msg) {
41
+        this.code = code;
42
+        this.msg = msg;
43
+    }
44
+
45
+    public String getMsg() {
46
+        return msg;
47
+    }
48
+
49
+    public int getCode() {
50
+        return code;
51
+    }
52
+
53
+    @Override
54
+    public String toString() {
55
+        final StringBuffer sb = new StringBuffer("ErrorCode{");
56
+        sb.append("code=").append(code);
57
+        sb.append(", msg='").append(msg).append('\'');
58
+        sb.append('}');
59
+        return sb.toString();
60
+    }
61
+}

+ 3 - 1
src/main/java/com/yingying/tourist/service/WechatService.java

@@ -1,5 +1,7 @@
1 1
 package com.yingying.tourist.service;
2 2
 
3
+import java.io.UnsupportedEncodingException;
4
+
3 5
 import com.yingying.tourist.domain.User;
4 6
 
5 7
 public interface WechatService {
@@ -34,6 +36,6 @@ public interface WechatService {
34 36
 	 * @param openId
35 37
 	 * @return
36 38
 	 */
37
-	User getWechatUser(String token, String openId);
39
+	User getWechatUser(String token, String openId) throws UnsupportedEncodingException;
38 40
 
39 41
 }

+ 6 - 2
src/main/java/com/yingying/tourist/service/impl/WechatServiceImpl.java

@@ -1,5 +1,7 @@
1 1
 package com.yingying.tourist.service.impl;
2 2
 
3
+import java.io.UnsupportedEncodingException;
4
+
3 5
 import org.springframework.stereotype.Service;
4 6
 
5 7
 import com.alibaba.fastjson.JSON;
@@ -74,11 +76,13 @@ public class WechatServiceImpl implements WechatService {
74 76
 	
75 77
 	
76 78
 	@Override
77
-	public User getWechatUser(String token, String openId) {
79
+	public User getWechatUser(String token, String openId) throws UnsupportedEncodingException {
78 80
 		String url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + token + "&openid=" + openId + "&lang=zh_CN";
79 81
 		String result = HttpRequestSimple.doGet(url);
80 82
 		
81
-		User user = JSON.parseObject(result, User.class);
83
+		String decodeResult = new String(result.getBytes("ISO-8859-1"), "UTF-8");
84
+		
85
+		User user = JSON.parseObject(decodeResult, User.class);
82 86
 		
83 87
 		return user;
84 88
 	  

+ 34 - 4
src/main/java/com/yingying/tourist/web/WechatController.java

@@ -1,7 +1,9 @@
1 1
 package com.yingying.tourist.web;
2 2
 
3
+import java.io.UnsupportedEncodingException;
3 4
 import java.util.Map;
4 5
 
6
+import org.apache.commons.lang3.StringUtils;
5 7
 import org.springframework.beans.factory.annotation.Autowired;
6 8
 import org.springframework.stereotype.Controller;
7 9
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -9,13 +11,18 @@ import org.springframework.web.bind.annotation.ResponseBody;
9 11
 
10 12
 import com.alibaba.fastjson.JSON;
11 13
 import com.alibaba.fastjson.JSONObject;
14
+import com.baomidou.mybatisplus.mapper.EntityWrapper;
12 15
 import com.yingying.tourist.common.WechatUtil;
13 16
 import com.yingying.tourist.domain.User;
17
+import com.yingying.tourist.exception.ErrorCode;
14 18
 import com.yingying.tourist.result.MessageResult;
15 19
 import com.yingying.tourist.service.UserService;
16 20
 import com.yingying.tourist.service.WechatService;
17 21
 
22
+import lombok.extern.slf4j.Slf4j;
23
+
18 24
 @Controller
25
+@Slf4j
19 26
 public class WechatController {
20 27
 	
21 28
 	@Autowired
@@ -42,19 +49,42 @@ public class WechatController {
42 49
 		
43 50
 		String sign = JSON.toJSONString(map);
44 51
 		
52
+		if(StringUtils.isBlank(sign)) {
53
+			return new MessageResult<String>().failure(ErrorCode.JS_SIGN_NULL);
54
+		}
55
+		
45 56
 		 return new MessageResult<String>().ok(sign);
46 57
 		
47 58
 	}
48
-	@RequestMapping(value="code")
59
+	@RequestMapping(value="code",produces="application/json;charset=utf-8")
49 60
 	@ResponseBody
50
-	public MessageResult<String>  code(String code) {
61
+	public MessageResult<String> code(String code) {
51 62
 		String result = wechatService.getAuthToken(code);
52 63
 		JSONObject jsonObject = JSONObject.parseObject(result);	
53 64
 		String openId = jsonObject.getString("openid");
54 65
 		String token = jsonObject.getString("access_token");
55 66
 		
56
-		User wechatUser = wechatService.getWechatUser(token, openId);
57
-		userService.insert(wechatUser);
67
+		if(StringUtils.isBlank(openId)) {
68
+			return new MessageResult<String>().failure(ErrorCode.OPENID_NULL);
69
+		}
70
+		
71
+		// 首先查询openid 是否在数据库中,如果存在直接返回openid
72
+		EntityWrapper<User> userWrapper = new EntityWrapper<>();
73
+        userWrapper.eq("openid", openId);
74
+        User user = userService.selectOne(userWrapper);
75
+        if (user != null) {
76
+			return new MessageResult<String>().ok(openId);
77
+		}
78
+			
79
+		try {
80
+			// 如果不存在,通过token和openid去获取用户信息,并插入到数据库中
81
+			User wechatUser = wechatService.getWechatUser(token, openId);
82
+			if(wechatUser != null) {
83
+				userService.insert(wechatUser);
84
+			}	
85
+		} catch (UnsupportedEncodingException e) {
86
+			log.info("编码集转换失败");
87
+		}
58 88
 		
59 89
 		return new MessageResult<String>().ok(openId);
60 90
 	}