package com.ruoyi.user.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; 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.RestController; import com.ruoyi.common.core.domain.R; import com.ruoyi.common.core.utils.StringUtils; import com.ruoyi.common.security.annotation.InnerAuth; import com.ruoyi.system.api.domain.AccountMember; import com.ruoyi.system.api.model.AccountRegisterRequest; import com.ruoyi.system.api.model.AccountResetPasswordRequest; import com.ruoyi.user.domain.AccountUser; import com.ruoyi.user.service.IAccountUserService; /** * C端会员 内部接口(仅服务间调用) * * 供认证服务(ruoyi-auth)在 C 端手机号一键登录 / 注册流程中调用, * 通过 @InnerAuth 限制仅内部来源(from-source=inner)可访问。 * * @author hugray */ @RestController @RequestMapping("/member") public class AccountMemberInnerController { @Autowired private IAccountUserService accountUserService; /** * 按手机号查询C端会员 * * @param phone 手机号 * @return 会员信息(不存在时 data 为空) */ @InnerAuth @GetMapping("/info/{phone}") public R info(@PathVariable("phone") String phone) { AccountUser accountUser = accountUserService.selectAccountUserByPhone(phone); if (StringUtils.isNull(accountUser)) { return R.ok(); } return R.ok(toAccountMember(accountUser)); } /** * 按用户名查询会员(服务商端账号密码登录使用) * * 返回结果含 password 密文与 providerFlag,供认证服务校验密码与服务商身份。 * * @param userName 用户名 * @return 会员信息(不存在时 data 为空) */ @InnerAuth @GetMapping("/info/username/{userName}") public R infoByUsername(@PathVariable("userName") String userName) { AccountUser accountUser = accountUserService.selectAccountUserByUsername(userName); if (StringUtils.isNull(accountUser)) { return R.ok(); } return R.ok(toAccountMember(accountUser)); } /** * 注册C端会员 * * @param request 注册请求(手机号、推荐码) * @return 注册成功后的会员信息 */ @InnerAuth @PostMapping("/register") public R register(@Validated @RequestBody AccountRegisterRequest request) { AccountUser accountUser = accountUserService.registerMember(request.getPhone(), request.getReferralCode()); return R.ok(toAccountMember(accountUser)); } /** * 重置C端会员密码(忘记密码) * * 认证服务完成手机号、账号状态与验证码校验后调用;此处 BCrypt 加密新密码并只更新 password 一列。 * * @param request 重置请求(手机号、新密码) * @return 处理结果 */ @InnerAuth @PostMapping("/password/reset") public R resetPassword(@Validated @RequestBody AccountResetPasswordRequest request) { accountUserService.resetPasswordByPhone(request.getPhone(), request.getPassword()); return R.ok(Boolean.TRUE); } /** * 会员实体转换为跨服务契约对象 * * 一并回传 password 密文与 providerFlag:前者供认证服务核对密码(沿用 RuoYi * 登录时下发密文、由 auth 侧 matchesPassword 的范式),后者供其判定服务商身份。 * 该对象仅经 @InnerAuth 内部接口流转,不对 C 端下发。 */ private AccountMember toAccountMember(AccountUser accountUser) { AccountMember member = new AccountMember(); member.setUserId(accountUser.getUserId()); member.setUserName(accountUser.getUserName()); member.setPhone(accountUser.getPhone()); member.setNickname(accountUser.getNickname()); member.setAvatar(accountUser.getAvatar()); member.setMyReferralCode(accountUser.getMyReferralCode()); member.setReferrerId(accountUser.getReferrerId()); member.setStatus(accountUser.getStatus()); member.setProviderFlag(accountUser.getProviderFlag()); member.setPassword(accountUser.getPassword()); return member; } }