You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
188 lines
7.7 KiB
188 lines
7.7 KiB
<?php
|
|
declare (strict_types=1);
|
|
|
|
namespace app\common\library\wxserver;
|
|
|
|
use app\common\model\WxserverAccount;
|
|
use think\facade\Cache;
|
|
use think\facade\Db;
|
|
|
|
class Server
|
|
{
|
|
const APPID = 'wxc03d95d3597e348a';
|
|
const SECRET = 'e2720e31673906be6e92afd377b927ba';
|
|
const TOKEN = 'obUzmSA4MvMjOblZ5NCpgecTZYaGtAg6';//消息校验Token
|
|
const KEY = '2tujZuThOCzcuoBcBmeqMPjL6phcsv2dY7pgwYJwdtX'; //消息加解密Key
|
|
protected WxBizMsgCrypt $crypt;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->crypt = new WxBizMsgCrypt(self::TOKEN, self::KEY, self::APPID);
|
|
}
|
|
|
|
/**
|
|
* @notes:保存Ticket
|
|
* @param string $xml
|
|
* @return mixed|string
|
|
* @author: wanghousheng
|
|
*/
|
|
public function getVerifyTicket(string $xml)
|
|
{
|
|
$ticket = '';
|
|
$xmlData = $this->crypt->fromXml($xml);
|
|
if (!empty($xmlData['Encrypt'])) {
|
|
//解密
|
|
$encryptXml = $this->crypt->decryptMsg($xmlData['Encrypt']);
|
|
$data = $this->crypt->fromXml($encryptXml);
|
|
if (!empty($data['ComponentVerifyTicket'])) {
|
|
$ticket = $data['ComponentVerifyTicket'];
|
|
Db::table('yoshop_wx_server')->insertGetId(['content' => '存入ticket成功&' . $ticket, 'created_at' => date('Y-m-d H:i:s')]);
|
|
Cache::set('component_verify_ticket', $ticket, 3600);
|
|
}
|
|
}
|
|
return $ticket;
|
|
}
|
|
|
|
/**
|
|
* @notes:获取令牌
|
|
* @return mixed|string
|
|
* @author: wanghousheng
|
|
*/
|
|
public function getComponentAccessToken()
|
|
{
|
|
if (Cache::has('ComponentAccessToken')) {
|
|
return Cache::get('ComponentAccessToken');
|
|
}
|
|
$url = 'https://api.weixin.qq.com/cgi-bin/component/api_component_token';
|
|
$ticket = Cache::get('component_verify_ticket');
|
|
if (!empty($ticket)) {
|
|
$data = [];
|
|
$data['component_appid'] = self::APPID;
|
|
$data['component_appsecret'] = self::SECRET;
|
|
$data['component_verify_ticket'] = $ticket;
|
|
$result = $this->curlPost($url, json_encode($data), 10);
|
|
$result = json_decode($result, true);
|
|
if (!empty($result['component_access_token'])) {
|
|
$expire = !empty($result['expires_in']) ? intval($result['expires_in']) : 7200;
|
|
Cache::set('ComponentAccessToken', $result['component_access_token'], $expire);
|
|
return $result['component_access_token'];
|
|
}
|
|
}
|
|
return '';
|
|
}
|
|
|
|
/**
|
|
* @notes:生成H5授权链接
|
|
* @param string $redirect_uri //授权后重定向的回调链接地址
|
|
* @param string $biz_appid //业务公众号appid
|
|
* @return string
|
|
* @author: wanghousheng
|
|
*/
|
|
public function jumpH5Url(string $redirect_uri, string $biz_appid = ''): string
|
|
{
|
|
$url = '';
|
|
$auth_code = $this->getPreAuthCode();
|
|
if ($auth_code) {
|
|
$url = "'https://open.weixin.qq.com/wxaopen/safe/bindcomponent?action=bindcomponent&no_scan=1";
|
|
$url .= "&component_appid=" . self::APPID . '&pre_auth_code=' . $auth_code . '&redirect_uri=' . $redirect_uri;
|
|
$url .= '&auth_type=3&biz_appid=' . $biz_appid . '#wechat_redirect';
|
|
}
|
|
return $url;
|
|
}
|
|
|
|
/**
|
|
* @notes:获取预授权码
|
|
* @return false|mixed
|
|
* @author: wanghousheng
|
|
*/
|
|
public function getPreAuthCode()
|
|
{
|
|
$token = $this->getComponentAccessToken();
|
|
if ($token) {
|
|
$url = 'https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=' . $token;
|
|
$data['component_appid'] = self::APPID;
|
|
$result = $this->curlPost($url, json_encode($data));
|
|
$result = json_decode($result, true);
|
|
if ($result && !empty($result['pre_auth_code'])) {
|
|
return $result['pre_auth_code'];
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* @notes:获取授权信息
|
|
* @param $authorization_code
|
|
* @author: wanghousheng
|
|
*/
|
|
public function authorizationInfo($authorization_code)
|
|
{
|
|
Db::table('yoshop_wx_server')->insertGetId(['content' => '回调进来了' . $authorization_code, 'created_at' => date('Y-m-d H:i:s')]);
|
|
$url = 'https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=' . $this->getComponentAccessToken();
|
|
$data = [];
|
|
$data['component_appid'] = self::APPID;
|
|
$data['authorization_code'] = $authorization_code;
|
|
$result = $this->curlPost($url, json_encode($data));
|
|
Db::table('yoshop_wx_server')->insertGetId(['content' => $result, 'created_at' => date('Y-m-d H:i:s')]);
|
|
$result = json_decode($result, true);
|
|
if ($result && !empty($result['authorization_info']) && !empty($result['authorization_info']['authorizer_appid'])) {
|
|
Db::table('yoshop_wx_server')->insertGetId(['content' => '获取详情' . $authorization_code, 'created_at' => date('Y-m-d H:i:s')]);
|
|
$url = 'https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token=' . $this->getComponentAccessToken();
|
|
$data = [];
|
|
$data['component_appid'] = self::APPID;
|
|
$data['authorizer_appid'] = $result['authorization_info']['authorizer_appid'];
|
|
$res = $this->curlPost($url, json_encode($data));
|
|
Db::table('yoshop_wx_server')->insertGetId(['content' => $res, 'created_at' => date('Y-m-d H:i:s')]);
|
|
$res = json_decode($res, true);
|
|
if ($res && !empty($res['authorizer_info']) && !empty($res['authorization_info'])) {
|
|
$authorizer_info = $res['authorizer_info'];
|
|
$authorization_info = $res['authorization_info'];
|
|
$insert_data['appid'] = $authorization_info['authorizer_appid'];
|
|
$insert_data['access_token'] = $result['authorization_info']['authorizer_access_token'];
|
|
$insert_data['refresh_token'] = $authorization_info['authorizer_refresh_token'];
|
|
$insert_data['nick_name'] = $authorizer_info['nick_name'];
|
|
$insert_data['head_image'] = $authorizer_info['head_img'];
|
|
$insert_data['user_name'] = $authorizer_info['user_name'];
|
|
$insert_data['qrcode_image'] = $authorizer_info['qrcode_url'];
|
|
$insert_data['principal_name'] = $authorizer_info['principal_name'];
|
|
$insert_data['signature'] = $authorizer_info['signature'];
|
|
$model = new WxserverAccount();
|
|
$model->insertGetId($insert_data);
|
|
}
|
|
}
|
|
}
|
|
|
|
private function curlPost($url, $post_data, $timeout = 5)
|
|
{
|
|
$ch = curl_init();
|
|
if (stripos($url, 'https://') !== false) {
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
|
curl_setopt($ch, CURLOPT_SSLVERSION, 1);
|
|
}
|
|
if (is_string($post_data)) {
|
|
$strPOST = $post_data;
|
|
} else {
|
|
$aPOST = array();
|
|
foreach ($post_data as $key => $val) {
|
|
$aPOST[] = $key . '=' . urlencode($val);
|
|
}
|
|
$strPOST = join('&', $aPOST);
|
|
}
|
|
curl_setopt($ch, CURLOPT_HTTP_VERSION, 1);
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $strPOST);
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
|
|
$result = curl_exec($ch);
|
|
$aStatus = curl_getinfo($ch);
|
|
curl_close($ch);
|
|
if (intval($aStatus['http_code']) == 200) {
|
|
return $result;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
} |