ThirdWxFacadeRepository.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace app\Repositories\Eloquent;
  3. use App\Repositories\Eloquent\BaseRepository;
  4. use App\Repositories\Contracts\ThirdWxInterface;
  5. use GuzzleHttp\Client as httpClient;
  6. /**
  7. *
  8. * @author lilin
  9. *
  10. */
  11. class ThirdWxFacadeRepository extends BaseRepository implements ThirdWxInterface
  12. {
  13. public function authorization()
  14. {
  15. $url = config('console.thirdWx.url');
  16. $weid = config('console.thirdWx.weid');
  17. $key = config('console.thirdWx.key');
  18. $time = time();
  19. $token = md5(md5($key.$weid.$time));
  20. try {
  21. $params = [
  22. 'ap' => 'api',
  23. 'op' => 'core',
  24. 'task' => 'toAuthorize',
  25. 'weid' => $weid,
  26. 'time' => $time,
  27. 'token' => $token,
  28. 'back_url' => urlencode(config('console.thirdWx.backUrl'))
  29. ];
  30. self::setLog(self::TYPENAME.'请求参数', $this->startTime(), $params);
  31. $queryArr = [];
  32. foreach ($params as $key=>$param){
  33. $queryArr[] = $key.'='.$param;
  34. }
  35. $result = $url.'?'.implode('&', $queryArr);
  36. self::setLog(self::TYPENAME.'返回结果111', $this->startTime(), ['result'=>$result]);
  37. return $this->response($result);
  38. } catch (\Exception $e) {
  39. $this->setLog(self::TYPENAME.'getAccessToken 远程请求失败', $this->startTime, [$e->getMessage()]);
  40. return $this->error()->fail();
  41. }
  42. }
  43. public function getUserInfo($weichatUid)
  44. {
  45. $url = config('console.thirdWx.url') . self::USERINFO_URL;
  46. $key = config('console.thirdWx.key');
  47. $time = time();
  48. $token = md5(md5($key.$weichatUid.$time));
  49. try {
  50. $params = [
  51. 'wechat_uid' => $weichatUid,
  52. 'time' => $time,
  53. 'token' => $token,
  54. ];
  55. $client = new httpClient();
  56. $res = $client->request('post', $url, ['form_params'=>$params]);
  57. $body = $res->getBody();
  58. $data = json_decode($body);
  59. // echo json_encode($url);
  60. // echo "<br />";
  61. // echo json_encode($params);
  62. // echo "<br />";
  63. // echo json_encode($data); exit;
  64. return $this->response($data);
  65. } catch (\Exception $e) {
  66. $this->setLog(self::TYPENAME.'getAccessToken 远程请求失败', $this->startTime, [$e->getMessage()]);
  67. $this->error()->fail();
  68. }
  69. }
  70. public function getJsSdk(string $weid, string $urls)
  71. {
  72. $url = config('console.thirdWx.url') . self::JSSDK_URL.'&weid='.$weid;
  73. try {
  74. $params = [
  75. 'url' => $urls
  76. ];
  77. $client = new httpClient();
  78. $res = $client->request('post', $url, ['form_params'=>$params]);
  79. $body = $res->getBody();
  80. self::setLog(self::TYPENAME, $this->startTime(), [$body]);
  81. $data = json_decode($body);
  82. return $this->response($data);
  83. } catch (\Exception $e) {
  84. $this->setLog(self::TYPENAME.'getJsSdk 远程请求失败', $this->startTime, [$e->getMessage()]);
  85. $this->error()->fail();
  86. }
  87. }
  88. }