1.微信用户导出接口

2.关闭订单和查询订单
master
litao 2 years ago
parent fba2ae16e2
commit 3dae7a9612
  1. 10
      src/main/java/org/springblade/modules/weixin/controller/AppEbLoginController.java
  2. 27
      src/main/java/org/springblade/modules/weixin/controller/WeChatUserController.java
  3. 44
      src/main/java/org/springblade/modules/weixin/excel/WeChatUserExcel.java
  4. 4
      src/main/java/org/springblade/modules/weixin/service/IWeChatUserService.java
  5. 26
      src/main/java/org/springblade/modules/weixin/service/impl/WeChatUserServiceImpl.java
  6. 86
      src/main/java/org/springblade/modules/weixin/utils/WxPayUtils.java

@ -109,7 +109,17 @@ public class AppEbLoginController {
Double totalPrice = order.getTotalPrice() * 100; Double totalPrice = order.getTotalPrice() * 100;
int total = totalPrice.intValue(); int total = totalPrice.intValue();
// int total = 1;
return R.data(WxPayUtils.getPayParam(openId, outTradeNo, total)); return R.data(WxPayUtils.getPayParam(openId, outTradeNo, total));
} }
@PostMapping("/cloceOrder")
public R cloceOrder(@RequestParam String outTradeNo) {
return R.data(WxPayUtils.cloceOrderJSApiV3(outTradeNo));
}
@GetMapping("/getOrder")
public R getOrder(String outTradeNo) {
return R.data(WxPayUtils.getOrderJSApiV3(outTradeNo));
}
} }

@ -1,17 +1,29 @@
package org.springblade.modules.weixin.controller; package org.springblade.modules.weixin.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.springblade.common.enums.OrderStatusEnum;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.log.exception.ServiceException; import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.Condition; import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query; import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R; import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func; import org.springblade.core.tool.utils.Func;
import org.springblade.modules.desk.entity.Order;
import org.springblade.modules.desk.vo.OrderExcelVO;
import org.springblade.modules.weixin.entity.WeChatUser; import org.springblade.modules.weixin.entity.WeChatUser;
import org.springblade.modules.weixin.excel.WeChatUserExcel;
import org.springblade.modules.weixin.service.IWeChatUserService; import org.springblade.modules.weixin.service.IWeChatUserService;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
@RestController @RestController
@AllArgsConstructor @AllArgsConstructor
@RequestMapping("/weChatUser") @RequestMapping("/weChatUser")
@ -32,6 +44,21 @@ public class WeChatUserController {
return R.data(weChatUserService.buyCount(Condition.getPage(query), weChatUser)); return R.data(weChatUserService.buyCount(Condition.getPage(query), weChatUser));
} }
/**
* 导出
*/
@GetMapping("/export")
public void export(HttpServletResponse response, WeChatUser weChatUser) {
List<WeChatUser> weChatUsers = weChatUserService.export(weChatUser);
List<WeChatUserExcel> vos = new ArrayList<>();
weChatUsers.forEach(item -> {
WeChatUserExcel vo = BeanUtil.copy(item, WeChatUserExcel.class);
vos.add(vo);
});
ExcelUtil.export(response, "微信用户" + DateUtil.time(), "微信用户表", vos, WeChatUserExcel.class);
}
@PostMapping("/save") @PostMapping("/save")
public R save(@RequestBody WeChatUser weChatUser) { public R save(@RequestBody WeChatUser weChatUser) {
long count = weChatUserService.count(Wrappers.<WeChatUser>lambdaQuery().eq(WeChatUser::getOpenId, weChatUser.getOpenId())); long count = weChatUserService.count(Wrappers.<WeChatUser>lambdaQuery().eq(WeChatUser::getOpenId, weChatUser.getOpenId()));

@ -0,0 +1,44 @@
package org.springblade.modules.weixin.excel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import lombok.Data;
@Data
@ColumnWidth(25)
@HeadRowHeight(20)
@ContentRowHeight(18)
public class WeChatUserExcel {
/**
* 微信用户唯一标识
*/
@ExcelProperty("openId")
private String openId;
/**
* 昵称
*/
@ExcelProperty("昵称")
private String username;
/**
* 手机
*/
@ExcelProperty("手机")
private String phone;
/**
* 头像
*/
@ExcelProperty("头像")
private String avatar;
/**
* 购买次数
*/
@ExcelProperty("购买次数")
private Integer count;
}

@ -20,6 +20,8 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseService; import org.springblade.core.mp.base.BaseService;
import org.springblade.modules.weixin.entity.WeChatUser; import org.springblade.modules.weixin.entity.WeChatUser;
import java.util.List;
/** /**
* 服务类 * 服务类
* *
@ -28,4 +30,6 @@ import org.springblade.modules.weixin.entity.WeChatUser;
public interface IWeChatUserService extends BaseService<WeChatUser> { public interface IWeChatUserService extends BaseService<WeChatUser> {
IPage<WeChatUser> buyCount(IPage<WeChatUser> page, WeChatUser weChatUser); IPage<WeChatUser> buyCount(IPage<WeChatUser> page, WeChatUser weChatUser);
List<WeChatUser> export(WeChatUser weChatUser);
} }

@ -16,8 +16,10 @@
*/ */
package org.springblade.modules.weixin.service.impl; package org.springblade.modules.weixin.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.springblade.core.mp.base.BaseServiceImpl; import org.springblade.core.mp.base.BaseServiceImpl;
@ -33,6 +35,7 @@ import java.util.stream.Collectors;
/** /**
* 服务实现类 * 服务实现类
*
* @author BladeX * @author BladeX
*/ */
@Service @Service
@ -43,7 +46,11 @@ public class WeChatUserServiceImpl extends BaseServiceImpl<WeChatUserMapper, WeC
@Override @Override
public IPage<WeChatUser> buyCount(IPage<WeChatUser> page, WeChatUser weChatUser) { public IPage<WeChatUser> buyCount(IPage<WeChatUser> page, WeChatUser weChatUser) {
IPage<WeChatUser> userIPage = this.page(page, Wrappers.<WeChatUser>lambdaQuery(weChatUser).select(WeChatUser::getUsername, WeChatUser::getPhone)); LambdaQueryWrapper<WeChatUser> wrapper = Wrappers.lambdaQuery(weChatUser);
wrapper.select(WeChatUser::getUsername, WeChatUser::getPhone);
wrapper.like(StringUtils.isNotBlank(weChatUser.getUsername()), WeChatUser::getUsername, weChatUser.getUsername());
IPage<WeChatUser> userIPage = this.page(page, wrapper);
List<WeChatUser> records = userIPage.getRecords(); List<WeChatUser> records = userIPage.getRecords();
if (CollectionUtils.isNotEmpty(records)) { if (CollectionUtils.isNotEmpty(records)) {
List<BuyCount> buyCount = weChatUserMapper.buyCount(); List<BuyCount> buyCount = weChatUserMapper.buyCount();
@ -58,5 +65,22 @@ public class WeChatUserServiceImpl extends BaseServiceImpl<WeChatUserMapper, WeC
return userIPage; return userIPage;
} }
@Override
public List<WeChatUser> export(WeChatUser weChatUser) {
LambdaQueryWrapper<WeChatUser> wrapper = Wrappers.lambdaQuery(weChatUser);
wrapper.like(StringUtils.isNotBlank(weChatUser.getUsername()), WeChatUser::getUsername, weChatUser.getUsername());
List<WeChatUser> users = this.list(wrapper);
if (CollectionUtils.isNotEmpty(users)) {
List<BuyCount> buyCount = weChatUserMapper.buyCount();
Map<String, Integer> collect = buyCount.stream().collect(Collectors.toMap(BuyCount::getPhone, BuyCount::getCount));
users.forEach(user -> {
Integer count = collect.get(user.getPhone());
user.setCount(count != null ? count : 0);
});
}
return users;
}
} }

@ -6,6 +6,7 @@ import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder;
import com.wechat.pay.contrib.apache.httpclient.auth.*; import com.wechat.pay.contrib.apache.httpclient.auth.*;
import com.wechat.pay.contrib.apache.httpclient.util.PemUtil; import com.wechat.pay.contrib.apache.httpclient.util.PemUtil;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
@ -44,6 +45,7 @@ public class WxPayUtils {
// JSApi下单url地址 // JSApi下单url地址
public static String JSApiPayUrl = "https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi"; public static String JSApiPayUrl = "https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi";
public static String ClocePayUrl = "https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/{out_trade_no}/close";
// 商户私钥 获取对应的签名需要 在微信支付商户平台-账户中心-API安全里设置 养宠物那边复制的 // 商户私钥 获取对应的签名需要 在微信支付商户平台-账户中心-API安全里设置 养宠物那边复制的
// 对应文档 https://pay.weixin.qq.com/wiki/doc/apiv3_partner/open/pay/chapter2_1.shtml // 对应文档 https://pay.weixin.qq.com/wiki/doc/apiv3_partner/open/pay/chapter2_1.shtml
@ -159,6 +161,89 @@ public class WxPayUtils {
return ""; return "";
} }
/**
* 关闭订单
* @param outTradeNo
* @return
*/
public static String cloceOrderJSApiV3(String outTradeNo) {
payLoading();
//请求URL
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/" + outTradeNo + "/close");
// 请求body参数
JSONObject amountJson = new JSONObject();
amountJson.put("mchid", mchId);
StringEntity entity = new StringEntity(JSONObject.toJSONString(amountJson), "utf-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
httpPost.setHeader("Accept", "application/json");
//完成签名并执行请求
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
return EntityUtils.toString(response.getEntity());
} else if (statusCode == 204) {
return "";
} else {
System.out.println("failed,resp code = " + statusCode + ",return body = " + EntityUtils.toString(response.getEntity()));
throw new IOException("request failed");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
assert response != null;
response.close();
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return "";
}
/**
* 查询订单
* @param outTradeNo
* @return
*/
public static String getOrderJSApiV3(String outTradeNo) {
payLoading();
//请求URL
HttpGet httpGet = new HttpGet("https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/" + outTradeNo + "?mchid=" + mchId);
// 请求body参数
httpGet.setHeader("Accept", "application/json");
//完成签名并执行请求
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
return EntityUtils.toString(response.getEntity());
} else if (statusCode == 204) {
return "";
} else {
System.out.println("failed,resp code = " + statusCode + ",return body = " + EntityUtils.toString(response.getEntity()));
throw new IOException("request failed");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
assert response != null;
response.close();
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return "";
}
/** /**
* 第四步生成支付参数 * 第四步生成支付参数
@ -202,7 +287,6 @@ public class WxPayUtils {
} }
/** /**
* 构造下单的json(第三步需要) * 构造下单的json(第三步需要)
* *

Loading…
Cancel
Save