1414
1515class Api extends AbstractAPI
1616{
17- const API_URL = 'https://openapi.gongmall.com ' ;
18- const SANDBOX_API_URL = 'https://openapi-qa.gongmall.com ' ;
17+ protected $ app ;
18+
19+ public const API_URL = 'https://openapi.gongmall.com ' ;
20+ public const SANDBOX_API_URL = 'https://openapi-qa.gongmall.com ' ;
1921
2022 protected $ apiKey ;
2123 protected $ apiSecret ;
2224 protected $ contractUrl ;
23-
24- private $ url ;
25+ private $ apiUrl ;
2526
2627 public function __construct (Gongmall $ app )
2728 {
29+ $ this ->app = $ app ;
2830 $ config = $ app ->getConfig ();
2931
3032 $ this ->apiKey = $ config ['apiKey ' ];
3133 $ this ->apiSecret = $ config ['apiSecret ' ];
3234 $ this ->contractUrl = $ config ['contractUrl ' ];
3335
34- //沙盒环境
36+ // 沙盒环境
3537 $ this ->apiUrl = (isset ($ config ['sandbox ' ]) && $ config ['sandbox ' ]) ? static ::SANDBOX_API_URL : static ::API_URL ;
3638 }
3739
3840 /**
39- * @param string $uri
40- * @param array $params
41+ * @param string $uri
42+ * @param array $params
4143 *
4244 * @return array
45+ * @throws \JsonException
4346 */
44- public function request ($ uri , $ params = [])
47+ public function request (string $ uri , $ params = []): array
4548 {
4649 $ http = $ this ->getHttp ();
4750
4851 $ params ['appKey ' ] = $ this ->apiKey ;
4952 $ params ['nonce ' ] = $ this ->createNonceStr ();
50- $ params ['timestamp ' ] = strval (microtime (true ) * 10000 );
53+ $ params ['timestamp ' ] = ( string ) (microtime (true ) * 10000 );
5154
5255 $ protocol = $ params ;
5356 $ protocol ['sign ' ] = $ this ->signature ($ params );
@@ -62,9 +65,11 @@ public function request($uri, $params = [])
6265 *
6366 * @see https://opendoc.gongmall.com/overview/jie-kou-gui-fan.html
6467 *
68+ * @param array $paramArr
69+ *
6570 * @return string
6671 */
67- protected function signature (array $ paramArr )
72+ protected function signature (array $ paramArr ): string
6873 {
6974 //去除空参数,不参与签名
7075 unset($ paramArr ['sign ' ]);
@@ -86,9 +91,11 @@ protected function signature(array $paramArr)
8691 *
8792 * @see https://opendoc.gongmall.com/dian-qian-he-tong/jie-ru-zhi-nan.html
8893 *
94+ * @param array $data
95+ *
8996 * @return string
9097 */
91- protected function employeeEncrypt (array $ data )
98+ protected function employeeEncrypt (array $ data ): string
9299 {
93100 // data为AES加密数据
94101 $ plaintext = urldecode (http_build_query ($ data ));
@@ -109,12 +116,12 @@ protected function employeeEncrypt(array $data)
109116 // 加密结果
110117 }
111118
112- private function createNonceStr ($ length = 16 )
119+ private function createNonceStr ($ length = 16 ): string
113120 {
114121 $ chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ' ;
115122 $ str = '' ;
116123 for ($ i = 0 ; $ i < $ length ; $ i ++) {
117- $ str .= substr ( $ chars, mt_rand (0 , strlen ($ chars ) - 1 ), 1 ) ;
124+ $ str .= $ chars[ random_int (0 , strlen ($ chars ) - 1 )] ;
118125 }
119126
120127 return $ str ;
0 commit comments