package com.gszc.controller; import com.alibaba.fastjson.JSONObject; import com.gszc.build.Result; import com.gszc.build.ResultBuilder; import com.gszc.entity.MiniUser; import com.gszc.entity.PcUser; import com.gszc.service.LoginService; import com.gszc.service.MiniAppService; import com.gszc.util.JwtUtils; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletResponse; @Api(value = "工商注册 API", tags = {"登录api"}) @RestController @RequestMapping("/login") public class LoginController { @Autowired LoginService loginService; @Autowired private MiniAppService miniAppService; @PostMapping("/decryptUserInfo") @ApiOperation(value = "解密用户信息", notes = "解密用户信息") @ApiImplicitParams({ @ApiImplicitParam(name = "userId", value = "userId", dataType = "string", paramType = "query"), @ApiImplicitParam(name = "encryptedData", value = "加密数据", dataType = "string", paramType = "query"), @ApiImplicitParam(name = "ivStr", value = "iv", dataType = "string", paramType = "query"), @ApiImplicitParam(name = "signature", value = "签名", dataType = "string", paramType = "query"), @ApiImplicitParam(name = "rawData", value = "原始数据", dataType = "string", paramType = "query"), }) public Result decryptUserInfo(String userId,String encryptedData ,String ivStr,String signature,String rawData){ MiniUser miniUser = miniAppService.decryptUserInfo(userId, encryptedData, ivStr, signature, rawData); return ResultBuilder.withPayload(miniUser).build(); } @PostMapping("/isNewUserLogin") @ApiOperation(value = "是否新用户", notes = "是否新用户") @ApiImplicitParams({ @ApiImplicitParam(name = "code", value = "code", dataType = "string", paramType = "query"), }) public Result isNewUserLogin(String code,HttpServletResponse response){ JSONObject result = miniAppService.isNewUser(code); Cookie cookie = new Cookie("username", result.getString("openId")); cookie.setMaxAge(60 * 60); cookie.setPath("/"); response.addCookie(cookie); String sign = JwtUtils.sign(result.getString("openId"), result.getString("openId"),"miniUser"); result.put("token",sign); result.remove("openId"); return ResultBuilder.withPayload(result).build(); } @ApiOperation(value = "获得手机号码", notes = "获得手机号码") @ApiImplicitParams({ @ApiImplicitParam(name = "userId", value = "userId", dataType = "string", paramType = "query"), @ApiImplicitParam(name = "encryptedData", value = "加密数据", dataType = "string", paramType = "query"), @ApiImplicitParam(name = "ivStr", value = "iv", dataType = "string", paramType = "query"), @ApiImplicitParam(name = "signature", value = "签名", dataType = "string", paramType = "query"), @ApiImplicitParam(name = "rawData", value = "原始数据", dataType = "string", paramType = "query"), }) @PostMapping("/decryptPhoneNumber") public Result decryptPhoneNumber(String userId,String encryptedData ,String ivStr,String signature,String rawData){ MiniUser miniUser = miniAppService.decryptPhoneNumber(userId, encryptedData, ivStr, signature, rawData); return ResultBuilder.withPayload(miniUser).build(); } /** * 小程序测试登录 * * @return */ @PostMapping("/minitestlogin") @ApiImplicitParams({ @ApiImplicitParam(name = "openid", value = "openid", dataType = "string", paramType = "query"), }) public Result minitestlogin(String openid, HttpServletResponse response) { boolean login = true; if (login) { Cookie cookie = new Cookie("username", openid); cookie.setMaxAge(60 * 60); cookie.setPath("/"); response.addCookie(cookie); String sign = JwtUtils.sign(openid, openid,"miniUser"); return ResultBuilder.withPayload(sign).build(); } else { return ResultBuilder.error("登录失败").build(); } } /** * pc端登录 * * @param username * @param password * @return */ @PostMapping("/login") @ApiImplicitParams({ @ApiImplicitParam(name = "username", value = "用户名", dataType = "string", paramType = "query"), @ApiImplicitParam(name = "password", value = "密码", dataType = "string", paramType = "query") }) public Result login(String username, String password, HttpServletResponse response) { PcUser pcUser = loginService.login(username, password); if (null!=pcUser) { Cookie cookie = new Cookie("username", username); cookie.setMaxAge(60 * 60); cookie.setPath("/"); response.addCookie(cookie); String sign = JwtUtils.sign(username, password,"pcUser"); JSONObject result = new JSONObject(); result.put("token",sign); pcUser.setPassword(null); result.put("user",pcUser); return ResultBuilder.withPayload(result).build(); } else { return ResultBuilder.error("登录失败").build(); } } /** * pc端手机验证码发送 * * @return */ @PostMapping("/sendCode") @ApiImplicitParams({ @ApiImplicitParam(name = "phone", value = "手机号码", dataType = "string", paramType = "query"), }) public Result sendCode(String phone) { boolean sendCode = loginService.sendCode(phone); if (sendCode) { return ResultBuilder.success().build(); } else { return ResultBuilder.error("登录失败").build(); } } /** * pc端手机验证码登录 * * @return */ @PostMapping("/phoneLogin") @ApiImplicitParams({ @ApiImplicitParam(name = "phone", value = "手机号码", dataType = "string", paramType = "query"), @ApiImplicitParam(name = "code", value = "验证码", dataType = "string", paramType = "query") }) public Result phoneLogin(String phone, String code, HttpServletResponse response) { PcUser pcUser = loginService.phoneLogin(phone, code); if (null!=pcUser) { Cookie cookie = new Cookie("username", pcUser.getLoginName()); cookie.setMaxAge(60 * 60); cookie.setPath("/"); response.addCookie(cookie); String sign = JwtUtils.sign(pcUser.getLoginName(), pcUser.getPassword(),"pcUser"); return ResultBuilder.withPayload(sign).build(); } else { return ResultBuilder.error("登录失败").build(); } } }