前言:
在我们购买商品或其他操作的时候,这时候微信公众号会推送相关模版消息。接下来简单介绍开发流程:(本文以订单推送为例)
首先在测试号新建模版消息
格式如下:
java" id="highlighter_619565">
1
2
3
4
5
6
|
{{first.data}}
用户名:{{keyword1.data}}
订单号:{{keyword2.data}}
订单金额:{{keyword3.data}}
商品信息:{{keyword4.data}}
{{remark.data}}
|
这里会生成一个模版id,后面会用到
然后后台在上传订单接口,上传成功后调用发送模版消息,代码实现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public void sendordertemplatemessage(order order) {
augewechatuser wechatuser = augewechatusermapper.selectbyphone(order.getphone());
string goodsinfo = "" ;
for (augesaleitem augesaleitem : order.getdata()) {
goodsinfo += augesaleitem.getitemname() + "×" + augesaleitem.getitemnum() + "\n\t\t\t" ;
}
datainfo first = new datainfo( "恭喜你购买成功!" , "#ff0000" );
datainfo keyword1 = new datainfo(wechatuser.getnickname(), "#ff0000" );
datainfo keyword2 = new datainfo(order.getordernumber(), "#ff0000" );
datainfo keyword3 = new datainfo( "¥" + order.getsumprice() + "元" , "#ff0000" );
datainfo keyword4 = new datainfo(goodsinfo, "#ff0000" );
datainfo remark = new datainfo( "欢迎再次购买!" , "#000000" );
orderdata orderdata = new orderdata(first, keyword1, keyword2, keyword3, keyword4, remark);
ordertemplatemessage templatemessage = new ordertemplatemessage();
templatemessage.settouser(wechatuser.getid());
templatemessage.settemplate_id(ordertemplateid); //模版id
templatemessage.setdata(orderdata);
okhttputil.getinstance().dopost(wechatconstant.gettemplateurl(wechataccesstokenservice.takeaccesstoken()),
json.tojsonstring(templatemessage));
}
|
简单介绍 okhttputil
okhttp是一个第三方类库,用于android中请求网络。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public string dopost(string url, string param) {
mediatype );
requestbody body = requestbody.create(json, param);
request request = new request.builder()
.url(url)
.post(body)
.build();
call call = okhttpclient.newcall(request);
try {
response response = call.execute();
return response.body().string();
} catch (ioexception e) {
e.printstacktrace();
}
return null ;
}
|
注意配置订单模版id
1
2
|
@value ( "#{setting.ordertemplateid}" )
private string ordertemplateid;
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/PreachChen/p/8652404.html