java小程序发送模板消息案例
程序员文章站
2022-04-18 13:02:16
java小程序发送模板消息
wxml:
发送模板消息
wxss:
.modelbutton{
margin-top:50%;
width: 50%;
}
js前端发送模...
java小程序发送模板消息
wxml:
wxss:
.modelbutton{ margin-top:50%; width: 50%; }
js前端发送模板消息:
注意:每个小程序的openid都是不一样的,这里使用的是我的小程序的openid,应替换为自己小程序的openid。
关于解密openid参考我的另一篇博客:https://blog.csdn.net/long_yi_1994/article/details/82977603
testsubmit: function(e) { console.log(e); var self = this; let method = 'get'; let openid = 'obk-w5fq76shfannc_8l_6xeew0m'; let _access_token = '14_z_mmrqz8f0lsoyphxhxlcy1qyqbiarasntyrchbqtknwdl3co46exwqsl8qr58yz01xme6shklgngkz1zhx0zqpnnsxjlgljdlejcr_qerpp76h-4zodzt4xtpqeaywhtnb_m-djizon3ayhzzjjafassc'; let url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/sendaccess_token=' + _access_token; let _jsondata = { access_token: _access_token, touser: 'ok_7b4mgyt6l9drxwinxjeecb_no', template_id: 'pz-qulr4atcagbacomjh09u0bava_st-lncp5jv7pcs', form_id: e.detail.formid, page: "pages/index/index", data: { "keyword1": { "value": "测试数据一", "color": "#173177" }, "keyword2": { "value": "测试数据二", "color": "#173177" }, "keyword3": { "value": "测试数据三", "color": "#173177" }, "keyword4": { "value": "测试数据四", "color": "#173177" }, "keyword5": { "value": "测试数据五", "color": "#173177" }, "keyword6": { "value": "测试数据六", "color": "#173177" } } }; wx.request({ url: "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/sendaccess_token="+_access_token, data: _jsondata, method: method, success: function(res) { console.log(res) wx.showtoast({ title: '发送成功', }) }, fail: function(err) { console.log('request fail ', err); }, complete: function(res) { console.log("request completed!"); } }) }
java代码后台发送模板消息:
后端发送模板消息需要获取前端表单提交的formid
controller:
@getmapping("/publishmodelmessage") public string publishmodelmessage(@requestparam(required = false) string formid,@requestparam(required = false) string openid,httpservletresponse response){ openid = "ok_7b4mgyt6l9drxwinxjeecb_no"; token token = commonutil.gettoken("wxe07fe139e9473040","0881d4dc47cd36cac667264238ad0bf2"); system.out.println(token.getaccesstoken()); wxmssvo wxmssvo = new wxmssvo(); wxmssvo.settemplate_id("pz-qulr4atcagbacomjh09u0bava_st-lncp5jv7pcs"); wxmssvo.settouser(openid); wxmssvo.setpage("pages/index/index"); wxmssvo.setrequest_url("https://api.weixin.qq.com/cgi-bin/message/wxopen/template/sendaccess_token=" + token.getaccesstoken()); wxmssvo.setform_id(formid); list list = new arraylist<>(); list.add(new templatedata("通知","#ffffff")); list.add(new templatedata("李银龙","#ffffff")); list.add(new templatedata("发布成功","#ffffff")); list.add(new templatedata(new date().tostring(),"#ffffff")); list.add(new templatedata("日常加班","#ffffff")); list.add(new templatedata("深圳市方寸科技服务有限公司","#ffffff")); wxmssvo.setparams(list); commonutil.sendtemplatemessage(wxmssvo); return null; }
工具类commonutil:
public class commonutil { public final static string token_url = "https://api.weixin.qq.com/cgi-bin/tokengrant_type=client_credential&appid=appid&secret=appsecret"; //获取小程序token public static token gettoken(string appid, string appsecret) { token token = null; string requesturl = token_url.replace("appid", appid).replace("appsecret", appsecret); // 发起get请求获取凭证 jsonobject jsonobject = httpsrequest(requesturl, "get", null); if (null != jsonobject) { try { token = new token(); token.setaccesstoken(jsonobject.getstring("access_token")); token.setexpiresin(jsonobject.getinteger("expires_in")); } catch (jsonexception e) { token = null; // 获取token失败 log.error("获取token失败 errcode:{} errmsg:{}", jsonobject.getinteger("errcode"), jsonobject.getstring("errmsg")); } } return token; } //发送模板消息 public static string sendtemplatemessage(wxmssvo wxmssvo) { string info = ""; try { //创建连接 url url = new url(wxmssvo.getrequest_url()); httpurlconnection connection = (httpurlconnection) url.openconnection(); connection.setdooutput(true); connection.setdoinput(true); connection.setrequestmethod("post"); connection.setusecaches(false); connection.setinstancefollowredirects(true); connection.setrequestproperty("content-type", "application/x-www-form-urlencoded"); connection.setrequestproperty("content-type", "utf-8"); connection.connect(); //post请求 dataoutputstream out = new dataoutputstream(connection.getoutputstream()); jsonobject obj = new jsonobject(); obj.put("access_token", wxmssvo.getaccess_token()); obj.put("touser", wxmssvo.gettouser()); obj.put("template_id", wxmssvo.gettemplate_id()); obj.put("form_id", wxmssvo.getform_id()); obj.put("page", wxmssvo.getpage()); jsonobject jsonobject = new jsonobject(); for (int i = 0; i < wxmssvo.getparams().size(); i++) { jsonobject datainfo = new jsonobject(); datainfo.put("value", wxmssvo.getparams().get(i).getvalue()); datainfo.put("color", wxmssvo.getparams().get(i).getcolor()); jsonobject.put("keyword" + (i + 1), datainfo); } obj.put("data", jsonobject); out.write(obj.tostring().getbytes()); out.flush(); out.close(); //读取响应 bufferedreader reader = new bufferedreader(new inputstreamreader(connection.getinputstream())); string lines; stringbuffer sb = new stringbuffer(""); while ((lines = reader.readline()) != null) { lines = new string(lines.getbytes(), "utf-8"); sb.append(lines); } info = sb.tostring(); system.out.println(sb); reader.close(); // 断开连接 connection.disconnect(); } catch (exception e) { e.printstacktrace(); } return info; } }
token类:
public class token { //接口访问凭证 private string accesstoken; //接口有效期,单位:秒 private int expiresin; public string getaccesstoken() { return accesstoken; } public void setaccesstoken(string accesstoken) { this.accesstoken = accesstoken; } public int getexpiresin() { return expiresin; } public void setexpiresin(int expiresin) { this.expiresin = expiresin; } }
模板消息请求参数封装类wxmssvo:
public class wxmssvo { private string touser; private string template_id; private string page; private string form_id; private string access_token; private string request_url; private list params = new arraylist<>(); }
模板消息详细参数封装templatedata:
public class templatedata { private string key; private string value; private string color; public templatedata(string value, string color) { this.value = value; this.color = color; } }