当前位置:网站首页>The web application scans the code to obtain the user information of zhezheng nail
The web application scans the code to obtain the user information of zhezheng nail
2022-04-22 09:35:00 【WXF_ Sir】

1、 Page display pin QR code , Obtain according to the configuration information .
Developing documents The second implementation in , Embed the pin login QR code into the page of its own project
@RequestMapping("getAppId")
@ResponseBody
public WebResponse getAppId() throws Exception {
try {
Map res = new HashMap();
// Application identification ( Customize )
res.put("appFlag", WebConfig.DTALK_SCAN_APP_FLAG);
// token url ( Customize , Configuration path : Nail open platform developer background - My app details - Application configuration - Web configuration )
res.put("redirectUrl",WebConfig.DTALK_SCAN_APP_REDIRECT_URL);
// Login domain name
res.put("domainUrl",WebConfig.DTALK_SCAN_APP_DOMAIN_URL);
return WebResponse.resSuccess(" To be successful ", res);
} catch (Exception e) {
return WebResponse.resFail(" Acquisition failure ", null);
}
}
Configuration information ①: Application identification

Configuration information ②: token url
Configure the interface for obtaining user information after code scanning for next business operation ( Full address , No parameters , Note that different environments are configured with different addresses )

Configuration information ③: Login domain name ( Construct login page )
choice Saas Configuration parameters of the environment

2、 Get nail user information
@RequestMapping(value = "getUserData")
@ResponseBody
public String getUserData(String code){
try {
// application access_token
String accessToken = redisValueTemplate.hGetAll(WebConfig.DTALK_SCAN_REDIS_TOKEN_KEY).get(WebConfig.DTALK_SCAN_REDIS_TOKEN_KEY);
logger.info("access token:" + accessToken);
ExecutableClient executableClient = ExecutableClient.getInstance();
// Unique identification of the application key( namely App Key)
executableClient.setAccessKey(WebConfig.DTALK_SCAN_APP_ID);
// Applied **( namely App Secret)
executableClient.setSecretKey(WebConfig.DTALK_SCAN_CORP_SECRET);
// Open platform domain name
executableClient.setDomainName(WebConfig.DINGDOMAIN);
executableClient.setProtocal("https");
executableClient.init();
String api ="/rpc/oauth2/getuserinfo_bycode.json";
PostClient postClient = executableClient.newPostClient(api);
// Set parameters
postClient.addParameter("access_token", accessToken);
postClient.addParameter("code", code);
// call API
String apiResult = postClient.post();
logger.info(" Zhejiang government nails 2.0 obtain Scan the code and return the user information json:" + apiResult);
JSONObject json = JSON.parseObject(apiResult);
JSONObject content = JSON.parseObject(json.getJSONObject("content").toString());
String accountId = content.getJSONObject("data").getString("accountId");
String lastName = content.getJSONObject("data").getString("lastName");
Map res = new HashMap();
res.put("name",lastName);
res.put("userId",accountId);
return FastJsonUtil.toJSONString(res);
}catch (Exception e){
logger.error(" error ",e);
return null;
}
}
Configuration information ④: application access_token
Here is a configuration item customized by the developer , To access accessToken One of the key name , It has little to do with the docking of nail platform , Just need to get this regularly accessToken
Access to applications access_token Development documentation for
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@Component
public class RedisValueTemplate {
private static int dbIdx = 1;
@Autowired
private RedisTemplate<String, String> redisTemplate;
public void hMset(String key, final Map<String, String> map) {
redisTemplate.execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) throws DataAccessException {
connection.select(dbIdx);
connection.hMSet(key.getBytes(), fromStringToByte(map));
return null;
}
});
}
public Map<String, String> hGetAll(final String key) {
return (Map<String, String>) redisTemplate.execute(new RedisCallback<Object>() {
@Override
public Map<String, String> doInRedis(RedisConnection connection)
throws DataAccessException {
connection.select(dbIdx);
Map<String, String> map = fromByteToString(connection.hGetAll(key.getBytes()));
return map;
}
});
}
public void selectDb(final int dbId) {
dbIdx = dbId;
}
private Map<byte[], byte[]> fromStringToByte(Map<String, String> map) {
Map<byte[], byte[]> resultMap = new HashMap<byte[], byte[]>();
Set<String> keySet = map.keySet();
for (String key : keySet) {
String value = map.get(key);
resultMap.put(key.getBytes(), value.getBytes());
}
return resultMap;
}
private Map<String, String> fromByteToString(Map<byte[], byte[]> map) {
Map<String, String> resultMap = new HashMap<String, String>();
Set<byte[]> keySet = map.keySet();
for (byte[] key : keySet) {
String value = new String(map.get(key));
String keyStr = new String(key);
resultMap.put(keyStr, value);
}
return resultMap;
}
}
Note that you need to check here pom file
In addition, it should be noted that scheduled tasks need to be set to start when the project is started ( Solution : At the start of the project Application Add... To the class file @EnableScheduling annotation ), Both of these problems may lead to the acquisition of access_token Failure
The relevant exception information is :java.lang.UnsupportedOperationException: Selecting a new database not supported due to shared connection. Use separate ConnectionFactorys to work with multiple databases
<!-- redis rely on -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
Configuration information ⑤: Apply credentials
Unique identification of the application key( namely App Key) and Applied **( namely App Secret)

Configuration information ⑥: Open platform domain name ( Use of debugging interface )
Still choose Saas Configuration parameters of the environment

If all goes well , You can successfully get the user information of nailing . The version of this docking is Zhejiang government nails , Non ordinary nail version . The basic development process of ordinary nail version is similar to that described in the article , A few places are slightly different , Follow the nail development document .
版权声明
本文为[WXF_ Sir]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220928173381.html
边栏推荐
猜你喜欢
随机推荐
理解MySQL的MVCC同一个事务第一次快照读和第二次快照读——笔记
遥感深度学习目标检测数据集整理
数值重映射方法(Remap)
etcd 中watchableStore 存储的实现解析
Led by the cloud number, the revenue of Sanda shares in 2021 was 42.704 billion yuan, a year-on-year increase of 33.21%
Text justify, orientation, combine text attributes
2022年熔化焊接与热切割操作证考试题模拟考试平台操作
Does pytorch model load the running test set and the running test set in the training process have inconsistent results?
Online yaml to properties tool
一文学会text-justify,orientation,combine文本属性
esp-01s在arduino中的开发(一)
Analysis of the factors affecting the switching speed of MOS transistor Kia MOS transistor
Transformer模型中应用的各类位置编码
Write a simple examination program to complete the interaction of questions and answers on the console. Questions are divided into single choice and multi choice.
云数引领下,桑达股份2021年营收427.04亿元,同比增长33.21%
Open3D点云处理
量化投资学习——介绍orderflow
L2-031 深入虎穴 (25 分) (dfs
超越 iTerm!号称下一代 Terminal 终端神器,用完爱不释手!
L3-002 特殊堆栈 (30 分) (二分 栈









