Practical js sdk development: card production and playback (1), sdk card production
I was busy with CanTK 2.0 development some time ago, so my blog has not been updated. CanTK 2.0 mainly enhances the development of games and rich media. Now the coding and testing are basically completed, and the documents are officially released after they are completed. There are many exciting functions in it, let's talk about it one by one when releasing it.
Recently, my colleague developed a greeting card production APP Based on CanTK. Although I was not involved in the development, I provided technical support for CanTK and GameBuilder. I think some things are interesting, write a few blogs to share with you. This greeting card APP is completely open-source and can be modified and released as needed.
CanTK (Canvas ToolKit) is an open-source game engine and APP framework. It is a powerful tool for developing HTML5 games and apps. If you like it, add stars to it on github, your support is our motivation: https://github.com/drawapp8/cantk
0. First Show the final result:
Online run: http://gamebuilder.duapp.com/apprun.php? Appid = osgames1-821423377846894
Online Editing: http://gamebuilder.duapp.com/gamebuilder.php? Appid = osgames1-821423377846894
Scan the Code:
1. Generate js sdk Configuration
There are already a lot of materials for generating js sdk configuration on the Internet. Let's make some improvements:
The Code is as follows:
<? Phpclass JSSDK {private $ appId; private $ appSecret; public function _ construct ($ appId, $ appSecret) {$ this-> appId = $ appId; $ this-> appSecret = $ appSecret;} public function getSignPackage ($ url) {$ jsapiTicket = $ this-> getJsApiTicket (); if (! $ Url) {$ url = "http: // $ _ SERVER [HTTP_HOST]". '/weixin/demo/index.html';} $ timestamp = time (); $ nonceStr = $ this-> createNonceStr (); // The Order of parameters here should be sorted by the key value ASCII code in ascending order $ string = "jsapi_ticket = $ jsapiTicket & noncestr = $ nonceStr × tamp = $ timestamp & url = $ url "; $ signature = sha1 ($ string); $ signPackage = array ("appId" => $ this-> appId, "nonceStr" => $ nonceStr, "timestamp" => $ timestamp, "url" => $ url, "signature" =>$ Signature, "rawString" =>$ string); return $ signPackage;} private function createNonceStr ($ length = 16) {$ chars = "Hangzhou "; $ str = ""; for ($ I = 0; $ I <$ length; $ I ++) {$ str. = substr ($ chars, mt_rand (0, strlen ($ chars)-1), 1) ;}return $ str ;} private function getFileName ($ name) {$ fileName = sys_get_temp_dir (). "/weixin -". $ name; Return $ fileName;} private function saveData ($ name, $ content) {$ fileName = $ this-> getFileName ($ name); $ fp = fopen ($ fileName, "wb +"); if ($ fp) {fwrite ($ fp, $ content); fclose ($ fp) ;}return ;} private function loadData ($ name, $ defaultContent) {$ fileName = $ this-> getFileName ($ name); $ content = file_get_contents ($ fileName); if ($ content) {return $ content ;} else {return $ defacontent content;} return;} Private function getJsApiTicket () {$ data = json_decode ($ this-> loadData ("jsapi_ticket.json", '{"jsapi_ticket": "", "expire_time ": 0} '); if ($ data-> expire_time <time () {$ accessToken = $ this-> getAccessToken (); $ url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket? Type = jsapi & access_token = $ accessToken "; $ res = json_decode ($ this-> httpGet ($ url); $ ticket = $ res-> ticket; if ($ ticket) {$ data-> expire_time = time () + 3600; $ data-> jsapi_ticket = $ ticket; $ this-> saveData ("jsapi_ticket.json ", json_encode ($ data) ;}} else {$ ticket = $ data-> jsapi_ticket;} return $ ticket;} private function getAccessToken () {$ data = json_decode ($ this-> loadData ("access_token.json ",' {"Access_token": "", "expire_time": 0} '); if ($ data-> expire_time <time () {$ url = "https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = $ this-> appId & secret = $ this-> appSecret "; $ res = json_decode ($ this-> httpGet ($ url); if (! Isset ($ res-> access_token) {print_r ($ res); exit;} $ access_token = $ res-> access_token; if ($ access_token) {$ data-> expire_time = time () + 3000; $ data-> access_token = $ access_token; $ this-> saveData ("access_token.json ", json_encode ($ data) ;}} else {$ access_token = $ data-> access_token;} return $ access_token;} private function httpGet ($ url) {$ curl = curl_init (); curl_setopt ($ curl, CURLOPT_RETURN TRANSFER, true); curl_setopt ($ curl, CURLOPT_TIMEOUT, 500); curl_setopt ($ curl, expires, false); curl_setopt ($ curl, expires, false); curl_setopt ($ curl, CURLOPT_URL, $ url); $ res = curl_exec ($ curl); curl_close ($ curl); return $ res ;}} session_start (); if (! Isset ($ _ REQUEST ['url']) {echo "invalid arguments"; exit;} $ appID = "xxxxx"; $ appSecret = "xxxxx "; $ url = $ _ REQUEST ['url']; $ url = base64_decode ($ url); $ jssdk = new JSSDK ($ appID, $ appSecret ); $ signPackage = $ jssdk-> getSignPackage ($ url); $ signPackage ['jsapilist'] = ['checkjsapi', 'chooseimag', 'previewimag', 'uploadimag ', 'downloadimag', 'getnetworktype']; echo json_encode ($ signPackage);?>