feat[用户模块]: 手机号获取登逻辑

This commit is contained in:
2024-12-24 16:20:39 +08:00
parent 6619d9183f
commit 9dc575a312
346 changed files with 1112 additions and 952 deletions
@@ -2,8 +2,10 @@ package com.central;
import com.central.common.lb.annotation.EnableFeignInterceptor;
import com.central.search.annotation.EnableSearchClient;
import com.central.user.properties.TencentProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@@ -15,6 +17,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableTransactionManagement
@EnableFeignInterceptor
@SpringBootApplication
@EnableConfigurationProperties(TencentProperties.class)
public class UserCenterApp {
public static void main(String[] args) {
SpringApplication.run(UserCenterApp.class, args);
@@ -1,10 +1,7 @@
package com.central.user.controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
@@ -17,7 +14,9 @@ import com.central.search.client.service.IQueryService;
import com.central.search.model.LogicDelDto;
import com.central.search.model.SearchDto;
import com.central.user.model.SysUserExcel;
import com.central.user.properties.TencentProperties;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
@@ -25,14 +24,21 @@ import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import com.central.user.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import jakarta.servlet.http.HttpServletResponse;
@@ -58,6 +64,9 @@ public class SysUserController {
@Autowired
private IQueryService queryService;
@Autowired
private TencentProperties tencentProperties;
/**
* 当前登录用户 LoginAppUser
*
@@ -315,10 +324,94 @@ public class SysUserController {
return sysUser;
}
/**
* APP更新用户手机号
* @param code
*/
@PutMapping("/code/mobile")
@CachePut(value = "user", key = "#sysUser.username", unless="#result == null")
//@AuditLog(operation = "'更新用户:' + #sysUser")
public void updateSysUserMobile(@RequestParam String code) {
SysUser sysUser = getLoginAppUser().getDatas();
sysUser.setMobile(getUserPhoneNumber(code));
appUserService.updateById(sysUser);
}
/**
* 是否超级管理员
*/
private boolean checkAdmin(long id) {
return id == 1L;
}
/**
* 获取微信访问token
* @return access_token
*/
private String getTencentAccessToken(){
RestTemplate restTemplate = new RestTemplate();
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("appid", tencentProperties.getAppId());
params.add("secret", tencentProperties.getAppSecret());
params.add("grant_type", "client_credential");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED);
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
ResponseEntity<String> responseEntity = restTemplate
.exchange("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + tencentProperties.getAppId()+"&secret=" + tencentProperties.getAppSecret(),
HttpMethod.GET,
requestEntity,
String.class);
if (responseEntity.getStatusCode().is2xxSuccessful() ) {
ObjectMapper objectMapper = new ObjectMapper();
try {
return objectMapper.readValue(responseEntity.getBody(), Map.class).get("access_token").toString();
} catch (Exception e) {
throw new EmptyStackException();
}
}else {
throw new EmptyStackException();
}
}
/**
* 获取微信用户手机号
* @return access_token
*/
private String getUserPhoneNumber(String code){
RestTemplate restTemplate = new RestTemplate();
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("code", code);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED);
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
ResponseEntity<String> responseEntity = restTemplate
.exchange("https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + getTencentAccessToken(),
HttpMethod.POST,
requestEntity,
String.class);
if (responseEntity.getStatusCode().is2xxSuccessful() ) {
ObjectMapper objectMapper = new ObjectMapper();
try {
return objectMapper.readValue(responseEntity.getBody(), Map.class).get("phone_info").toString();
} catch (Exception e) {
throw new EmptyStackException();
}
}else {
throw new EmptyStackException();
}
}
}
@@ -0,0 +1,25 @@
package com.central.user.properties;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
/**
* 腾讯认证配置
*
* @author lsl
* @version 1.0
* @date 2024/12/20
* <p>
*
*/
@Setter
@Getter
@ConfigurationProperties(prefix = "zlt.tencent")
@RefreshScope
public class TencentProperties {
private String appId;
private String appSecret;
private String url;
}
@@ -56,7 +56,11 @@ zlt:
- com.central.user.mapper.SysRoleMapper.findList
# 菜单列表
- com.central.user.mapper.SysMenuMapper.selectList
#审计日志
tencent:
app-id: ${zlt.security.tencent.app-id}
app-secret: ${zlt.security.tencent.app-secret}
#审计日志
# audit-log:
# enabled: true
# log-type: db
@@ -64,4 +68,4 @@ zlt:
# driver-class-name: com.mysql.cj.jdbc.Driver
# jdbc-url: jdbc:mysql://${zlt.datasource.ip}:3306/logger-center?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai
# username: ${zlt.datasource.username}
# password: ${zlt.datasource.password}
# password: ${zlt.datasource.password}