10.用户资料管理(查询和修改)

用户资料管理(查询和修改)

一、查询

/**
*

  • 根据用户id查询UserInfo
  • 1.请求路径:/users
  • 2.请求参数:请求头的Authorization:token(必须),用户id:userID(非必须)
  • 3.响应数据:UserInfo
    */

1.1 QueryUserinfoController(表现层)

package com.tanhua.server.controller;

import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.domain.vo.UserInfoVo;
import com.tanhua.server.service.QueryUserInfoService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class QueryUserinfoController {


    @Autowired
    private QueryUserInfoService service;


    /**
     *根据id查询用户详细信息
     * 1.请求路径:/users
     * 2.请求方式:post
     * 3.请求参数:请求头中的token,或则传过来的用户id
     * 4.响应结果:UserInfo
     *
     *
     */
    @GetMapping
    public ResponseEntity  queryUserINfoById(@RequestHeader("Authorization") String token,Long userId){
        //1.校验token合法性
        boolean verifyToken = JwtUtils.verifyToken(token);
        if(!verifyToken){
            return ResponseEntity.status(401).body(null);
        }
        //2.获取token中携带的用户id
        Claims claims = JwtUtils.getClaims(token);
        Integer id = (Integer) claims.get("id");
        if(userId == null){
            userId=Long.valueOf(id);
        }

        //3.调用业务层完成信息查询
        UserInfoVo userInfoVo = service.queryByid(userId);

        return ResponseEntity.ok(userInfoVo);
    }


   

1.2QueryUserInfoService(业务层)

package com.tanhua.server.service;

import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.domain.vo.UserInfoVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

@Service
public class QueryUserInfoService {

    @DubboReference
    private UserInfoApi userInfoApi;


    /**
     * 根据id查询用户详情信息
     * @param id
     * @return
     */
    public UserInfoVo queryByid(Long id){
        UserInfo userInfo = userInfoApi.queryUserInfo(id);

        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo, userInfoVo);//属性拷贝,同名同类型
        //对UserInfo的age做一个非空校验
        if(userInfo.getAge() != null){
            userInfoVo.setAge(userInfo.getAge().toString());
        }

        return userInfoVo;
    }

1.3UserInfoVo

@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserInfoVo implements Serializable {

    private Long id; //用户id
    private String nickname; //昵称
    private String avatar; //用户头像
    private String birthday; //生日
    private String gender; //性别
    private String age; //年龄
    private String city; //城市
    private String income; //收入
    private String education; //学历
    private String profession; //行业
    private Integer marriage; //婚姻状态
}

二、修改

2.1表现层

package com.tanhua.server.controller;

import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.domain.vo.UserInfoVo;
import com.tanhua.server.service.QueryUserInfoService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class QueryUserinfoController {


    @Autowired
    private QueryUserInfoService service;

    /**
     *
     * 修改用户详情:
     * 1.请求路径:/users
     * 2.请求参数:请求头中的token,以及UserInfo
     * 3.响应数据:无
     */
    @PutMapping
    public  ResponseEntity  updateUserInfo(@RequestHeader("Authorization") String token,
                                           @RequestBody UserInfo userInfo){


        //1.校验token合法性
        boolean verifyToken = JwtUtils.verifyToken(token);
        if(!verifyToken){
            return ResponseEntity.status(401).body(null);
        }
        //2.解析token获取里面的数据id
        Claims claims = JwtUtils.getClaims(token);
        Integer id = (Integer) claims.get("id");
        //3.把token里面的userid设置给UserInfo
        userInfo.setId(Long.valueOf(id));
        //4.调用业务层完成数据修改


        service.updateUserInfo(userInfo);
        return ResponseEntity.status(401).body(null);

    }
}

2.2业务层

package com.tanhua.server.service;

import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.domain.vo.UserInfoVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

@Service
public class QueryUserInfoService {

    @DubboReference
    private UserInfoApi userInfoApi;


    /**
     * 根据用户id修改信息
     * @param userInfo
     */
    public void updateUserInfo(UserInfo userInfo) {
        userInfoApi.updateUserInfo(userInfo);
    }
}

posted @ 2022-10-30 12:09  给我手牵你走  阅读(92)  评论(0编辑  收藏  举报