解决业务系统不限制手机号码重复时,根据手机号码获取用户列表的问题

This commit is contained in:
heavyrian2012
2026-04-10 21:53:24 +08:00
parent f1b62ad64a
commit da781180a7
7 changed files with 194 additions and 2 deletions
@@ -0,0 +1,65 @@
/*
* This file is part of the Wildfire Chat package.
* (c) Heavyrain2012 <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
package com.xiaoleilu.loServer.action.admin;
import cn.wildfirechat.common.APIPath;
import cn.wildfirechat.common.ErrorCode;
import cn.wildfirechat.pojos.InputGetUserInfo;
import cn.wildfirechat.pojos.InputOutputUserInfo;
import cn.wildfirechat.pojos.OutputUserInfoList;
import cn.wildfirechat.proto.WFCMessage;
import com.xiaoleilu.loServer.RestResult;
import com.xiaoleilu.loServer.annotation.HttpMethod;
import com.xiaoleilu.loServer.annotation.Route;
import com.xiaoleilu.loServer.handler.Request;
import com.xiaoleilu.loServer.handler.Response;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.util.internal.StringUtil;
import java.util.ArrayList;
import java.util.List;
@Route(APIPath.User_Get_By_Mobile)
@HttpMethod("POST")
public class GetUsersByMobileAction extends AdminAction {
@Override
public boolean isTransactionAction() {
return true;
}
@Override
public boolean action(Request request, Response response) {
if (request.getNettyRequest() instanceof FullHttpRequest) {
InputGetUserInfo email = getRequestBody(request.getNettyRequest(), InputGetUserInfo.class);
if (!StringUtil.isNullOrEmpty(email.getMobile())) {
List<WFCMessage.User> users = messagesStore.getUserInfosByMobile(email.getMobile(), email.isIncludeDeleted());
List<InputOutputUserInfo> list = new ArrayList<>();
for (WFCMessage.User user : users) {
list.add(InputOutputUserInfo.fromPbUser(user));
}
RestResult result;
if (list.isEmpty()) {
result = RestResult.resultOf(ErrorCode.ERROR_CODE_NOT_EXIST);
} else {
OutputUserInfoList outputUserInfoList = new OutputUserInfoList();
outputUserInfoList.userInfos = list;
result = RestResult.ok(outputUserInfoList);
}
setResponseContent(result, response);
} else {
setResponseContent(RestResult.resultOf(ErrorCode.INVALID_PARAMETER), response);
}
}
return true;
}
}
@@ -3091,6 +3091,102 @@ public class DatabaseStore {
return outList;
}
List<WFCMessage.User> getUserInfosByMobile(String mobile, boolean includeDeleted) {
Connection connection = null;
PreparedStatement statement = null;
ResultSet rs = null;
List<WFCMessage.User> outList = new ArrayList<>();
try {
connection = DBUtil.getConnection();
String sql = "select `_uid`, `_name`" +
", `_display_name`" +
", `_portrait`" +
", `_mobile`" +
", `_gender`" +
", `_email`" +
", `_address`" +
", `_company`" +
", `_social`" +
", `_extra`" +
", `_type`" +
", `_deleted`" +
", `_dt` from t_user where `_mobile` = ?";
if(!includeDeleted) {
sql += " and `_deleted` = 0";
}
statement = connection.prepareStatement(sql);
statement.setString(1, mobile);
rs = statement.executeQuery();
while (rs.next()) {
WFCMessage.User.Builder builder = WFCMessage.User.newBuilder();
int index = 1;
String value = rs.getString(index++);
value = (value == null ? "" : value);
builder.setUid(value);
value = rs.getString(index++);
value = (value == null ? "" : value);
builder.setName(value);
value = rs.getString(index++);
value = (value == null ? "" : value);
builder.setDisplayName(value);
value = rs.getString(index++);
value = (value == null ? "" : value);
builder.setPortrait(value);
value = rs.getString(index++);
value = (value == null ? "" : value);
builder.setMobile(value);
int gender = rs.getInt(index++);
builder.setGender(gender);
value = rs.getString(index++);
value = (value == null ? "" : value);
builder.setEmail(value);
value = rs.getString(index++);
value = (value == null ? "" : value);
builder.setAddress(value);
value = rs.getString(index++);
value = (value == null ? "" : value);
builder.setCompany(value);
value = rs.getString(index++);
value = (value == null ? "" : value);
builder.setSocial(value);
value = rs.getString(index++);
value = (value == null ? "" : value);
builder.setExtra(value);
int type = rs.getInt(index++);
builder.setType(type);
int deleted = rs.getInt(index++);
builder.setDeleted(deleted);
long longValue = rs.getLong(index++);
if(longValue <= 0)
longValue = 1;
builder.setUpdateDt(longValue);
outList.add(builder.build());
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Utility.printExecption(LOG, e, RDBS_Exception);
} finally {
DBUtil.closeDB(connection, statement, rs);
}
return outList;
}
List<WFCMessage.User> getAllUsers(int count, int offset) {
Connection connection = null;
PreparedStatement statement = null;
@@ -3272,6 +3272,11 @@ public class MemoryMessagesStore implements IMessagesStore {
return databaseStore.getUserInfosByEmail(email);
}
@Override
public List<WFCMessage.User> getUserInfosByMobile(String mobile, boolean includeDeleted) {
return databaseStore.getUserInfosByMobile(mobile, includeDeleted);
}
@Override
public List<WFCMessage.User> getUserInfoList(int count, int offset) {
return databaseStore.getAllUsers(count, offset);
@@ -154,6 +154,7 @@ public interface IMessagesStore {
WFCMessage.User getUserInfoByName(String name);
WFCMessage.User getUserInfoByMobile(String mobile);
List<WFCMessage.User> getUserInfosByEmail(String email);
List<WFCMessage.User> getUserInfosByMobile(String mobile, boolean includeDeleted);
ErrorCode searchUser(String userId, String keyword, int searchType, int userType, int page, List<WFCMessage.User> users);
List<WFCMessage.User> getUserInfoList(int count, int offset);
@@ -43,6 +43,7 @@ public interface APIPath {
String User_Get_Info = "/admin/user/get_info";
String User_Batch_Get_Infos = "/admin/user/batch_get_infos";
String User_Get_Email_Info = "/admin/user/get_info_by_email";
String User_Get_By_Mobile = "/admin/user/get_info_by_mobile";
String User_Get_All = "/admin/user/all";
String User_Get_Robot_Info = "/admin/user/get_robot_info";
String User_Get_All_Robots = "/admin/user/get_all_robots";
@@ -196,7 +196,7 @@ public class Main {
userInfo.setUserId("userId1");
//用户名,一般是用户登录帐号,也必须保证唯一性。也就是说所有用户的userId必须不能重复,所有用户的name必须不能重复,但可以同一个用户的userId和name是同一个,一般建议userId使用一个uuid,name是"微信号"且可以修改,
userInfo.setName("user1");
userInfo.setMobile("13900000000");
userInfo.setMobile("13900000001");
userInfo.setDisplayName("user 1");
// 调用SDK创建用户
@@ -277,6 +277,15 @@ public class Main {
System.exit(-1);
}
// 通过手机号获取用户信息,如果一个手机号码属于多个用户,这里都会返回。
IMResult<OutputUserInfoList> resultGetUsers = UserAdmin.getUsersByMobile("13900000001", false);
if (resultGetUsers != null && resultGetUsers.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
System.out.println("Get user success");
} else {
System.out.println("Create user failure");
System.exit(-1);
}
// 通过用户ID获取用户信息
IMResult<InputOutputUserInfo> resultGetUserInfo3 = UserAdmin.getUserByUserId(userInfo.getUserId());
if (resultGetUserInfo3 != null && resultGetUserInfo3.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) {
@@ -70,6 +70,7 @@ public class UserAdmin {
/**
* 根据手机号获取用户信息(不包含已删除用户)
* @deprecated 用户手机号码不要求唯一,正常应该业务系统保证电话号码唯一。如果业务系统不限制电话号码唯一,可能一个号码有多个用户,此方法只能返回一个,此时建议使用 {@link #getUsersByMobile(String mobile, boolean includeDeleted)}
* @param mobile 手机号
* @return 用户信息
* @throws Exception 请求失败时抛出异常
@@ -79,7 +80,8 @@ public class UserAdmin {
}
/**
* 根据手机号获取用户信息
* 根据手机号获取用户信息,
* @deprecated 用户手机号码不要求唯一,正常应该业务系统保证电话号码唯一。如果业务系统不限制电话号码唯一,可能一个号码有多个用户,此方法只能返回一个,此时建议使用 {@link #getUsersByMobile(String mobile, boolean includeDeleted)}
* @param mobile 手机号
* @param includeDeleted 是否包含已删除的用户
* @return 用户信息
@@ -91,6 +93,19 @@ public class UserAdmin {
return AdminHttpUtils.httpJsonPost(path, getUserInfo, InputOutputUserInfo.class);
}
/**
* 根据电话号码获取用户信息列表
* @param mobile 邮箱地址
* @param includeDeleted 是否包含已删除的用户
* @return 用户信息列表
* @throws Exception 请求失败时抛出异常
*/
public static IMResult<OutputUserInfoList> getUsersByMobile(String mobile, boolean includeDeleted) throws Exception {
String path = APIPath.User_Get_By_Mobile;
InputGetUserInfo getUserInfo = new InputGetUserInfo(null, null, mobile, includeDeleted);
return AdminHttpUtils.httpJsonPost(path, getUserInfo, OutputUserInfoList.class);
}
/**
* 根据邮箱获取用户信息列表
* @param email 邮箱地址