Skip to content

Commit 5641b71

Browse files
1. 重构部分代码
1 parent 88b4dd8 commit 5641b71

2 files changed

Lines changed: 31 additions & 55 deletions

File tree

src/Common.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,34 @@ public static function getNonce($type = 1, $length = 16)
5757
}
5858
return $nonce;
5959
}
60+
61+
/**
62+
* 根据错误码获取错误信息
63+
* @param $errorCode
64+
* @return string
65+
*/
66+
public static function getErrorMsg($errorCode)
67+
{
68+
switch ($errorCode) {
69+
case '40029':
70+
$errorMsg = 'code 非法';
71+
break;
72+
case '-41001':
73+
$errorMsg = 'sessionKey 非法';
74+
break;
75+
case '-41002':
76+
$errorMsg = 'iv 非法';
77+
break;
78+
case '-41003':
79+
$errorMsg = '解密失败';
80+
break;
81+
case '41008':
82+
$errorMsg = '缺少code参数';
83+
break;
84+
default:
85+
$errorMsg = '操作失败,错误码:' . $errorCode;
86+
}
87+
return $errorMsg;
88+
}
6089
}
6190

src/MiniProgram.php

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public function decryptData($paramArr = array())
120120

121121
// 4.生成3rd_session
122122
$data = json_decode($data, true);
123-
$session3rd = $this->getNonceStr(16);
123+
$session3rd = Common::getNonce();
124124

125125
// 5.返回相关数据
126126
$res['code'] = 100;
@@ -143,7 +143,7 @@ public function createWXAQRCode($postParamArr = array())
143143
'access_token'=>$this->accessToken
144144
);
145145
$url = self::API_HOST . self::WXAQRCODE_PATH . http_build_query($urlParamArr);
146-
$res = $this->http_request($url, json_encode($postParamArr));
146+
$res = Common::httpRequest($url, json_encode($postParamArr));
147147
return $res;
148148
}
149149

@@ -176,57 +176,4 @@ public function getWxacodeUnlimit($postParamArr = array())
176176
$res = Common::httpRequest($url, json_encode($postParamArr));
177177
return $res;
178178
}
179-
180-
/**
181-
* 获取随机字符串
182-
* @param int $length
183-
* @return string
184-
*/
185-
private function getNonceStr($length = 16)
186-
{
187-
$str2 = time();
188-
$length2 = strlen($str2);
189-
$length1 = $length - $length2;
190-
if ($length1 <= 0) {
191-
$length1 = 6;
192-
}
193-
194-
$chars = "abcdefghijklmnopqrstuvwxyz";
195-
$str1 = "";
196-
for ($i = 0; $i < $length1; $i++) {
197-
$str1 .= substr($chars, mt_rand(0, strlen($chars)-1), 1);
198-
}
199-
$str = $str1 . $str2;
200-
201-
return $str;
202-
}
203-
204-
/**
205-
* 根据错误码获取错误信息
206-
* @param $errorCode
207-
* @return string
208-
*/
209-
private function getErrorMsg($errorCode)
210-
{
211-
switch ($errorCode) {
212-
case '40029':
213-
$errorMsg = 'code 非法';
214-
break;
215-
case '-41001':
216-
$errorMsg = 'sessionKey 非法';
217-
break;
218-
case '-41002':
219-
$errorMsg = 'iv 非法';
220-
break;
221-
case '-41003':
222-
$errorMsg = '解密失败';
223-
break;
224-
case '41008':
225-
$errorMsg = '缺少code参数';
226-
break;
227-
default:
228-
$errorMsg = '操作失败,错误码:' . $errorCode;
229-
}
230-
return $errorMsg;
231-
}
232179
}

0 commit comments

Comments
 (0)