From 80a5f1285757fd3edb87674c131b9501e8890472 Mon Sep 17 00:00:00 2001 From: dzq Date: Thu, 4 Dec 2025 15:42:52 +0800 Subject: [PATCH] =?UTF-8?q?feat(wx=5Fuser):=20=E6=B7=BB=E5=8A=A0=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E5=A4=B4=E5=83=8F=E5=AD=97=E6=AE=B5=E5=8F=8A=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在wx_user表中新增avatar字段存储用户头像 - 在DTO和Entity中添加对应字段 - 新增根据openid更新用户头像和昵称的接口 - 修改相关SQL查询以包含头像字段 --- .../api/controller/WxController.java | 39 +++++++++++++++++++ .../domain/ab98/user/db/Ab98UserEntity.java | 4 ++ .../domain/ab98/user/db/Ab98UserMapper.java | 4 +- .../domain/ab98/user/dto/Ab98UserDTO.java | 3 ++ .../wx/user/WxUserApplicationService.java | 37 ++++++++++++++++++ .../wx/user/command/AddWxUserCommand.java | 3 ++ .../domain/wx/user/db/WxUserEntity.java | 4 ++ .../domain/wx/user/dto/WxUserDTO.java | 3 ++ doc/sql/wxUser.sql | 1 + sql/20251029_wx_user.sql | 2 + 10 files changed, 98 insertions(+), 2 deletions(-) diff --git a/agileboot-api/src/main/java/com/agileboot/api/controller/WxController.java b/agileboot-api/src/main/java/com/agileboot/api/controller/WxController.java index 8c107c7..e6e7bb9 100644 --- a/agileboot-api/src/main/java/com/agileboot/api/controller/WxController.java +++ b/agileboot-api/src/main/java/com/agileboot/api/controller/WxController.java @@ -119,4 +119,43 @@ public class WxController { return ResponseDTO.fail(new ApiException(ErrorCode.Client.COMMON_REQUEST_PARAMETERS_INVALID, e.getMessage())); } } + + /** + * 根据openid更新用户昵称和头像 + * @param openid 微信用户openid + * @param nickName 昵称(可选,为空时不更新) + * @param avatar 头像(可选,为空时不更新) + * @return 更新后的用户信息 + */ + @GetMapping("/updateUserByOpenid") + public ResponseDTO updateUserByOpenid( + @RequestParam("openid") String openid, + @RequestParam(value = "nickName", required = false) String nickName, + @RequestParam(value = "avatar", required = false) String avatar) { + try { + // 校验openid是否为空 + if (StringUtils.isBlank(openid)) { + throw new ApiException(ErrorCode.Client.COMMON_REQUEST_PARAMETERS_INVALID, "openid不能为空"); + } + + // 至少需要提供一个要更新的字段 + if (StringUtils.isBlank(nickName) && StringUtils.isBlank(avatar)) { + throw new ApiException(ErrorCode.Client.COMMON_REQUEST_PARAMETERS_INVALID, "至少需要提供昵称或头像其中一个字段"); + } + + // 更新用户信息 + WxUserDTO wxUserDTO = wxUserApplicationService.updateUserByOpenid(openid, nickName, avatar); + + if (wxUserDTO == null) { + return ResponseDTO.fail(new ApiException(ErrorCode.Client.COMMON_REQUEST_PARAMETERS_INVALID, "用户不存在")); + } + + log.info("根据openid {} 更新用户信息成功", openid); + + return ResponseDTO.ok(wxUserDTO); + } catch (Exception e) { + log.error("更新用户信息失败", e); + return ResponseDTO.fail(new ApiException(ErrorCode.Client.COMMON_REQUEST_PARAMETERS_INVALID, e.getMessage())); + } + } } diff --git a/agileboot-domain/src/main/java/com/agileboot/domain/ab98/user/db/Ab98UserEntity.java b/agileboot-domain/src/main/java/com/agileboot/domain/ab98/user/db/Ab98UserEntity.java index 414c9ea..1dd118e 100644 --- a/agileboot-domain/src/main/java/com/agileboot/domain/ab98/user/db/Ab98UserEntity.java +++ b/agileboot-domain/src/main/java/com/agileboot/domain/ab98/user/db/Ab98UserEntity.java @@ -103,6 +103,10 @@ public class Ab98UserEntity extends BaseEntity { @TableField(exist = false) private String wxNickName; + @ApiModelProperty("微信用户头像") + @TableField(exist = false) + private String wxAvatar; + @Override public Serializable pkVal() { diff --git a/agileboot-domain/src/main/java/com/agileboot/domain/ab98/user/db/Ab98UserMapper.java b/agileboot-domain/src/main/java/com/agileboot/domain/ab98/user/db/Ab98UserMapper.java index adf830f..45d2226 100644 --- a/agileboot-domain/src/main/java/com/agileboot/domain/ab98/user/db/Ab98UserMapper.java +++ b/agileboot-domain/src/main/java/com/agileboot/domain/ab98/user/db/Ab98UserMapper.java @@ -61,14 +61,14 @@ public interface Ab98UserMapper extends BaseMapper { ); @Select("SELECT * FROM (" + - "SELECT DISTINCT u.ab98_user_id, u.openid, u.userid, u.qy_user_id, u.name, u.tel, u.idnum, u.sex, u.face_img, u.idcard_front, u.idcard_back, u.address, u.registered, u.creator_id, u.create_time, u.updater_id, u.update_time, u.deleted, u.ab98_balance, ub.balance, ub.use_balance, ub.balance_limit, w.wx_user_id, w.openid as wx_user_openid, w.nick_name as wx_nick_name " + + "SELECT DISTINCT u.ab98_user_id, u.openid, u.userid, u.qy_user_id, u.name, u.tel, u.idnum, u.sex, u.face_img, u.idcard_front, u.idcard_back, u.address, u.registered, u.creator_id, u.create_time, u.updater_id, u.update_time, u.deleted, u.ab98_balance, ub.balance, ub.use_balance, ub.balance_limit, w.wx_user_id, w.openid as wx_user_openid, w.nick_name as wx_nick_name, w.avatar as wx_avatar " + "FROM ab98_user u " + "LEFT JOIN ab98_user_tag t ON u.ab98_user_id = t.ab98_user_id " + "LEFT JOIN user_balance ub ON u.ab98_user_id = ub.ab98_user_id " + "LEFT JOIN wx_user w ON u.ab98_user_id = w.ab98_user_id " + "${ew.customSqlSegment}" + " UNION ALL " + - "SELECT NULL as ab98_user_id, NULL as openid, NULL as userid, NULL as qy_user_id, NULL as name, NULL as tel, NULL as idnum, NULL as sex, NULL as face_img, NULL as idcard_front, NULL as idcard_back, NULL as address, NULL as registered, NULL as creator_id, w.create_time as create_time, NULL as updater_id, w.update_time as update_time, NULL as deleted, NULL as ab98_balance, NULL as balance, NULL as use_balance, NULL as balance_limit, w.wx_user_id, w.openid as wx_user_openid, w.nick_name as wx_nick_name " + + "SELECT NULL as ab98_user_id, NULL as openid, NULL as userid, NULL as qy_user_id, NULL as name, NULL as tel, NULL as idnum, NULL as sex, NULL as face_img, NULL as idcard_front, NULL as idcard_back, NULL as address, NULL as registered, NULL as creator_id, w.create_time as create_time, NULL as updater_id, w.update_time as update_time, NULL as deleted, NULL as ab98_balance, NULL as balance, NULL as use_balance, NULL as balance_limit, w.wx_user_id, w.openid as wx_user_openid, w.nick_name as wx_nick_name, w.avatar as wx_avatar " + "FROM wx_user w " + "WHERE w.ab98_user_id IS NULL" + ") t ORDER BY create_time DESC") diff --git a/agileboot-domain/src/main/java/com/agileboot/domain/ab98/user/dto/Ab98UserDTO.java b/agileboot-domain/src/main/java/com/agileboot/domain/ab98/user/dto/Ab98UserDTO.java index 5a79bf5..b336db4 100644 --- a/agileboot-domain/src/main/java/com/agileboot/domain/ab98/user/dto/Ab98UserDTO.java +++ b/agileboot-domain/src/main/java/com/agileboot/domain/ab98/user/dto/Ab98UserDTO.java @@ -98,6 +98,9 @@ public class Ab98UserDTO { @ExcelColumn(name = "微信用户昵称") private String wxNickName; + @ExcelColumn(name = "微信用户头像") + private String wxAvatar; + @ExcelColumn(name = "注册状态") private String registeredStatus; diff --git a/agileboot-domain/src/main/java/com/agileboot/domain/wx/user/WxUserApplicationService.java b/agileboot-domain/src/main/java/com/agileboot/domain/wx/user/WxUserApplicationService.java index eb838b6..b601ccd 100644 --- a/agileboot-domain/src/main/java/com/agileboot/domain/wx/user/WxUserApplicationService.java +++ b/agileboot-domain/src/main/java/com/agileboot/domain/wx/user/WxUserApplicationService.java @@ -366,4 +366,41 @@ public class WxUserApplicationService { return true; } + + /** + * 根据openid更新用户昵称和头像 + * + * @param openid 微信openid + * @param nickName 昵称(可选,为空时不更新) + * @param avatar 头像(可选,为空时不更新) + * @return 更新后的用户信息 + */ + @Transactional(rollbackFor = Exception.class) + public WxUserDTO updateUserByOpenid(String openid, String nickName, String avatar) { + if (StringUtils.isBlank(openid)) { + return null; + } + + // 查询用户 + WxUserEntity wxUserEntity = userService.getByOpenid(openid); + if (wxUserEntity == null) { + return null; + } + + // 更新昵称(如果传入不为空) + if (StringUtils.isNotBlank(nickName)) { + wxUserEntity.setNickName(nickName); + } + + // 更新头像(如果传入不为空) + if (StringUtils.isNotBlank(avatar)) { + wxUserEntity.setAvatar(avatar); + } + + // 保存更新 + userService.updateById(wxUserEntity); + + // 返回更新后的用户信息 + return new WxUserDTO(wxUserEntity); + } } diff --git a/agileboot-domain/src/main/java/com/agileboot/domain/wx/user/command/AddWxUserCommand.java b/agileboot-domain/src/main/java/com/agileboot/domain/wx/user/command/AddWxUserCommand.java index 79032d1..4b0b272 100644 --- a/agileboot-domain/src/main/java/com/agileboot/domain/wx/user/command/AddWxUserCommand.java +++ b/agileboot-domain/src/main/java/com/agileboot/domain/wx/user/command/AddWxUserCommand.java @@ -30,6 +30,9 @@ public class AddWxUserCommand { @ExcelColumn(name = "余额(分)") private String wxBalance; // 使用 String 接收,前端传入 + @ExcelColumn(name = "用户头像") + private String avatar; + @ExcelColumn(name = "备注") private String remark; } diff --git a/agileboot-domain/src/main/java/com/agileboot/domain/wx/user/db/WxUserEntity.java b/agileboot-domain/src/main/java/com/agileboot/domain/wx/user/db/WxUserEntity.java index 8fef569..6ac4d79 100644 --- a/agileboot-domain/src/main/java/com/agileboot/domain/wx/user/db/WxUserEntity.java +++ b/agileboot-domain/src/main/java/com/agileboot/domain/wx/user/db/WxUserEntity.java @@ -54,6 +54,10 @@ public class WxUserEntity extends BaseEntity { @TableField("wx_balance") private Integer wxBalance; + @ApiModelProperty("用户头像") + @TableField("avatar") + private String avatar; + @Override public Serializable pkVal() { return this.wxUserId; diff --git a/agileboot-domain/src/main/java/com/agileboot/domain/wx/user/dto/WxUserDTO.java b/agileboot-domain/src/main/java/com/agileboot/domain/wx/user/dto/WxUserDTO.java index ee4ee99..b3cbe0f 100644 --- a/agileboot-domain/src/main/java/com/agileboot/domain/wx/user/dto/WxUserDTO.java +++ b/agileboot-domain/src/main/java/com/agileboot/domain/wx/user/dto/WxUserDTO.java @@ -60,6 +60,9 @@ public class WxUserDTO { @ExcelColumn(name = "余额(元)") private BigDecimal wxBalanceYuan; // 转换为元显示 + @ExcelColumn(name = "用户头像") + private String avatar; + @ExcelColumn(name = "创建时间") private Date createTime; diff --git a/doc/sql/wxUser.sql b/doc/sql/wxUser.sql index ae315f0..7f8efbe 100644 --- a/doc/sql/wxUser.sql +++ b/doc/sql/wxUser.sql @@ -67,6 +67,7 @@ CREATE TABLE `wx_user` ( `updater_id` bigint DEFAULT '0' COMMENT '更新者ID', `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', `deleted` tinyint(1) NOT NULL DEFAULT '0' COMMENT '删除标志(0存在 1删除)', + `avatar` tinytext COLLATE utf8mb4_unicode_ci COMMENT '用户头像', PRIMARY KEY (`wx_user_id`), KEY `idx_openid` (`openid`), KEY `idx_tel` (`tel`), diff --git a/sql/20251029_wx_user.sql b/sql/20251029_wx_user.sql index dd2f676..e40f54d 100644 --- a/sql/20251029_wx_user.sql +++ b/sql/20251029_wx_user.sql @@ -16,3 +16,5 @@ CREATE TABLE `wx_user` ( KEY `idx_tel` (`tel`), KEY `idx_nick_name` (`nick_name`) ) ENGINE=InnoDB AUTO_INCREMENT=75 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='微信用户信息表'; + +ALTER TABLE `wx_user` ADD COLUMN `avatar` tinytext COMMENT '用户头像'; \ No newline at end of file