總結:java
遇到的坑:ajax
①要想調用微信的模版消息,咱們就須要獲取該用戶關注的這個公衆號openId,json
要獲取openId就要獲取進入時候的code參數,code參數經過一個連接獲取。那麼咱們在進入公衆號後的時候就須要回調這個連接,而後獲取到openId。微信
這裏回調裏面有一個redirect_uri參數,這個是回調的URL編碼後的值。咱們既然要回調,就須要在公衆號管理端將回調域名設置爲咱們請求的域名(注意域名一個月只能修改三次)。ide
②在調用微信獲取code的連接時,不能是ajax請求,不然回調將沒法完成。應該是微信拒絕ajax請求。
編碼
/* * 登陸後,若是openId爲空,則保存openId到用戶表中 */ String weixinOpenId = user.getWeixinOpenId(); if(Tools.isEmpty(weixinOpenId)){ String ssString = WechatUtils.getUserAuthURL(true, weixinConstant.wReturnPrefix + request.getContextPath() + "/wechatLoginNoOpenId.do","0"); response.sendRedirect(ssString); }
③請求的參數中url爲模板消息點擊後跳轉的頁面。url
if(!Tools.isEmpty(openId)){ //將參數轉爲json String templateMsgJson = WechatUtils.wrapJsonData(openId, weixinConstant.wReturnPrefix + weixinConstant.ContextPath + "/inform/informWXView.do?id="+id, weixinConstant.template_id_inform, weixinConstant.firstData_inform, "李建波", "下午三點開會",//TODO "請及時處理", weixinConstant.remarkData); //發送模板通知 WechatUtils.sendTemplateMsg(templateMsgJson); }else{ System.err.println("----------------通知人員的openId爲空"); }