微信公衆號開發

1.獲取調用微信API的tokennode

api:https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credentialsql

參數:app_idapi

         app_secret服務器

(參數在微信官方管理平臺能夠查閱)微信

返回:access_token。app

2.JSSDK系列微信公衆平臺

2.1微信上傳圖片以後,把數據存儲在微信服務器。異步

wx.chooseImage({
    count: 1, // 默認9
        // sizeType: ['original', 'compressed'], 
        // 能夠指定是原圖仍是壓縮圖,默認兩者都有
        // sourceType: ['album', 'camera'], 
        // 能夠指定來源是相冊仍是相機,默認兩者都有
    isShowProgressTips: 1, // 默認爲1,顯示進度提示
    sizeType: ['compressed'],
    // 能夠指定是原圖仍是壓縮圖,默認兩者都有
    sourceType: ['camera'], 
    // 能夠指定來源是相冊仍是相機,默認兩者都有
    success: function(res) {
      var localIds = res.localIds;
      // 返回選定照片的本地ID列表,localId能夠做爲img標籤的src屬性顯示圖片
      wx.uploadImage({
         localId: localIds[0], 
         // 須要上傳的圖片的本地ID,由chooseImage接口得到
         isShowProgressTips: 1, // 默認爲1,顯示進度提示
         success: function(res) {
             //alert("upload succ!");
             var serverId = res.serverId; 
             // 返回圖片的服務器端ID
             //alert("serverId"+serverId);
             uploadWXImgId(mediatype, serverId);
        },
           fail: function(err) {}
       });
    },
    error: function() {},
    fail: function(err) {}
});
function uploadWXImgId(mediatype, serverId){
  //追蹤上傳圖片的進度step1,上傳到微信成功
}

2.2 經過獲取臨時素材接口,把圖片獲取到指定服務器url

 先指定下載到本地服務器:spa

conf.oss.local_weixin_upload_folder = "/mydata/oss/weixin_upload_img"; 
//服務器上存儲weixin上傳的圖片
//folder = conf.oss.local_weixin_upload_folder ;
//bucketFolder =  
conf.oss.bucket_folder_new || "weixin_photos"; //weixin_photos
//targetFolder = year + "/" + month + "/" + day; 2017/05/24
var filePath = path.resolve(folder, bucketFolder, targetFolder, mediaId + ".jpg");

下載到本地服務器以後,異步記錄日誌:

var sql_step2 = sqlTemplate["upload_imgs"]["step2"];
var step2_param = {
    download_folder: fileFolder,
    id: id
}
if (err) {
    step2_param.download_err = err;
        step2_param.download_result = -1;
} else {
    step2_param.download_result = 1;
} 
        //異步插入日誌
    db.query(null, sql_step2, step2_param).catch(function(err) {
    console.log("記錄下載結果出錯", err);
});

若是上傳到tencent雲,則應該是這樣:

//從tencent雲獲取簽名的參數
qcloud.conf.setAppInfo(conf_oss.appId, 
                                 conf_oss.secretId,
                               conf_oss.secretKey);
//filepath,本地須要上傳到騰訊雲的路徑,
//conf_oss.bucket 對象儲存控制檯建立的Bucket
//target_path 上傳到騰訊雲的訪問url
return new RSVP.Promise(function(resolve, reject) {
     qcloud.cos.upload(filePath, conf_oss.bucket, target_path, 
     function(ret) {}
});

須要參照nodejs第三方庫:qcloud_cos

3.微信公衆號被關注以後,觸發事件

須要在微信公衆平臺進行服務器配置,以下:

配置好服務器地址以後,就能夠寫業務邏輯,用戶在關注與取消公衆號時,微信會推事件給開發者,參數以下解析:

var xml = req.body.xml || {};
var scene_id = xml.eventkey && xml.eventkey[0];
var openid = xml.fromusername && xml.fromusername[0];
var event = xml.event && xml.event[0].toLocaleLowerCase();

返回給客戶端的消息寫須要必定的格式,以下:

function getMsgContent(xml, text) {
    var result;
    result = "<xml>";
    result += "<ToUserName><![CDATA[" + xml.fromusername[0] + "]]></ToUserName>";
    result += "<FromUserName><![CDATA[" + xml.tousername[0] + "]]></FromUserName>";
    result += "<CreateTime>" + xml.createtime[0] + "</CreateTime>";
    result += "<MsgType><![CDATA[text]]></MsgType>";
    result += "<Content><![CDATA[" + text + "]]></Content>";
    result += "</xml>";
    console.log("return content is:" + result);
    return result;
}
相關文章
相關標籤/搜索