9.完善用户信息和上传头像

完善用户信息和上传头像

一、完善用户信息

/**
* 完善用户信息:表现层
* 请求路径:/user/loginReginfo
* 请求方式:post
* 请求参数:Headers(请求头):token;Body(请求体):UserInfo
* 响应数据:null
*/

1.1服务提供者

(1)实体类、封装数据

package com.tanhua.model.domain;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Date;

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

    /**
     * 由于userinfo表和user表之间是一对一关系
     *   userInfo的id来源于user表的id
     */
    @TableId(type= IdType.INPUT)
    private Long id; //用户id
    private String nickname; //昵称
    private String avatar; //用户头像
    private String birthday; //生日
    private String gender; //性别
    private Integer age; //年龄
    private String city; //城市
    private String income; //收入
    private String education; //学历
    private String profession; //行业
    private Integer marriage; //婚姻状态
    private String tags; //用户标签:多个用逗号分隔
    private String coverPic; // 封面图片
    private Date created;
    private Date updated;

    //用户状态,1为正常,2为冻结
    @TableField(exist = false)
    private String userStatus = "1";
}

(2),dao(mapper),操作数据层

package com.tanhua.dubbo.mappers;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tanhua.model.domain.UserInfo;

public interface UserInfoMapper extends BaseMapper<UserInfo> {
}

(3)业务层接口

package com.tanhua.dubbo.api;

import com.tanhua.model.domain.UserInfo;

public interface UserInfoApi {
    //完善用户信息
    void saveUserInfo(UserInfo userInfo);

    //根据id修改用户信息
    void updateUserInfo(UserInfo userInfo);
}

(4)业务层实现类

package com.tanhua.dubbo.api;

import com.tanhua.dubbo.mappers.UserInfoMapper;
import com.tanhua.model.domain.UserInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class UserInfoAplImpl  implements UserInfoApi{

    @Autowired
    private UserInfoMapper userInfoMapper;


    /**
     * 完善用户信息,添加到数据库tb_user_info表
     * @param userInfo
     */
    public void saveUserInfo(UserInfo userInfo) {

        userInfoMapper.insert(userInfo);
    }

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

1.2服务消费者

1.校验token是否合法

2.获取token中携带的Userid给UserInfo设置id

3.调用UserInfoService保存数据

UserController

package com.tanhua.server.controller;

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

import java.io.IOException;

@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserInfoService userInfoService;

    /**
     * 完善用户信息:表现层
     * 请求路径:/user/loginReginfo
     * 请求方式:post
     * 请求参数:Headers(请求头):token;Body(请求体):UserInfo
     * 响应数据:null
     * @return
     */
    @PostMapping("/loginReginfo")
    public ResponseEntity uploadUserHeadImag(@RequestBody UserInfo userInfo,
                                             @RequestHeader("Authorization") String token){
        //1.校验token是否合法
        boolean result = JwtUtils.verifyToken(token);
        if(!result){
            //token为空,用户未登录
            return ResponseEntity.status(401).body(null);
        }

        //2.获取token中携带的Userid给UserInfo设置id
        Claims claims = JwtUtils.getClaims(token);
        Integer id = (Integer) claims.get("id");


        userInfo.setId(Long.valueOf(id));

        //3.调用UserInfoService保存数据
        userInfoService.saveUserInfo(userInfo);


        return ResponseEntity.ok(null);

    }
}

UserInfoService

package com.tanhua.server.service;

import com.tanhua.autoconfig.template.FaceApiTemplate;
import com.tanhua.autoconfig.template.OssTemplate;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.domain.UserInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Service
public class UserInfoService {


    @DubboReference
    private UserInfoApi userInfoApi;

    @Autowired
    private OssTemplate ossTemplate;

    @Autowired
    private FaceApiTemplate faceApiTemplate;
    /**
     * 向数据库表添加用户信息
     * @param userInfo
     */
    public void saveUserInfo(UserInfo userInfo) {
        userInfoApi.saveUserInfo(userInfo);
    }
}

二、上传头像

UserController

package com.tanhua.server.controller;

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

import java.io.IOException;

@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserInfoService userInfoService;

    /**
     * 用户上传头像
     * 1.请求路径:/user/loginReginfo/head
     * 2.请求方式:post
     * 3.请求参数:Authorization:token;headPhoto(用户上传的头像文件)
     * 4.响应结果:null
     * @return
     */
    @PostMapping("/loginReginfo/head")
    public ResponseEntity uploadUserimage(MultipartFile headPhoto,
                                          @RequestHeader("Authorization") String token ) throws IOException {


        //1.校验token的合法性
        boolean result = JwtUtils.verifyToken(token);
        if(!result){
            return ResponseEntity.status(401).body(null);
        }
        //2.获取token中携带的用户id
        Claims claims = JwtUtils.getClaims(token);
        Long id = (Long) claims.get("id");

        //3.调用业务层完成头像上传
        userInfoService.uploadUserHeadImage(headPhoto,id);

        return ResponseEntity.ok(null);

    }
}

UserInfoService

1.调用封装好的阿里云oss组件把用户上传的头像上传到阿里云oss,得到图片文件访问路径

2.调用百度人脸识别组件检测图片是否包含人脸

3.图片不包含人脸,抛出异常

4.图片包含人脸,调用api更新用户信息表

package com.tanhua.server.service;

import com.tanhua.autoconfig.template.FaceApiTemplate;
import com.tanhua.autoconfig.template.OssTemplate;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.domain.UserInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Service
public class UserInfoService {


    @DubboReference
    private UserInfoApi userInfoApi;

    @Autowired
    private OssTemplate ossTemplate;

    @Autowired
    private FaceApiTemplate faceApiTemplate;
    /**
     * 向数据库表添加用户信息
     * @param userInfo
     */
    public void saveUserInfo(UserInfo userInfo) {
        userInfoApi.saveUserInfo(userInfo);
    }




    /**
     * 用户上传头像;业务逻辑层
     *
     */
    public void uploadUserHeadImage(MultipartFile headPhoto, Long userId) throws IOException {


        //1.调用封装好的阿里云oss组件把用户上传的头像上传到阿里云oss,得到图片文件访问路径
        String headPhotoUrl = ossTemplate.upload(headPhoto.getOriginalFilename(), headPhoto.getInputStream());
        //2.调用百度人脸识别组件检测图片是否包含人脸
        boolean result = faceApiTemplate.checkFace(headPhotoUrl);
        if(!result){
            //图片不包含人脸,抛出异常
            throw new RuntimeException("图片卜包含人脸");
        }

        //3.图片包含人脸
        UserInfo userInfo = new UserInfo();
        userInfo.setId(userId);
        userInfo.setAvatar(headPhotoUrl);
        userInfoApi.updateUserInfo(userInfo);
    }


}

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