2025-10-15 17:38:13 +08:00
|
|
|
|
package com.cdzy.user.controller;
|
|
|
|
|
|
|
|
|
|
|
|
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-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;
|
2025-10-15 17:38:13 +08:00
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
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-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) {
|
|
|
|
|
|
return JsonResult.failed("微信登录失败");
|
|
|
|
|
|
}
|
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")
|
|
|
|
|
|
public JsonResult<?> verifyRealName(@RequestBody 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) {
|
|
|
|
|
|
return JsonResult.failed("用户还有未完成的订单,不能注销", unPayed);
|
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 用户信息详情
|
|
|
|
|
|
*/
|
|
|
|
|
|
@GetMapping("getInfo")
|
2025-10-16 11:39:47 +08:00
|
|
|
|
public JsonResult<?> getInfo(@RequestParam(name = "userId") Long userId) {
|
|
|
|
|
|
EbikeUser user = ebikeUserService.getById(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")
|
|
|
|
|
|
public JsonResult<?> page(@RequestBody EbikeUserPageDto userPageDto) {
|
|
|
|
|
|
Page<EbikeUserVo> list = ebikeUserService.queryPage(userPageDto);
|
|
|
|
|
|
return JsonResult.success(list);
|
|
|
|
|
|
}
|
2025-10-30 09:21:18 +08:00
|
|
|
|
}
|