123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- <?php
- namespace app\common\library\Douyin;
- use think\Config;
- use app\common\Service\ShopConfigService;
- use think\Log;
- class Service
- {
- private $appid;
- private $secret;
- const GET_AUTH_CODE_URL = "https://open.douyin.com/platform/oauth/connect";
- const GET_ACCESS_TOKEN_URL = "https://open.douyin.com/oauth/access_token";
- const GET_USERINFO_URL = "https://open.douyin.com/oauth/userinfo/";
- /**
- * 构造函数
- * @param $sessionKey string 用户在小程序登录后获取的会话密钥
- * @param $appid string 小程序的appid
- */
- public function __construct($addons = "")
- {
- $appId = ShopConfigService::getConfigField("shop.platform.douyin_mini_program.app_id", false);
- $secret = ShopConfigService::getConfigField("shop.platform.douyin_mini_program.secret", false);
- $this->secret = $secret;
- $this->appid = $appId;
- }
- /**
- * 获取用户信息
- */
- public function getUserInfo($code,$anonymous_code ,$encryptedData='',$iv=''){
- //获取用户信息
- $queryarr = [
- "appid" => $this->appid ,
- "secret" => $this->secret ,
- 'code' =>$code,
- ];
- $ret = $this->curl_post('https://developer.toutiao.com/api/apps/v2/jscode2session',json_encode($queryarr));
- if ($ret){
- //解密数据
- if($encryptedData){
- $key = $ret['data']['session_key'];
- // $ret['mobile'] = $phone[''];
- $phone = openssl_decrypt(base64_decode($encryptedData,true), 'AES-128-CBC', base64_decode($key), OPENSSL_RAW_DATA, base64_decode($iv));
- $ret['data']['mobile'] = json_decode($phone,true)['phoneNumber'];
- }
- // print_r($phone);die;
- return $ret;
- }
-
- return [];
- }
- /**
- * 获取用户信息
- * @param array $code
- * @return array
- */
- public function getUserInfo2($code)
- {
- //获取access_token
- $dataK = $this->getAccessToken($code);
- if ($dataK['data']['error_code'] != 0){
- return ["error_code"=>$dataK['data']['error_code'],"description"=>$dataK['data']['description']];
- }
- var_dump($dataK);
- $data = isset($dataK['data']) ? $dataK['data'] : $dataK;
- $access_token = isset($data['access_token']) ? $data['access_token'] : '';
- $refresh_token = isset($data['refresh_token']) ? $data['refresh_token'] : '';
- $expires_in = isset($data['expires_in']) ? $data['expires_in'] : 0;
- // print_r($dataK);die;
- if ($access_token) {
- $openid = isset($data['open_id']) ? $data['open_id'] : '';
- $unionid = isset($data['union_id']) ? $data['union_id'] : '';
- //获取用户信息
- $queryarr = [
- "access_token" => $access_token,
- "open_id" => $openid
- ];
- $ret = $this->curl_post(self::GET_USERINFO_URL, $queryarr,["Content-Type"=>"application/json"]);
- var_dump($ret);
- $userinfo = $ret['data'];
- $userinfo['openid'] = $userinfo['open_id'];
- $userinfo['unionid'] = $userinfo['union_id'];
- if (!$userinfo || isset($userinfo['errcode'])) {
- return [];
- }
- $userinfo = $userinfo ? $userinfo : [];
- $userinfo['avatar'] = isset($userinfo['avatar']) ? $userinfo['avatar'] : '';
- $data = [
- 'access_token' => $access_token,
- 'refresh_token' => $refresh_token,
- 'expires_in' => $expires_in,
- 'openid' => $openid,
- 'unionid' => $unionid,
- 'userinfo' => $userinfo
- ];
- return $data;
- }
- return [];
- }
- /**
- * 获取access_token
- * @param string code
- * @return array
- */
- public function getAccessToken($code = '')
- {
- if (!$code) {
- return [];
- }
- $queryarr = array(
- "client_key" => $this->appid,
- "client_secret" => $this->sessionKey,
- "code" => $code,
- "grant_type" => "authorization_code",
- );
- $response = $this->curl_post(self::GET_ACCESS_TOKEN_URL, $queryarr,["Content-Type"=>"application/json"]);
- return $response ? $response : [];
- }
- public function curl_post($url,$post_data,$header=[])
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_HEADER, false); // 显示返回的Header区域内容
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $output = curl_exec($ch);
- curl_close($ch);
- return json_decode($output,true);
- }
- }
|