Service.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace app\common\library\Douyin;
  3. use think\Config;
  4. use app\common\Service\ShopConfigService;
  5. use think\Log;
  6. class Service
  7. {
  8. private $appid;
  9. private $secret;
  10. const GET_AUTH_CODE_URL = "https://open.douyin.com/platform/oauth/connect";
  11. const GET_ACCESS_TOKEN_URL = "https://open.douyin.com/oauth/access_token";
  12. const GET_USERINFO_URL = "https://open.douyin.com/oauth/userinfo/";
  13. /**
  14. * 构造函数
  15. * @param $sessionKey string 用户在小程序登录后获取的会话密钥
  16. * @param $appid string 小程序的appid
  17. */
  18. public function __construct($addons = "")
  19. {
  20. $appId = ShopConfigService::getConfigField("shop.platform.douyin_mini_program.app_id", false);
  21. $secret = ShopConfigService::getConfigField("shop.platform.douyin_mini_program.secret", false);
  22. $this->secret = $secret;
  23. $this->appid = $appId;
  24. }
  25. /**
  26. * 获取用户信息
  27. */
  28. public function getUserInfo($code,$anonymous_code ,$encryptedData='',$iv=''){
  29. //获取用户信息
  30. $queryarr = [
  31. "appid" => $this->appid ,
  32. "secret" => $this->secret ,
  33. 'code' =>$code,
  34. ];
  35. $ret = $this->curl_post('https://developer.toutiao.com/api/apps/v2/jscode2session',json_encode($queryarr));
  36. if ($ret){
  37. //解密数据
  38. if($encryptedData){
  39. $key = $ret['data']['session_key'];
  40. // $ret['mobile'] = $phone[''];
  41. $phone = openssl_decrypt(base64_decode($encryptedData,true), 'AES-128-CBC', base64_decode($key), OPENSSL_RAW_DATA, base64_decode($iv));
  42. $ret['data']['mobile'] = json_decode($phone,true)['phoneNumber'];
  43. }
  44. // print_r($phone);die;
  45. return $ret;
  46. }
  47. return [];
  48. }
  49. /**
  50. * 获取用户信息
  51. * @param array $code
  52. * @return array
  53. */
  54. public function getUserInfo2($code)
  55. {
  56. //获取access_token
  57. $dataK = $this->getAccessToken($code);
  58. if ($dataK['data']['error_code'] != 0){
  59. return ["error_code"=>$dataK['data']['error_code'],"description"=>$dataK['data']['description']];
  60. }
  61. var_dump($dataK);
  62. $data = isset($dataK['data']) ? $dataK['data'] : $dataK;
  63. $access_token = isset($data['access_token']) ? $data['access_token'] : '';
  64. $refresh_token = isset($data['refresh_token']) ? $data['refresh_token'] : '';
  65. $expires_in = isset($data['expires_in']) ? $data['expires_in'] : 0;
  66. // print_r($dataK);die;
  67. if ($access_token) {
  68. $openid = isset($data['open_id']) ? $data['open_id'] : '';
  69. $unionid = isset($data['union_id']) ? $data['union_id'] : '';
  70. //获取用户信息
  71. $queryarr = [
  72. "access_token" => $access_token,
  73. "open_id" => $openid
  74. ];
  75. $ret = $this->curl_post(self::GET_USERINFO_URL, $queryarr,["Content-Type"=>"application/json"]);
  76. var_dump($ret);
  77. $userinfo = $ret['data'];
  78. $userinfo['openid'] = $userinfo['open_id'];
  79. $userinfo['unionid'] = $userinfo['union_id'];
  80. if (!$userinfo || isset($userinfo['errcode'])) {
  81. return [];
  82. }
  83. $userinfo = $userinfo ? $userinfo : [];
  84. $userinfo['avatar'] = isset($userinfo['avatar']) ? $userinfo['avatar'] : '';
  85. $data = [
  86. 'access_token' => $access_token,
  87. 'refresh_token' => $refresh_token,
  88. 'expires_in' => $expires_in,
  89. 'openid' => $openid,
  90. 'unionid' => $unionid,
  91. 'userinfo' => $userinfo
  92. ];
  93. return $data;
  94. }
  95. return [];
  96. }
  97. /**
  98. * 获取access_token
  99. * @param string code
  100. * @return array
  101. */
  102. public function getAccessToken($code = '')
  103. {
  104. if (!$code) {
  105. return [];
  106. }
  107. $queryarr = array(
  108. "client_key" => $this->appid,
  109. "client_secret" => $this->sessionKey,
  110. "code" => $code,
  111. "grant_type" => "authorization_code",
  112. );
  113. $response = $this->curl_post(self::GET_ACCESS_TOKEN_URL, $queryarr,["Content-Type"=>"application/json"]);
  114. return $response ? $response : [];
  115. }
  116. public function curl_post($url,$post_data,$header=[])
  117. {
  118. $ch = curl_init();
  119. curl_setopt($ch, CURLOPT_URL, $url);
  120. curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
  121. curl_setopt($ch, CURLOPT_POST, true);
  122. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  123. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  124. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  125. curl_setopt($ch, CURLOPT_HEADER, false); // 显示返回的Header区域内容
  126. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  127. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  128. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  129. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  130. $output = curl_exec($ch);
  131. curl_close($ch);
  132. return json_decode($output,true);
  133. }
  134. }