Skip to content

Commit d9c337d

Browse files
committed
v1.0
1 parent 5a7436e commit d9c337d

File tree

13 files changed

+122
-83
lines changed

13 files changed

+122
-83
lines changed

.gitignore

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
.idea
21
*.DS_Store
3-
/vendor
2+
*.log
3+
.idea
4+
.php_cs.cache
5+
.phpunit.result.cache
6+
.subsplit
47
/coverage
5-
sftp-config.json
8+
/vendor
69
composer.lock
7-
.subsplit
8-
.php_cs.cache
9-
*.log
10+
sftp-config.json

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
language: php
22

33
php:
4-
- 7.1
54
- 7.2
65
- 7.3
76
- 7.4

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](http://keepachangelog.com/)
6+
and this project adheres to [Semantic Versioning](http://semver.org/).
7+
8+
## [v1.0.0] - 2021-01-12
9+
### Changed
10+
- v1.0 发布
11+
- 增加新电签接口 `getContractStatusV2` 原接口已经被官方标记为废弃
12+
13+
### Removed
14+
- 代码重构,最低支持 PHP ^7.2

composer.json

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
{
2-
"name": "bolechen\/gongmall-php-sdk",
3-
"description": "工猫(gongmall.com)sdk for php,非官方支持。基于官网文档:https:\/\/opendoc.gongmall.com 实现",
2+
"name": "bolechen/gongmall-php-sdk",
3+
"description": "工猫(gongmall.com)sdk for php,非官方支持。基于官网文档:https://opendoc.gongmall.com 实现",
44
"license": "MIT",
55
"authors": [{
66
"name": "Bole Chen",
77
"email": "[email protected]"
88
}],
99
"require": {
10-
"hanson/foundation-sdk": "3.0.*|^4.0.3"
10+
"php": "^7.2",
11+
"hanson/foundation-sdk": "3.0.*|^4.0.3",
12+
"ext-json": "*",
13+
"ext-openssl": "*"
1114
},
1215
"autoload": {
1316
"psr-4": {
14-
"Bolechen\\Gongmall\\": "src"
17+
"Bolechen\\Gongmall\\": "src",
18+
"Bolechen\\Gongmall\\Test\\": "tests"
1519
}
1620
},
1721
"require-dev": {
18-
"phpunit/phpunit": "~7.0"
22+
"phpunit/phpunit": "~8.0"
1923
},
2024
"scripts": {
2125
"test": "vendor/bin/phpunit"

src/Api.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,43 @@
1414

1515
class 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;

src/Company.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Company extends Api
1919
*
2020
* @return array
2121
*/
22-
public function getBalance()
22+
public function getBalance(): array
2323
{
2424
return $this->request('/api/company/getBalance');
2525
}

src/Employee.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,55 @@ class Employee extends Api
1717
*
1818
* @see https://opendoc.gongmall.com/dian-qian-he-tong/jie-ru-zhi-nan.html
1919
*
20+
* @param array $params
21+
*
2022
* @return string
2123
*/
22-
public function getContractUrl(array $params)
24+
public function getContractUrl(array $params): string
2325
{
2426
return $this->contractUrl.'&data='.base64_encode($this->employeeEncrypt($params));
2527
}
2628

2729
/**
28-
* 查询电签结果.
30+
* 查询电签结果(废弃).
2931
*
3032
* @see https://opendoc.gongmall.com/dian-qian-he-tong/cha-xun-dian-qian-jie-guo.html
3133
*
34+
* @param array $params
35+
*
3236
* @return array
37+
*
38+
* @deprecated 官方标记为已废弃,使用 getContractStatusV2 替代
3339
*/
34-
public function getContractStatus(array $params)
40+
public function getContractStatus(array $params): array
3541
{
3642
return $this->request('/api/employee/getContractStatus', $params);
3743
}
3844

45+
/**
46+
* 查询电签结果.
47+
*
48+
* @see https://opendoc.gongmall.com/dian-qian-he-tong/cha-xun-dian-qian-jie-guo-v2.html
49+
*
50+
* @param array $params
51+
*
52+
* @return array
53+
*/
54+
public function getContractStatusV2(array $params): array
55+
{
56+
return $this->request('/api/employee/getContractStatusV2', $params);
57+
}
58+
3959
/**
4060
* 修改员工银行卡
4161
*
4262
* @see https://opendoc.gongmall.com/shi-shi-ti-xian/xiu-gai-yin-hang-ka.html
4363
*
64+
* @param array $params
65+
*
4466
* @return array
4567
*/
46-
public function syncBankAccount(array $params)
68+
public function syncBankAccount(array $params): array
4769
{
4870
return $this->request('/api/employee/v2/syncBankAccount', $params);
4971
}

src/Gongmall.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
/**
1616
* Class Gongmall.
1717
*
18-
* @property \Bolechen\Gongmall\Employee $employee
19-
* @property \Bolechen\Gongmall\Withdraw $withdraw
20-
* @property \Bolechen\Gongmall\Company $company
21-
* @property \Bolechen\Gongmall\Push $push
18+
* @property Employee $employee
19+
* @property Withdraw $withdraw
20+
* @property Company $company
21+
* @property Push $push
2222
*/
2323
class Gongmall extends Foundation
2424
{
@@ -29,12 +29,12 @@ class Gongmall extends Foundation
2929
/**
3030
* API 请求
3131
*
32-
* @param string $uri
33-
* @param array $params
32+
* @param string $uri
33+
* @param array $params
3434
*
3535
* @return array
3636
*/
37-
public function request($uri, $params = [])
37+
public function request(string $uri, $params = []): array
3838
{
3939
return $this->api->request($uri, $params);
4040
}

src/GongmallServiceProvider.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class GongmallServiceProvider implements ServiceProviderInterface
2323
*
2424
* @param Container $pimple A container instance
2525
*/
26-
public function register(Container $pimple)
26+
public function register(Container $pimple): void
2727
{
2828
$pimple['employee'] = function ($pimple) {
2929
return new Employee($pimple);
@@ -38,11 +38,7 @@ public function register(Container $pimple)
3838
};
3939

4040
$pimple['push'] = function ($pimple) {
41-
return new Push(
42-
$pimple->getConfig()['apiKey'],
43-
$pimple->getConfig()['apiSecret'],
44-
$pimple['request']
45-
);
41+
return new Push($pimple);
4642
};
4743
}
4844
}

src/Push.php

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,57 +10,43 @@
1010

1111
namespace Bolechen\Gongmall;
1212

13-
use Symfony\Component\HttpFoundation\Request;
13+
use RuntimeException;
1414
use Symfony\Component\HttpFoundation\Response;
1515

1616
class Push extends Api
1717
{
18-
/**
19-
* @var Request
20-
*/
21-
private $request;
22-
23-
public function __construct($apiKey, $apiSecret, Request $request)
24-
{
25-
$this->apiKey = $apiKey;
26-
$this->apiSecret = $apiSecret;
27-
$this->request = $request;
28-
}
29-
3018
/**
3119
* @param null|mixed $postRaw
3220
*
33-
* @throws Exception
34-
*
3521
* @return array|Response
3622
*/
3723
public function parse($postRaw = null)
3824
{
3925
if (!$postRaw) {
40-
$postRaw = $this->request->getContent();
26+
$postRaw = $this->app->request->getContent();
4127
}
4228

4329
parse_str($postRaw, $data);
4430
if (!is_array($data)) {
45-
throw new \Exception('数据不正确');
31+
throw new RuntimeException('数据不正确');
4632
}
4733

4834
$this->checkSign($data);
4935

5036
return $data;
5137
}
5238

53-
public function checkSign(array $data)
39+
public function checkSign(array $data): void
5440
{
5541
$sign = $this->signature($data);
5642

57-
if (!isset($data['sign']) || $sign != $data['sign']) {
58-
throw new \Exception('签名不正确');
43+
if (!isset($data['sign']) || $sign !== $data['sign']) {
44+
throw new RuntimeException('签名不正确');
5945
}
6046
}
6147

62-
public function response()
48+
public function response(): Response
6349
{
64-
return Response::create(json_encode(['code' => 0, 'msg' => 'success']));
50+
return new Response(json_encode(['code' => 0, 'msg' => 'success']));
6551
}
6652
}

0 commit comments

Comments
 (0)