diff --git a/yshop-common/src/main/java/co/yixiang/utils/IpUtil.java b/yshop-common/src/main/java/co/yixiang/utils/IpUtil.java new file mode 100644 index 00000000..abf02cbf --- /dev/null +++ b/yshop-common/src/main/java/co/yixiang/utils/IpUtil.java @@ -0,0 +1,165 @@ +package co.yixiang.utils; + +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.List; + +public class IpUtil { + private IpUtil(){} + /** + * 此方法描述的是:获得服务器的IP地址 + * @author: zhangyang33@sinopharm.com + * @version: 2014年9月5日 下午4:57:15 + */ + public static String getLocalIP() { + String sIP = ""; + InetAddress ip = null; + try { + boolean bFindIP = false; + Enumeration netInterfaces = (Enumeration) NetworkInterface + .getNetworkInterfaces(); + while (netInterfaces.hasMoreElements()) { + if (bFindIP) { + break; + } + NetworkInterface ni = (NetworkInterface) netInterfaces + .nextElement(); + Enumeration ips = ni.getInetAddresses(); + while (ips.hasMoreElements()) { + ip = (InetAddress) ips.nextElement(); + if (!ip.isLoopbackAddress() + && ip.getHostAddress().matches( + "(\\d{1,3}\\.){3}\\d{1,3}")) { + bFindIP = true; + break; + } + } + } + } catch (Exception e) { + } + if (null != ip) { + sIP = ip.getHostAddress(); + } + return sIP; + } + /** + * 此方法描述的是:获得服务器的IP地址(多网卡) + * @author: zhangyang33@sinopharm.com + * @version: 2014年9月5日 下午4:57:15 + */ + public static List getLocalIPS() { + InetAddress ip = null; + List ipList = new ArrayList(); + try { + Enumeration netInterfaces = (Enumeration) NetworkInterface + .getNetworkInterfaces(); + while (netInterfaces.hasMoreElements()) { + NetworkInterface ni = (NetworkInterface) netInterfaces + .nextElement(); + Enumeration ips = ni.getInetAddresses(); + while (ips.hasMoreElements()) { + ip = (InetAddress) ips.nextElement(); + if (!ip.isLoopbackAddress() + && ip.getHostAddress().matches( + "(\\d{1,3}\\.){3}\\d{1,3}")) { + ipList.add(ip.getHostAddress()); + } + } + } + } catch (Exception e) { + } + return ipList; + } + /** + * 此方法描述的是:获得服务器的MAC地址 + * @author: zhangyang33@sinopharm.com + * @version: 2014年9月5日 下午1:27:25 + */ + public static String getMacId() { + String macId = ""; + InetAddress ip = null; + NetworkInterface ni = null; + try { + boolean bFindIP = false; + Enumeration netInterfaces = (Enumeration) NetworkInterface + .getNetworkInterfaces(); + while (netInterfaces.hasMoreElements()) { + if (bFindIP) { + break; + } + ni = (NetworkInterface) netInterfaces + .nextElement(); + // ----------特定情况,可以考虑用ni.getName判断 + // 遍历所有ip + Enumeration ips = ni.getInetAddresses(); + while (ips.hasMoreElements()) { + ip = (InetAddress) ips.nextElement(); + if (!ip.isLoopbackAddress() // 非127.0.0.1 + && ip.getHostAddress().matches( + "(\\d{1,3}\\.){3}\\d{1,3}")) { + bFindIP = true; + break; + } + } + } + } catch (Exception e) { + } + if (null != ip) { + try { + macId = getMacFromBytes(ni.getHardwareAddress()); + } catch (SocketException e) { + } + } + return macId; + } + /** + * 此方法描述的是:获得服务器的MAC地址(多网卡) + * @author: zhangyang33@sinopharm.com + * @version: 2014年9月5日 下午1:27:25 + */ + public static List getMacIds() { + InetAddress ip = null; + NetworkInterface ni = null; + List macList = new ArrayList(); + try { + Enumeration netInterfaces = (Enumeration) NetworkInterface + .getNetworkInterfaces(); + while (netInterfaces.hasMoreElements()) { + ni = (NetworkInterface) netInterfaces + .nextElement(); + // ----------特定情况,可以考虑用ni.getName判断 + // 遍历所有ip + Enumeration ips = ni.getInetAddresses(); + while (ips.hasMoreElements()) { + ip = (InetAddress) ips.nextElement(); + if (!ip.isLoopbackAddress() // 非127.0.0.1 + && ip.getHostAddress().matches( + "(\\d{1,3}\\.){3}\\d{1,3}")) { + macList.add(getMacFromBytes(ni.getHardwareAddress())); + } + } + } + } catch (Exception e) { + } + return macList; + } + private static String getMacFromBytes(byte[] bytes) { + StringBuffer mac = new StringBuffer(); + byte currentByte; + boolean first = false; + for (byte b : bytes) { + if (first) { + mac.append("-"); + } + currentByte = (byte) ((b & 240) >> 4); + mac.append(Integer.toHexString(currentByte)); + currentByte = (byte) (b & 15); + mac.append(Integer.toHexString(currentByte)); + first = true; + } + return mac.toString().toUpperCase(); + } +} diff --git a/yshop-weixin/src/main/java/co/yixiang/mp/service/WeixinPayService.java b/yshop-weixin/src/main/java/co/yixiang/mp/service/WeixinPayService.java index 3b0b4e1e..6e1b5f0b 100644 --- a/yshop-weixin/src/main/java/co/yixiang/mp/service/WeixinPayService.java +++ b/yshop-weixin/src/main/java/co/yixiang/mp/service/WeixinPayService.java @@ -21,6 +21,7 @@ import co.yixiang.modules.user.service.YxUserRechargeService; import co.yixiang.modules.user.service.YxUserService; import co.yixiang.modules.user.service.dto.WechatUserDto; import co.yixiang.mp.config.WxPayConfiguration; +import co.yixiang.utils.IpUtil; import co.yixiang.utils.RedisUtils; import co.yixiang.utils.ShopKeyUtils; import com.baomidou.mybatisplus.core.toolkit.Wrappers; @@ -108,7 +109,7 @@ public class WeixinPayService { WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest(); orderRequest.setOutTradeNo(orderId); orderRequest.setTotalFee(payPrice); - orderRequest.setSpbillCreateIp("127.0.0.1"); + orderRequest.setSpbillCreateIp(IpUtil.getLocalIP()); orderRequest.setNotifyUrl(this.getApiUrl() + "/api/wechat/notify"); orderRequest.setBody(body); orderRequest.setAttach(attach); @@ -184,7 +185,7 @@ public class WeixinPayService { entPayRequest.setReUserName(userName); entPayRequest.setAmount(amount); entPayRequest.setDescription("提现"); - entPayRequest.setSpbillCreateIp("127.0.0.1"); + entPayRequest.setSpbillCreateIp(IpUtil.getLocalIP()); wxPayService.getEntPayService().entPay(entPayRequest); }