QRCodeController.java 7.74 KB
package com.xkl.controller.qrcode;

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.CurrentUser;
import com.xkl.authorization.annotation.LogAnnotation;
import com.xkl.authorization.annotation.Sign;
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.User;
import com.xkl.domain.XklMemberEntity;
import com.xkl.domain.XklMemberOpenidEntity;
import com.xkl.model.QrCodeModel;
import com.xkl.model.ResultModel;
import com.xkl.model.UsrInfoModel;
import com.xkl.repository.UserRepository;
import com.xkl.repository.XklMemberOpenidRespository;
import com.xkl.repository.XklMemberRespository;
import com.xkl.security.AntiXSS;
import com.xkl.service.IAdminOpsService;
import com.xkl.service.ILoginService;
import com.xkl.service.IQRCodeService;
import com.xkl.service.QRCodeServiceImpl;
import com.xkl.tools.DESTools;
import com.xkl.tools.DatetimeTools;
import com.xkl.tools.HttpTools;
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;

import javax.servlet.http.HttpServletRequest;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by zhaoyue on 2017/03/11.
 */
@RestController
@RequestMapping("/qrcode")
public class QRCodeController {
    @Autowired
    private IQRCodeService qrCodeService;
    @Autowired
    private ILoginService loginService;
    @Autowired
    private XklMemberOpenidRespository xklMemberOpenidRespository;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private XklMemberRespository xklMemberRespository;


    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟

    @LogAnnotation
    @AntiXSS
    @RequestMapping(value = "/getQrWithAccPwd", method = RequestMethod.GET)
    @ApiOperation(value = "使用用户名密码得到qrcode")
    public ResponseEntity<ResultModel> getQrWithAccPwd(HttpServletRequest request, @RequestParam String username, @RequestParam String password, @RequestParam long t) {
        Assert.notNull(username, "username can not be empty");
        Assert.notNull(password, "password can not be empty");

        User user = loginService.check(username, password);
        if (user == null) {//用户,密码错误
            return new ResponseEntity<>(ResultModel.error(ResultStatus.USERNAME_OR_PASSWORD_ERROR), HttpStatus.OK);
        } else {
            String qrCode = qrCodeService.getQRCodeWithAccount(user.getLoginAccount());
            QrCodeModel qrModel = new QrCodeModel(qrCode);
            return new ResponseEntity<>(ResultModel.ok(qrModel), HttpStatus.OK);
        }
    }

    @LogAnnotation
    @AntiXSS
    @Sign
    @RequestMapping(value = "/getQrWithOpenId", method = RequestMethod.GET)
    @ApiOperation(value = "使用openid获取qrCode")
    public ResponseEntity<ResultModel> getQrWithOpenId(HttpServletRequest request, @RequestParam String openId, @RequestParam int openIdType,
                                                       @RequestParam String sign, @RequestParam long t, @RequestParam int type) {
        if (!(boolean) request.getAttribute("signAspect")) {
            return new ResponseEntity<>(ResultModel.error(ResultStatus.SIGN_ERROR), HttpStatus.OK);
        }
        XklMemberOpenidEntity openidEntity = xklMemberOpenidRespository.findOneByOpenidAndType(openId, openIdType);
        // openId 未找到
        if (openidEntity == null) {
            return new ResponseEntity<>(ResultModel.ok(ResultStatus.OPENID_ERROR), HttpStatus.OK);
        }
        User user = userRepository.findOne(openidEntity.getAccountId());
        // 用户不存在
        if (user == null) {
            return new ResponseEntity<>(ResultModel.ok(ResultStatus.USER_NOT_FOUND), HttpStatus.OK);
        }
        String qrCode = qrCodeService.getQRCodeWithAccount(user.getLoginAccount());
        QrCodeModel qrModel = new QrCodeModel(qrCode);
        return new ResponseEntity<>(ResultModel.ok(qrModel), HttpStatus.OK);
    }


    @LogAnnotation
    @AntiXSS
    @Authorization
    @Sign
    @RequestMapping(value = "/getQrWithToken", method = RequestMethod.GET)
    @ApiOperation(value = "使用token得到qrcode")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "请输入登录返回信息:userId_tokens", required = true, dataType = "string", paramType = "header"),
    })
    public ResponseEntity<ResultModel> getQrWithToken(HttpServletRequest request, @CurrentUser User user,
                                                      @RequestParam String sign, @RequestParam long t, @RequestParam int type) {
        if (!(boolean) request.getAttribute("signAspect")) {
            return new ResponseEntity<>(ResultModel.error(ResultStatus.SIGN_ERROR), HttpStatus.OK);
        }
        String qrCode = qrCodeService.getQRCodeWithAccount(user.getLoginAccount());
        QrCodeModel qrModel = new QrCodeModel(qrCode);
        return new ResponseEntity<>(ResultModel.ok(qrModel), HttpStatus.OK);
    }

    @LogAnnotation
    @AntiXSS
    @RequestMapping(value = "/getUsrInfoWithQr", method = RequestMethod.GET)
    @ApiOperation(value = "使用qrcode查询个人信息")
    // 接口返回的qrcode都是正常的qrcode,但是客户端使用的时候必须加密才能使用。
    public ResponseEntity<ResultModel> getUsrInfoWithQr(HttpServletRequest request,
                                                        @RequestParam String encryptQr, @RequestParam long t) {
        String qrcode = qrCodeService.getDecryptHttpParaQr(encryptQr, t);
        if (qrcode == null) {
            return new ResponseEntity<>(ResultModel.error(ResultStatus.INVALID_QR_CODE), HttpStatus.OK);
        }
        String account = qrCodeService.getAccountWithQRCode(qrcode);
        User user = userRepository.findByLoginAccountAndStatus(account, true);
        if (user == null) {
            return new ResponseEntity<>(ResultModel.error(ResultStatus.USER_NOT_FOUND), HttpStatus.OK);
        }
        XklMemberEntity xklMemberEntity = xklMemberRespository.findOne((long) user.getMemberId());
        Date birthDate = new Date();
        try {
            birthDate = sdf.parse(xklMemberEntity.getBirthDate());
        } catch (Exception e) {
            e.printStackTrace();
        }
        int age = DatetimeTools.getAge(birthDate, new Date());
        UsrInfoModel usrInfo = new UsrInfoModel(user.getLoginAccount(), xklMemberEntity.getName(),
                xklMemberEntity.getIdcard(), xklMemberEntity.getPhone(), xklMemberEntity.isSex() ? 1 : 0, age,
                xklMemberEntity.getCurPulse(), xklMemberEntity.getCurBreathrate(), qrcode, genWebloginUrl(qrcode));
        return new ResponseEntity<>(ResultModel.ok(usrInfo), HttpStatus.OK);
    }

    private static String genWebloginUrl(String qrcode) {
        String now = HttpTools.getNow();
        try {
            String encryptQr = DESTools.encrypt(qrcode.trim() + now, QRCodeServiceImpl.getHttpParaQrKey());
            String interUrl = Constants.LOCAL_QRVERIFY_URL + encryptQr + "&t=" + now;
            return Constants.WEB_LOGIN_URL + URLEncoder.encode(interUrl, "utf-8");
        } catch (Exception e) {
            return null;
        }
    }
}