ThirdUpFacadeRepository.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace app\Repositories\Eloquent;
  3. use App\Repositories\Eloquent\BaseRepository;
  4. use GuzzleHttp\Client as httpClient;
  5. use App\Repositories\Contracts\ThirdUpInterface;
  6. /**
  7. *
  8. * @author lilin
  9. *
  10. */
  11. class ThirdUpFacadeRepository extends BaseRepository implements ThirdUpInterface
  12. {
  13. public function sign(string $op, string $task, string $version, array $parameter)
  14. {
  15. $appid = config('console.thirdUP.appid');
  16. $key = config('console.thirdUP.key');
  17. ksort($parameter);
  18. $pStr = '';
  19. foreach ($parameter as $v) {
  20. $pStr .= $v;
  21. }
  22. $str = $appid . $op . $task . $version . $pStr . $key;
  23. return md5($str);
  24. }
  25. public function fixedParameter($op, $task, $parameter, $version = '1.0.0')
  26. {
  27. $fixedParameter = [
  28. 'op' => $op,
  29. 'task' => $task,
  30. 'version' => $version,
  31. 'appid' => config('console.thirdUP.appid')
  32. ];
  33. //密钥
  34. $sign = $this->sign($fixedParameter['op'], $fixedParameter['task'], $fixedParameter['version'], $parameter);
  35. //加入
  36. $fixedParameter['sign'] = $sign;
  37. return $fixedParameter;
  38. }
  39. public function login(string $username, string $password)
  40. {
  41. //请求参数
  42. $parameter = [
  43. 'username' => $username,
  44. 'password' => $password
  45. ];
  46. //固定参数
  47. $fixedParameter = $this->fixedParameter('customer', 'login', $parameter);
  48. //请求地址
  49. $url = config('console.thirdUP.url');
  50. try {
  51. $parameter = array_merge($fixedParameter, $parameter);
  52. $client = new httpClient();
  53. $res = $client->request('post', $url, ['form_params'=>$parameter]);
  54. $body = $res->getBody();
  55. $this->setLog(self::TYPENAME.'login 远程请求成功', $this->startTime, [$body]);
  56. $data = json_decode($body);
  57. if ($data->status == FALSE){
  58. $this->error()->fail($data->message);
  59. }
  60. return $this->response($data);
  61. } catch (\Exception $e) {
  62. $this->setLog(self::TYPENAME.'login 远程请求失败', $this->startTime, [$e->getMessage()]);
  63. $this->error()->fail($e->getMessage());
  64. }
  65. }
  66. }