ebike-plus/ebike-user/src/main/java/com/cdzy/user/controller/EbikeUserController.java

219 lines
7.3 KiB
Java
Raw Normal View History

2025-10-15 17:38:13 +08:00
package com.cdzy.user.controller;
2025-12-25 17:16:59 +08:00
import com.cdzy.common.enums.Code;
import com.cdzy.common.ex.EbikeException;
2025-10-15 17:38:13 +08:00
import com.cdzy.common.model.response.JsonResult;
import com.cdzy.user.model.dto.EbikeUserPageDto;
import com.cdzy.user.model.dto.UserValidateDto;
import com.cdzy.user.model.dto.WechatInfoDto;
2025-10-29 17:01:46 +08:00
import com.cdzy.user.model.entity.EbikeOrder;
2025-10-21 09:57:21 +08:00
import com.cdzy.user.model.entity.EbikeUser;
2025-10-15 17:38:13 +08:00
import com.cdzy.user.model.vo.EbikeUserVo;
2025-10-29 17:01:46 +08:00
import com.cdzy.user.service.EbikeOrderService;
2025-12-23 10:19:35 +08:00
import com.cdzy.user.service.EbikeUserRealInfoService;
2025-10-15 17:38:13 +08:00
import com.cdzy.user.service.EbikeUserService;
2025-11-07 17:11:39 +08:00
import com.cdzy.user.utils.RedisUtil;
2025-10-15 17:38:13 +08:00
import com.cdzy.user.utils.VerifyUtil;
2025-12-25 17:16:59 +08:00
import com.ebike.feign.clients.OperationsFeignClient;
import com.ebike.feign.model.vo.FeignEbikeRegionVo;
2025-10-30 09:21:18 +08:00
import com.fasterxml.jackson.databind.JsonNode;
2025-10-15 17:38:13 +08:00
import com.mybatisflex.core.paginate.Page;
import jakarta.annotation.Resource;
2025-11-07 17:11:39 +08:00
import org.springframework.util.StringUtils;
2026-01-15 10:19:58 +08:00
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
2025-10-15 17:38:13 +08:00
2025-11-07 17:11:39 +08:00
import java.util.Map;
2025-10-15 17:38:13 +08:00
/**
* 用户登录控制层
*
2025-11-13 09:57:37 +08:00
* @author yanglei
* @since 2025-10-15 08:56
2025-10-15 17:38:13 +08:00
*/
@RestController
@RequestMapping("/ebikeUser")
public class EbikeUserController {
2025-11-07 17:11:39 +08:00
@Resource
private RedisUtil redisUtil;
2025-10-15 17:38:13 +08:00
@Resource
private VerifyUtil verifyUtil;
@Resource
private EbikeUserService ebikeUserService;
2025-10-16 11:39:47 +08:00
@Resource
2025-10-29 17:01:46 +08:00
private EbikeOrderService ebikeOrderTransactionService;
2025-10-16 11:39:47 +08:00
2025-12-23 10:19:35 +08:00
@Resource
private EbikeUserRealInfoService ebikeUserRealInfoService;
2025-12-25 17:16:59 +08:00
@Resource
private OperationsFeignClient operationsFeignClient;
2025-10-15 17:38:13 +08:00
/**
* 用户微信无感登录
*
* @param code 微信登录返回的code
*/
2025-11-07 17:11:39 +08:00
@GetMapping("login")
2025-10-15 17:38:13 +08:00
public JsonResult<?> silentLogin(@RequestParam(name = "js_code") String code) {
2025-10-30 09:21:18 +08:00
JsonNode result = verifyUtil.wechatAuthority(code);
2025-10-15 17:38:13 +08:00
if (result == null) {
2025-12-31 15:33:41 +08:00
throw new EbikeException("微信登录失败");
2025-10-15 17:38:13 +08:00
}
2025-10-30 09:21:18 +08:00
if (result.has("errcode")) {
return JsonResult.failed(String.format("微信登录失败 %s", result.get("errmsg").asText()));
2025-10-15 17:38:13 +08:00
}
2025-11-07 17:11:39 +08:00
// 储存session_key到Redis(2小时过期)
String sessionKey = result.get("session_key").asText();
String openid = result.get("openid").asText();
redisUtil.addWechatSession(openid, sessionKey);
Map<String, Object> data = ebikeUserService.checkUserExists(openid);
return JsonResult.success(data);
2025-10-15 17:38:13 +08:00
}
/**
2025-11-07 17:11:39 +08:00
* 保存微信用户信息
2025-10-15 17:38:13 +08:00
*
* @param wechatInfoDto 微信解密请求参数
*/
2025-11-07 17:11:39 +08:00
@PostMapping("saveWechatUserInfo")
2025-10-15 17:38:13 +08:00
public JsonResult<?> getEncryptInfo(@RequestBody WechatInfoDto wechatInfoDto) {
2025-11-07 17:11:39 +08:00
String sessionKey = redisUtil.getWechatSession(wechatInfoDto.getOpenId());
if (!StringUtils.hasText(sessionKey)) {
return JsonResult.failed("session已过期请重新登录");
2025-10-15 17:38:13 +08:00
}
2025-11-07 17:11:39 +08:00
// 解密手机号
String decryptedData = verifyUtil.decryptData(
wechatInfoDto.getEncryptedData(),
sessionKey,
wechatInfoDto.getIv()
);
2025-10-15 17:38:13 +08:00
if (decryptedData == null || decryptedData.isEmpty()) {
2025-11-07 17:11:39 +08:00
return JsonResult.failed("手机号解密失败!");
2025-10-15 17:38:13 +08:00
}
2025-11-07 17:11:39 +08:00
ebikeUserService.updateUserInfo(decryptedData, wechatInfoDto.getOpenId());
return JsonResult.success();
}
2025-10-30 09:21:18 +08:00
2025-10-15 17:38:13 +08:00
/**
* 用户实名验证
*
* @param userValidateDto 用户实名验证请求
* @return 验证结果
*/
@PostMapping("/verifyRealName")
2026-01-15 10:19:58 +08:00
public JsonResult<?> verifyRealName(@RequestBody @Validated UserValidateDto userValidateDto) {
2025-12-23 10:19:35 +08:00
verifyUtil.verifyRealName(userValidateDto);
return JsonResult.success();
}
/**
* 根据用户id校验用户是否已进行实名认证
*
* @param userId 用户id
* @return true 已进行实名验证 false 实名认证失败或未进行实名认证
*/
@GetMapping("/checkUserIsVerify")
public JsonResult<?> checkUserIsVerify(@RequestParam(name = "userId") Long userId) {
boolean result = ebikeUserRealInfoService.checkUserIsVerify(userId);
return JsonResult.success(result);
2025-10-15 17:38:13 +08:00
}
2025-12-23 10:19:35 +08:00
2025-10-15 17:38:13 +08:00
/**
* 注销用户信息
*
* @param ebikeUser 用户信息
* @return {@code true} 注销成功{@code false} 注销失败
*/
@PostMapping("deRegister")
public JsonResult<?> deRegister(@RequestBody EbikeUser ebikeUser) {
// 需要检测用户是否还有未支付的订单,如果有则不能注销
2025-10-29 17:01:46 +08:00
EbikeOrder unPayed = ebikeOrderTransactionService.checkHistoryOrder(ebikeUser.getUserId());
2025-10-29 14:06:51 +08:00
if (unPayed != null) {
2025-12-31 15:33:41 +08:00
throw new EbikeException("用户还有未完成的订单,不能注销");
2025-10-16 11:39:47 +08:00
}
2025-10-15 17:38:13 +08:00
// 注销用户信息
2025-10-29 14:06:51 +08:00
ebikeUserService.deRegister(ebikeUser);
return JsonResult.success();
2025-10-15 17:38:13 +08:00
}
/**
* 根据主键更新用户信息
*
* @param ebikeUser 用户信息
* @return {@code true} 更新成功{@code false} 更新失败
*/
@PostMapping("update")
public JsonResult<?> update(@RequestBody EbikeUser ebikeUser) {
2025-10-29 14:06:51 +08:00
ebikeUserService.updateById(ebikeUser);
return JsonResult.success();
2025-10-15 17:38:13 +08:00
}
/**
* 根据用户信息主键获取详细信息
*
2025-10-16 11:39:47 +08:00
* @param userId 用户信息主键
2025-10-15 17:38:13 +08:00
* @return 用户信息详情
*/
2025-12-31 15:33:41 +08:00
@GetMapping("getInfoByUserId")
public JsonResult<?> getInfoByUserId(@RequestParam(name = "userId") Long userId) {
EbikeUserVo user = ebikeUserService.getUserInfoByUserId(userId);
2025-10-29 14:06:51 +08:00
return JsonResult.success(user);
2025-10-15 17:38:13 +08:00
}
/**
* 根据用户微信openId获取详细信息
*
* @param openId 用户微信openId
* @return 用户信息详情
*/
@GetMapping("getInfoByOpenId")
public JsonResult<?> getInfoByOpenId(@RequestParam(name = "openId") String openId) {
EbikeUserVo user = ebikeUserService.getUserByOpenId(openId);
2025-10-29 14:06:51 +08:00
return JsonResult.success(user);
2025-10-15 17:38:13 +08:00
}
/**
* 分页查询用户信息
*
* @param userPageDto 分页查询参数
* @return 分页对象
*/
@PostMapping("page")
2026-01-15 10:19:58 +08:00
public JsonResult<?> page(@RequestBody EbikeUserPageDto userPageDto) {
2025-10-15 17:38:13 +08:00
Page<EbikeUserVo> list = ebikeUserService.queryPage(userPageDto);
return JsonResult.success(list);
}
2025-12-25 17:16:59 +08:00
/**
* 查询当前位置的所在运营区
*
* @param lng 经度
* @param lat 维度
* @return 运营区信息
*/
@GetMapping("getRegionByLocation")
public JsonResult<?> getRegionByLocation(@RequestParam("lng") double lng,
@RequestParam("lat") double lat,
@RequestParam("radius") double radius) {
JsonResult<FeignEbikeRegionVo> jsonResult = operationsFeignClient.getRegionByLocation(lng, lat, radius);
2025-12-25 17:16:59 +08:00
if (jsonResult.getCode() != Code.SUCCESS) {
throw new EbikeException(jsonResult.getMessage());
}
return JsonResult.success(jsonResult.getData());
}
2025-10-30 09:21:18 +08:00
}