1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- <?php
- namespace app\Repositories\Eloquent;
- use App\Repositories\Eloquent\BaseRepository;
- use GuzzleHttp\Client as httpClient;
- use App\Repositories\Contracts\ThirdUpInterface;
- /**
- *
- * @author lilin
- *
- */
- class ThirdUpFacadeRepository extends BaseRepository implements ThirdUpInterface
- {
- public function sign(string $op, string $task, string $version, array $parameter)
- {
- $appid = config('console.thirdUP.appid');
- $key = config('console.thirdUP.key');
- ksort($parameter);
-
- $pStr = '';
- foreach ($parameter as $v) {
- $pStr .= $v;
- }
-
- $str = $appid . $op . $task . $version . $pStr . $key;
- return md5($str);
- }
-
- public function fixedParameter($op, $task, $parameter, $version = '1.0.0')
- {
- $fixedParameter = [
- 'op' => $op,
- 'task' => $task,
- 'version' => $version,
- 'appid' => config('console.thirdUP.appid')
- ];
-
- //密钥
- $sign = $this->sign($fixedParameter['op'], $fixedParameter['task'], $fixedParameter['version'], $parameter);
- //加入
- $fixedParameter['sign'] = $sign;
-
- return $fixedParameter;
- }
-
- public function login(string $username, string $password)
- {
- //请求参数
- $parameter = [
- 'username' => $username,
- 'password' => $password
- ];
- //固定参数
- $fixedParameter = $this->fixedParameter('customer', 'login', $parameter);
- //请求地址
- $url = config('console.thirdUP.url');
-
- try {
- $parameter = array_merge($fixedParameter, $parameter);
- $client = new httpClient();
- $res = $client->request('post', $url, ['form_params'=>$parameter]);
- $body = $res->getBody();
- $this->setLog(self::TYPENAME.'login 远程请求成功', $this->startTime, [$body]);
- $data = json_decode($body);
-
- if ($data->status == FALSE){
- $this->error()->fail($data->message);
- }
-
- return $this->response($data);
- } catch (\Exception $e) {
- $this->setLog(self::TYPENAME.'login 远程请求失败', $this->startTime, [$e->getMessage()]);
- $this->error()->fail($e->getMessage());
- }
- }
-
- }
|