1.微信å
¬ä¼å·å¼åä¹å¦ä½ä½¿ç¨JSSDK
2.如何开发一款JSSDK
3.å¦ä½å¨Reactä¸è°ç¨å¾®ä¿¡çjsSDK
å¾®ä¿¡å ¬ä¼å·å¼åä¹å¦ä½ä½¿ç¨JSSDK
微信å¼å交æµç¾¤ï¼æ¬¢è¿çè¨ã转åãæèµ
ç³»åæç« åèå°å æéå¼åå¾®ä¿¡å ¬ä¼å·
项ç®æºç åèå°å ç¹æç¹æ--欢è¿Start
æå¡å·ã订é å·å¯ä»¥éè¿ç»å½ å¾®ä¿¡å ¬ä¼å¹³å° æ¥ç å¼å>æ¥å£æé
使ç¨JSSDK主è¦å æ¬
1ãå¤æå½å客æ·ç«¯çæ¬æ¯å¦æ¯ææå®JSæ¥å£ã
2ãå享æ¥å£ï¼å¾®ä¿¡è®¤è¯ï¼
3ãå¾åæ¥å£
4ãé³é¢æ¥å£
5ãæºè½æ¥å£ï¼è¯å«è¯é³å¹¶è¿åç»æï¼
6ã设å¤ä¿¡æ¯ï¼è·åç½ç»ç¶æï¼
7ãå°çä½ç½®
8ãçé¢æä½
9ã微信æ«ä¸æ«
ã微信å°åºï¼æå¡å·å¿ é¡»éè¿å¾®ä¿¡è®¤è¯ï¼
ã微信å¡å¸ ï¼å¾®ä¿¡è®¤è¯ï¼
ã微信æ¯ä»ï¼æå¡å·å¿ é¡»éè¿å¾®ä¿¡è®¤è¯ï¼
å®æ¹åèææ¡£
æ¥éª¤ä¸ï¼ç»å®åå
å ç»å½å¾®ä¿¡å ¬ä¼å¹³å°è¿å ¥ å ¬ä¼å·è®¾ç½® ç åè½è®¾ç½® éå¡«å JSæ¥å£å®å ¨åå ã
æ¥éª¤äºï¼å¼å ¥JSæ件
å¨éè¦è°ç¨JSæ¥å£ç页é¢å¼å ¥å¦ä¸JSæ件ï¼ï¼æ¯æW',例源apk源码制作 accessTokenUrlin.qq.com/cgi-bin/token', ticketUrl:'com/cgi-bin/ticket/getticket',
}
exports.sign = function (url,callback) { var noncestr = config.noncestr,
timestamp = Math.floor(Date.now()/), //精确å°ç§
...
request(config.accessTokenUrl + '?grant_type=' + config.grant_type + '&appid=' + config.appid + '&secret=' + config.secret ,function(error, response, body){ if (!error && response.statusCode == ) { var tokenMap = JSON.parse(body);
request(config.ticketUrl + '?access_token=' + tokenMap.access_token + '&type=jsapi', function(error, resp, json){ if (!error && response.statusCode == ) { var ticketMap = JSON.parse(json);
cache.put('ticket',ticketMap.ticket,config.cache_duration); //å å ¥ç¼å
callback({ noncestr:noncestr, timestamp:timestamp, url:url, jsapi_ticket:ticketMap.ticket, signature:sha1('jsapi_ticket=' + ticketMap.ticket + '&noncestr=' + noncestr + '×tamp=' + timestamp + '&url=' + url)
});
}
})
}
})
}
}
è¿éçæµç¨åæ们åææè¿°ç并æ äºè´ãé¦å æ¯éè¿appIdåsecretè·å¾è°ç¨è·åjsapi_ticketçaccess_tokenï¼ç¶åéè¿è¯¥access_tokenè·å¾æ们ç¾åéè¦ç¨å°çjsapi_ticketãnoncestræ们æ¯æåé便填å好çãtimestampçç®æ³ä¹æ¯è¾ç®åãæåå°±æ¯éè¿sha1è¿ä¸ªåºæä¾çæ¹æ³ï¼å°jsapi_ticketï¼noncestrï¼timestampå页é¢urlè¿è¡sha1ç¾åï¼ç¶åå°ä»¥ä¸è¿äºä¿¡æ¯éè¿callbackè¿åç»ä¸å±è°ç¨å½æ°ã
é£ä¹æ们å¾ä¸çä¸æ们çä¸å±è°ç¨å½æ°ãå ¶å®å°±æ¯æ们çexpressè·¯ç±:
app.get("/api/signature", function(req,res) { const url = req.query.url.split('#')[0];signature.sign(url,function(signatureMap){
signatureMap.appId = wechat_cfg.appid;
res.send(signatureMap);
});
})
æ ¹æ®å¾®ä¿¡å¼åææ¡£éæ±ï¼æ们é¦å éè¦å°ä¼ è¿æ¥çurlçéç¹åé¢çæ°æ®ç»å»æï¼ä¿çåé¢çææé¨åãç¶åå°±æ¯è°ç¨ä¸é¢çsignæ¹æ³æ¥çæç¾åãä¸é¢çç¾åæ¹æ³æåä¼ è¿æ¥çjsonæ°æ®å°±æ¯è¿éçsignatureMapãæ们æç»ä¼å°è¿äºæ°æ®åéåreact客æ·ç«¯ã
åæ¶ï¼éè¿ä¸é¢çwx.configç示ä¾ï¼æ们ç¥éè¿éè¦ç¨å°å¾®ä¿¡å ¬ä¼å·çappIdãæ以è¿éä¸å¹¶å°å ¶æ¾å°signatureMapä¸è¿è¡è¿åã
é£ä¹å°æ¤ä¸ºæ¢ï¼react客æ·ç«¯è°ç¨æå¡ç«¯ç"/api/signature"è¿åçæ°æ®ç¤ºä¾å¦ä¸:
{noncestr: 'Wm3WZYTPz0wzccnW',
timestamp: ,
url: 'com/?code=kGsdxcm7F1PAFfUudkGsdr&state=',
jsapi_ticket: 'sM4AOVdWfPE4DxkXGEs8VBqyVbs-TKGYp4d_ZSQa0Q5WvvMUPNQ6XGpyEcgKOD_xID_GrMCaalSmIF9JbrGaOg',
signature: 'ffaf4b9eb0dfcfefe3daae3c'
}
3. 客æ·ç«¯è·åç¾åä¿¡æ¯3.1 è·åç¾åä¿¡æ¯å¹¶æ³¨å ¥jssdk
åä¹åçè·å微信ç¨æ·ä¿¡æ¯ä¸æ ·ï¼æ们è¿éä¼å»ºç«ä¸ä¸ªæ°çSourceæ件WechatSdkSource.jsæ¥è°ç¨è¿ç¨æå¡å¨ç"/api/signature"æ¥å£:
var WechatSdkSource = {fetchSignatureMap() { return {
remote(state,url) { return co(function *() { let signatureMap = null; const getSignatureMapUrl = `/api/signature`; try { let result = yield request.get(getSignatureMapUrl).query({ url:url});
signatureMap = result.body;
} catch (e) {
signatureMap = null;
} //console.log("userInfo:", userInfo);
return signatureMap;
});
},
local() { // Never check locally, always fetch remotely.
return null;
}, success: WechatSdkActions.updateSignatureMap, error: WechatSdkActions.getSignatureMapFailed, loading: WechatSdkActions.getSignatureMap,
}
}
};
è¿éä¼ è¿æ¥çurlç±ä¸é¢å°è¦è°åçä¸å±å½æ°æçæãæ´ä¸ªæµç¨å°±æ²¡æä»ä¹å¥½è¯´çäºï¼è¯´ç½äºå°±æ¯éè¿ç¸åºçåºåéä¸ä¸ªå¸¦æurlçqueryåæ°ç请æ±å°æå¡å¨ç«¯æ¥è¯·æ±ç¾åä¿¡æ¯ï¼ç¸ä¿¡æè·çè¿ä¸ªç³»åæç« çæåé½æ¯å¾æ¸ æ¥çäºãæç»è¯·æ±æåè¿åçæ¶åå°±ä¼è°ç¨WechatSdkActionsçupdateSignatureMapè¿ä¸ªActionã
var alt = require('../alt');module.exports = alt.generateActions( 'updateSignatureMap', 'getSignatureMap', 'getSignatureMapFailed',);
èè¿ä¸ªactionå°±ä¼è§¦åæçå¬çWechatSdkStoreçonUpdateSignatureMapè¿ä¸ªåè°ãclass WechatSdkStore { constructor() { this.signatureMap = []; this.errorMessage = null; this.ready = false; this.bindActions(WechatSdkActions); this.exportAsync(WechatSdkSource);