UpSoftAccountController.java 4.88 KB
package com.xkl.controller.uploadsoft;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.wordnik.swagger.annotations.ApiOperation;
import com.xkl.authorization.annotation.Authorization;
import com.xkl.authorization.annotation.CurrentAdmin;
import com.xkl.authorization.manager.ITokenManager;
import com.xkl.authorization.model.TokenModel;
import com.xkl.config.Constants;
import com.xkl.config.ResultStatus;
import com.xkl.domain.XklAdminEntity;
import com.xkl.domain.XklCompanyEntity;
import com.xkl.model.AdminLoginModel;
import com.xkl.model.ResultModel;
import com.xkl.repository.AMPMachineRepository;
import com.xkl.repository.AdminRepository;
import com.xkl.repository.XklCompanyRepository;
import com.xkl.security.SecurityTool;
import com.xkl.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * AMP报告上传软件客户端登录及退出接口。
 * 获取和删除token的请求地址,在Restful设计中其实就对应着登录和退出登录的资源映射
 */
@RestController
@Api("AMP报告上传软件客户端登录及退出接口")
@RequestMapping("/upsoft/account")
public class UpSoftAccountController {
    @Autowired
    private ILoginService loginService;
    @Autowired
    private AdminRepository adminRepository;
    @Autowired
    private AMPMachineRepository ampMachineRepository;
    @Autowired
    private ITokenManager tokenManager;
    @Autowired
    private XklCompanyRepository xklCompanyRepository;
    public static final String UPSOFT_TOKEN_PREFIX = "UPSOFTTOKEN";

    @RequestMapping(method = RequestMethod.POST)
    @ApiOperation(value = "报告上传软件登录", notes = "login")

    public ResponseEntity<ResultModel> login(@RequestParam String account, @RequestParam String password) {
        //  , @RequestParam String ampserial, @RequestParam String ampkey
        Assert.notNull(account, "account can not be empty");
        Assert.notNull(password, "password can not be empty");

        XklAdminEntity admin = loginService.checkAdmin(account, password);
        if (admin == null) {//用户,密码错误
            return new ResponseEntity<>(ResultModel.error(ResultStatus.USERNAME_OR_PASSWORD_ERROR), HttpStatus.OK);
        }

        //生成一个token,保存用户登录状态
        TokenModel model = tokenManager.createToken(UPSOFT_TOKEN_PREFIX + admin.getId());

        XklCompanyEntity companyEntity = xklCompanyRepository.findById(admin.getCoid());
        if (companyEntity == null) {
            return new ResponseEntity<>(ResultModel.error(ResultStatus.COMPANY_ERROR), HttpStatus.OK);
        }

        AdminLoginModel adminLoginModel = new AdminLoginModel(admin.getId(), admin.getAccount(), companyEntity.getId(), companyEntity.getName(), model.getUserId(), model.getToken());
        return new ResponseEntity<>(ResultModel.ok(adminLoginModel), HttpStatus.OK);
    }

    @RequestMapping(method = RequestMethod.DELETE)
    @Authorization
    @ApiOperation(value = "报告上传软件注销")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "请输入登录返回信息:userId_tokens", required = true, dataType = "string", paramType = "header"),
    })
    public ResponseEntity<ResultModel> logout(@CurrentAdmin XklAdminEntity admin) {
        tokenManager.deleteToken(UPSOFT_TOKEN_PREFIX + admin.getId());
        return new ResponseEntity<>(ResultModel.ok(), HttpStatus.OK);
    }

    @RequestMapping(method = RequestMethod.PUT)
    @Authorization
    @ApiOperation(value = "报告上传软件修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "请以如下格式输入登录返回信息:adminId_tokens", required = true, dataType = "string", paramType = "header"),
    })
    public ResponseEntity<ResultModel> modpwd(@CurrentAdmin XklAdminEntity admin, @RequestParam String newpwd) {
        Assert.notNull(newpwd, "password can not be empty");
        String salt = SecurityTool.genSalt();
        String adminType = Integer.toString(admin.getType());
        String str = admin.getAccount() + newpwd + adminType + salt; // 构建待加密字符串
        String pass2Db = SecurityTool.encode(SecurityTool.ALGORITHM_MD5, str);
        admin.setPwd(pass2Db);
        admin.setSalt(salt);
        adminRepository.save(admin);
        tokenManager.deleteToken(UPSOFT_TOKEN_PREFIX + admin.getId());
        return new ResponseEntity<>(ResultModel.ok(), HttpStatus.OK);
    }
}