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

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.annotation.CurrentUser;
import com.xkl.authorization.manager.ITokenManager;
import com.xkl.authorization.model.TokenModel;
import com.xkl.config.ResultStatus;
import com.xkl.domain.AMPMachine;
import com.xkl.domain.Admin;
import com.xkl.model.ResultModel;
import com.xkl.repository.AMPMachineRepository;
import com.xkl.repository.AdminRepository;
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
@RequestMapping("/upsoftaccount")
public class UpSoftAccountController {

    @Autowired
    private AdminRepository adminRepository;
    @Autowired
    private AMPMachineRepository ampMachineRepository;
    @Autowired
    private ITokenManager tokenManager;

    @RequestMapping(method = RequestMethod.POST)
    @ApiOperation(value = "报告上传软件登录")
    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");
        Assert.notNull(ampserial, "ampserial can not be empty");
        Assert.notNull(ampkey, "ampkey can not be empty");

        AMPMachine ampMachine = ampMachineRepository.findBySecretKey(ampkey.trim());
        if (ampMachine == null ||// 未找到密钥所对应的机器
                !ampMachine.getAMPSerial().equals(ampserial) ||//amp序号不符合
                ampMachine.getStatus() != 1) {//用户无效
            return new ResponseEntity<>(ResultModel.error(ResultStatus.AMP_KEY_ERROR), HttpStatus.NOT_FOUND);

        }

        Admin admin = adminRepository.findByAccount(account);
        if (admin == null ||  //未注册
                !admin.getPwd().equals(password) ||//密码错误
                admin.getStatus() != 1) {//用户无效
            //提示用户名或密码错误
            return new ResponseEntity<>(ResultModel.error(ResultStatus.USERNAME_OR_PASSWORD_ERROR), HttpStatus.NOT_FOUND);
        }

        //生成一个token,保存用户登录状态
        TokenModel model = tokenManager.createToken(admin.getId());
        return new ResponseEntity<>(ResultModel.ok(model), 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 Admin admin) {
        tokenManager.deleteToken(admin.getId());
        return new ResponseEntity<>(ResultModel.ok(), HttpStatus.OK);
    }

    @RequestMapping(value = "/modpwd", method = RequestMethod.PUT)
    @Authorization
    @ApiOperation(value = "报告上传软件修改用户密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "请以如下格式输入登录返回信息:adminId_tokens", required = true, dataType = "string", paramType = "header"),
    })
    public ResponseEntity<ResultModel> modpwd(@CurrentAdmin Admin admin, @RequestParam String newpwd) {

        admin = adminRepository.findById(admin.getId());
        admin.setPwd(newpwd);
        adminRepository.save(admin);
        return new ResponseEntity<>(ResultModel.ok(), HttpStatus.OK);
    }
}