初始化仓库
This commit is contained in:
136
common/services/WeappService.php
Normal file
136
common/services/WeappService.php
Normal file
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
|
||||
namespace common\services;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use yii\base\Exception;
|
||||
|
||||
class WeappService
|
||||
{
|
||||
public $appid = '';
|
||||
public $secret = '';
|
||||
protected static $guzzleOptions = ['http_errors' => false];
|
||||
|
||||
public $base_url = "https://api.weixin.qq.com";
|
||||
|
||||
public function __construct($config = [])
|
||||
{
|
||||
if(!\Yii::$app->params['wechat']['app_id'] || !\Yii::$app->params['wechat']['app_secret']){
|
||||
throw new Exception('请设置后台小程序配置');
|
||||
}
|
||||
$this->appid = \Yii::$app->params['wechat']['app_id'];
|
||||
$this->secret = \Yii::$app->params['wechat']['app_secret'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a fresh instance of the Guzzle HTTP client.
|
||||
*
|
||||
* @return \GuzzleHttp\Client
|
||||
*/
|
||||
protected function getHttpClient()
|
||||
{
|
||||
return new Client(self::$guzzleOptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* 小程序登录
|
||||
* @param $code
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function login($code)
|
||||
{
|
||||
$response = $this->getHttpClient()->get($this->base_url.'/sns/jscode2session', [
|
||||
'query' => array_filter([
|
||||
'appid' => $this->appid,
|
||||
'secret' => $this->secret,
|
||||
'js_code' => $code,
|
||||
'grant_type' => 'authorization_code'
|
||||
]),
|
||||
]);
|
||||
$result = json_decode($response->getBody(), true);
|
||||
if(isset($result['errcode'])){
|
||||
throw new Exception($result['errmsg']);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
/**
|
||||
* 获取后台接口access_token
|
||||
*/
|
||||
public function getAccessToken()
|
||||
{
|
||||
$key = 'wechat.common.access_token.'.$this->appid;
|
||||
$cache = \Yii::$app->cache;
|
||||
|
||||
$access_token = $cache->get($key);
|
||||
if($access_token == false){
|
||||
//app.huiliaoyaofang.com
|
||||
//job过来的没有HTTP_HOST;
|
||||
// if(!isset($_SERVER['HTTP_HOST']) || $_SERVER['HTTP_HOST']!=='app.huiliaoyaofang.com'){
|
||||
// $response = $this->getHttpClient()->get('https://app.huiliaoyaofang.com/api/v1/noauth/token');
|
||||
// $result = json_decode($response->getBody(), true);
|
||||
// $access_token = $result['data']['token'];
|
||||
// }else{
|
||||
if(!isset($_SERVER['HTTP_HOST']) || $_SERVER['HTTP_HOST']=='hy.api.ctkj88.com') {
|
||||
$response = $this->getHttpClient()->get('http://hy.api.ctkj88.com/service/v1/user/token');
|
||||
$result = json_decode($response->getBody(), true);
|
||||
$access_token = $result['data']['token'];
|
||||
}else{
|
||||
$response = $this->getHttpClient()->get($this->base_url.'/cgi-bin/token', [
|
||||
'query' => array_filter([
|
||||
'appid' => $this->appid,
|
||||
'secret' => $this->secret,
|
||||
'grant_type' => 'client_credential'
|
||||
]),
|
||||
]);
|
||||
$result = json_decode($response->getBody(), true);
|
||||
if(isset($result['errcode'])){
|
||||
throw new Exception($result['errmsg']);
|
||||
}
|
||||
$access_token = $result['access_token'];
|
||||
$cache->set($key,$access_token,$result['expires_in']-30*60);
|
||||
}
|
||||
}
|
||||
return $access_token;
|
||||
}
|
||||
/**
|
||||
* 获取小程序码
|
||||
* @param $scene
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getQrcode($scene,$page='',$type='base64')
|
||||
{
|
||||
$data['scene'] = $scene;
|
||||
if($page){
|
||||
$data['page'] = $page;
|
||||
}
|
||||
|
||||
$params = [
|
||||
'json' => $data,
|
||||
];
|
||||
|
||||
$response = $this->getHttpClient()->post($this->base_url.'/wxa/getwxacodeunlimit?access_token='.$this->getAccessToken(),$params);
|
||||
|
||||
$body = $response->getBody();
|
||||
//\Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
|
||||
try {
|
||||
$buffer = $body->getContents();
|
||||
if($type=='base64'){
|
||||
$type = getimagesizefromstring($buffer)['mime']; //获取二进制流图片格式
|
||||
$base64String = 'data:' . $type . ';base64,' . chunk_split(base64_encode($buffer));
|
||||
// header('Content-Type: '.getimagesizefromstring($buffer)['mime']);
|
||||
return $base64String;
|
||||
}else{
|
||||
return $buffer;
|
||||
}
|
||||
}catch (\Exception $exception){
|
||||
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
|
||||
$result = json_decode($response->getBody(), true);
|
||||
if(isset($result['errcode'])){
|
||||
throw new Exception($result['errmsg']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user