邏輯流程是,在Force.com 裏Campaign 的 創建一個記錄,生成帶參數的二維碼參數是Campaign ID
新建一個對象WeChatMsgTemplate用來存儲推送消息的模板,並創建LookUp的關係到Campaign
掃描二維碼自動查詢到Campaign 對應的WeChatMsgTemplate 推送多條信息。
首先 生成二維碼文檔鏈接。 生成二維碼首先要有Token 生成Token文檔鏈接
如果編程中遇到 Http請求沒有發送成功 檢查Remote Site Settings
以下是 生成Token的代碼
private static string appId = 'xxx';
private static String app_secret = 'xxxx';
public static String GetToken(){
string url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET';
url = url.replace('APPID', appId).replace('APPSECRET', app_secret);
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();
req.setEndpoint(url);
req.setMethod('GET');
res = http.send(req);
ReturedToken result = (ReturedToken) Json.deserialize(res.getBody(), ReturedToken.class);
return result.access_token;
}
ReturedToken 對象是 通過Json工具自動創建的
填寫Json 自動生成類 和測試類http://json2apex.herokuapp.com/
生成Ticket 代碼
public static String GetTicket(String Token,String scene_str){
string url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKEN';
url = url.replace('TOKEN', Token);
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();
req.setEndpoint(url);
//system.debug('url:' + url);
req.setMethod('POST');
//req.setHeader('Content-Type', 'text/xml');
req.setBody('{"action_name": "QR_LIMIT_STR_SCENE", "action_info": {"scene": {"scene_str": " ' + scene_str + ' "}}}');
res = http.send(req);
ReturedTicket TK = (ReturedTicket) Json.deserialize(res.getBody(), ReturedTicket.class);
return TK.Ticket;
}
最後生成二維碼
public static String CreateQRcode (String scence){
String token = GetToken();
String url = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=TICKET';
String Ticket = GetTicket(token, scence);
url = url.replace('TICKET', Ticket);
return url;
}
每一次掃描二維碼 都會發送一個POST的請求到接口信息配置的URL裏
這裏創建先一個global 類
@RestResource(urlMapping='/WeChat/*')
global with sharing class WeChatWebservice{
@HttpGet
global static void doGet(){
String signature = RestContext.request.params.get('signature');
//String msg_signature = RestContext.request.params.get('msg_signature');
String timestamp = RestContext.request.params.get('timestamp');
String nonce = RestContext.request.params.get('nonce');
String echostr = RestContext.request.params.get('echostr');
System.debug('signature: ' + signature + '==========');
//System.debug('msg_signature: ' + msg_signature + '==========');
System.debug('timestamp: ' + timestamp + '==========');
System.debug('nonce: ' + nonce + '==========');
System.debug('echostr: ' + echostr + '==========');
RestContext.response.addHeader('Content-Type', 'text/plain');
RestContext.response.responseBody = Blob.valueOf(echostr);
}
@HttpPost
global static void doPost(){
RestRequest req = RestContext.request;
String msg = req.requestBody.toString();
//system.debug('msg: ' + msg);
WeChatRequest wReq = new WeChatRequest(msg);
//system.debug('wReq: ' + wReq);
if(wReq.getEventType() == 'SCAN') {
String CampaignID = wReq.getClickKey().trim();
String UserName = wReq.getFromUser().trim();
WeChatGetToken.WeChatMsg(CampaignID, UserName);
}
if(wReq.getEventType() == 'subscribe') {
//字符串截取
String temp = wReq.getClickKey().substringAfter('qrscene_');
system.debug('temp:' + temp);
String CampaignID = temp.trim();
String UserName = wReq.getFromUser().trim();
WeChatGetToken.WeChatMsg(CampaignID, UserName);
}
}
}
接下來創建站點
微信端的URL 配置
https://wechattony-developer-edition.ap2.force.com/services/apexrest/WeChat
站點名稱 + /services/apexrest/ + global 的方法名稱
發送消息到微信的方法
public static void WeChatMsg(String CampID, String OpenID){
Campaign camp = new Campaign();
camp = [SELECT name,(SELECT id,title__c, description__c, url__c, picurl__c from WeChatMsgTemplate__r) from Campaign where id =: CampID.trim()];
//system.debug('WeChatMsgTemplate__r:'+ camp.WeChatMsgTemplate__r);
if(camp != null){
List<WeChatMsgTemplate__c> WMsg = new List<WeChatMsgTemplate__c>();
WMsg = camp.WeChatMsgTemplate__r;
//system.debug('WMsg:'+ WMsg);
WeChatMsgTemplate temp = new WeChatMsgTemplate();
//List<WeChatMsgTemplate.Articles> artList = new List<WeChatMsgTemplate.Articles>();
for(WeChatMsgTemplate__c m :WMsg){
WeChatMsgTemplate.Articles art = new WeChatMsgTemplate.Articles();
art.title = m.title__c;
art.description = m.description__c;
art.url = m.url__c;
art.picurl = m.picurl__c;
temp.News.articles.add(art);
//system.debug('art:'+ art);
}
temp.touser = OpenID;
temp.msgtype = 'news';
String CampJson = JSON.serialize(temp);
//system.debug('CampJson'+ CampJson);
String Token = GetToken();
string url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN';
url = url.replace('ACCESS_TOKEN', Token);
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();
req.setEndpoint(url);
req.setMethod('POST');
//String JSONString = JSON.serialize(ul);
//system.debug('JSONString:' + JSONString);
req.setBody(CampJson);
res = http.send(req);
}
}
WeChatRequest 這個不是我寫的方法 就不公開了 各位就發揮自己的想象去接受請求 處理請求就好了
寫了這麼多 希望對大家有幫助。如果發現有的類不存在 那就是通過工具 複製json對象創建的類 上面有介紹
有問題可以在下面留言