测试号管理:https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index
在开发之前需要做的事情:
1.申请一个公众号测试账号,一分钟就可以申请下来
2.然后需要一个微信验证token的地址,这个大家可以网上找一下 比较简单。
验证token的代码贴一下
package com.cjkj.message.controller; import com.cjkj.message.utils.SignUtil; import org.apache.log4j.Logger; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.PrintWriter; /** * @program: cjkj * @description: * @author: Mr.Wang * @create: 2020-03-25 17:11 **/ @Controller @RequestMapping("/wechat") public class WechatController { private static Logger logger = Logger.getLogger(WechatController.class); @RequestMapping(value = "security", method = RequestMethod.GET) public void doGet( HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "signature", required = true) String signature, @RequestParam(value = "timestamp", required = true) String timestamp, @RequestParam(value = "nonce", required = true) String nonce, @RequestParam(value = "echostr", required = true) String echostr) { try { if (SignUtil.checkSignature(signature, timestamp, nonce)) { PrintWriter out = response.getWriter(); out.print(echostr); out.close(); } else { logger.info("这里存在非法请求!"); } } catch (Exception e) { logger.error(e, e); } } @RequestMapping(value = "security", method = RequestMethod.POST) // post方法用于接收微信服务端消息 public void DoPost() { System.out.println("这是post方法!"); } }
3.token验证成功后,用自己的微信扫描关注公众号。
4.创建一个自定义的模板。
我的模板事这个:
{ {first.DATA}} 用户名:{ {keyword1.DATA}} 订单号:{ {keyword2.DATA}} 订单金额:{ {keyword3.DATA}} 商品信息:{ {keyword4.DATA}} { {remark.DATA}} 复制粘贴进去就可以了。 |
5.上边的话我们就可以看到appID,appsecret,关注用户的微信号,也就是openid。然后还有自己船舰的模板id(模板ID(用于接口调用))
接下来我们用代码就可以实现了。
我的代码中包含了一些业务代码,大家看的时候跳过。同时我们的appID,appsecret保存在数据库,当调用的时候 ,通过参数appkey去查询到这两个参数。
wechat: #获取token的地址 getTokenUrl: https://api.weixin.qq.com/cgi-bin/token #发送公众号消息的地址 sendMsgUrl: https://api.weixin.qq.com/cgi-bin/message/template/send #发送小程序的消息地址 sendMiniPUrl: https://api.weixin.qq.com/cgi-bin/message/subscribe/send
@RestController @RequestMapping("/wechat") public class WechatMessageController { private static Logger logger = Logger.getLogger(WechatMessageController.class); @Autowired private WechatService wechatService; /** * 公众号发送模板消息 * @return */ @ApiOperation(value="公众号发送模板消息", notes="根据参数发送模板消息") @ApiImplicitParams({ @ApiImplicitParam(name = "WechatTokenEntity", value = "公众号实体", required = true, paramType = "query", dataType = "String"), @ApiImplicitParam(name = "data", value = "模板信息参数json结构的字符串", required = true, paramType = "query", dataType = "String"), @ApiImplicitParam(name = "miniprogram", value = "小程序参数json结构字符串", required = true, paramType = "query", dataType = "String") }) @RequestMapping(value="/sendMessage" ,method = {RequestMethod.POST}) public com.cjwl.ResultData sendMessage(HttpServletRequest request, WechatOfficialEntity wechatOfficialEntity, @RequestParam String data, @RequestParam String miniProgram) { return wechatService.sendMsg(request, wechatOfficialEntity, data, miniProgram); } /** * 小程序发送模板消息 * @return */ @ApiImplicitParams({ @ApiImplicitParam(name = "MiniProgramEntity", value = "小程序实体", required = true, paramType = "query", dataType = "String"), @ApiImplicitParam(name = "data", value = "发送信息参数json结构的字符串", required = true, paramType = "query", dataType = "String"), @ApiImplicitParam(name = "miniprogram", value = "小程序参数json结构字符串", required = true, paramType = "query", dataType = "String") }) @RequestMapping(value="/sendMiniProgramMessage", method = {RequestMethod.POST}) public com.cjwl.ResultData miniPSendMessage(HttpServletRequest re