chengkun
2025-04-18 1bb985f32f2efe0f9dd69f3cf29a1c809b1cf96d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
 
/**
 * Created by PhpStorm.
 * User: sunyaoyao
 * Date: 2015/9/7
 * Time: 14:26
 */
class NuomiRequestClient
{
 
    private $nopAppId = 'yrO7h9xWcw';
 
 
    public $connectTimeout;
    public $readTimeout;
 
 
    //入参默认开启
    public $checkRequest = true;
 
    /**
     * nop平台
     */
    CONST PLATFROM_NOP = 'nop';
 
    /**
     * openapi平台
     */
    CONST PLATFROM_OPENAPI = 'openapi';
 
 
    /**
     * @desc 传入在上文中已经设置过参数的request实体,然后执行请求
     * @notice1 如果是请求银河的接口,就会域名都会换
     * @param NuomiIntegrationCashierOrderConsumeRequest | NuomiIntegrationCashierGetUserInfoRequest $request
     * @return
     * 入口函数
     */
    public function exec($request)
    {
 
        /**
         * 第一部分:首先根据request里面的所属的平台来决定相关的参数
         */
 
        /**
         * 1.从request类中获取的各种参数
         */
        $requestPlatform = $request->getRequestPlatform();
        $req = $request->getApiParams();
 
        if( $requestPlatform == self::PLATFROM_NOP ){
 
            //2.组装给nop平台的系统参数
            $sysParams['nop_appid'] = $this->nopAppId;
            $sysParams['nop_method'] = $request->getNopMethod();
            $sysParams['nop_timestamp'] = time();
 
            //3.计算nop所需要的md5签名
            $sysParams['nop_sign'] = 'nuomiopenplatfomsdk';
 
            //4.银河的接口
            $requestUrl = $request->getUrlForNop($sysParams);
 
        }
        else if ( $requestPlatform == self::PLATFROM_OPENAPI ){
 
            /**
             * @notice1:appKey,每个应用方都不一样,所以不能由配置文件RequestClient类固定
             * @notice2:其他的参数,都是业务方的参数,所以不能由配置文件RequestClient类固定
             * @notice3:私钥,每个应用方也不一样,所以不能由配置文件RequestClient类固定
             */
            $requestUrl = $request->getUrlForOpenApi();
 
            /**
             * 7.openapi的参数签名的键名是sign,不是rsaSign,这个是不一样的地方
             */
            $req['sign'] = $req['rsaSign'];
 
            unset( $req['rsaSign'] );
        }
 
 
        /**
         * 第二部分:对参数有效性进行检验
         */
        if ($this->checkRequest) {
 
            try {
                $request->checkRequestParams($req);
            }
            catch (Exception $e) {
                $result['errno'] = $e->getCode();
                $result['errmsg'] = $e->getMessage();
                return $result;
            }
        }
 
        /**
         * 第三部分:请求相关接口
         */
        $result = $this->call($requestUrl, $req);
        return $result;
    }
 
 
    /**
     * @desc 为开放平台计算签名
     * @notice1 这个签名是rsa形式的
     * @param array $assocArr
     * @param $rsaPriKeyStr
     * @return bool|string $sign
     * @throws Exception
     */
 
    private function generateSignForOpenApi($assocArr,$rsaPriKeyStr){
 
        $sign = '';
        if (empty($rsaPriKeyStr) || empty($assocArr)) {
            return $sign;
        }
 
        if (!function_exists('openssl_pkey_get_private') || !function_exists('openssl_sign')) {
            throw new Exception('SYS_ERR_OPENSSL_NOT_SUPPORT');
        }
 
        $priKey = openssl_pkey_get_private($rsaPriKeyStr);
 
        if (isset($assocArr['sign'])) {
            unset($assocArr['sign']);
        }
 
        ksort($assocArr); //按字母升序排序
 
        $parts = array();
        foreach ($assocArr as $k => $v) {
            $parts[] = $k . '=' . $v;
        }
        $str = implode('&', $parts);
        openssl_sign($str, $sign, $priKey);
        openssl_free_key($priKey);
 
        return base64_encode($sign);
    }
 
 
    /**
     * @param string $url
     * @param array $arrParams
     * @return mixed
     * @throws Exception
     */
    private function call( $url ,$arrParams)
    {
        $postFields = $arrParams;
 
        /**
         * 第二部分:初始化curl的相关参数
         */
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FAILONERROR, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
        if ($this->readTimeout) {
            curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout);
        }
 
        if ($this->connectTimeout) {
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
        }
 
        curl_setopt($ch, CURLOPT_USERAGENT, "nuomiopenplatfomsdk");
 
        //https 请求
        if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }
 
        if (is_array($postFields) && 0 < count($postFields)) {
            $postBodyString = "";
            $postMultipart = false;
            foreach ($postFields as $k => $v) {
                if ("@" != substr($v, 0, 1))//判断是不是文件上传
                {
                    $postBodyString .= "$k=" . urlencode($v) . "&";
                }
                else//文件上传用multipart/form-data,否则用www-form-urlencoded
                {
                    $postMultipart = true;
                }
            }
            unset($k, $v);
            curl_setopt($ch, CURLOPT_POST, true);
            if ($postMultipart) {
                curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
            } else {
                $header = array("content-type: application/x-www-form-urlencoded; charset=UTF-8");
                curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
                curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString, 0, -1));
            }
        }
 
        $reponse = curl_exec($ch);
 
        if (curl_errno($ch)) {
            throw new Exception(curl_error($ch), 0);
        } else {
            $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            if (200 !== $httpStatusCode) {
                throw new Exception($reponse, $httpStatusCode);
            }
        }
        curl_close($ch);
        return $reponse;
    }
}