微信小程序发送消息
andy最近在做小程序相关的项目,目前有一种业务需要给客户推送相关业务消息。研究多天后,终于拨开迷雾,柳暗花明,特此提醒采坑的小伙伴注意了。
- 1.获取access_token
- 2.前端构建formId
- 3.前端获取code
- 4.前端发送formId、code(获取openId对应的code)到后台服务器
- 5.后端构建请求参数,发送模板消息
1.获取access_token
-微信公众号与微信小程序获取access_token的地址是一致的。
-请求地址
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
-参数说明
参数1:grant_type--获取access_token填写client_credential参数2:appid--当前小程序申请的ID参数3:secret--当前小程序ID对应的秘钥返回参数:{"access_token":"ACCESS_TOKEN","expires_in":7200}PS:ACCESS_TOKEN需要2小时之内刷新,保证其有效性。
2.前端构建formId
-前端将form通过form表单将formId以及获取openId的参数封装,调用后台服务器。
-form表单
<form bindsubmit="formSubmit" report-submit="true"> <button formType="submit" class='btn'> <view class="vv"> fff </view> </button></form>
-获取formId方法:
submitInfo: function (e) {console.log(e.detail.formId);}ps:这个e.detail.fromId,就是formid,真机才会产生,模拟器中为'the formId is a mock one'</form>
3.前端获取code
-调用接口获取登录凭证(code)进而换取用户登录态信息。用户允许登录后,回调内容会带上 code(有效期五分钟),开发者需要将 code 发送到开发者服务器后台,使用code 换取 session_key api,将 code 换成 openid 和 session_key
-获取登录凭证code
onLaunch: function() { wx.login({ success: function(res) { if (res.code) { //发起网络请求 wx.request({ url: 'https://test.com/', data: { code: res.code } }) } else { console.log('获取用户登录态失败!' + res.errMsg) } } }); }
4.前端发送formId、code(获取openId对应的code)到后台服务器
-将formId、code传递到后台服务端,调用后台发送消息接口
-获取登录凭证code
sendMsg: function(code,formId) { if (res.code) { //发起网络请求 wx.request({ url: 'https://test.com/sendMsg', data: { code: code, formId:formId } }) } else { console.log('发送消息失败' + res.errMsg) } } });
5.后端构建请求参数,发送模板消息
-后台服务接受到前端请求后,获取openId,构建参数,生成消息,调用微信消息接口
-1.后台接受前端接口代码
/** * 向客户推送消息 * @param param * @return */ @RequestMapping(value ="/sendMessageToUser", method = RequestMethod.POST) @ResponseBody public ResultDto sendMessage(@RequestBody Map<String,String> map) { log.debug("sendMessageToUser----"+map); ResultDto result= new ResultDto(); Map<String, Object> data1= new HashMap<String, Object>(); String formId=map.get("formId"); String code=map.get("code"); if(StringUtils.isEmpty(formId)||StringUtils.isEmpty(code)){ result.setResultCode("-2"); result.setResultMsg("暂无记录"); return result; } String appId="wx121cfbfc0"; //获取token String token=wxMiniService.getAccessToken(appId); //获取openId String openId = WxUtil.getWxOpenId(code); String messageUrl="https://api.weixin.qq.com/cgi bin/message/wxopen/template/send?access_token=%s"; log.debug("weixin URl----"+messageUrl); String url = String.format(messageUrl,token); JSONObject param = new JSONObject(); param.put("touser", openId); param.put("template_id", "YlncmgonQQu6GiZOPgQRDI"); //param.put("page", ""); param.put("form_id",formId); param.put("emphasis_keyword", "keyword1.DATA"); JSONObject keyword1 = new JSONObject(); keyword1.put("value", "102038475556272"); JSONObject keyword2 = new JSONObject(); keyword2.put("value", "南xxx山"); JSONObject keyword3 = new JSONObject(); keyword3.put("value", "xxxxx"); JSONObject keyword4 = new JSONObject(); keyword4.put("value", "2018-08-10 18:34:21"); JSONObject keyword5 = new JSONObject(); keyword5.put("value", "请及时通xxxx"); JSONObject data = new JSONObject(); data.put("keyword1", keyword1); data.put("keyword2", keyword2); data.put("keyword3", keyword3); data.put("keyword4", keyword4); data.put("keyword5", keyword5); param.put("data", data); log.debug("sendMessage weixin----"+param); String rep=HttpUtils.httpPost(url, param); if(!StringUtils.isEmpty(rep)){ data1.put("msg", JSON.parse(rep)); result.setData(data1); } log.info("sendMessage:"+rep); return result; }
结语
-写到这里所有的流程应该都完了,上述方法里面涉及到一些公共方法,鉴于时间关系我就不贴了,如果各位如果有什么问题,可以私信或者加我微信(739659553)
-PS:追加一张服务通知的截图