@ -1,11 +1,10 @@
package co.yixiang.modules.inform.service.impl ;
package co.yixiang.modules.inform ;
import cn.hutool.core.util.StrUtil ;
import cn.hutool.json.JSONUtil ;
import cn.iocoder.yudao.framework.common.exception.YshopException ;
import co.yixiang.constant.SystemConfigConstants ;
import co.yixiang.modules.inform.domin.* ;
import co.yixiang.modules.inform.service.SendMsgService ;
import co.yixiang.modules.order.service.YxStoreOrderService ;
import co.yixiang.modules.order.service.dto.YxStoreOrderDto ;
import co.yixiang.modules.shop.service.YxSystemConfigService ;
@ -20,7 +19,8 @@ import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils ;
import org.jsoup.nodes.Element ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.stereotype.Service ;
import org.springframework.stereotype.Component ;
import java.text.SimpleDateFormat ;
import java.util.ArrayList ;
import java.util.HashMap ;
@ -28,39 +28,35 @@ import java.util.Map;
import org.jsoup.Jsoup ;
import org.jsoup.nodes.Document ;
import org.jsoup.nodes.Node ;
import org.jsoup.nodes.TextNode ;
@Service
public class SendMsgServiceImpl implements SendMsgService {
@Component
public class SendMsgService {
@Autowired
private YxStoreOrderService yxStoreOrderService ;
@Autowired
private TemplateCard templateCard ;
@Autowired
private YxSystemConfigService systemConfigService ;
@SneakyThrows
@Override
public String inform ( long orderId , int type ) {
public String inform ( long orderId , int type ) {
CloseableHttpClient httpClient = HttpClients . createDefault ( ) ; //实例化对象
// String webhook_url="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=a2f0f8c9-e406-4f6b-86e4-d6ac9e4df88e";
String webhook_url = systemConfigService . getData ( SystemConfigConstants . ROBOT_URL ) ;
if ( StrUtil . isBlank ( webhook_url ) ) {
if ( StrUtil . isBlank ( webhook_url ) ) {
throw new YshopException ( "未配置企业微信群机器人" ) ;
}
HttpPost httpPost = new HttpPost ( webhook_url ) ;
HttpPost httpPost = new HttpPost ( webhook_url ) ;
httpPost . addHeader ( "Content-Type" , "application/json; charset=utf-8" ) ;
TemplateCard templateCard = this . creatTemplatecard ( orderId , type ) ;
TemplateCard templateCard = this . creatTemplatecard ( orderId , type ) ;
Map < String , Object > param = new HashMap < > ( ) ;
param . put ( "msgtype" , "template_card" ) ;
param . put ( "template_card" , JSONObject . parseObject ( JSONUtil . toJsonStr ( templateCard ) ) ) ;
String jsonParam = JSONObject . toJSONString ( param ) ;
StringEntity stringEntity = new StringEntity ( jsonParam , "utf-8" ) ;
StringEntity stringEntity = new StringEntity ( jsonParam , "utf-8" ) ;
httpPost . setEntity ( stringEntity ) ;
CloseableHttpResponse response = httpClient . execute ( httpPost ) ;
CloseableHttpResponse response = httpClient . execute ( httpPost ) ;
// 发送成功接收返回值
if ( response . getStatusLine ( ) . getStatusCode ( ) = = HttpStatus . SC_OK ) {
String result = EntityUtils . toString ( response . getEntity ( ) , "utf-8" ) ;
@ -76,45 +72,59 @@ public class SendMsgServiceImpl implements SendMsgService {
}
//创建订单消息
public TemplateCard creatTemplatecard ( long orderId , int type ) {
YxStoreOrderDto yxStoreOrderDto = yxStoreOrderService . getOrderDetailByOrderId ( orderId ) ;
public TemplateCard creatTemplatecard ( long orderId , int type ) {
YxStoreOrderDto yxStoreOrderDto = yxStoreOrderService . getOrderDetailByOrderId ( orderId ) ;
String mainTitle ;
ArrayList < HorizontalContent > arrayList = new ArrayList < > ( ) ;
ArrayList < HorizontalContent > arrayList = new ArrayList < > ( ) ;
arrayList . add ( new HorizontalContent ( ) . setKeyname ( "会员名称" ) . setValue ( yxStoreOrderDto . getRealName ( ) ) ) ;
arrayList . add ( new HorizontalContent ( ) . setKeyname ( "会员手机号" ) . setValue ( yxStoreOrderDto . getUserPhone ( ) ) ) ;
if ( type = = 0 ) { //支付时候
mainTitle = "有新的在线订单来啦" ;
if ( type = = 0 ) { //支付时候
mainTitle = "有新的在线订单来啦" ;
arrayList . add ( new HorizontalContent ( ) . setKeyname ( "下单地址" ) . setValue ( yxStoreOrderDto . getUserAddress ( ) ) ) ;
arrayList . add ( new HorizontalContent ( ) . setKeyname ( "下单时间" ) . setValue ( new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) . format ( yxStoreOrderDto . getCreateTime ( ) ) ) ) ;
} else {
mainTitle = "有新的退款申请" ;
} else {
mainTitle = "有新的退款申请" ;
//获取Document模型
Document docDesc = Jsoup . parse ( yxStoreOrderDto . getStatusName ( ) ) ;
//获取span节点
ArrayList < Element > list = docDesc . getElementsByTag ( "span" ) ;
ArrayList < Element > list = docDesc . getElementsByTag ( "span" ) ;
//获取文本内容,并且截取只需要标题后面的
arrayList . add ( new HorizontalContent ( ) . setKeyname ( "退款原因" ) . setValue ( list . get ( 0 ) . text ( ) . substring ( 5 ) ) ) ;
arrayList . add ( new HorizontalContent ( ) . setKeyname ( "备注说明" ) . setValue ( list . get ( 1 ) . text ( ) . substring ( 5 ) . length ( ) = = 0 ? list . get ( 1 ) . text ( ) . substring ( 5 ) : "本次退款无备注" ) ) ;
arrayList . add ( new HorizontalContent ( ) . setKeyname ( "备注说明" ) . setValue ( list . get ( 1 ) . text ( ) . substring ( 5 ) . length ( ) = = 0 ? list . get ( 1 ) . text ( ) . substring ( 5 ) : "本次退款无备注" ) ) ;
arrayList . add ( new HorizontalContent ( ) . setKeyname ( "申请时间" ) . setValue ( list . get ( 2 ) . text ( ) . substring ( 5 ) ) ) ;
}
templateCard . setHorizontal_content_list ( arrayList ) ;
templateCard . setCard_type ( "text_notice" ) ;
templateCard . setSource ( new Souce ( ) . setDesc ( "眼界甄选" ) . setIcon_url ( "https://wework.qpic.cn/wwpic/252813_jOfDHtcISzuodLa_1629280209/0" ) . setDesc_color ( 0 ) ) ;
templateCard . setMain_title ( new MainTitle ( ) . setTitle ( mainTitle ) . setDesc ( "订单号:" + orderId ) ) ;
templateCard . setEmphasis_content ( new EmphasisContent ( ) . setTitle ( yxStoreOrderDto . getPayPrice ( ) . toString ( ) ) . setDesc ( "订单总金额" ) ) ;
// templateCard.setQuote_area(new QuoteArea().setType(0).setUrl("").setAppid("APPID").setTitle("订单详情").setQuote_text("眼镜*1 0.01"));
templateCard . setSub_title_text ( "订单类型:" + yxStoreOrderDto . getPinkName ( ) ) ; //订单类型
ArrayList < Jump > arrayList1 = new ArrayList < > ( ) ;
// templateCard.setHorizontal_content_list(arrayList);
// templateCard.setCard_type("text_notice");
// templateCard.setSource(new Souce().setDesc("眼界甄选").setIcon_url("https://wework.qpic.cn/wwpic/252813_jOfDHtcISzuodLa_1629280209/0").setDesc_color(0));
// templateCard.setMain_title(new MainTitle().setTitle(mainTitle).setDesc("订单号:"+orderId));
// templateCard.setEmphasis_content(new EmphasisContent().setTitle(yxStoreOrderDto.getPayPrice().toString()).setDesc("订单总金额"));
//// templateCard.setQuote_area(new QuoteArea().setType(0).setUrl("").setAppid("APPID").setTitle("订单详情").setQuote_text("眼镜*1 0.01"));
// templateCard.setSub_title_text("订单类型:"+yxStoreOrderDto.getPinkName());//订单类型
// templateCard.setJump_list(arrayList1);
// templateCard.setCard_action(new CardAction().setType(1).setUrl(cardActionUrl));
ArrayList < Jump > arrayList1 = new ArrayList < > ( ) ;
//机器人消息跳转地址
String cardActionUrl = systemConfigService . getData ( SystemConfigConstants . CARD_ACTION_URL ) ;
if ( StrUtil . isBlank ( cardActionUrl ) ) {
if ( StrUtil . isBlank ( cardActionUrl ) ) {
// throw new YshopException("未配置企业微信群机器人跳转地址");
cardActionUrl = "未配置跳转网址" ;
cardActionUrl = "未配置跳转网址" ;
}
arrayList1 . add ( new Jump ( ) . setType ( 1 ) . setUrl ( cardActionUrl ) . setTitle ( "前去处理订单" ) ) ;
templateCard . setJump_list ( arrayList1 ) ;
templateCard . setCard_action ( new CardAction ( ) . setType ( 1 ) . setUrl ( cardActionUrl ) ) ;
return templateCard ;
return TemplateCard . builder ( )
. horizontalContentList ( arrayList ) . card_type ( "text_notice" )
. source ( Souce . builder ( )
. desc ( "眼界甄选" )
. icon_url ( "https://wework.qpic.cn/wwpic/252813_jOfDHtcISzuodLa_1629280209/0" )
. desc_color ( 0 )
. build ( ) )
. main_title ( MainTitle . builder ( ) . title ( mainTitle ) . desc ( "订单号:" + orderId ) . build ( ) )
. emphasis_content ( EmphasisContent . builder ( ) . title ( yxStoreOrderDto . getPayPrice ( ) . toString ( ) ) . desc ( "订单总金额" ) . build ( ) )
. sub_title_text ( "订单类型:" + yxStoreOrderDto . getPinkName ( ) )
. jump_list ( arrayList1 )
. card_action ( CardAction . builder ( ) . type ( 1 ) . url ( cardActionUrl ) . build ( ) )
. build ( ) ;
}
}