package project.web.api; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import kernel.exception.BusinessException; import kernel.util.StringUtils; import kernel.web.Page; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.alibaba.fastjson.JSONObject; import io.netty.util.internal.StringUtil; import kernel.web.ResultObject; import project.blockchain.AdminChannelBlockchainService; import project.user.UserWalletAddress; import project.user.UserWalletAddressService; import project.web.admin.AdminChannelBlockchainController; import javax.servlet.http.HttpServletRequest; /** * 用户钱包地址 * */ @RestController @CrossOrigin public class UserWalletAddressController { @Autowired UserWalletAddressService userWalletAddressService; private Logger logger = LoggerFactory.getLogger(UserWalletAddressController.class); private final String action = "/api/user!"; @Autowired private AdminChannelBlockchainService adminChannelBlockchainService; /** * 未生成钱包地址的用户 */ @RequestMapping(action + "queryNoWalletAddressUser.action") public Object queryNoWalletAddressUser() { ResultObject resultObject = new ResultObject(); // 未生成钱包地址 String state = "0"; List list = userWalletAddressService.queryByState(state); List partyIds = new ArrayList<>(); for(UserWalletAddress wallet : list) { partyIds.add(wallet.getPartyId()); } resultObject.setData(partyIds); return resultObject; } /** * 接收生成好的钱包地址 */ @RequestMapping(action + "receiveUserWalletAddress.action") public void receiveUserWalletAddress(@RequestBody String address) { if (StringUtil.isNullOrEmpty(address)) { return; } JSONObject postResult = JSONObject.parseObject(address); List userIds = JSONObject.parseArray(postResult.getJSONArray("address").toJSONString(), UserWalletAddress.class); userWalletAddressService.updateBatch(userIds); } @RequestMapping(action + "appList") public Object apiList(HttpServletRequest request) { String pageNoStr = request.getParameter("pageNo"); String message = request.getParameter("message"); String error = request.getParameter("error"); String name_para = request.getParameter("name_para"); String coin_para = request.getParameter("coin_para"); ResultObject resultObject = new ResultObject(); int pageNo=1; Page page=null; int pageSize=300; try { pageNo=checkAndSetPageNo(pageNoStr); page = this.adminChannelBlockchainService.pagedQuery(pageNo, pageSize, name_para, coin_para); } catch (BusinessException e) { logger.error(" error ", e); resultObject.setCode("1"); resultObject.setMsg(e.getMessage()); return resultObject; } catch (Throwable t) { logger.error(" error ", t); resultObject.setCode("1"); resultObject.setMsg( t.getMessage()); return resultObject; } Map map = new HashMap<>(); map.put("pageNo", pageNo); map.put("pageSize", pageSize); map.put("page", page); map.put("message", message); map.put("error", error); map.put("name_para", name_para); map.put("coin_para", coin_para); resultObject.setData(map); return resultObject; } protected int pageNo = 1; /** * 检查并设置pageNo */ public int checkAndSetPageNo(String pageNoStr) { if (StringUtils.isNullOrEmpty(pageNoStr)) { this.pageNo = 1; return 1; } Pattern pattern = Pattern.compile("-?[0-9]+(\\.[0-9]+)?"); Matcher isNum = pattern.matcher(pageNoStr); if (!isNum.matches()) { this.pageNo = 1; return 1; } int pageNo = Integer.valueOf(pageNoStr).intValue(); if (pageNo <= 0) { this.pageNo = 1; return 1; } else { this.pageNo = pageNo; return pageNo; } } }