package com.cdzy.user.controller; import com.alibaba.fastjson2.JSONObject; 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.EbikeOrderTransaction; import com.cdzy.user.model.entity.EbikeUser; import com.cdzy.user.model.vo.EbikeUserVo; import com.cdzy.user.service.EbikeOrderTransactionService; import com.cdzy.user.service.EbikeUserService; import com.cdzy.user.utils.VerifyUtil; import com.mybatisflex.core.paginate.Page; import jakarta.annotation.Resource; import org.springframework.web.bind.annotation.*; /** * 用户登录控制层 * * @author: yanglei * @since: 2025-10-15 08:56 */ @RestController @RequestMapping("/ebikeUser") public class EbikeUserController { @Resource private VerifyUtil verifyUtil; @Resource private EbikeUserService ebikeUserService; @Resource private EbikeOrderTransactionService ebikeOrderTransactionService; /** * 用户微信无感登录。 * * @param code 微信登录返回的code */ @RequestMapping("login") public JsonResult silentLogin(@RequestParam(name = "js_code") String code) { JSONObject result = verifyUtil.wechatAuthority(code); if (result == null) { return JsonResult.failed("微信登录失败"); } if (result.containsKey("errcode")) { return JsonResult.failed(String.format("微信登录失败 %s", result.getString("errmsg"))); } return JsonResult.success("微信登录成功", result.getString("openid")); } /** * 获取用户加密信息。 * * @param wechatInfoDto 微信解密请求参数 */ @PostMapping("getEncryptInfo") public JsonResult getEncryptInfo(@RequestBody WechatInfoDto wechatInfoDto) { JSONObject result = verifyUtil.wechatAuthority(wechatInfoDto.getJsCode()); if (result == null) { return JsonResult.failed("解密失败"); } if (result.containsKey("errcode")) { return JsonResult.failed(String.format("解密失败 %s", result.getString("errmsg"))); } String decryptedData = verifyUtil.decryptData(wechatInfoDto.getEncryptedData(), result.getString("session_key"), wechatInfoDto.getIv()); if (decryptedData == null || decryptedData.isEmpty()) { return JsonResult.failed("解密失败"); } JSONObject jsonObject = JSONObject.parseObject(decryptedData); return JsonResult.success("解密成功", jsonObject.getString("phoneNumber")); } /** * 用户实名验证 * * @param userValidateDto 用户实名验证请求 * @return 验证结果 */ @PostMapping("/verifyRealName") public JsonResult verifyRealName(@RequestBody UserValidateDto userValidateDto) { return verifyUtil.verifyRealName(userValidateDto); } /** * 添加用户信息。 * * @param ebikeUser 用户信息 * @return {@code true} 添加成功,{@code false} 添加失败 */ @PostMapping("save") public JsonResult save(@RequestBody EbikeUser ebikeUser) { boolean r = ebikeUserService.save(ebikeUser); return r ? JsonResult.success("添加用户信息成功", ebikeUser.getUserId()) : JsonResult.failed("添加用户信息失败"); } /** * 根据主键删除用户信息。 * * @param userId 主键 * @return {@code true} 删除成功,{@code false} 删除失败 */ @PostMapping("remove") public JsonResult remove(@RequestParam(name = "userId") Long userId) { boolean r = ebikeUserService.removeById(userId); return r ? JsonResult.success() : JsonResult.failed("删除用户信息失败"); } /** * 注销用户信息。 * * @param ebikeUser 用户信息 * @return {@code true} 注销成功,{@code false} 注销失败 */ @PostMapping("deRegister") public JsonResult deRegister(@RequestBody EbikeUser ebikeUser) { // 需要检测用户是否还有未支付的订单,如果有则不能注销 EbikeOrderTransaction unpayed = ebikeOrderTransactionService.checkHistoryOrder(ebikeUser.getUserId()); if (unpayed != null) { return JsonResult.failed("用户还有未完成的订单,不能注销", unpayed); } // 注销用户信息 boolean r = ebikeUserService.deRegister(ebikeUser); return r ? JsonResult.success() : JsonResult.failed("注销用户信息失败"); } /** * 根据主键更新用户信息。 * * @param ebikeUser 用户信息 * @return {@code true} 更新成功,{@code false} 更新失败 */ @PostMapping("update") public JsonResult update(@RequestBody EbikeUser ebikeUser) { boolean r = ebikeUserService.updateById(ebikeUser); return r ? JsonResult.success() : JsonResult.failed("更新用户信息失败"); } /** * 根据用户信息主键获取详细信息。 * * @param userId 用户信息主键 * @return 用户信息详情 */ @GetMapping("getInfo") public JsonResult getInfo(@RequestParam(name = "userId") Long userId) { EbikeUser user = ebikeUserService.getById(userId); return user == null ? JsonResult.failed("用户信息不存在") : JsonResult.success(user); } /** * 根据用户微信openId获取详细信息。 * * @param openId 用户微信openId * @return 用户信息详情 */ @GetMapping("getInfoByOpenId") public JsonResult getInfoByOpenId(@RequestParam(name = "openId") String openId) { EbikeUserVo user = ebikeUserService.getUserByOpenId(openId); return user == null ? JsonResult.failed("用户信息不存在") : JsonResult.success(user); } /** * 根据用户信息手机号获取详细信息。 * * @param mobile 用户信息手机号 * @return 用户信息详情 */ @GetMapping("getInfoByMobile") public JsonResult getInfoByMobile(@RequestParam(name = "mobile") String mobile) { EbikeUser user = ebikeUserService.getUserByMobile(mobile); return user == null ? JsonResult.failed("用户信息不存在") : JsonResult.success(user); } /** * 分页查询用户信息。 * * @param userPageDto 分页查询参数 * @return 分页对象 */ @PostMapping("page") public JsonResult page(@RequestBody EbikeUserPageDto userPageDto) { Page list = ebikeUserService.queryPage(userPageDto); return JsonResult.success(list); } }