@@ -13,6 +13,7 @@ class MiniProgram
13
13
private $ appId = '' ;
14
14
private $ appSecret = '' ;
15
15
private $ accessToken = '' ;
16
+ private $ defaultParamInfo = array ();
16
17
public $ accessTokenData = array ();
17
18
18
19
const API_HOST = 'https://api.weixin.qq.com ' ;
@@ -38,6 +39,7 @@ public function __construct($appId, $appSecret, $accessToken = '')
38
39
$ this ->accessTokenData = $ this ->getAccessTokenData ();
39
40
$ this ->accessTokenData && $ this ->accessToken = $ this ->accessTokenData ['access_token ' ];
40
41
}
42
+ $ this ->defaultParamInfo = include_once 'DefaultParamInfo.php ' ;
41
43
}
42
44
43
45
/**
@@ -85,17 +87,20 @@ public function jscode2Session($code)
85
87
*/
86
88
public function decryptData ($ paramArr = array ())
87
89
{
90
+ // 扩展原参数数组
91
+ $ finalParamArr = Common::extendArrayData ($ this ->defaultParamInfo [__FUNCTION__ ], $ paramArr );
92
+
88
93
// 初始化返回数据
89
94
$ res ['code ' ] = -100 ;
90
95
$ res ['msg ' ] = '操作失败 ' ;
91
96
$ res ['data ' ] = array ();
92
97
93
98
// 1.获取openid、session_key(若存在session_key,则默认理解为session_key未过期,直接使用其进行解密)
94
- if (isset ( $ paramArr [ ' sessionKey ' ]) && $ paramArr ['sessionKey ' ]) {
95
- $ openId = isset ( $ paramArr ['openId ' ]) ? $ paramArr [ ' openId ' ] : '' ;
96
- $ sessionKey = $ paramArr ['sessionKey ' ];
99
+ if ($ finalParamArr ['sessionKey ' ]) {
100
+ $ openId = $ finalParamArr ['openId ' ];
101
+ $ sessionKey = $ finalParamArr ['sessionKey ' ];
97
102
} else {
98
- $ sessionData = $ this ->jscode2Session ($ paramArr ['code ' ]);
103
+ $ sessionData = $ this ->jscode2Session ($ finalParamArr ['code ' ]);
99
104
if (isset ($ sessionData ['errcode ' ])) {
100
105
$ res ['code ' ] = -101 ;
101
106
$ res ['msg ' ] = Common::getErrorMsg ($ sessionData ['errcode ' ]);
@@ -106,8 +111,8 @@ public function decryptData($paramArr = array())
106
111
}
107
112
108
113
// 2.计算签名并与传入签名进行校验
109
- $ newSignature = sha1 ($ paramArr ['rawData ' ] . $ sessionKey );
110
- if ($ newSignature !== $ paramArr ['signature ' ]) {
114
+ $ newSignature = sha1 ($ finalParamArr ['rawData ' ] . $ sessionKey );
115
+ if ($ newSignature !== $ finalParamArr ['signature ' ]) {
111
116
$ res ['code ' ] = -102 ;
112
117
$ res ['msg ' ] = '签名不匹配 ' ;
113
118
return $ res ;
@@ -116,7 +121,7 @@ public function decryptData($paramArr = array())
116
121
// 3.使用sessionKey解密加密数据包
117
122
include_once "wxBizDataCrypt/wxBizDataCrypt.php " ;
118
123
$ pc = new \WXBizDataCrypt ($ this ->appId , $ sessionKey );
119
- $ errCode = $ pc ->decryptData ($ paramArr ['encryptedData ' ], $ paramArr ['iv ' ], $ data );
124
+ $ errCode = $ pc ->decryptData ($ finalParamArr ['encryptedData ' ], $ finalParamArr ['iv ' ], $ data );
120
125
if (!empty ($ errCode )) {
121
126
$ res ['code ' ] = -103 ;
122
127
$ res ['msg ' ] = Common::getErrorMsg ($ errCode );
@@ -125,7 +130,7 @@ public function decryptData($paramArr = array())
125
130
126
131
// 4.生成3rd_session
127
132
$ data = json_decode ($ data , true );
128
- $ session3rd = Common::getNonce ();
133
+ $ session3rd = Common::isWin () === true ? Common:: getNonce () : Common:: getNonceByURandom ();
129
134
130
135
// 5.返回相关数据
131
136
$ res ['code ' ] = 100 ;
@@ -144,11 +149,12 @@ public function decryptData($paramArr = array())
144
149
*/
145
150
public function createWXAQRCode ($ postParamArr = array ())
146
151
{
152
+ $ finalParamArr = Common::extendArrayData ($ this ->defaultParamInfo [__FUNCTION__ ], $ postParamArr );
147
153
$ urlParamArr = array (
148
154
'access_token ' =>$ this ->accessToken
149
155
);
150
156
$ url = self ::API_HOST . self ::WXAQRCODE_PATH . http_build_query ($ urlParamArr );
151
- $ res = Common::httpRequest ($ url , json_encode ($ postParamArr ));
157
+ $ res = Common::httpRequest ($ url , json_encode ($ finalParamArr ));
152
158
return $ res ;
153
159
}
154
160
@@ -159,11 +165,12 @@ public function createWXAQRCode($postParamArr = array())
159
165
*/
160
166
public function getWXACode ($ postParamArr = array ())
161
167
{
168
+ $ finalParamArr = Common::extendArrayData ($ this ->defaultParamInfo [__FUNCTION__ ], $ postParamArr );
162
169
$ urlParamArr = array (
163
170
'access_token ' =>$ this ->accessToken
164
171
);
165
172
$ url = self ::API_HOST . self ::WXACODE_PATH . http_build_query ($ urlParamArr );
166
- $ res = Common::httpRequest ($ url , json_encode ($ postParamArr ));
173
+ $ res = Common::httpRequest ($ url , json_encode ($ finalParamArr ));
167
174
return $ res ;
168
175
}
169
176
@@ -174,11 +181,12 @@ public function getWXACode($postParamArr = array())
174
181
*/
175
182
public function getWxacodeUnlimit ($ postParamArr = array ())
176
183
{
184
+ $ finalParamArr = Common::extendArrayData ($ this ->defaultParamInfo [__FUNCTION__ ], $ postParamArr );
177
185
$ urlParamArr = array (
178
186
'access_token ' =>$ this ->accessToken
179
187
);
180
188
$ url = self ::API_HOST . self ::WXACODE_UNLIMIT_PATH . http_build_query ($ urlParamArr );
181
- $ res = Common::httpRequest ($ url , json_encode ($ postParamArr ));
189
+ $ res = Common::httpRequest ($ url , json_encode ($ finalParamArr ));
182
190
return $ res ;
183
191
}
184
192
}
0 commit comments