| | |
| | | package com.panzhihua.shop_backstage.api; |
| | | |
| | | import javax.annotation.Resource; |
| | | |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.util.ObjectUtils; |
| | | import org.springframework.validation.annotation.Validated; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.panzhihua.common.controller.BaseController; |
| | | import com.panzhihua.common.exceptions.ServiceException; |
| | | import com.panzhihua.common.interfaces.ShopOperLog; |
| | | import com.panzhihua.common.model.vos.LoginReturnVO; |
| | | import com.panzhihua.common.model.vos.LoginUserInfoVO; |
| | |
| | | import com.panzhihua.common.service.auth.TokenService; |
| | | import com.panzhihua.common.service.community.CommunityService; |
| | | import com.panzhihua.common.service.user.UserService; |
| | | import com.panzhihua.shop_backstage.config.MyAESUtil; |
| | | import com.panzhihua.shop_backstage.model.vos.LoginBody; |
| | | |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.util.ObjectUtils; |
| | | import org.springframework.validation.annotation.Validated; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.time.Duration; |
| | | |
| | | @Slf4j |
| | | @RestController |
| | | @RequestMapping("/") |
| | | @Api(tags = {"登录"}) |
| | | public class LoginApi extends BaseController { |
| | | private static String LOGIN_FAIL = "LOGIN_FAIL_"; |
| | | @Resource |
| | | private TokenService tokenService; |
| | | @Resource |
| | | private UserService userService; |
| | | @Resource |
| | | private CommunityService communityService; |
| | | @Resource |
| | | private RedisTemplate redisTemplate; |
| | | |
| | | @ApiOperation(value = "商家后台登录", response = LoginReturnVO.class) |
| | | @ShopOperLog(operType = 1) |
| | |
| | | return R.fail("账户密码不能为空"); |
| | | } |
| | | log.info("登录用户信息【{}】", loginBody); |
| | | |
| | | try { |
| | | password = MyAESUtil.Decrypt(password, "Ryo7M3n8loC5Abcd"); |
| | | } catch (Exception e) { |
| | | return R.fail("账号或密码错误!"); |
| | | } |
| | | boolean flag = redisTemplate.hasKey(LOGIN_FAIL + account); |
| | | try { |
| | | R<ShopStoreVO> storeR = communityService.getUserStoreInfoByAccount(account); |
| | | if (R.isOk(storeR)) { |
| | |
| | | JSONObject.parseObject(JSONObject.toJSONString(storeR.getData()), ShopStoreVO.class); |
| | | Boolean accountValid = shopStoreVO.getStatus() == 1; |
| | | if (accountValid) { |
| | | if (flag) { |
| | | Integer time = (Integer) redisTemplate.opsForValue().get(LOGIN_FAIL + account); |
| | | if (time >= 5) { |
| | | redisTemplate.opsForValue().set(LOGIN_FAIL + account, 5, Duration.ofMinutes(5)); |
| | | throw new ServiceException("登录错误超过限制,请五分钟后重试"); |
| | | } |
| | | } |
| | | R r = tokenService.loginShopBackStage(account, password); |
| | | return r; |
| | | } else { |
| | |
| | | } |
| | | return R.fail(storeR.getMsg()); |
| | | } catch (Exception e) { |
| | | return R.fail("登陆失败了"); |
| | | lockLogin(flag, account); |
| | | return R.fail("账号或密码错误!"); |
| | | } |
| | | } |
| | | |
| | | |
| | | private void lockLogin(Boolean flag, String userName) { |
| | | if (flag) { |
| | | Integer time = (Integer) redisTemplate.opsForValue().get(LOGIN_FAIL + userName); |
| | | redisTemplate.opsForValue().set(LOGIN_FAIL + userName, time + 1, Duration.ofMinutes(5)); |
| | | } else { |
| | | redisTemplate.opsForValue().set(LOGIN_FAIL + userName, 1, Duration.ofMinutes(5)); |
| | | } |
| | | } |
| | | |
| | | |
| | | @ApiOperation(value = "修改密码") |
| | | @PutMapping("changepassword") |
| | | @ShopOperLog(operType = 2) |