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