Commit 03543ae4 by 吕明尚

微信公众号的模版消息推送

parent 9c2288c3
......@@ -239,7 +239,9 @@ public class WeChatConstants {
/** 公众号发送模板消息的url */
public static final String WECHAT_PUBLIC_SEND_TEMPLATE_URL = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={}";
//微信公众号的获取用户列表
public static final String WECHAT_PUBLIC_USER_GET_URL = "https://api.weixin.qq.com/cgi-bin/user/get?access_token={}}&next_openid={}";
public static final String WECHAT_PUBLIC_USER_GET_URL = "https://api.weixin.qq.com/cgi-bin/user/get?access_token={}&next_openid={}";
//微信公众账号的用户信息
public static final String WECHAT_PUBLIC_USER_INFO_URL = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token={}";
......
package share.web.controller.system;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
......@@ -10,15 +11,19 @@ import org.springframework.web.bind.annotation.*;
import share.common.core.domain.R;
import share.common.core.redis.RedisUtil;
import share.common.utils.JsonConvertUtil;
import share.system.domain.SConsumerToken;
import share.system.domain.TemplateMessage;
import share.system.request.RegisterThirdSConsumerRequest;
import share.system.request.WxBindingPhoneRequest;
import share.system.request.WxRegisterPhoneRequest;
import share.system.response.LoginResponse;
import share.system.service.SConsumerCenterService;
import share.system.service.SConsumerTokenService;
import share.system.service.WechatNewService;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
......@@ -39,6 +44,12 @@ public class WeChatController {
@Autowired
private RedisUtil redisUtil;
@Autowired
private WechatNewService wechatNewService;
@Autowired
private SConsumerTokenService sConsumerTokenService;
/**
* 微信登录小程序授权登录
......@@ -104,6 +115,19 @@ public class WeChatController {
return R.ok(miniTempList);
}
@GetMapping("/test")
public void getWechatUSERList() {
List<String> list = new ArrayList<>();
List<String> wechatUSERList = wechatNewService.getWechatUSERList(list, "");
Map<String, String> stringStringMap = wechatNewService.WechatUsetListInfo(wechatUSERList);
System.out.println(stringStringMap);
List<SConsumerToken> sConsumerTokenList = sConsumerTokenService.list(new LambdaQueryWrapper<SConsumerToken>().isNotNull(SConsumerToken::getUnionId).isNull(SConsumerToken::getOpenId));
sConsumerTokenList.forEach(item -> {
item.setOpenId(stringStringMap.get(item.getUnionId()));
sConsumerTokenService.updateById(item);
});
System.out.println(sConsumerTokenList);
}
}
......
......@@ -87,6 +87,9 @@ public class RedisTask {
@Autowired
private WechatNewService wechatNewService1;
@Autowired
private SConsumerTokenService sConsumerTokenService;
//15分钟的常量
final long FIFTEEN_MINUTES = 60 * 15;
......@@ -495,4 +498,20 @@ public class RedisTask {
if (!CollectionUtils.isEmpty(roomLabels)) roomLabelService.saveBatch(roomLabels);
}
//自动更新用户unionid
public void AutoUpdateOpenid() {
List<SConsumerToken> sConsumerTokenList = sConsumerTokenService.list(new LambdaQueryWrapper<SConsumerToken>().isNotNull(SConsumerToken::getUnionId).isNull(SConsumerToken::getOpenId));
if (CollectionUtils.isEmpty(sConsumerTokenList)) {
return;
}
List<String> list = new ArrayList<>();
List<String> wechatUSERList = wechatNewService.getWechatUSERList(list, "");
Map<String, String> stringStringMap = wechatNewService.WechatUsetListInfo(wechatUSERList);
List<SConsumerToken> updateList = new ArrayList<>();
sConsumerTokenList.forEach(item -> {
item.setOpenId(stringStringMap.get(item.getUnionId()));
updateList.add(item);
});
if (!CollectionUtils.isEmpty(updateList)) sConsumerTokenService.updateBatchById(updateList);
}
}
package share.system.domain.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import java.util.List;
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value = "WeChatAccessTokenVo", description = "公众号用户列表")
public class UserOpenIdVo {
private Integer total;
private Integer count;
private List<String> data;
private String next_openid;
@ApiModel(value = "miniprogram对象", description = "微信模板发送类")
public class Miniprogram {
@ApiModelProperty(value = "所需跳转到的小程序appid", required = true)
private String appid;
@ApiModelProperty(value = "所需跳转到小程序的具体页面路径")
private String pagepath;
}
......@@ -20,13 +20,7 @@ public class OfficialAccountTemplate {
private String template_id;
@ApiModelProperty(value = "跳小程序所需数据,不需跳小程序可不用传该数据")
private String miniprogram;
@ApiModelProperty(value = "所需跳转到的小程序appid", required = true)
private String appid;
@ApiModelProperty(value = "所需跳转到小程序的具体页面路径")
private String pagepath;
private Miniprogram miniprogram;
@ApiModelProperty(value = "发送内容")
private HashMap<String, SendTemplateMessageItemVo> data;
......
package share.system.domain.vo;
import lombok.Data;
@Data
public class WachatUser {
private String openid;
private String lang = "zh_CN";
}
......@@ -91,7 +91,9 @@ public interface WechatNewService {
Boolean sendPublicTemplateMessage(SOrder byOrderNo, MessageReminderEnum messageReminderEnum, Long consumerId);
UserOpenIdVo getWechatUSERList();
List<String> getWechatUSERList(List<String> openIds, String next_openid);
Map<String, String> WechatUsetListInfo(List<String> openIds);
/**
* 微信小程序发送订阅消息
* @param templateMessage 消息对象
......
......@@ -952,7 +952,7 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper, SOrder> impleme
consumers.forEach(o -> {
phones.add(o.getPhone());
//公众号发送订单预订成功通知
// wechatNewService.sendPublicTemplateMessage(sOrder,MessageReminderEnum.ORDER_RESERVE,o.getId());
wechatNewService.sendPublicTemplateMessage(sOrder, MessageReminderEnum.ORDER_RESERVE, o.getId());
});
smsService.sendSmsOrderPredetermine(phones, sOrder, sStore, room);
}
......
......@@ -35,10 +35,7 @@ import share.system.service.*;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
......@@ -521,7 +518,9 @@ public class WechatNewServiceImpl implements WechatNewService {
if (data.containsKey("errcode") && !data.getString("errcode").equals("0") && !data.getString("errcode").equals("40014")) {
if (data.containsKey("errmsg")) {
wxExceptionDispose(data, "微信公众号发送模板消息异常");
throw new BaseException("微信接口调用失败:" + data.getString("errcode") + data.getString("errmsg"));
// throw new BaseException("微信接口调用失败:" + data.getString("errcode") + data.getString("errmsg"));
logger.error("微信接口调用失败:" + data.getString("errcode") + data.getString("errmsg"));
}
} else if (data.getString("errcode").equals("40014")) {
return sendPublicTemplateMessage(templateMessage);
......@@ -579,9 +578,10 @@ public class WechatNewServiceImpl implements WechatNewService {
@Override
public Boolean sendPublicTemplateMessage(SOrder byOrderNo, MessageReminderEnum messageReminderEnum, Long consumerId) {
OfficialAccountTemplate officialAccountTemplate = new OfficialAccountTemplate();
officialAccountTemplate.setAppid(weChatConfig.getAppId());
String pagepath = "/pages/orderResult/orderResult?orderNo=" + byOrderNo.getOrderNo();
officialAccountTemplate.setPagepath(pagepath);
Miniprogram miniprogram = new Miniprogram();
miniprogram.setAppid(weChatConfig.getAppId());
miniprogram.setPagepath("/pages/orderResult/orderResult?orderNo=" + byOrderNo.getOrderNo());
officialAccountTemplate.setMiniprogram(miniprogram);
HashMap<String, SendTemplateMessageItemVo> data = new HashMap<>();
SStore sStore = storeService.getById(byOrderNo.getStoreId());
SRoomVo sRoomVo = roomService.selectSRoomById(byOrderNo.getRoomId());
......@@ -623,18 +623,68 @@ public class WechatNewServiceImpl implements WechatNewService {
//商品名称
data.put("thing21", new SendTemplateMessageItemVo(msg));
}
officialAccountTemplate.setData(data);
return sendPublicTemplateMessage(officialAccountTemplate);
}
@Override
public UserOpenIdVo getWechatUSERList() {
String accessToken = getMiniAccessToken();
String url = StrUtil.format(WeChatConstants.WECHAT_PUBLIC_USER_GET_URL, accessToken, "");
public List<String> getWechatUSERList(List<String> openIds, String next_openid) {
String accessToken = getPublicAccessToken();
String url;
if (StringUtils.isEmpty(next_openid)) {
url = StrUtil.format(WeChatConstants.WECHAT_PUBLIC_USER_GET_URL, accessToken, "");
} else {
url = StrUtil.format(WeChatConstants.WECHAT_PUBLIC_USER_GET_URL, accessToken, next_openid);
}
JSONObject data = restTemplateUtil.getData(url);
if (ObjectUtil.isNull(data)) {
throw new BaseException("微信平台接口异常,没任何数据返回!");
}
return JSONObject.parseObject(data.toJSONString(), UserOpenIdVo.class);
JSONObject dataJSONArray = data.getJSONObject("data");
JSONArray openids = dataJSONArray.getJSONArray("openid");
//转list
List<String> openIdList = openids.toJavaList(String.class);
openIds.addAll(openIdList);
if (data.getInteger("total") > openIds.size()) {
return getWechatUSERList(openIds, data.getString("next_openid"));
}
return openIds;
}
@Override
public Map<String, String> WechatUsetListInfo(List<String> openIds) {
Map<String, String> map = new HashMap<>();
String accessToken = getPublicAccessToken();
String url = StrUtil.format(WeChatConstants.WECHAT_PUBLIC_USER_INFO_URL, accessToken);
List<WachatUser> list = new ArrayList<>();
openIds.stream().forEach(o -> {
WachatUser wachatUser = new WachatUser();
wachatUser.setOpenid(o);
list.add(wachatUser);
});
Map<String, Object> listMap = new HashMap<>();
listMap.put("user_list", list);
JSONObject jsonObject = new JSONObject(listMap);
String result = restTemplateUtil.postJsonData(url, jsonObject);
JSONObject data = JSONObject.parseObject(result);
if (ObjectUtil.isNull(data)) {
throw new BaseException("微信平台接口异常,没任何数据返回!");
}
if (data.containsKey("errcode") && !data.getString("errcode").equals("0")) {
if (data.containsKey("errmsg")) {
// 保存到微信异常表
// wxExceptionDispose(data, "微信小程序登录凭证校验异常");
throw new BaseException("微信接口调用失败:" + data.getString("errcode") + data.getString("errmsg"));
}
}
JSONArray userInfoList = data.getJSONArray("user_info_list");
//转list
List<JSONObject> userInfos = userInfoList.toJavaList(JSONObject.class);
userInfos.stream().forEach(o -> {
map.put(o.getString("unionid"), o.getString("openid"));
});
return map;
}
@Override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment