package com.zy.asrs.openapi.controller; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.zy.asrs.common.openapi.entity.HostKey; import com.zy.asrs.common.openapi.service.HostKeyService; import com.zy.asrs.common.utils.Utils; import com.zy.asrs.framework.annotations.ManagerAuth; import com.zy.asrs.framework.common.Cools; import com.zy.asrs.framework.common.R; import com.zy.asrs.framework.domain.KeyValueVo; import com.zy.asrs.framework.common.DateUtils; import com.zy.asrs.common.web.BaseController; import org.apache.commons.codec.digest.DigestUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.*; @RestController public class HostKeyController extends BaseController { @Autowired private HostKeyService hostKeyService; @RequestMapping(value = "/hostKey/{id}/auth") @ManagerAuth public R get(@PathVariable("id") String id) { return R.ok(hostKeyService.getById(String.valueOf(id))); } @RequestMapping(value = "/hostKey/list/auth") @ManagerAuth public R list() { return R.ok(hostKeyService.getList()); } @RequestMapping(value = "/hostKey/switchSign/auth") @ManagerAuth public R switchSign(HostKey hostKey) { Date now = new Date(); HostKey one = hostKeyService.getOne(new LambdaQueryWrapper().eq(HostKey::getHostId, hostKey.getHostId())); if (one == null) { return R.error("未生成签名密钥"); } if (one.getSignKey() == null) { return R.error("未生成签名密钥"); } one.setSignStatus(hostKey.getSignStatus()); one.setCreateTime(now); hostKeyService.saveOrUpdate(one); return R.ok().add(hostKey); } @RequestMapping(value = "/hostKey/generateSignKey/auth") @ManagerAuth public R generateSignKey(HostKey hostKey) { StringBuffer buffer = new StringBuffer(); buffer.append(Utils.generateString(32)); buffer.append(System.currentTimeMillis()); String md5Hex = DigestUtils.md5Hex(buffer.toString()); HostKey one = hostKeyService.getOne(new LambdaQueryWrapper().eq(HostKey::getHostId, hostKey.getHostId())); if (one != null) { hostKey = one; }else { hostKey.setSignStatus(0); } hostKey.setSignKey(md5Hex); hostKey.setCreateTime(new Date()); hostKeyService.saveOrUpdate(hostKey); return R.ok().add(hostKey); } @RequestMapping(value = "/hostKey/generateAppKey/auth") @ManagerAuth public R generateAppKey(HostKey hostKey) { StringBuffer buffer = new StringBuffer(); buffer.append(Utils.generateString(32)); buffer.append(System.currentTimeMillis()); String md5Hex = DigestUtils.md5Hex(buffer.toString()); HostKey one = hostKeyService.getOne(new LambdaQueryWrapper().eq(HostKey::getHostId, hostKey.getHostId())); if (one != null) { hostKey = one; }else { hostKey.setSignStatus(0); } hostKey.setAppKey(md5Hex); hostKey.setCreateTime(new Date()); hostKeyService.saveOrUpdate(hostKey); return R.ok().add(hostKey); } @RequestMapping(value = "/hostKey/update/auth") @ManagerAuth public R update(HostKey hostKey){ if (Cools.isEmpty(hostKey) || null==hostKey.getId()){ return R.error(); } hostKeyService.updateById(hostKey); return R.ok(); } @RequestMapping(value = "/hostKey/delete/auth") @ManagerAuth public R delete(@RequestParam(value="ids[]") Long[] ids){ for (Long id : ids){ hostKeyService.removeById(id); } return R.ok(); } @RequestMapping(value = "/hostKeyQuery/auth") @ManagerAuth public R query(String condition) { LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.like(HostKey::getId, condition); Page page = hostKeyService.page(new Page<>(0, 10), wrapper); List> result = new ArrayList<>(); for (HostKey hostKey : page.getRecords()){ Map map = new HashMap<>(); map.put("id", hostKey.getId()); map.put("value", hostKey.getId()); result.add(map); } return R.ok(result); } @RequestMapping("/hostKey/all/get/kv") @ManagerAuth public R getDataKV(@RequestParam(required = false) String condition) { List vos = new ArrayList<>(); LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); if (!Cools.isEmpty(condition)) { wrapper.like(HostKey::getId, condition); } hostKeyService.page(new Page<>(1, 30), wrapper).getRecords().forEach(item -> vos.add(new KeyValueVo(String.valueOf(item.getId()), item.getId()))); return R.ok().add(vos); } }