Issues (54)

src/Service/Contract/AlterSign.php (4 issues)

1
<?php
2
3
namespace tinymeng\Chinaums\Service\Contract;
4
5
use tinymeng\Chinaums\Service\Contract\Base;
6
use tinymeng\Chinaums\Tools\DES;
7
use Exception;
8
9
/**
10
 * 变更签约接口
11
 */
12
class AlterSign extends Base
13
{
14
    /**
15
     * @var string 接口地址
16
     */
17
    protected $api = '/self-contract-nmrs/interface/autoReg';
18
    /**
19
     * @var array $body 请求参数
20
     */
21
    protected $body = [
22
        'service' => 'alter_sign',
23
        'sign_type' => 'SHA-256',
24
    ];
25
    /**
26
     * 必传的值
27
     * @var array
28
     */
29
    protected $require = ['service', 'accesser_id', 'sign_type', 'request_date', 'request_seq', 'ums_reg_id'];
30
31
    /**
32
     * 获取get前端签约方式
33
     *
34
     * @param array $data
35
     * @return void
36
     */
37
    public function getUrl($data = [])
38
    {
39
        $data['accesser_id'] = $this->config['accesser_id'];
40
        $key = $this->config['private_key'];
41
        if ($data) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
$data is a non-empty array, thus is always true.
Loading history...
42
            $this->body = array_merge($this->body, $data);
43
        }
44
        try {
45
            $this->validate();
46
            $data = $this->body;
47
            $gateway  = $this->config['gateway'] . $this->api;
48
            $data = json_encode($data);
49
            $sign = hash('sha256', $data);
50
            $method = $this->method;
51
            $des = new DES($key, $method, DES::OUTPUT_HEX);
52
            // 加密
53
            $str = $des->encrypt($data);
54
            $url = $gateway . '?sign_data=' . $sign . '&json_data=' . $str . '&accesser_id=' . $this->config['accesser_id'];
55
            return json_encode(['res_code' => '0000', 'res_msg' => 'success', 'url' => $url], JSON_UNESCAPED_SLASHES);
0 ignored issues
show
Bug Best Practice introduced by
The expression return json_encode(array...JSON_UNESCAPED_SLASHES) returns the type string which is incompatible with the documented return type void.
Loading history...
56
        } catch (Exception $e) {
57
            return json_encode(['res_code' => -1, 'res_msg' => $e->getMessage(), 'request_seq' => null]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return json_encode(array...'request_seq' => null)) returns the type string which is incompatible with the documented return type void.
Loading history...
58
        }
59
    }
60
}
61