package com.vci.rmip.code.client.codeapply.Apply410; import com.alibaba.fastjson.JSONObject; import com.vci.base.ui.swing.VCIOptionPane; import com.vci.base.ui.swing.VCISwingUtil; import com.vci.base.ui.swing.components.VCIJButton; import com.vci.base.ui.swing.components.VCIJDialog; import com.vci.base.ui.swing.components.VCIJPanel; import com.vci.rmip.code.client.codeapply.Apply410.object.UserObject; import com.vci.rmip.code.client.codeapply.Apply410.object.TokenUserObject; import com.vci.rmip.code.client.codeapply.Apply410.utils.ConfigUtils; import com.vci.rmip.code.client.codeapply.Apply410.utils.HttpUtil; import javax.swing.*; import org.apache.commons.lang.StringUtils; import java.awt.*; import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; public class CodeApplyFor410Dialog extends VCIJDialog { private static final long serialVersionUID = -3286164100581857853L; private static TokenUserObject tokenUserObject = new TokenUserObject(); private String userName = null; private String codeValue = null; private CodeApplyFor410DialogActionListener actionListener = new CodeApplyFor410DialogActionListener(this); private VCIJButton okBtn = VCISwingUtil.createVCIJButton("ok", "确认", "确认", "ok.gif", actionListener); private VCIJButton cancelBtn = VCISwingUtil.createVCIJButton("cancel", "取消", "取消", "cancel.gif", actionListener); private CodeApplyPanelFor410 mainPal = null; private Component parentComponet = null; private Map attrNameAndValMap = new LinkedHashMap();//存储属性名称(包含内部名称和外部名称)和对应的值 private String tokenUrl= ConfigUtils.getConfigValue("410.code.token.url","http://127.0.0.1:37000/ubcs-auth/oauth/token"); private String[] fields; private String[] values; /** * 存储分类的Map,key:分类名称,value:分类值 */ private String clsfName = null; /** * 存储属性和值的Map,key:属性名称,value:属性值 */ private String deptName = null; private String[] libName = ConfigUtils.getConfigValue("410.PDM.LibCodeName").split("#");//ConfigUtils.getConfigValue("410.PDM.LibCodeName").split("#");//代码项名称 public CodeApplyFor410Dialog(Frame frame, String clsfName, String deptName) { this(frame, "pdmUser", clsfName, deptName,new String[]{}); } public CodeApplyFor410Dialog(JDialog dialog, String clsfName, String deptName) { this(dialog, "pdmUser", clsfName, deptName,new String[]{}); } public CodeApplyFor410Dialog(JDialog dialog, String clsfName, String deptName,String[] fields,String[] values) { this(dialog, "pdmUser", clsfName, deptName,new String[]{},fields,values); } public CodeApplyFor410Dialog(Frame frame, String clsfName, String deptName,String[] fields,String[] values){ super(frame, true); VCISwingUtil.setClientMainFrame(frame); //this.setModal(true); parentComponet = frame; this.userName = userName; this.clsfName = clsfName; this.deptName = deptName; this.fields=fields; this.values=values; //this.libName = libName; init(); } public CodeApplyFor410Dialog(Frame frame, String userName, String clsfName, String deptName, String[] libName){ super(frame, true); VCISwingUtil.setClientMainFrame(frame); try { this.setModal(true); parentComponet = frame; this.userName = userName; this.clsfName = clsfName; this.deptName = deptName; //this.libName = libName; init(); } catch (Exception e) { e.printStackTrace(); } } public CodeApplyFor410Dialog(JDialog dialog, String userName, String clsfName, String deptName, String[] libName) { super(dialog, true); VCISwingUtil.setClientMainFrame(new Frame()); try { this.setModal(true); parentComponet = dialog; this.userName = userName; this.clsfName = clsfName; this.deptName = deptName; // this.libName = libName; init(); } catch (Exception e) { e.printStackTrace(); } } public CodeApplyFor410Dialog(JDialog dialog, String userName, String clsfName, String deptName, String[] libName,String[] fields,String[] values) { super(dialog, true); VCISwingUtil.setClientMainFrame(new Frame()); try { this.setModal(true); parentComponet = dialog; this.userName = userName; this.clsfName = clsfName; this.deptName = deptName; this.fields = fields; this.values = values; init(); } catch (Exception e) { e.printStackTrace(); } } private void initUserInfo() throws Exception { /*userOb.set("127.0.0.1"); userOb.setModules("TC集成代码申请"); userOb.setUserName(userName);*/ Map headerMap=new HashMap<>(); headerMap.put("Tenant-Id",ConfigUtils.getConfigValue("410.code.token.tenantId","000000")); headerMap.put("Authorization",ConfigUtils.getConfigValue("410.code.token.authorization","Basic c3dvcmQ6c3dvcmRfc2VjcmV0")); headerMap.put("Content-Type",ConfigUtils.getConfigValue("410.code.token.contentType","application/x-www-form-urlencoded")); String userName=ConfigUtils.getConfigValue("410.code.token.username","pdmUser"); String password=ConfigUtils.getConfigValue("410.code.token.password","21232f297a57a5a743894a0e4a801fc3"); Map parmarMap=new HashMap<>(); //userObject.set("Tenant-Id",ConfigUtils.getConfigValue("410.code.token.tenantId","000000")); /*userObject.setUsername("admin"); userObject.setPassword("21232f297a57a5a743894a0e4a801fc3"); userObject.setGrant_type("password"); userObject.setScope("all"); Object object = JSONObject.toJSON(userObject);*/ parmarMap.put("username",userName); //password=MD5PassWord(password); parmarMap.put("password",password); parmarMap.put("scope","all"); parmarMap.put("grant_type","password"); String result=HttpUtil.sendFormPost(tokenUrl,parmarMap,headerMap); if (StringUtils.isNotBlank(result)) { tokenUserObject = JSONObject.toJavaObject(JSONObject.parseObject(result), TokenUserObject.class); }else{ throw new Exception("验证用户鉴权信息失败!"); } } private String MD5PassWord (String passWord){ try { MessageDigest md = MessageDigest.getInstance("MD5"); byte[] a=md.digest(passWord.getBytes()); String codestr = new BigInteger(1,a ).toString(16); System.out.println(codestr); return codestr; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return passWord; } public void buildDialog() { Dimension dime = Toolkit.getDefaultToolkit().getScreenSize(); int x = dime.width/2 - 400; int y = dime.height/2-400; this.setLocation(x, y); this.setSize(new Dimension(900, 800)); this.setResizable(true); this.setVisible(true); } private void init() { boolean isConnect = verifyConnect(); if (isConnect) { try { initUserInfo(); bulidPal(); }catch (Exception e){ showMessageBox( "服务出现异常:->"+e.getMessage()); } } else { showMessageBox("无法连接代码服务器!"); } } private void bulidPal() throws Exception { this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); this.setTitle("代码申请"); Map displayValues = new HashMap<>(); if(fields != null && fields.length > 0 && values !=null && values.length > 0){ for (int i = 0; i < fields.length; i++) { displayValues.put(fields[i] , values[i]); } } mainPal = new CodeApplyPanelFor410(this,tokenUserObject, true, clsfName, deptName,libName,displayValues); VCIJPanel btnPal = new VCIJPanel(); btnPal.add(okBtn); btnPal.add(cancelBtn); this.setLayout(new BorderLayout()); this.add(mainPal,BorderLayout.CENTER); this.add(btnPal,BorderLayout.SOUTH); buildDialog(); } public boolean verifyConnect() { try { // if (ClientSession.getFrameworkService() != null) { // return true; // } return true; } catch (Exception e) { e.printStackTrace(); } return false; } public String getApplyCode() { return mainPal.getApplyCode(); } public String getCodeValue() { return codeValue; } public void setCodeValue(String codeValue) { this.codeValue = codeValue; } public String getAttributeValue(String attribute) { if(attrNameAndValMap.containsKey(attribute)) { return attrNameAndValMap.get(attribute); } return ""; } public void setAttrNameAndValMap() { this.attrNameAndValMap = mainPal.getAttrNameAndValMap(); } private void showMessageBox(String message) { VCIOptionPane.showMessage(parentComponet, message); } }