feat: 用户信息修改接口
This commit is contained in:
@@ -2,21 +2,19 @@ package com.ivmiku.tutorial.controller;
|
||||
|
||||
import cn.binarywang.wx.miniapp.api.WxMaService;
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
|
||||
import cn.binarywang.wx.miniapp.util.WxMaConfigHolder;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.ivmiku.tutorial.entity.User;
|
||||
import com.ivmiku.tutorial.entity.UserDTO;
|
||||
import com.ivmiku.tutorial.mapper.UserMapper;
|
||||
import com.ivmiku.tutorial.response.Result;
|
||||
import com.ivmiku.tutorial.service.UserService;
|
||||
import com.ivmiku.tutorial.utils.RedisUtil;
|
||||
import jakarta.annotation.Resource;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
@@ -32,17 +30,16 @@ public class UserController {
|
||||
@Resource
|
||||
private RedisUtil redisUtil;
|
||||
|
||||
@Resource
|
||||
private UserService userService;
|
||||
|
||||
@GetMapping("/login")
|
||||
public Object login(@RequestParam(name = "code") String code,
|
||||
@RequestParam(name = "rawdata") String rawData,
|
||||
@RequestParam(name = "signature") String signature,
|
||||
@RequestParam(name = "encryptedData") String encryptedData,
|
||||
@RequestParam(name = "iv") String iv) {
|
||||
@RequestParam(name = "signature") String signature) {
|
||||
WxMaJscode2SessionResult session;
|
||||
WxMaUserInfo userInfo;
|
||||
try {
|
||||
session = wxMaService.getUserService().getSessionInfo(code);
|
||||
userInfo = wxMaService.getUserService().getUserInfo(session.getSessionKey(), encryptedData, iv);
|
||||
} catch (WxErrorException e) {
|
||||
return JSON.toJSON(Result.error(e.getMessage()));
|
||||
}
|
||||
@@ -77,4 +74,11 @@ public class UserController {
|
||||
}
|
||||
return Result.ok();
|
||||
}
|
||||
|
||||
@PostMapping("/edit")
|
||||
public Object editInfo(@RequestBody UserDTO userDTO) {
|
||||
String loginId = (String) StpUtil.getLoginId();
|
||||
userService.changeUserInfo(userDTO, loginId);
|
||||
return Result.ok();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.ivmiku.tutorial.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class UserDTO {
|
||||
private String nickname;
|
||||
private String avatarUrl;
|
||||
private String phoneNum;
|
||||
private String birthday;
|
||||
private String gender;
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
package com.ivmiku.tutorial.service;
|
||||
|
||||
import cn.hutool.core.util.DesensitizedUtil;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import com.ivmiku.tutorial.entity.User;
|
||||
import com.ivmiku.tutorial.entity.UserDTO;
|
||||
import com.ivmiku.tutorial.mapper.UserMapper;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -13,4 +16,30 @@ public class UserService {
|
||||
public User selectUserById(String id) {
|
||||
return userMapper.selectById(id);
|
||||
}
|
||||
|
||||
public void changeUserInfo(UserDTO userDTO, String openid) {
|
||||
String nickname = userDTO.getNickname();
|
||||
String avatarUrl = userDTO.getAvatarUrl();
|
||||
String gender = userDTO.getGender();
|
||||
String phoneNum = userDTO.getPhoneNum();
|
||||
String birthday = userDTO.getBirthday();
|
||||
User user = userMapper.selectById(openid);
|
||||
if (nickname != null) {
|
||||
user.setNickname(nickname);
|
||||
}
|
||||
if (avatarUrl != null) {
|
||||
user.setAvatarUrl(avatarUrl);
|
||||
}
|
||||
if (gender != null) {
|
||||
user.setGender(gender);
|
||||
}
|
||||
if (phoneNum != null) {
|
||||
phoneNum = DesensitizedUtil.mobilePhone(phoneNum);
|
||||
user.setPhoneNum(phoneNum);
|
||||
}
|
||||
if (birthday != null) {
|
||||
user.setBirthday(birthday);
|
||||
}
|
||||
userMapper.updateById(user);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user