1. 程式人生 > >微信模板消息發送

微信模板消息發送

href mode ssa shu from ole on() pin 申請

整個開發流程,我在“簡書” 上看到了一個完整的開發流程。

https://www.jianshu.com/p/eb0e9c4dcdfe

微信官方接口為:

https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277


自己開發中總結了一下,如下,方便後續備查:

①微信的模板消息,其實是微信公眾號上推送給用戶的一條消息記錄。

在開發的時候,需要獲取到用戶的openId(用戶關註公眾號就會產生一個唯一的openId),然後通過openId 推送給對應的用戶,這樣用戶就能收到這條消息了。

②訂閱號必須升級為服務號才能獲取“模板消息” 的接口權限,並且必須通過認證。(服務號接功能更多,但是群發消息由訂閱號的每天一條變成了一個月4條。)

③開通模板消息的時候會讓我們選擇2個行業,並且每月只能修改一次。我們可以搜索模板,直接使用通用模板。如果我們需要自定義模板,那麽就需要自己申請。

④微信公眾號的tokenId,有效期只有2個小時,所以我們需要采用定時器每一個小時50分鐘去獲取一次,同時,在程序啟動完成之後要獲取一次。

⑤系統用戶登錄的時候我們就判斷是否有openId,如果沒有,那麽就去獲取openId 。(我們OA系統是開發在微信公眾號裏面的,所以用戶登錄系統前就已經關註了公眾號)

登錄接口調用成功後,調用首頁接口,接口中判斷如果沒有openId,那麽就跳轉到A接口(微信需要的接口去獲取openId,然後存入表中),A接口最後再次請求首頁,這時已經有OpenId了,那麽直接展示首頁內容。

微信模板消息開發流程:
①微信用戶點擊登錄調用loginController中的login_login_wechat
成功後調用weixin_index

 /**訪問系統首頁·微信版
	 * @param changeMenu:切換菜單參數
	 * @return
	 */
	@RequestMapping(value="/weixin_index")
	public ModelAndView login_weixin_index(HttpServletRequest request,HttpServletResponse response){
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try{
			Session session = Jurisdiction.getSession();
			User user = (User)session.getAttribute(Const.sessionUser);				//讀取session中的用戶信息(單獨用戶信息)
			if (user != null && user.getRole()!=null && !user.getRole().getRights().trim().equals("")) {
				session.setAttribute(Const.sessionUserName, user.getUserName());				//放入用戶名到session
				this.getRemortIP(user.getUserName());	//更新登錄IP
				mv.setViewName("weixin/index/main");
				mv.addObject("user", user);
				
				/*
				 * 登錄後,如果openId為空,則保存openId到用戶表中 TODO
				 */
//				String weixinOpenId = user.getWeixinOpenId();
//				if(Tools.isEmpty(weixinOpenId)){
//					String ssString = WechatUtils.getUserAuthURL(true,
//							weixinConstant.wReturnPrefix + request.getContextPath()
//							+ "/wechatLoginNoOpenId.do","0");
//					response.sendRedirect(ssString);
//				}
				
				// 我的待辦·待審核數量
				String userId = user.getId();
				List<DBTodo> todoList  = new ArrayList<>();
				//登錄人待辦
 				
			}else {
				mv.setViewName("weixin/index/login");//session失效後跳轉登錄頁面
			}
		} catch(Exception e){
			mv.setViewName("weixin/index/login");
			logger.error(e.getMessage(), e);
		}
		
		mv.addObject("pd",pd);
		return mv;
	}



/**
	 * 微信登錄,沒有openId
	 * @return
	 */
	@RequestMapping(value="/wechatLoginNoOpenId")
	@ResponseBody
	public void wechatLoginNoOpenId(HttpServletRequest request,HttpServletResponse response){
		try {
			String code = ServletRequestUtils.getStringParameter(request, "code");
			String result = WechatUtils.getUserOpenId(code);
			if (result != null) {
				JSONObject jSONObject = JSONObject.fromObject(result);
				String openId = jSONObject.getString("openid");
				System.err.println("======================當前登錄用戶登錄微信openId:"+openId);
				User user =(User)Jurisdiction.getSession().getAttribute(Const.sessionUser);
				user.setWeixinOpenId(openId);
				userService.editU(user);
				//再次請求微信首頁,這次openId已經有值了直接進入首頁
				response.sendRedirect(weixinConstant.wReturnPrefix + request.getContextPath()
				+ "/weixin_index.do");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

web.xml 中
系統系統完成後獲取一次微信的tokenId。

  <!-- 程序啟動完成執行-->
  <!-- <servlet>  
    <servlet-name>init</servlet-name>  
    <servlet-class>com.kentra.listener.WebAppInitListener</servlet-class>  
    <load-on-startup>3</load-on-startup>
  </servlet> -->


模板消息的發送示例,請參考 InformController.java 中的goSend方法

或WechatUtils.java 中的main方法。


微信模板消息發送