Browse Source

js授权接口修改

zjm 5 years ago
parent
commit
afefd6c3e2

+ 0 - 1
src/main/java/com/yingying/tourist/common/WechatUtil.java

@@ -40,7 +40,6 @@ public class WechatUtil {
40 40
 		}
41 41
 
42 42
 		ret.put("url", url);
43
-		ret.put("jsapi_ticket", jsapi_ticket);
44 43
 		ret.put("nonceStr", nonce_str);
45 44
 		ret.put("timestamp", timestamp);
46 45
 		ret.put("signature", signature);

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

@@ -5,6 +5,7 @@ import java.util.concurrent.TimeUnit;
5 5
 
6 6
 import org.apache.commons.lang3.StringUtils;
7 7
 import org.springframework.beans.factory.annotation.Autowired;
8
+import org.springframework.beans.factory.annotation.Value;
8 9
 import org.springframework.data.redis.core.StringRedisTemplate;
9 10
 import org.springframework.data.redis.core.ValueOperations;
10 11
 import org.springframework.stereotype.Service;
@@ -21,8 +22,12 @@ public class WechatServiceImpl implements WechatService {
21 22
 	@Autowired
22 23
 	private StringRedisTemplate redisTemplate;
23 24
 
24
-	private static String appId = "wx00b0504317473c38";
25
-	private static String appSecret = "88d1172e0a53444298efe61c763bf894";
25
+	
26
+	
27
+	@Value("${weixin.appid}")
28
+	private String appId;
29
+	@Value("${weixin.appSecret}")
30
+	private String appSecret;
26 31
 
27 32
 	/**
28 33
 	 * 微信测试号

+ 32 - 36
src/main/java/com/yingying/tourist/web/WechatController.java

@@ -27,80 +27,76 @@ import lombok.extern.slf4j.Slf4j;
27 27
 @Controller
28 28
 @Slf4j
29 29
 public class WechatController {
30
-	
30
+
31 31
 	@Autowired
32 32
 	private WechatService wechatService;
33
-	
33
+
34 34
 	@Autowired
35 35
 	private UserService userService;
36
-	
36
+
37 37
 	@Autowired
38 38
 	private StringRedisTemplate redisTemplate;
39 39
 
40
-	@RequestMapping(value="token")
40
+	@RequestMapping(value = "token")
41 41
 	@ResponseBody
42 42
 	public String checkToken(String signature, String echostr, String timestamp, String nonce) {
43 43
 
44 44
 		return echostr;
45 45
 	}
46
-	
47
-	
48
-	@RequestMapping(value="checkJsAuth")
46
+
47
+	@RequestMapping(value = "checkJsAuth")
49 48
 	@ResponseBody
50 49
 	public MessageResult<String> checkJsAuth(String url) {
51
-		
50
+
51
+		// 先从redis取jsapi_ticket,如果未取到,调用微信接口获取
52 52
 		ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
53
-		String value = opsForValue.get("jsapi_ticket");
54
-		if(StringUtils.isNoneBlank(value)) {
55
-			return new MessageResult<String>().ok(value);
53
+		String jsapiTicket = opsForValue.get("jsapi_ticket");
54
+		if (StringUtils.isBlank(jsapiTicket)) {
55
+			jsapiTicket = wechatService.getTicket();
56
+			opsForValue.set("jsapi_ticket", jsapiTicket, 7200, TimeUnit.SECONDS);
56 57
 		}
57
-			
58
-		String jsapi_ticket = wechatService.getTicket();
59
-		
60
-		opsForValue.set("jsapi_ticket", jsapi_ticket, 7200, TimeUnit.SECONDS);
61
-		
62
-		
63
-		Map<String, String> map = WechatUtil.sign(jsapi_ticket, url);
64
-		
58
+		Map<String, String> map = WechatUtil.sign(jsapiTicket, url);
59
+
65 60
 		String sign = JSON.toJSONString(map);
66
-		
67
-		if(StringUtils.isBlank(sign)) {
61
+
62
+		if (StringUtils.isBlank(sign)) {
68 63
 			return new MessageResult<String>().failure(ErrorCode.JS_SIGN_NULL);
69 64
 		}
70
-		
71
-		 return new MessageResult<String>().ok(sign);
72
-		
65
+
66
+		return new MessageResult<String>().ok(sign);
67
+
73 68
 	}
74
-	@RequestMapping(value="code",produces="application/json;charset=utf-8")
69
+
70
+	@RequestMapping(value = "code", produces = "application/json;charset=utf-8")
75 71
 	@ResponseBody
76 72
 	public MessageResult<String> code(String code) {
77 73
 		String result = wechatService.getAuthToken(code);
78
-		JSONObject jsonObject = JSONObject.parseObject(result);	
74
+		JSONObject jsonObject = JSONObject.parseObject(result);
79 75
 		String openId = jsonObject.getString("openid");
80 76
 		String token = jsonObject.getString("access_token");
81
-		
82
-		if(StringUtils.isBlank(openId)) {
77
+
78
+		if (StringUtils.isBlank(openId)) {
83 79
 			return new MessageResult<String>().failure(ErrorCode.OPENID_NULL);
84 80
 		}
85
-		
81
+
86 82
 		// 首先查询openid 是否在数据库中,如果存在直接返回openid
87 83
 		EntityWrapper<User> userWrapper = new EntityWrapper<>();
88
-        userWrapper.eq("openid", openId);
89
-        User user = userService.selectOne(userWrapper);
90
-        if (user != null) {
84
+		userWrapper.eq("openid", openId);
85
+		User user = userService.selectOne(userWrapper);
86
+		if (user != null) {
91 87
 			return new MessageResult<String>().ok(openId);
92 88
 		}
93
-			
89
+
94 90
 		try {
95 91
 			// 如果不存在,通过token和openid去获取用户信息,并插入到数据库中
96 92
 			User wechatUser = wechatService.getWechatUser(token, openId);
97
-			if(wechatUser != null) {
93
+			if (wechatUser != null) {
98 94
 				userService.insert(wechatUser);
99
-			}	
95
+			}
100 96
 		} catch (UnsupportedEncodingException e) {
101 97
 			log.info("编码集转换失败");
102 98
 		}
103
-		
99
+
104 100
 		return new MessageResult<String>().ok(openId);
105 101
 	}
106 102
 }