time()) { //\Logging\LogInfo("token正常,直接使用! " . $access_token); return $access_token; } else { return $this->refreshAccessToken($fileName, $appid, $secret); } } else { return $this->refreshAccessToken($fileName, $appid, $secret); } } /**刷新token */ public function refreshAccessToken($fileName, $appid, $secret) { // GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET $url = "https://api.weixin.qq.com/cgi-bin/token"; $get = ['grant_type' => 'client_credential', 'appid' => $appid, 'secret' => $secret]; $ret = \CommFunc\curl_get($url, $get); $ret = json_decode($ret, true); if ($ret['errcode'] != 0) { \Logging\LogError("request token error: " . print_r($ret, true)); return ""; } $access_token = $ret['access_token']; $expires_in = $ret['expires_in']; // 凭证有效时间,单位:秒。目前是7200秒之内的值。 $data = ['access_token' => $access_token, 'expires' => $expires_in + time()]; file_put_contents($fileName, json_encode($data)); \Logging\LogInfo("刷新token: " . print_r($ret, true)); \Logging\LogInfo("保存token: " . print_r($data, true)); return $access_token; } /**删除已经上报到微信的key-value数据 */ public function removeUserStorage($appid, $secret, $openid, $sessionKey, $removeKeyList) { $token = $this->getAccessToken($appid, $secret); if (!$token) { return; } if (!count($removeKeyList)) { Logging\LogInfo("removeUserStorage 没有指定要移除的key "); return; } $postData = ["key" => $removeKeyList]; // storage.removeUserStorage // 参考: https://developers.weixin.qq.com/minigame/dev/api-backend/open-api/data/storage.removeUserStorage.html // POST https://api.weixin.qq.com/wxa/remove_user_storage?access_token=ACCESS_TOKEN&signature=SIGNATURE&openid=OPENID&sig_method=SIG_METHOD // 其他参数get,修改的kv数据才post Logging\LogInfo("removeUserStorage: " . print_r($postData, true)); Logging\LogInfo("openid: " . $openid . " sessionKey: " . $sessionKey); $signature = hash_hmac('sha256', json_encode($postData), $sessionKey); Logging\LogInfo("signature: " . $signature); $url = "https://api.weixin.qq.com/wxa/remove_user_storage?access_token=" . $token . "&signature=" . $signature . "&openid=" . $openid . "&sig_method=hmac_sha256"; Logging\LogInfo("url: " . $url); Logging\LogInfo("post data json_encode : " . json_encode($postData)); $ret = CommFunc\curl_post($url, $postData); Logging\LogInfo("ret: " . $ret); return $ret; } /**设置互动数据值*/ public function setUserInteractiveData($appid, $secret, $openid, $sessionKey, $kv_list) { $token = $this->getAccessToken($appid, $secret); if (!$token) { return; } if (!count($kv_list)) { Logging\LogInfo("setUserInteractiveData 没有指定要设置的 kv_list"); return; } $postData = ["kv_list" => $kv_list]; // storage.setUserInteractiveData // 参考: https://developers.weixin.qq.com/minigame/dev/api-backend/open-api/data/storage.setUserInteractiveData.html // POST https://api.weixin.qq.com/wxa/setuserinteractivedata?access_token=ACCESS_TOKEN&signature=SIGNATURE&openid=OPENID&sig_method=SIG_METHOD // 其他参数get,修改的kv数据才post Logging\LogInfo("setUserInteractiveData: " . print_r($postData, true)); Logging\LogInfo("openid: " . $openid . " sessionKey: " . $sessionKey); $signature = hash_hmac('sha256', json_encode($postData), $sessionKey); Logging\LogInfo("signature: " . $signature); $url = "https://api.weixin.qq.com/wxa/setuserinteractivedata?access_token=" . $token . "&signature=" . $signature . "&openid=" . $openid . "&sig_method=hmac_sha256"; Logging\LogInfo("url: " . $url); Logging\LogInfo("post data json_encode : " . json_encode($postData)); $ret = CommFunc\curl_post($url, $postData); Logging\LogInfo("ret: " . $ret); return $ret; } }