|
@@ -7,6 +7,10 @@ import org.springframework.web.bind.annotation.RequestBody;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
|
|
+import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
|
|
|
|
+import io.swagger.v3.oas.annotations.responses.ApiResponses;
|
|
|
|
|
+import io.swagger.v3.oas.annotations.media.Content;
|
|
|
|
|
+import io.swagger.v3.oas.annotations.media.Schema;
|
|
|
import work.baiyun.chronicdiseaseapp.common.R;
|
|
import work.baiyun.chronicdiseaseapp.common.R;
|
|
|
import work.baiyun.chronicdiseaseapp.model.vo.GetOpenidRequest;
|
|
import work.baiyun.chronicdiseaseapp.model.vo.GetOpenidRequest;
|
|
|
import work.baiyun.chronicdiseaseapp.service.WeChatService;
|
|
import work.baiyun.chronicdiseaseapp.service.WeChatService;
|
|
@@ -14,6 +18,7 @@ import work.baiyun.chronicdiseaseapp.enums.PermissionGroup;
|
|
|
import work.baiyun.chronicdiseaseapp.mapper.UserInfoMapper;
|
|
import work.baiyun.chronicdiseaseapp.mapper.UserInfoMapper;
|
|
|
import work.baiyun.chronicdiseaseapp.model.po.UserInfo;
|
|
import work.baiyun.chronicdiseaseapp.model.po.UserInfo;
|
|
|
import work.baiyun.chronicdiseaseapp.service.TokenService;
|
|
import work.baiyun.chronicdiseaseapp.service.TokenService;
|
|
|
|
|
+import work.baiyun.chronicdiseaseapp.enums.ErrorCode;
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
|
|
|
@@ -37,21 +42,34 @@ public class WeChatController {
|
|
|
|
|
|
|
|
|
|
|
|
|
@Operation(summary = "获取 openid", description = "根据小程序 code 获取 openid 并创建/返回用户 token")
|
|
@Operation(summary = "获取 openid", description = "根据小程序 code 获取 openid 并创建/返回用户 token")
|
|
|
-
|
|
|
|
|
|
|
+ @ApiResponses(value = {
|
|
|
|
|
+ @ApiResponse(responseCode = "200", description = "成功获取openid并返回token",
|
|
|
|
|
+ content = @Content(mediaType = "application/json",
|
|
|
|
|
+ schema = @Schema(implementation = Map.class))),
|
|
|
|
|
+ @ApiResponse(responseCode = "400", description = "请求参数错误",
|
|
|
|
|
+ content = @Content(mediaType = "application/json",
|
|
|
|
|
+ schema = @Schema(implementation = Void.class))),
|
|
|
|
|
+ @ApiResponse(responseCode = "500", description = "服务器内部错误",
|
|
|
|
|
+ content = @Content(mediaType = "application/json",
|
|
|
|
|
+ schema = @Schema(implementation = Void.class)))
|
|
|
|
|
+ })
|
|
|
@PostMapping(path = "/get_openid", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
@PostMapping(path = "/get_openid", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
public R<?> getOpenid(@RequestBody(required = false) GetOpenidRequest req) {
|
|
public R<?> getOpenid(@RequestBody(required = false) GetOpenidRequest req) {
|
|
|
if (req == null || req.getCode() == null || req.getCode().isEmpty()) {
|
|
if (req == null || req.getCode() == null || req.getCode().isEmpty()) {
|
|
|
- return R.fail(400, "Missing code parameter");
|
|
|
|
|
|
|
+ return R.fail(ErrorCode.PARAMETER_ERROR.getCode(),
|
|
|
|
|
+ ErrorCode.PARAMETER_ERROR.getMessage() + ": Missing code parameter");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// role is required now
|
|
// role is required now
|
|
|
if (req.getRole() == null) {
|
|
if (req.getRole() == null) {
|
|
|
- return R.fail(400, "Missing role parameter");
|
|
|
|
|
|
|
+ return R.fail(ErrorCode.WECHAT_ROLE_REQUIRED.getCode(),
|
|
|
|
|
+ ErrorCode.WECHAT_ROLE_REQUIRED.getMessage());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
String openid = weChatService.getOpenId(req.getCode());
|
|
String openid = weChatService.getOpenId(req.getCode());
|
|
|
if (openid == null) {
|
|
if (openid == null) {
|
|
|
- return R.fail(500, "Failed to get openid");
|
|
|
|
|
|
|
+ return R.fail(ErrorCode.WECHAT_GET_OPENID_FAILED.getCode(),
|
|
|
|
|
+ ErrorCode.WECHAT_GET_OPENID_FAILED.getMessage());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// map incoming role int to PermissionGroup enum
|
|
// map incoming role int to PermissionGroup enum
|
|
@@ -59,12 +77,13 @@ public class WeChatController {
|
|
|
try {
|
|
try {
|
|
|
pg = PermissionGroup.fromCode(req.getRole());
|
|
pg = PermissionGroup.fromCode(req.getRole());
|
|
|
} catch (IllegalArgumentException e) {
|
|
} catch (IllegalArgumentException e) {
|
|
|
- return R.fail(400, "Invalid role parameter");
|
|
|
|
|
|
|
+ return R.fail(ErrorCode.WECHAT_ROLE_INVALID.getCode(),
|
|
|
|
|
+ ErrorCode.WECHAT_ROLE_INVALID.getMessage());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 查找是否存在 role + wx_openid 的用户
|
|
// 查找是否存在 role + wx_openid 的用户
|
|
|
QueryWrapper<UserInfo> qw = new QueryWrapper<>();
|
|
QueryWrapper<UserInfo> qw = new QueryWrapper<>();
|
|
|
- qw.eq("role", pg.getCode()).eq("wx_openid", openid);
|
|
|
|
|
|
|
+ qw.eq("role", pg.getCode()).eq("wx_openid", openid);
|
|
|
UserInfo ui = userInfoMapper.selectOne(qw);
|
|
UserInfo ui = userInfoMapper.selectOne(qw);
|
|
|
if (ui == null) {
|
|
if (ui == null) {
|
|
|
// create new user info with provided role and openid
|
|
// create new user info with provided role and openid
|
|
@@ -91,23 +110,39 @@ public class WeChatController {
|
|
|
* 向后兼容:如果没有 Authorization header,仍然支持 X-Token 或 token header,或 POST body 中的 { "token": "..." }。
|
|
* 向后兼容:如果没有 Authorization header,仍然支持 X-Token 或 token header,或 POST body 中的 { "token": "..." }。
|
|
|
*/
|
|
*/
|
|
|
@Operation(summary = "获取用户信息", description = "根据 token 返回当前用户信息(支持 Authorization/X-Token/token)")
|
|
@Operation(summary = "获取用户信息", description = "根据 token 返回当前用户信息(支持 Authorization/X-Token/token)")
|
|
|
|
|
+ @ApiResponses(value = {
|
|
|
|
|
+ @ApiResponse(responseCode = "200", description = "成功获取用户信息",
|
|
|
|
|
+ content = @Content(mediaType = "application/json",
|
|
|
|
|
+ schema = @Schema(implementation = Map.class))),
|
|
|
|
|
+ @ApiResponse(responseCode = "401", description = "未授权访问",
|
|
|
|
|
+ content = @Content(mediaType = "application/json",
|
|
|
|
|
+ schema = @Schema(implementation = Void.class))),
|
|
|
|
|
+ @ApiResponse(responseCode = "404", description = "用户不存在",
|
|
|
|
|
+ content = @Content(mediaType = "application/json",
|
|
|
|
|
+ schema = @Schema(implementation = Void.class))),
|
|
|
|
|
+ @ApiResponse(responseCode = "500", description = "服务器内部错误",
|
|
|
|
|
+ content = @Content(mediaType = "application/json",
|
|
|
|
|
+ schema = @Schema(implementation = Void.class)))
|
|
|
|
|
+ })
|
|
|
@PostMapping(path = "/user_info", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
@PostMapping(path = "/user_info", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
public R<?> getUserInfo(@RequestBody(required = false) Map<String, String> body, HttpServletRequest request) {
|
|
public R<?> getUserInfo(@RequestBody(required = false) Map<String, String> body, HttpServletRequest request) {
|
|
|
Long userId;
|
|
Long userId;
|
|
|
try {
|
|
try {
|
|
|
userId = work.baiyun.chronicdiseaseapp.util.SecurityUtils.getCurrentUserId();
|
|
userId = work.baiyun.chronicdiseaseapp.util.SecurityUtils.getCurrentUserId();
|
|
|
} catch (work.baiyun.chronicdiseaseapp.exception.CustomException e) {
|
|
} catch (work.baiyun.chronicdiseaseapp.exception.CustomException e) {
|
|
|
- return R.fail(401, "No valid userId");
|
|
|
|
|
|
|
+ return R.fail(ErrorCode.UNAUTHORIZED.getCode(),
|
|
|
|
|
+ ErrorCode.UNAUTHORIZED.getMessage());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
UserInfo ui = userInfoMapper.selectById(userId);
|
|
UserInfo ui = userInfoMapper.selectById(userId);
|
|
|
if (ui == null) {
|
|
if (ui == null) {
|
|
|
- return R.fail(404, "User not found");
|
|
|
|
|
|
|
+ return R.fail(ErrorCode.USER_NOT_EXIST.getCode(),
|
|
|
|
|
+ ErrorCode.USER_NOT_EXIST.getMessage());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
Map<String, Object> out = new HashMap<>();
|
|
Map<String, Object> out = new HashMap<>();
|
|
|
out.put("id", ui.getId());
|
|
out.put("id", ui.getId());
|
|
|
- out.put("wx_openid", ui.getWxOpenid());
|
|
|
|
|
|
|
+ out.put("wx_openid", ui.getWxOpenid());
|
|
|
out.put("role", ui.getRole() != null ? ui.getRole().getCode() : null);
|
|
out.put("role", ui.getRole() != null ? ui.getRole().getCode() : null);
|
|
|
out.put("avatar", ui.getAvatar());
|
|
out.put("avatar", ui.getAvatar());
|
|
|
out.put("nickname", ui.getNickname());
|
|
out.put("nickname", ui.getNickname());
|
|
@@ -120,6 +155,23 @@ public class WeChatController {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Operation(summary = "更新用户信息", description = "更新用户的头像、昵称、手机号、年龄、地址等")
|
|
@Operation(summary = "更新用户信息", description = "更新用户的头像、昵称、手机号、年龄、地址等")
|
|
|
|
|
+ @ApiResponses(value = {
|
|
|
|
|
+ @ApiResponse(responseCode = "200", description = "用户信息更新成功",
|
|
|
|
|
+ content = @Content(mediaType = "application/json",
|
|
|
|
|
+ schema = @Schema(implementation = Void.class))),
|
|
|
|
|
+ @ApiResponse(responseCode = "400", description = "请求参数错误",
|
|
|
|
|
+ content = @Content(mediaType = "application/json",
|
|
|
|
|
+ schema = @Schema(implementation = Void.class))),
|
|
|
|
|
+ @ApiResponse(responseCode = "401", description = "未授权访问",
|
|
|
|
|
+ content = @Content(mediaType = "application/json",
|
|
|
|
|
+ schema = @Schema(implementation = Void.class))),
|
|
|
|
|
+ @ApiResponse(responseCode = "404", description = "用户不存在",
|
|
|
|
|
+ content = @Content(mediaType = "application/json",
|
|
|
|
|
+ schema = @Schema(implementation = Void.class))),
|
|
|
|
|
+ @ApiResponse(responseCode = "500", description = "服务器内部错误",
|
|
|
|
|
+ content = @Content(mediaType = "application/json",
|
|
|
|
|
+ schema = @Schema(implementation = Void.class)))
|
|
|
|
|
+ })
|
|
|
@PostMapping(path = "/update_user_info", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
@PostMapping(path = "/update_user_info", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
public R<?> updateUserInfo(@RequestBody(required = false) work.baiyun.chronicdiseaseapp.model.vo.UpdateUserInfoRequest req,
|
|
public R<?> updateUserInfo(@RequestBody(required = false) work.baiyun.chronicdiseaseapp.model.vo.UpdateUserInfoRequest req,
|
|
|
HttpServletRequest request) {
|
|
HttpServletRequest request) {
|
|
@@ -127,12 +179,14 @@ public class WeChatController {
|
|
|
try {
|
|
try {
|
|
|
userId = work.baiyun.chronicdiseaseapp.util.SecurityUtils.getCurrentUserId();
|
|
userId = work.baiyun.chronicdiseaseapp.util.SecurityUtils.getCurrentUserId();
|
|
|
} catch (work.baiyun.chronicdiseaseapp.exception.CustomException e) {
|
|
} catch (work.baiyun.chronicdiseaseapp.exception.CustomException e) {
|
|
|
- return R.fail(401, "No valid userId");
|
|
|
|
|
|
|
+ return R.fail(ErrorCode.UNAUTHORIZED.getCode(),
|
|
|
|
|
+ ErrorCode.UNAUTHORIZED.getMessage());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
UserInfo ui = userInfoMapper.selectById(userId);
|
|
UserInfo ui = userInfoMapper.selectById(userId);
|
|
|
if (ui == null) {
|
|
if (ui == null) {
|
|
|
- return R.fail(404, "User not found");
|
|
|
|
|
|
|
+ return R.fail(ErrorCode.USER_NOT_EXIST.getCode(),
|
|
|
|
|
+ ErrorCode.USER_NOT_EXIST.getMessage());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (req != null) {
|
|
if (req != null) {
|
|
@@ -145,7 +199,8 @@ public class WeChatController {
|
|
|
try {
|
|
try {
|
|
|
ui.setSex(work.baiyun.chronicdiseaseapp.enums.Gender.fromCode(req.getSex()));
|
|
ui.setSex(work.baiyun.chronicdiseaseapp.enums.Gender.fromCode(req.getSex()));
|
|
|
} catch (IllegalArgumentException e) {
|
|
} catch (IllegalArgumentException e) {
|
|
|
- return R.fail(400, "Invalid sex value");
|
|
|
|
|
|
|
+ return R.fail(ErrorCode.PARAMETER_ERROR.getCode(),
|
|
|
|
|
+ ErrorCode.PARAMETER_ERROR.getMessage() + ": Invalid sex value");
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
userInfoMapper.updateById(ui);
|
|
userInfoMapper.updateById(ui);
|
|
@@ -153,4 +208,4 @@ public class WeChatController {
|
|
|
|
|
|
|
|
return R.success(200, "ok");
|
|
return R.success(200, "ok");
|
|
|
}
|
|
}
|
|
|
-}
|
|
|
|
|
|
|
+}
|