1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 微信小程序向公众号推送模板消息(根据用户登录小程序openid实现向同一主体下对应公众

微信小程序向公众号推送模板消息(根据用户登录小程序openid实现向同一主体下对应公众

时间:2018-09-24 17:34:22

相关推荐

微信小程序向公众号推送模板消息(根据用户登录小程序openid实现向同一主体下对应公众

最近文章更新没有那么频繁,可能每隔一个月左右会更一篇文章,主要就是我正在学习的内容demo或者工作中刚好用到的内容,有的篇幅内容相较于之前的文章会略长,可以根据目录检测自己需要查看的段落。

这里只针对第二种方式写了demo,后期可能对第一种方式进行补充。

方式一:

向用户推送消息需要用到用户的openid,当用户关注公众号后就会生产一个唯一不变的openid,小程序是在登录注册时就可以获取到openid,但是小程序的openid和公众号的openid是各自独立的,我们通过返回的unionid来对二者进行关联。

方式二【本文演示的】

通过微信提供的接口也可以实现,通过用户登录小程序产生的openid来实现消息的推送(用户如果未登录小程序不能实现消息的推送,也必须对公众号进行关注)

官方文档地址:https://developers./miniprogram/dev/OpenApiDoc/mp-message-management/uniform-message/sendUniformMessage.html

注意:当前小程序和公众号必须在同一主体下

请求的参数主要包含

access_token 可以通过get请求来进行获取touser 用户的openid,小程序返回的openidmp_template_msg 推送公众号消息的参数 appid 公众号的idtemplate_id 模板的idurl 模板所要跳转的内容miniprogram 模板索要跳转的小程序data 小程序要发送的消息内容

参数示例

一个消息发送的实例

{"touser":"OPENID","mp_template_msg":{"appid":"APPID ","template_id":"TEMPLATE_ID","url":"/download","miniprogram":{"appid":"xiaochengxuappid12345","pagepath":"index?foo=bar"},"data":{"first":{"value":"恭喜你购买成功!","color":"#173177"},"keyword1":{"value":"巧克力","color":"#173177"},"keyword2":{"value":"39.8元","color":"#173177"},"keyword3":{"value":"9月22日","color":"#173177"},"remark":{"value":"欢迎再次购买!","color":"#173177"}}}}

返回成功数据的实例

{"errCode": 0,"errMsg": "openapi.uniformMessage.send:ok"}

整合Springboot

用到的依赖

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.73</version></dependency><!-- 缓存 用来保存access_token--><dependency><groupId>com.github.ben-manes.caffeine</groupId><artifactId>caffeine</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency><!-- lombok包中携带Slf4j日志 --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><!-- 定时任务 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-quartz</artifactId></dependency></dependencies>

首先你要获取到access_token

官方文档地址:/wxwiki/9a186d136f1e9dce26e9593cadbc7130083b48d0.html

公众号在进行接口调用时都会需要access_token,这相当于我们和微信服务器进行交互的凭证,access_token每两个小时会刷新一次,新的token生效后前面的token会失效,两个token进行交替时5min内新老token都可以使用。我们每天对接口的调用是有次数限制的,每天最多调用2000次。

1.1 创建一个实体类,用来保存微信相关的配置信息

@Data@Component@PropertySource(value = "classpath:/application.yml")@ConfigurationProperties(prefix = "wechat")public class WeChatProperties {/*** 微信公众号的appid*/@Value("${appId}")private String appId;/*** 微信小程序的secret*/@Value("${secret}")private String secret;/*** 获取access_token的url*/@Value("${accessUrl}")private String accessUrl;/*** 消息发送的id*/@Value("${uniformSend}")private String uniformSend;/*** 微信小程序的appid*/@Value("${miniAppId}")private String miniAppId;/*** 微信小程序跳转页面路径*/@Value("${pagepath}")private String pagepath;/*** 获取token的定时任务corn*/@Value("${corn}")private String corn;}

1.2 这里我直接使用的restTemplate接口接口的调用,也可以使用httpclient,自由选择

所以这里需要对restTemplate进行配置

/*** @desc:* @author: LiuChang* @since: /7/22*/@Configurationpublic class RestTemplateConfig {/*** rest 模板** @param clientHttpRequestFactory* @return*/@Beanpublic RestTemplate restTemplate(ClientHttpRequestFactory clientHttpRequestFactory) {return new RestTemplate(clientHttpRequestFactory);}/*** 请求连接池的配置信息** @return*/@Beanpublic ClientHttpRequestFactory simpleClientHttpRequestFactory() {SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();factory.setConnectTimeout(15000);factory.setReadTimeout(5000);return factory;}}

在yaml配置文件中指定配置信息

server:port: 7777wechat:#公众号的appidappId: #小程序的appidminiAppId: #小程序的secretsecret: accessUrl: https://api./cgi-bin/tokenuniformSend: https://api./cgi-bin/message/wxopen/template/uniform_send?access_token=#小程序跳转的urlpagepath: pages/index#定时任务执行corn: 0 0 0/2 * * ?

调用接口获取token

@ResourceRestTemplate restTemplate;@ResourceWeChatProperties weChatProperties;public String getAccessToken() {String url = weChatProperties.getAccessUrl();// 这里的参数要和下面的Map Key值对应String path = "?grant_type={grant_type}&appid={appid}&secret={secret}";Map<String, String> params = new HashMap<>(3);params.put("grant_type", "client_credential");params.put("appid", weChatProperties.getMiniAppId());params.put("secret", weChatProperties.getSecret());ResponseEntity<String> forObject = restTemplate.getForEntity(url + path, String.class, params);JSONObject jsonObject = JSONObject.parseObject(forObject.getBody());String accessToken = jsonObject.getString("access_token");if (null == accessToken) {log.error("获取access_token失败");} else {//将token保存到缓存中//caffeineCache.put("access_token", accessToken);}return accessToken;}

引入缓存(可忽略)

2.1 缓存的配置文件 我这里缓存用的caffeine,主要是读的性能比较好,由于项目只有token用到了缓存就引入整个,也可以使用redis/memoryCache

/*** @desc: token的缓存配置* @author: LiuChang* @since: /7/22*/@Configurationpublic class TokenCacheConfig {@Beanpublic Cache caffeineCache() {return Caffeine.newBuilder()// 设置最后一次写入或访问后经过固定时间过期//.expireAfterWrite(90, TimeUnit.MINUTES)// 初始的缓存空间大小.initialCapacity(100)// 缓存的最大条数.maximumSize(1000).build();}}

2.2 使用cache

@ResourceCache<String, Object> caffeineCache;//获取caffeineCache.getIfPresent("access_token")//放入数据caffeineCache.put("access_token", accessToken);

引入定时任务(可忽略)

3.1 需要执行的定时任务

@Slf4j@Componentpublic class QuartzJob extends QuartzJobBean {@ResourceWxMessageSendService wxMessageSendService;@Overrideprotected void executeInternal(JobExecutionContext context) throws JobExecutionException {try {// 定时刷新tokenString token = wxMessageSendService.getAccessToken();log.info(token);} catch (Exception e) {log.error("任务失败 error:{}", e.getMessage());}}}

3.2 定时任务的配置文件

@Configurationpublic class QuartzConfig {/*** corn表达式*/@Value("${wechat.corn}")private String restartCron;@Beanpublic JobDetail restartJob() {return JobBuilder.newJob(QuartzJob.class).withIdentity("QuartzJob").storeDurably().build();}@Beanpublic Trigger restartTrigger() {CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(restartCron);return TriggerBuilder.newTrigger().forJob(restartJob()).withIdentity("QuartzJob").withSchedule(scheduleBuilder).build();}}

3.3 启动类增加注解

@SpringBootApplication@EnableScheduling

调用官方接口,推送消息测试

消息内容的实体类

/*** @desc: 微信推送消息类* @author: LiuChang* @since: /7/25*/@Data@AllArgsConstructor@NoArgsConstructorpublic class WxMessageBean {private MessageBean first;private MessageBean keyword1;private MessageBean keyword2;private MessageBean keyword3;private MessageBean keyword4;private MessageBean remark;/*** 自定义构造,first和remark必传,keyword根据需要自定义个数** @param first* @param remark* @param keyword*/public WxMessageBean(MessageBean first, MessageBean remark, MessageBean... keyword) {this.first = first;int count = 1;for (MessageBean keyword1 : keyword) {if (count == 1) {this.keyword1 = keyword1;} else if (count == 2) {this.keyword2 = keyword1;} else if (count == 3) {this.keyword3 = keyword1;} else if (count == 4) {this.keyword4 = keyword1;}count++;}this.remark = remark;}}

@Data@AllArgsConstructor@NoArgsConstructorpublic class MessageBean {/*** 消息内容*/private String value;/*** color*/private String color;}

接口编写

@Service@Slf4jpublic class WxMessageSendService {@ResourceRestTemplate restTemplate;@ResourceWeChatProperties weChatProperties;@ResourceCache<String, Object> caffeineCache;/*** 推送消息** @param openId* @param message* @return*/public String messageSend(String openId, Object message) {String token = (String) caffeineCache.getIfPresent(ConstantEnum.ACCESS_TOKEN.getCode());if (token == null) {token = this.getAccessToken();}String url = weChatProperties.getUniformSend() + token;// 这里的参数要和下面的Map Key值对应JSONObject obj = new JSONObject();JSONObject mpTemplateMsg = new JSONObject();JSONObject mini = new JSONObject();mini.put("appid", weChatProperties.getMiniAppId());mini.put("pagepath", weChatProperties.getPagepath());mpTemplateMsg.put("data", message);mpTemplateMsg.put("miniprogram", mini);///微信公众号appidmpTemplateMsg.put("appid", weChatProperties.getAppId());///微信公众号模板idmpTemplateMsg.put("template_id", template_id);obj.put("touser", openId);obj.put("mp_template_msg", mpTemplateMsg);ResponseEntity<String> forObject = restTemplate.postForEntity(url, obj, String.class);JSONObject object = JSON.parseObject(forObject.getBody());Integer errcode = (Integer) object.get("errcode");if (errcode == 0) {log.info("消息推送成功");return null;} else if (errcode == 40003) {log.error("推送消息的openid错误,openid:{},消息内容:{}", openId, message);return "推送消息的openid错误";} else if (errcode == 43004) {log.error("该用户未关注公众号,openid:{},消息内容:{}", openId, message);return "该用户未关注公众号";} else {return null;}}//下面还有个方法是获取access_token的 这里省略没写,在上面获取token已经将方法写了}

调用接口

@RestController@RequestMapping("/message_send")public class WxMessageSendController {@ResourceWxMessageSendService wxMessageSendService;/*** 通过openid和消息内容向指定用户推送公众号模板消息** @return*/@PostMapping()public String messageSend() {WxMessageBean messageBean = new WxMessageBean(new MessageBean("你有新的待审批消息,请点击查看", "#173177"),new MessageBean("footer", "#173177"),new MessageBean("keyword1", "#173177"));return wxMessageSendService.messageSend(openid, messageBean);}}

调用接口后的结果

注意事项:

两个appid和secret要对应填写正确别写反openid是用户小程序的idaccess_token 参数填写正确

微信小程序向公众号推送模板消息(根据用户登录小程序openid实现向同一主体下对应公众号推送模板消息)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。