浏览代码

增加个人信息

zhou-hao 5 年之前
父节点
当前提交
c2aa242d44

+ 53 - 0
jetlinks-manager/authentication-manager/src/main/java/org/jetlinks/community/auth/entity/UserDetail.java

@@ -0,0 +1,53 @@
+package org.jetlinks.community.auth.entity;
+
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.Setter;
+import org.hswebframework.web.system.authorization.api.entity.UserEntity;
+
+import java.util.List;
+
+@Getter
+@Setter
+@NoArgsConstructor
+public class UserDetail {
+
+    private String id;
+
+    private String name;
+
+    private String email;
+
+    private String telephone;
+
+    private String avatar;
+
+    private String description;
+
+    private String username;
+
+    private long createTime;
+
+    public static UserDetail of(UserEntity entity,UserDetailEntity detailEntity) {
+        return new UserDetail().with(entity).with(detailEntity);
+    }
+
+    public UserDetail with(UserDetailEntity entity) {
+        this.setAvatar(entity.getAvatar());
+        this.setDescription(entity.getDescription());
+        this.setTelephone(entity.getTelephone());
+        this.setEmail(entity.getEmail());
+
+        return this;
+    }
+
+    public UserDetail with(UserEntity entity) {
+        this.setId(entity.getId());
+        this.setName(entity.getName());
+        if (entity.getCreateTime() != null) {
+            setCreateTime(entity.getCreateTime());
+        }
+        return this;
+    }
+
+}

+ 35 - 0
jetlinks-manager/authentication-manager/src/main/java/org/jetlinks/community/auth/entity/UserDetailEntity.java

@@ -0,0 +1,35 @@
+package org.jetlinks.community.auth.entity;
+
+import lombok.Getter;
+import lombok.Setter;
+import org.hibernate.validator.constraints.URL;
+import org.hswebframework.web.api.crud.entity.GenericEntity;
+
+import javax.persistence.Column;
+import javax.persistence.Table;
+import javax.validation.constraints.Email;
+import javax.validation.constraints.NotBlank;
+
+@Table(name = "s_user_detail")
+@Getter
+@Setter
+public class UserDetailEntity extends GenericEntity<String> {
+
+    @Column(nullable = false)
+    @NotBlank(message = "姓名不能为空")
+    private String name;
+
+    @Column
+    @Email(message = "邮件格式错误")
+    private String email;
+
+    @Column(length = 32)
+    private String telephone;
+
+    @Column(length = 2000)
+    @URL(message = "头像格式错误")
+    private String avatar;
+
+    @Column(length = 2000)
+    private String description;
+}

+ 47 - 0
jetlinks-manager/authentication-manager/src/main/java/org/jetlinks/community/auth/service/UserDetailService.java

@@ -0,0 +1,47 @@
+package org.jetlinks.community.auth.service;
+
+import lombok.AllArgsConstructor;
+import org.hswebframework.web.bean.FastBeanCopier;
+import org.hswebframework.web.crud.service.GenericReactiveCrudService;
+import org.hswebframework.web.system.authorization.api.entity.UserEntity;
+import org.hswebframework.web.system.authorization.api.service.reactive.ReactiveUserService;
+import org.hswebframework.web.validator.ValidatorUtils;
+import org.jetlinks.community.auth.entity.UserDetail;
+import org.jetlinks.community.auth.entity.UserDetailEntity;
+import org.jetlinks.community.auth.service.request.SaveUserDetailRequest;
+import org.springframework.stereotype.Service;
+import reactor.core.publisher.Mono;
+
+@Service
+@AllArgsConstructor
+public class UserDetailService extends GenericReactiveCrudService<UserDetailEntity, String> {
+
+    private final ReactiveUserService userService;
+
+    private final static UserDetailEntity emptyDetail = new UserDetailEntity();
+
+    public Mono<UserDetail> findUserDetail(String userId) {
+        return Mono
+            .zip(
+                userService.findById(userId), // 基本信息
+                this.findById(userId).defaultIfEmpty(emptyDetail), // 详情
+                UserDetail::of
+            );
+    }
+
+
+    public Mono<Void> saveUserDetail(String userId, SaveUserDetailRequest request) {
+        ValidatorUtils.tryValidate(request);
+        UserDetailEntity entity = FastBeanCopier.copy(request, new UserDetailEntity());
+        entity.setId(userId);
+
+        UserEntity userEntity = new UserEntity();
+        userEntity.setId(userId);
+        userEntity.setName(request.getName());
+
+        return save(Mono.just(entity))
+            .then(userService.saveUser(Mono.just(userEntity)))
+            .then();
+    }
+
+}

+ 23 - 0
jetlinks-manager/authentication-manager/src/main/java/org/jetlinks/community/auth/service/request/SaveUserDetailRequest.java

@@ -0,0 +1,23 @@
+package org.jetlinks.community.auth.service.request;
+
+import lombok.Getter;
+import lombok.Setter;
+
+import javax.validation.constraints.NotBlank;
+
+@Getter
+@Setter
+public class SaveUserDetailRequest {
+
+    @NotBlank(message = "姓名不能为空")
+    private String name;
+
+    private String email;
+
+    private String telephone;
+
+    private String avatar;
+
+    private String description;
+
+}

+ 46 - 0
jetlinks-manager/authentication-manager/src/main/java/org/jetlinks/community/auth/web/UserDetailController.java

@@ -0,0 +1,46 @@
+package org.jetlinks.community.auth.web;
+
+import lombok.AllArgsConstructor;
+import org.hswebframework.web.authorization.Authentication;
+import org.hswebframework.web.authorization.exception.UnAuthorizedException;
+import org.jetlinks.community.auth.entity.UserDetail;
+import org.jetlinks.community.auth.service.UserDetailService;
+import org.jetlinks.community.auth.service.request.SaveUserDetailRequest;
+import org.springframework.web.bind.annotation.*;
+import reactor.core.publisher.Mono;
+
+@RestController
+@RequestMapping("/user/detail")
+@AllArgsConstructor
+public class UserDetailController {
+
+    private final UserDetailService userDetailService;
+
+    /**
+     * 获取当前登录用户详情
+     *
+     * @return 用户详情
+     */
+    @GetMapping
+    public Mono<UserDetail> getCurrentLoginUserDetail() {
+        return Authentication
+            .currentReactive()
+            .switchIfEmpty(Mono.error(UnAuthorizedException::new))
+            .flatMap(autz -> userDetailService.findUserDetail(autz.getUser().getId()));
+    }
+
+    /**
+     * 保存当前用户详情
+     *
+     * @return 用户详情
+     */
+    @PutMapping
+    public Mono<Void> saveUserDetail(@RequestBody Mono<SaveUserDetailRequest> request) {
+        return Authentication
+            .currentReactive()
+            .zipWith(request)
+            .switchIfEmpty(Mono.error(UnAuthorizedException::new))
+            .flatMap(tp2 -> userDetailService.saveUserDetail(tp2.getT1().getUser().getId(), tp2.getT2()));
+    }
+
+}