ChinaMobileRestApiController.java 11.6 KB
Newer Older
liqin's avatar
liqin committed
1 2 3 4
package cn.wisenergy.chnmuseum.party.web.controller;

import cn.wisenergy.chnmuseum.party.auth.SHA256PasswordEncryptionService;
import cn.wisenergy.chnmuseum.party.auth.util.JwtTokenUtil;
liqin's avatar
liqin committed
5
import cn.wisenergy.chnmuseum.party.common.enums.LanguageEnum;
liqin's avatar
liqin committed
6
import cn.wisenergy.chnmuseum.party.common.util.TimeUtils;
liqin's avatar
liqin committed
7
import cn.wisenergy.chnmuseum.party.common.vo.GenericPageParam;
liqin's avatar
liqin committed
8
import cn.wisenergy.chnmuseum.party.model.Employee;
liqin's avatar
liqin committed
9 10
import cn.wisenergy.chnmuseum.party.model.ExhibitionBoard;
import cn.wisenergy.chnmuseum.party.service.ExhibitionBoardService;
liqin's avatar
liqin committed
11 12 13
import cn.wisenergy.chnmuseum.party.service.impl.EmployeeServiceImpl;
import cn.wisenergy.chnmuseum.party.web.controller.base.BaseController;
import com.alibaba.fastjson.JSONObject;
liqin's avatar
liqin committed
14 15
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
liqin's avatar
liqin committed
16
import io.swagger.annotations.Api;
liqin's avatar
liqin committed
17 18
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
liqin's avatar
liqin committed
19
import io.swagger.annotations.ApiOperation;
liqin's avatar
liqin committed
20
import lombok.extern.slf4j.Slf4j;
liqin's avatar
liqin committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;

liqin's avatar
liqin committed
36 37
@Slf4j
@Api(tags = {"中国移动API"})
liqin's avatar
liqin committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51
@RestController("/cmRestApi")
public class ChinaMobileRestApiController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ChinaMobileRestApiController.class);

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private EmployeeServiceImpl employeeService;

    @Resource
    private SysLogController sysLogController;

liqin's avatar
liqin committed
52 53 54
    @Resource
    private ExhibitionBoardService exhibitionBoardService;

liqin's avatar
liqin committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    private static final String SHIRO_JWT_TOKEN = "shiro:jwt:token:";
    //用户登录次数计数  redisKey 前缀
    private static final String SHIRO_LOGIN_COUNT = "shiro_login_count_";
    //用户登录是否被锁定    一小时 redisKey 前缀
    private static final String SHIRO_IS_LOCK = "shiro_is_lock_";

    /**
     * 管理员ajax登录请求 后端用户登录
     *
     * @param username
     * @param password
     * @return
     */
    @RequestMapping(value = "/user/webLogin", method = RequestMethod.POST)
    public ResponseEntity<JSONObject> login(@RequestParam(value = "username") String username,
                                            @RequestParam(value = "password") String password,
                                            @RequestParam(value = "boxNo") String boxNo) {
        JSONObject resultMap = new JSONObject(true);
        Employee employee;
        if (StringUtils.isNoneBlank(username)) {
            //访问一次,计数一次
            ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();
            if ("LOCK".equals(opsForValue.get(SHIRO_IS_LOCK + username))) {
                resultMap.put("status", 400);
                resultMap.put("message", "由于密码输入错误次数大于5次,12小时内帐号已禁止登录!请您联系相关管理人员,联系电话:13924551212,邮箱:325346534@zh.com。");
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(resultMap);
            }
            employee = employeeService.selectByUsername(username);
            if (employee == null) {
                resultMap.put("status", 500);
                resultMap.put("message", "用户名或密码不正确!");
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(resultMap);
            }
            try {
                byte[] salt = employee.getPasswordSalt();
                if (!new String(SHA256PasswordEncryptionService.createPasswordHash(password, salt)).equals(new String(employee.getPasswordHash()))) {
                    opsForValue.increment(SHIRO_LOGIN_COUNT + username, 1);
                    //计数大于5时,设置用户被锁定一小时
                    String s = opsForValue.get(SHIRO_LOGIN_COUNT + username);
                    if (StringUtils.isNotBlank(s)) {
                        if (Integer.parseInt(s) >= 5) {
                            opsForValue.set(SHIRO_IS_LOCK + username, "LOCK");
                            stringRedisTemplate.expire(SHIRO_IS_LOCK + username, 12, TimeUnit.HOURS);
                        }
                    }
                    throw new IncorrectCredentialsException("用户名或密码不正确!");
                }
                String token = JwtTokenUtil.sign(username, employee.getId());
                // 将token信息存入Redis
                stringRedisTemplate.opsForValue().set(SHIRO_JWT_TOKEN + token, employee.getId(), 240, TimeUnit.MINUTES);

                JSONObject jsonObject = new JSONObject(true);
                jsonObject.put("token", token);
                jsonObject.put("userId", employee.getId());
                jsonObject.put("userName", employee.getUsername());
                jsonObject.put("expire", TimeUtils.format(LocalDateTime.now().plusMinutes(240), TimeUtils.FORMAT_ONE));
liqin's avatar
liqin committed
111 112
                jsonObject.put("orgCode", "");
                jsonObject.put("orgName", "");
liqin's avatar
liqin committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

                resultMap.put("resultCode", 200);
                resultMap.put("message", "成功");
                resultMap.put("data", jsonObject);
                return ResponseEntity.status(HttpStatus.OK).body(resultMap);
            } catch (Exception e) {
                resultMap.put("status", 500);
                resultMap.put("message", e.getMessage());
            }
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(resultMap);
    }

    @ApiOperation(value = "获取单个成员信息")
    @GetMapping(value = "/user/getUserInfo")
    public ResponseEntity<JSONObject> getById(String userId, @RequestHeader("token") String token) {
        try {
            Employee employee = employeeService.selectByEmpId(userId);
liqin's avatar
liqin committed
131

liqin's avatar
liqin committed
132 133 134 135 136 137 138 139 140 141
            if (null == employee) {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
            }

            JSONObject jsonObject = new JSONObject(true);
            jsonObject.put("token", token);
            jsonObject.put("userId", employee.getId());
            jsonObject.put("userName", employee.getUsername());
            long expire = stringRedisTemplate.getExpire(SHIRO_JWT_TOKEN + token) == null ? 0L : stringRedisTemplate.getExpire(SHIRO_JWT_TOKEN + token);
            jsonObject.put("expire", TimeUtils.format(LocalDateTime.now().plusMinutes(expire), TimeUtils.FORMAT_ONE));
liqin's avatar
liqin committed
142 143 144 145 146 147 148 149
//          BankBranchInfo bankBranch = this.employeeService.getById(Id);
//          if (bankBranch != null) {
//               employee.setBankBranchName(bankBranch.getName());
//          }
            jsonObject.put("orgCode", "");
            jsonObject.put("orgName", "");

            JSONObject resultMap = new JSONObject(true);
liqin's avatar
liqin committed
150 151 152 153 154 155 156 157 158 159
            resultMap.put("resultCode", 200);
            resultMap.put("message", "成功");
            resultMap.put("data", jsonObject);
            return ResponseEntity.ok(resultMap);
        } catch (Exception e) {
            logger.error("查询成员信息错误!", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

liqin's avatar
liqin committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
    @RequestMapping(value = "/user/logout", method = RequestMethod.GET)
    public ResponseEntity<JSONObject> logout(@RequestHeader(value = "token") String token) {
        try {
            if (StringUtils.isNotBlank(token)) {
                SecurityUtils.getSubject().logout();
                this.stringRedisTemplate.delete(SHIRO_JWT_TOKEN + token);
            }
            JSONObject resultMap = new JSONObject(true);
            resultMap.put("resultCode", 200);
            resultMap.put("message", "成功");
            resultMap.put("data", "");
            return ResponseEntity.status(HttpStatus.OK).body(resultMap);
        } catch (Exception e) {
            LOGGER.error("注销错误!", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

liqin's avatar
liqin committed
178 179 180 181 182 183 184
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "_index", value = "分页起始偏移量", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "_size", value = "返回条数", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "nameOrCode", value = "名称或编码", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "startDate", value = "创建时间-开始", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endDate", value = "创建时间-结束", paramType = "query", dataType = "String")
    })
liqin's avatar
liqin committed
185
    @PostMapping("/exhibitionBoard/getPage")
liqin's avatar
liqin committed
186 187 188 189 190 191 192 193
    @ApiOperation(value = "获取展板分页列表", notes = "获取展板分页列表")
    public ResponseEntity<JSONObject> getExhibitionBoardPageList(GenericPageParam genericPageParam) {
        LambdaQueryWrapper<ExhibitionBoard> queryWrapper = new LambdaQueryWrapper<>();
        // 对名称或编码模糊查询
        if (StringUtils.isNotBlank(genericPageParam.getNameOrCode())) {
            queryWrapper.like(ExhibitionBoard::getName, genericPageParam.getNameOrCode());
        }
        // 对版权方模糊查询
liqin's avatar
liqin committed
194
        if (StringUtils.isNotBlank(genericPageParam.getBoardCopyrightOwnerId())) {
liqin's avatar
liqin committed
195
            queryWrapper.like(ExhibitionBoard::getBoardCopyrightOwnerId, genericPageParam.getBoardCopyrightOwnerId());
liqin's avatar
liqin committed
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
        }
        // 根据创建时间区间检索
        if (genericPageParam.getStartDate() != null && genericPageParam.getEndDate() != null) {
            queryWrapper.ge(ExhibitionBoard::getCreateTime, genericPageParam.getStartDate().atTime(0, 0, 0))
                    .le(ExhibitionBoard::getCreateTime, genericPageParam.getEndDate().atTime(23, 59, 59));
        }
        // 设置排序规则
        queryWrapper.orderByDesc(ExhibitionBoard::getCreateTime);
        // 设置查询内容
        queryWrapper.select(
                ExhibitionBoard::getId,
                ExhibitionBoard::getName,
                ExhibitionBoard::getCreateTime,
                ExhibitionBoard::getUpdateTime);
        Page<ExhibitionBoard> page = this.exhibitionBoardService.page(getPage(), queryWrapper);
        for (ExhibitionBoard exhibitionBoard : page.getRecords()) {

        }
        JSONObject resultMap = new JSONObject();
        resultMap.put("resultCode", 200);
        resultMap.put("message", "成功");
        resultMap.put("data", page);
        return ResponseEntity.ok(resultMap);
    }

    @ApiOperation(value = "获取展板详情", notes = "获取展板详情")
    @ApiImplicitParams({
liqin's avatar
liqin committed
223 224
            @ApiImplicitParam(name = "boardId", value = "展板ID", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "language", value = "语言", dataType = "String", paramType = "query"),
liqin's avatar
liqin committed
225
    })
liqin's avatar
liqin committed
226 227
    @GetMapping("/exhibitionBoard/getBoardInfo")
    public JSONObject getById(@RequestParam(value = "boardId") String id, @RequestParam("language") LanguageEnum language) {
liqin's avatar
liqin committed
228
        ExhibitionBoard exhibitionBoard = exhibitionBoardService.getById(id);
liqin's avatar
liqin committed
229 230 231 232 233
        JSONObject resultMap = new JSONObject();
        resultMap.put("resultCode", 200);
        resultMap.put("message", "成功");
        resultMap.put("data", exhibitionBoard);
        return resultMap;
liqin's avatar
liqin committed
234 235
    }

liqin's avatar
liqin committed
236
}