通讯录查询修改

master
Xusan 2024-05-17 17:09:03 +08:00
parent cffc2b463e
commit f573de621e
10 changed files with 544 additions and 7 deletions

17
pom.xml
View File

@ -198,6 +198,23 @@
<version>1.30.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.15</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.13</version>
</dependency>
</dependencies>
<build>

View File

@ -10,6 +10,7 @@ import com.whdc.model.group.Update;
import com.whdc.model.vo.AuthToken;
import com.whdc.model.vo.LoginVo;
import com.whdc.service.IUserService;
import com.whdc.utils.HttpUtil;
import com.whdc.service.impl.UserServiceImpl;
import com.whdc.utils.ResultJson;
import io.swagger.annotations.Api;
@ -22,6 +23,7 @@ import org.springframework.web.bind.annotation.*;
import java.lang.reflect.InvocationTargetException;
import java.util.Objects;
import java.util.UUID;
import static com.whdc.service.impl.UserServiceImpl.getPassword;
@ -170,4 +172,22 @@ public class UserController {
}
@ApiOperation(value = "发送验证码")
@GetMapping(value = "pushCode/{phone}")
public ResultJson pushCode(@PathVariable("phone") String phone) {
return ResultJson.ok(service.pushCode(phone));
}
@ApiOperation(value = "手机验证码登录")
@GetMapping(value = "loginByCode/{phone}/{code}")
public ResultJson loginByCode(@PathVariable("phone") String phone,@PathVariable("code") String code) {
return ResultJson.ok(service.loginByCode(phone,code));
}
}

View File

@ -22,4 +22,5 @@ public class MyConstant {
* -
*/
public static final String ROLE_PUSH = "data_zh_om";
public static final String REDIS_KEY = "fxkh:txl:";
}

View File

@ -18,4 +18,7 @@ public class CommDto extends FindPageDto {
@ApiModelProperty(value = "用户手机号")
private String phone;
@ApiModelProperty(value = "0:添加用户,1:修改用户,2:修改权限,3:删除用户")
private String type;
}

View File

@ -36,4 +36,7 @@ public interface IUserService extends IService<User> {
boolean updateLastDate(Integer id);
ResultJson updatePwd(UserDto dto);
boolean pushCode(String phone);
LoginVo loginByCode(String phone,String code);
}

View File

@ -1,7 +1,9 @@
package com.whdc.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.whdc.exception.MyException;
import com.whdc.mapper.AddressBookMapper;
import com.whdc.mapper.UserMapper;
@ -12,17 +14,24 @@ import com.whdc.model.entity.User;
import com.whdc.model.vo.LoginVo;
import com.whdc.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whdc.utils.HttpUtil;
import org.apache.commons.lang3.StringUtils;
import com.whdc.utils.ResultJson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import static com.whdc.model.MyConstant.REDIS_KEY;
/**
@ -41,6 +50,9 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
@Autowired
private AddressBookMapper addressBookMapper;
@Autowired
private RedisTemplate stringRedisTemplate;
@Override
public IPage<User> page(User dto) {
return baseMapper.page(new Page<>(), dto);
@ -116,6 +128,132 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
.update();
}
@Override
public boolean pushCode(String phone) {
// 匹配手机号是否存在
LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper();
queryWrapper.eq(AddressBook::getPhone, phone);
if (addressBookMapper.selectOne(queryWrapper) == null) {
throw new MyException("当前手机号不存在");
}
// 判断是否以发送
String key = REDIS_KEY + "code:" + phone;
String code = String.valueOf(stringRedisTemplate.opsForValue().get(key));
if (StringUtils.isBlank(code)) {
// 生成验证码
String numCode = getNumCode();
push(phone, numCode);
stringRedisTemplate.opsForValue().set(key, numCode, 60, TimeUnit.SECONDS);
} else {
throw new MyException("请勿重复发送验证码");
}
return true;
}
@Override
public LoginVo loginByCode(String phone, String code) {
// 匹配手机号是否存在
LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper();
queryWrapper.eq(AddressBook::getPhone, phone);
AddressBook addressBook = addressBookMapper.selectOne(queryWrapper);
if (addressBook == null) {
throw new MyException("当前手机号不存在");
}
// 判断是否以发送
String key = REDIS_KEY + "code:" + phone;
String pushCode = String.valueOf(stringRedisTemplate.opsForValue().get(key));
if (StringUtils.isBlank(pushCode)) {
throw new MyException("当前验证码已过期");
}
if (!pushCode.equals(code)) {
throw new MyException("验证码错误");
}
User sysUser = this.lambdaQuery().eq(User::getAbId, addressBook.getId()).one();
if (sysUser == null) {
throw new MyException("当前账号暂未注册");
}
LoginVo out = new LoginVo();
BeanUtils.copyProperties(sysUser, out);
out.setName(addressBook.getName());
return out;
}
public static String getCode() {
Random yzm = new Random(); //定义一个随机生成数技术,用来生成随机数
//2用String常用API-charAit生成验证码
String yzm1 = "1234567890abcdefghijklmnopqrstuvwxwzABCDEFGHIJKLMNOPQRSTUVWXYZ";//定义一个String变量存放需要的数据一共58位
String yzm3 = "";//定义一个空的Atring变量用来接收生成的验证码
for (int i = 0; i < 5; i++) {
int a = yzm.nextInt(58);//随机生成0-57之间的数提供索引位置
yzm3 += yzm1.charAt(a);//用get 和提供的索引找到相应位置的数据给变量
}
return yzm3;
}
public static String getNumCode() {
//1用随机生成数方法生成验证码
Random yzm = new Random(); //定义一个随机生成数技术,用来生成随机数
String yzm2 = ""; //定义一个空的Atring变量用来接收生成的验证码
for (int i = 0; i < 5; i++) { //循环5次每次生成一位5位验证码
int a = yzm.nextInt(3); //验证码包括数字、大小写字母组成
switch (a) { //a: 0 1 2
case 0: // 数字 小写字母 大写字母
char s = (char) (yzm.nextInt(26) + 65);
yzm2 = yzm2 + s;
break;
case 1:
char s1 = (char) (yzm.nextInt(26) + 97);
yzm2 = yzm2 + s1;
break;
case 2:
int s2 = yzm.nextInt(10);
yzm2 = yzm2 + s2;
break;
}
}
return yzm2;
}
/**
*
*
* @param phone
* @param code
*/
public void push(String phone, String code) {
String content = "您正在登录湖北省防汛抗旱通讯录,验证码为:" + code + ",若非本人操作,请勿将验证码泄露他人。";
String body = "{\n" +
" \"channel\": \"[phone]\",\n" +
" \"content\": \"" + content + "\",\n" +
" \"devices\": \"['" + phone + "']\",\n" +
" \"foreignId\": \"" + UUID.randomUUID() + "\",\n" +
" \"source\": \"山洪app\",\n" +
// " \"token\": \"\",\n" +
// 消息类别3为监测预警4为预报预警5自定义短信,示例值(3)
" \"type\": 5\n" +
"}";
HttpUtil.sendPost("http://223.75.53.141:82/shpush/v1/push/", body);
}
@Override
public ResultJson updatePwd(UserDto dto) {

View File

@ -0,0 +1,351 @@
package com.whdc.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.web.client.RestTemplate;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
@Slf4j
public class HttpUtil {
private static final RestTemplate REST_TEMPLATE = new RestTemplate();
public static String doGetThrEx(String url, JSONObject json) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add(HttpHeaders.CONTENT_TYPE, "application/json");
if (Objects.nonNull(json)){
StringBuilder sb = new StringBuilder();
for (String key : json.keySet()) {
if (sb.length() > 0) {
sb.append("&");
}
sb.append(key).append("=").append(json.get(key));
}
url += "?" + sb;
String token = json.getString("token");
if (StringUtils.isNotBlank(token)){
httpHeaders.add("X-Access-Token", token);
}
}
org.springframework.http.HttpEntity<JSONObject> requestEntity = new org.springframework.http.HttpEntity<>(json, httpHeaders);
return REST_TEMPLATE.exchange(url, HttpMethod.GET, requestEntity, String.class).getBody();
}
public static String doGet(String url, JSONObject json) {
try {
return doGetThrEx(url, json);
} catch (Exception e) {
throw new RuntimeException("请求异常:" + e.getMessage(), e);
}
}
public static String get(String _url) {
InputStream is = null;
BufferedReader br = null;
HttpURLConnection conn = null;
String jstr = null;
try {
log.info("请求地址: " + _url);
URL url = new URL(_url);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
conn.connect();
if (conn.getResponseCode() == 200) {
is = conn.getInputStream();
if (is != null) {
br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
StringBuffer sb = new StringBuffer();
String tmp = "";
while ((tmp = br.readLine()) != null) {
sb.append(tmp);
}
jstr = sb.toString();
log.info("响应参数: " + jstr);
jstr = jsonFormat(jstr);
return jstr;
}
} else if (conn.getResponseCode() == 500) {
is = conn.getErrorStream();
if (is != null) {
br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
StringBuffer sb = new StringBuffer();
String tmp = "";
while ((tmp = br.readLine()) != null) {
sb.append(tmp);
}
jstr = sb.toString();
if (jstr.contains("Token失效请重新登录")) { // 第三方token失效处理
return "{\"code\": 301,\"extMessage\":\"Token失效请重新登录\"}";
}
}
}
} catch (Exception ignore) {
} finally {
if (br != null) {
try {
br.close();
} catch (IOException ignore) {
}
}
if (is != null) {
try {
is.close();
} catch (IOException ignore) {
}
}
if (conn != null) {
try {
conn.disconnect();
} catch (Exception ignore) {
}
}
}
return null;
}
public static String get(String _url, String authorization) {
InputStream is = null;
BufferedReader br = null;
HttpURLConnection conn = null;
String jstr = null;
try {
log.info("请求地址: " + _url);
URL url = new URL(_url);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Authorization", authorization);
conn.setRequestMethod("GET");
conn.setConnectTimeout(50000);
conn.setReadTimeout(50000);
conn.connect();
if (conn.getResponseCode() == 200) {
is = conn.getInputStream();
if (is != null) {
br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
StringBuffer sb = new StringBuffer();
String tmp = "";
while ((tmp = br.readLine()) != null) {
sb.append(tmp);
}
jstr = sb.toString();
log.info("响应参数: " + jstr);
jstr = jsonFormat(jstr);
return jstr;
}
} else if (conn.getResponseCode() == 500) {
is = conn.getErrorStream();
if (is != null) {
br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
StringBuffer sb = new StringBuffer();
String tmp = "";
while ((tmp = br.readLine()) != null) {
sb.append(tmp);
}
jstr = sb.toString();
if (jstr.contains("Token失效请重新登录")) { // 第三方token失效处理
return "{\"code\": 301,\"extMessage\":\"Token失效请重新登录\"}";
}
}
}
} catch (Exception ignore) {
} finally {
if (br != null) {
try {
br.close();
} catch (IOException ignore) {
}
}
if (is != null) {
try {
is.close();
} catch (IOException ignore) {
}
}
if (conn != null) {
try {
conn.disconnect();
} catch (Exception ignore) {
}
}
}
return null;
}
public static String get(String _url, String token, String forward) {
InputStream is = null;
BufferedReader br = null;
HttpURLConnection conn = null;
String jstr = null;
try {
log.info("请求地址: " + _url);
URL url = new URL(_url);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("X-Access-Token", token);
conn.setRequestProperty("url", forward);
conn.setRequestMethod("GET");
conn.setConnectTimeout(50000);
conn.setReadTimeout(50000);
conn.connect();
if (conn.getResponseCode() == 200) {
is = conn.getInputStream();
if (is != null) {
br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
StringBuffer sb = new StringBuffer();
String tmp = "";
while ((tmp = br.readLine()) != null) {
sb.append(tmp);
}
jstr = sb.toString();
log.info("响应参数: " + jstr);
jstr = jsonFormat(jstr);
return jstr;
}
} else if (conn.getResponseCode() == 500) {
is = conn.getErrorStream();
if (is != null) {
br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
StringBuffer sb = new StringBuffer();
String tmp = "";
while ((tmp = br.readLine()) != null) {
sb.append(tmp);
}
jstr = sb.toString();
if (jstr.contains("Token失效请重新登录")) { // 第三方token失效处理
return "{\"code\": 301,\"extMessage\":\"Token失效请重新登录\"}";
}
}
}
} catch (Exception ignore) {
} finally {
if (br != null) {
try {
br.close();
} catch (IOException ignore) {
}
}
if (is != null) {
try {
is.close();
} catch (IOException ignore) {
}
}
if (conn != null) {
try {
conn.disconnect();
} catch (Exception ignore) {
}
}
}
return null;
}
/**
* @param url
* @param jsonData
* @return
*/
public static String sendPost(String url, String jsonData) {
return sendPost(url, jsonData, null);
}
public static String sendPost(String url, String jsonData, JSONObject header) {
CloseableHttpResponse response = null;
CloseableHttpClient httpClient = null;
String responseContent = null;
try {
httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "application/json");
if (Objects.nonNull(header)) {
for (String key : header.keySet()) {
// httpPost.addHeader("X-Access-Token", token);
httpPost.addHeader(key, header.getString(key));
}
}
if (StringUtils.isNotBlank(jsonData)){
httpPost.setEntity(new StringEntity(jsonData, "UTF-8"));
}
log.info("请求地址: " + url);
log.info("请求参数: " + jsonData);
response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
responseContent = EntityUtils.toString(entity, "UTF-8");
responseContent = jsonFormat(responseContent);
log.info("响应参数: " + responseContent);
} catch (Exception e) {
log.error("发送请求异常", e);
throw new RuntimeException("发送请求异常", e);
} finally {
try {
if (null != response) {
response.close();
}
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return responseContent;
}
private static String jsonFormat(String str) {
if (JSON.isValidObject(str)) {
str = JSON.parseObject(str).toJSONString();
}
return str;
}
}

View File

@ -8,12 +8,12 @@ spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: dm.jdbc.driver.DmDriver
# url: jdbc:dm://10.0.41.113:5236?schema=FXKH_TXL
# username: SYSDBA
# password: SYSDBA001
url: jdbc:dm://10.42.6.247:5236?schema=FXKH_TXL
username: SHZH
password: Shzh_890
url: jdbc:dm://local.gunshiiot.com:5236?schema=FXKH_TXL
username: SYSDBA
password: SYSDBA001
# url: jdbc:dm://10.42.6.247:5236?schema=FXKH_TXL
# username: SHZH
# password: Shzh_890
druid:
initialSize: 5
minIdle: 5

View File

@ -34,6 +34,7 @@
AND AB.PHONE LIKE CONCAT('', #{dto.phone}, '%')
</if>
ORDER BY F.SORT NULLS LAST,F.SORT,AB.SORT
GROUP BY AB.ID
</select>

View File

@ -14,6 +14,9 @@
<if test="dto.phone != null and dto.phone != '' ">
AND A.PHONE LIKE CONCAT('%', #{dto.phone}, '%')
</if>
<if test="dto.type != null and dto.type != '' ">
AND A.TYPE = #{dto.type}
</if>
</where>
</select>