//新版接口 if (StringUtils.isBlank(iv)) { String url = String.format(AppletUtils.LOGIN_GET_PHONE, miniMessageService.getDoctorMiniAccessToken()); JSONObject request = new JSONObject(); request.put("code", phoneCode); ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class); // 解析相应内容(转换成json对象) String sr = response.getBody(); JSONObject json = JSONObject.parseObject(sr); log.info("新版接口获取手机号返回" + json.toString()); if (json.get("errcode") != null && !"0".equals(json.getString("errcode"))) { throw new Exception(json.getString("errmsg")); } String phoneInfo = json.getString("phone_info"); if (StringUtils.isNotBlank(phoneInfo)) { JSONObject phoneInfoTemp = JSONObject.parseObject(phoneInfo); return phoneInfoTemp.getString("phoneNumber"); } } else { //旧版接口 log.info("旧版接口解密开始:sessionKey:"+sessionKey); String result = AesCommonUtils.decryptWX(phoneCode, sessionKey, iv, "UTF-8"); log.info("旧版接口解密:"+result); if (null != result && result.length() > 0) { JSONObject json = JSONObject.parseObject(result); if (json.containsKey("phoneNumber")) { return json.getString("phoneNumber"); } } }
时间: 2024-02-15 16:28:26 浏览: 28
这是一段Java代码,大概的逻辑如下:
1. 检查参数iv是否为空,如果为空,则使用新版接口获取手机号码,否则使用旧版接口。
2. 如果使用新版接口,则构造请求URL,将手机号码验证码等信息封装成json格式请求体,使用RestTemplate发送POST请求,并接收响应体。
3. 解析响应体,如果errcode不为0,则抛出异常。
4. 从响应体中获取phone_info字段,解析出其中的phoneNumber字段并返回。
5. 如果使用旧版接口,则调用AesCommonUtils.decryptWX方法对phoneCode进行解密,得到解密后的字符串。
6. 解析得到的字符串,如果包含phoneNumber字段,则返回该字段的值。
需要注意的是,这段代码中使用了一些第三方库和工具类,例如StringUtils、JSONObject、RestTemplate等,这些工具类需要提前导入并配置好。
相关问题
StringUtils.isBlank和StringUtils.isEmpty区别
StringUtils.isBlank和StringUtils.isEmpty都是来自于Apache Commons Lang库的工具方法,用于判断字符串是否为空。
StringUtils.isEmpty方法用于判断字符串是否为空或长度为0或由空白符(whitespace)构成,返回布尔值。例如:
```
StringUtils.isEmpty(null) = true
StringUtils.isEmpty("") = true
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty(" ") = false
StringUtils.isEmpty("abc") = false
StringUtils.isEmpty(" abc ") = false
```
StringUtils.isBlank方法则更加严格,用于判断字符串是否为空或长度为0或由空白符(whitespace)构成,其中空白符包括空格、制表符、换行符等,也返回布尔值。例如:
```
StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank("abc") = false
StringUtils.isBlank(" abc ") = false
```
因此,如果只需要判断字符串是否为空或长度为0,可以使用StringUtils.isEmpty方法;如果需要判断字符串是否为空或长度为0或仅由空白符构成,应该使用StringUtils.isBlank方法。
if(StringUtils.isBlank(seat) && StringUtils.isBlank(cabin) && StringUtils.isBlank(personal)) {return null;}
这段代码使用了StringUtils类中的isBlank方法,判断了三个字符串变量seat、cabin和personal是否为空或者只包含空格。如果这三个变量都为空或者只包含空格,那么返回null。isBlank方法与isEmpty方法的区别在于,isBlank方法不仅会判断字符串是否为空,还会判断字符串是否只包含空格。而isEmpty方法只会判断字符串是否为空。
```java
if (StringUtils.isBlank(seat) && StringUtils.isBlank(cabin) && StringUtils.isBlank(personal)) {
return null;
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)