hxp
2025-06-09 6c3f6335c70859ded94a1ad8d218acb0ac34239c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
 
class WXFunc
{
 
    public function __construct()
    {
    }
 
    /**获取访问微信服务器api的全局token */
    public function getAccessToken($appid, $secret)
    {
        $fileName = "global-access-token.json";
        $tokenJson = json_decode(file_get_contents($fileName), true);
 
        if ($tokenJson) {
            $access_token = $tokenJson["access_token"];
            $expires = $tokenJson["expires"];
            if ($access_token and $expires > 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;
    }
}