feat(wx_user): 添加用户头像字段及相关功能

- 在wx_user表中新增avatar字段存储用户头像
- 在DTO和Entity中添加对应字段
- 新增根据openid更新用户头像和昵称的接口
- 修改相关SQL查询以包含头像字段
This commit is contained in:
dzq 2025-12-04 15:42:52 +08:00
parent fa395bd51e
commit 80a5f12857
10 changed files with 98 additions and 2 deletions

View File

@ -119,4 +119,43 @@ public class WxController {
return ResponseDTO.fail(new ApiException(ErrorCode.Client.COMMON_REQUEST_PARAMETERS_INVALID, e.getMessage())); 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<WxUserDTO> 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()));
}
}
} }

View File

@ -103,6 +103,10 @@ public class Ab98UserEntity extends BaseEntity<Ab98UserEntity> {
@TableField(exist = false) @TableField(exist = false)
private String wxNickName; private String wxNickName;
@ApiModelProperty("微信用户头像")
@TableField(exist = false)
private String wxAvatar;
@Override @Override
public Serializable pkVal() { public Serializable pkVal() {

View File

@ -61,14 +61,14 @@ public interface Ab98UserMapper extends BaseMapper<Ab98UserEntity> {
); );
@Select("SELECT * FROM (" + @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 " + "FROM ab98_user u " +
"LEFT JOIN ab98_user_tag t ON u.ab98_user_id = t.ab98_user_id " + "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 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 " + "LEFT JOIN wx_user w ON u.ab98_user_id = w.ab98_user_id " +
"${ew.customSqlSegment}" + "${ew.customSqlSegment}" +
" UNION ALL " + " 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 " + "FROM wx_user w " +
"WHERE w.ab98_user_id IS NULL" + "WHERE w.ab98_user_id IS NULL" +
") t ORDER BY create_time DESC") ") t ORDER BY create_time DESC")

View File

@ -98,6 +98,9 @@ public class Ab98UserDTO {
@ExcelColumn(name = "微信用户昵称") @ExcelColumn(name = "微信用户昵称")
private String wxNickName; private String wxNickName;
@ExcelColumn(name = "微信用户头像")
private String wxAvatar;
@ExcelColumn(name = "注册状态") @ExcelColumn(name = "注册状态")
private String registeredStatus; private String registeredStatus;

View File

@ -366,4 +366,41 @@ public class WxUserApplicationService {
return true; 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);
}
} }

View File

@ -30,6 +30,9 @@ public class AddWxUserCommand {
@ExcelColumn(name = "余额(分)") @ExcelColumn(name = "余额(分)")
private String wxBalance; // 使用 String 接收前端传入 private String wxBalance; // 使用 String 接收前端传入
@ExcelColumn(name = "用户头像")
private String avatar;
@ExcelColumn(name = "备注") @ExcelColumn(name = "备注")
private String remark; private String remark;
} }

View File

@ -54,6 +54,10 @@ public class WxUserEntity extends BaseEntity<WxUserEntity> {
@TableField("wx_balance") @TableField("wx_balance")
private Integer wxBalance; private Integer wxBalance;
@ApiModelProperty("用户头像")
@TableField("avatar")
private String avatar;
@Override @Override
public Serializable pkVal() { public Serializable pkVal() {
return this.wxUserId; return this.wxUserId;

View File

@ -60,6 +60,9 @@ public class WxUserDTO {
@ExcelColumn(name = "余额(元)") @ExcelColumn(name = "余额(元)")
private BigDecimal wxBalanceYuan; // 转换为元显示 private BigDecimal wxBalanceYuan; // 转换为元显示
@ExcelColumn(name = "用户头像")
private String avatar;
@ExcelColumn(name = "创建时间") @ExcelColumn(name = "创建时间")
private Date createTime; private Date createTime;

View File

@ -67,6 +67,7 @@ CREATE TABLE `wx_user` (
`updater_id` bigint DEFAULT '0' COMMENT '更新者ID', `updater_id` bigint DEFAULT '0' COMMENT '更新者ID',
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`deleted` tinyint(1) NOT NULL DEFAULT '0' COMMENT '删除标志0存在 1删除', `deleted` tinyint(1) NOT NULL DEFAULT '0' COMMENT '删除标志0存在 1删除',
`avatar` tinytext COLLATE utf8mb4_unicode_ci COMMENT '用户头像',
PRIMARY KEY (`wx_user_id`), PRIMARY KEY (`wx_user_id`),
KEY `idx_openid` (`openid`), KEY `idx_openid` (`openid`),
KEY `idx_tel` (`tel`), KEY `idx_tel` (`tel`),

View File

@ -16,3 +16,5 @@ CREATE TABLE `wx_user` (
KEY `idx_tel` (`tel`), KEY `idx_tel` (`tel`),
KEY `idx_nick_name` (`nick_name`) KEY `idx_nick_name` (`nick_name`)
) ENGINE=InnoDB AUTO_INCREMENT=75 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='微信用户信息表'; ) ENGINE=InnoDB AUTO_INCREMENT=75 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='微信用户信息表';
ALTER TABLE `wx_user` ADD COLUMN `avatar` tinytext COMMENT '用户头像';