package com.gxwebsoft.common.core.utils; import cn.hutool.http.HttpRequest; import com.alibaba.fastjson.JSONObject; import com.gxwebsoft.common.system.entity.*; import com.wechat.pay.java.service.partnerpayments.jsapi.model.Transaction; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.HashMap; @Component public class RequestUtil { private static final String SERVER_HOST = "https://server.gxwebsoft.com/api"; private static final String MODULES_HOST = "https://modules.gxwebsoft.com/api"; private static String ACCESS_TOKEN; private static String TENANT_ID; public void setTenantId(String tenantId) { TENANT_ID = tenantId; } public void setAccessToken(String token) { ACCESS_TOKEN = token; } // 余额支付通知 public void pushBalancePayNotify(Transaction transaction, Payment payment) { System.out.println("payment = " + payment); System.out.println("transaction = " + transaction); // 设置租户ID setTenantId(payment.getTenantId().toString()); // 推送支付通知地址 String path = payment.getNotifyUrl(); try { // 链式构建请求 HttpRequest.post(path) .header("Tenantid", TENANT_ID) .body(JSONUtil.toJSONString(transaction))//表单内容 .timeout(20000)//超时,毫秒 .execute().body(); } catch (Exception e) { e.printStackTrace(); } } // 微信支付通知 public void pushWxPayNotify(Transaction transaction, Payment payment) { // 设置租户ID setTenantId(payment.getTenantId().toString()); // 推送支付通知地址 String path = payment.getNotifyUrl(); try { // 链式构建请求 HttpRequest.post(path) .header("Tenantid", TENANT_ID) .body(JSONUtil.toJSONString(transaction))//表单内容 .timeout(20000)//超时,毫秒 .execute().body(); } catch (Exception e) { e.printStackTrace(); } } public User getMerchantAccountByPhone(String phone) { String path = "/shop/merchant-account/getMerchantAccountByPhone/" + phone; try { // 链式构建请求 String result = HttpRequest.get(MODULES_HOST.concat(path)) .header("Authorization", ACCESS_TOKEN) .header("Tenantid", TENANT_ID) .timeout(20000)//超时,毫秒 .execute().body(); JSONObject jsonObject = JSONObject.parseObject(result); final String data = jsonObject.getString("data"); return JSONObject.parseObject(data, User.class); } catch (Exception e) { e.printStackTrace(); } return null; } public User getByUserId(Integer userId) { String path = "/system/user/" + userId; try { // 链式构建请求 String result = HttpRequest.get(MODULES_HOST.concat(path)) .header("Authorization", ACCESS_TOKEN) .header("Tenantid", TENANT_ID) .timeout(20000)//超时,毫秒 .execute().body(); JSONObject jsonObject = JSONObject.parseObject(result); final String data = jsonObject.getString("data"); return JSONObject.parseObject(data, User.class); } catch (Exception e) { e.printStackTrace(); } return null; } public User getUserByPhone(String phone) { String path = "/system/user/getByPhone/" + phone; try { // 链式构建请求 String result = HttpRequest.get(SERVER_HOST.concat(path)) .header("Authorization", ACCESS_TOKEN) .header("Tenantid", TENANT_ID) .timeout(20000)//超时,毫秒 .execute().body(); JSONObject jsonObject = JSONObject.parseObject(result); final String data = jsonObject.getString("data"); return JSONObject.parseObject(data, User.class); } catch (Exception e) { e.printStackTrace(); } return null; } // 新增用户 public boolean saveUserByPhone(MerchantAccount merchantAccount) { String path = "/system/user/"; try { HashMap map = new HashMap<>(); map.put("nickname", merchantAccount.getRealName()); map.put("username", merchantAccount.getPhone()); map.put("realName", merchantAccount.getRealName()); map.put("phone", merchantAccount.getPhone()); map.put("password", merchantAccount.getPassword()); final ArrayList roles = new ArrayList<>(); final UserRole userRole = new UserRole(); userRole.setUserId(merchantAccount.getUserId()); userRole.setRoleId(merchantAccount.getRoleId()); userRole.setTenantId(merchantAccount.getTenantId()); roles.add(userRole); map.put("roles", roles); map.put("tenantId", TENANT_ID); // 链式构建请求 String result = HttpRequest.post(SERVER_HOST.concat(path)) .header("Authorization", ACCESS_TOKEN) .header("Tenantid", TENANT_ID) .body(JSONUtil.toJSONString(map))//表单内容 .timeout(20000)//超时,毫秒 .execute().body(); } catch (Exception e) { e.printStackTrace(); } return true; } }