liujie
2023-05-15 6225c37d2f53adf26daf6b4859af5fb5c6fad088
src/main/java/com/stylefeng/guns/modular/api/ApiController.java
@@ -8,11 +8,14 @@
import com.stylefeng.guns.core.util.Convert;
import com.stylefeng.guns.core.util.JwtTokenUtil;
import com.stylefeng.guns.core.util.MD5Util;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.dao.TCompanyMapper;
import com.stylefeng.guns.modular.system.dao.UserMapper;
import com.stylefeng.guns.modular.system.model.TCompany;
import com.stylefeng.guns.modular.system.model.User;
import com.stylefeng.guns.modular.system.model.UserInfo;
import com.stylefeng.guns.modular.system.utils.EmailUtil;
import com.stylefeng.guns.modular.system.utils.RedisUtil;
import com.stylefeng.guns.modular.system.utils.tips.SuccessTip;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
@@ -26,6 +29,7 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
/**
 * 接口控制器提供
@@ -43,6 +47,10 @@
    @Resource
    private TCompanyMapper companyMapper;
    @Autowired
    private RedisUtil redisUtil;
    /**
     * api登录接口,通过账号密码获取token
     */
@@ -60,7 +68,7 @@
        //获取数据库中的账号密码,准备比对
        List<TCompany> user = companyMapper.selectList(new EntityWrapper<TCompany>().eq("account",username));
        if (user.size()==0) {
            return new ErrorTip(500, "账号密码错误!");
            return new ErrorTip(500, "Account password error!");
        }
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(user, userInfo);
@@ -77,14 +85,16 @@
//        boolean passwordTrueFlag = md5CredentialsMatcher.doCredentialsMatch(
//                usernamePasswordToken, simpleAuthenticationInfo);
        String encrypt = MD5Util.encrypt(password);
        if(user.get(0).getStatus()==3){
            return new ErrorTip(500, "Your account has been frozen, please contact the platform!");
        }
        if (!encrypt.equals(user.get(0).getPassword())) {
            return new ErrorTip(500, "账号密码错误!");
            return new ErrorTip(500, "Account password error!");
        } else {
            TCompany company = user.get(0);
            HashMap<String, Object> result = new HashMap<>();
            result.put("token", JwtTokenUtil.generateToken(String.valueOf(company.getId())));
            result.put("company", company);
            super.getSession().setAttribute("companyId",String.valueOf(company.getId()));
            return result;
        }
    }
@@ -128,12 +138,18 @@
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户账号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "password", value = "用户密码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "code", value = "验证码", required = true, dataType = "String"),
    })
    public Object forget(@RequestParam("username") String username,
                         @RequestParam("password") String password) {
                         @RequestParam("password") String password,
                         @RequestParam("code") String code) {
        List<TCompany> user = companyMapper.selectList(new EntityWrapper<TCompany>().eq("account",username));
        String value = redisUtil.getValue(username);
        if(!code.equals(value)){
            return new ErrorTip(5001, "Verification code error!");
        }
        if (user.size()==0){
            return new ErrorTip(500, "账号不存在!");
            return new ErrorTip(500, "Account does not exist!");
        }
        user.get(0).setPassword(MD5Util.encrypt(password));
        companyMapper.updateById(user.get(0));
@@ -141,4 +157,35 @@
    }
    @PostMapping("/sendCode")
    @ApiOperation(value = "发送验证码", notes = "发送验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "email", value = "用户邮箱", required = true, dataType = "String"),
    })
    public Object sendCode(@RequestParam("email") String email) {
        String randomNumber = getRandomString(6);
        redisUtil.setStrValue(email,randomNumber,300);
        try {
            EmailUtil.sendMailGMail(email, randomNumber);
            return new com.stylefeng.guns.core.base.tips.SuccessTip();
        }catch (Exception e){
            e.printStackTrace();
            return new ErrorTip(500,"ERROR");
        }
    }
    private  String getRandomString(int length) {
        String base = "0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < length; ++i) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }
}