|
|
|
@ -1,16 +1,16 @@ |
|
|
|
<?php |
|
|
|
declare (strict_types = 1); |
|
|
|
declare (strict_types=1); |
|
|
|
|
|
|
|
namespace app\service\wechat; |
|
|
|
|
|
|
|
use app\model\WechatUser; |
|
|
|
use app\service\BaseService; |
|
|
|
use fast\Http; |
|
|
|
use fast\FuncException; |
|
|
|
|
|
|
|
class WechatService extends BaseService |
|
|
|
{ |
|
|
|
protected $token = ""; |
|
|
|
protected $appid = ""; |
|
|
|
protected $token = ""; |
|
|
|
protected $appid = ""; |
|
|
|
protected $appsecret = ""; |
|
|
|
|
|
|
|
protected $redis = null; |
|
|
|
@ -19,24 +19,25 @@ class WechatService extends BaseService |
|
|
|
{ |
|
|
|
parent::__construct(); |
|
|
|
|
|
|
|
if($token = env('wechat.token')){ |
|
|
|
if ($token = env('wechat.token')) { |
|
|
|
$this->token = $token; |
|
|
|
} |
|
|
|
|
|
|
|
$this->appid = env('wechat.appid'); |
|
|
|
$this->appid = env('wechat.appid'); |
|
|
|
$this->appsecret = env('wechat.appsecret'); |
|
|
|
|
|
|
|
$this->redis = new \Redis(); |
|
|
|
$this->redis->connect('127.0.0.1', 6379); |
|
|
|
} |
|
|
|
|
|
|
|
public function wechatChekToken($signature, $timestamp, $nonce){ |
|
|
|
public function wechatChekToken($signature, $timestamp, $nonce): bool |
|
|
|
{ |
|
|
|
|
|
|
|
$data = array($this->token, $timestamp, $nonce); |
|
|
|
sort($data, SORT_STRING); |
|
|
|
$str = sha1(implode( $data )); |
|
|
|
$str = sha1(implode($data)); |
|
|
|
|
|
|
|
if($str == $signature){ |
|
|
|
if ($str == $signature) { |
|
|
|
return true; |
|
|
|
} |
|
|
|
return false; |
|
|
|
@ -45,95 +46,79 @@ class WechatService extends BaseService |
|
|
|
/** |
|
|
|
* 获取accessToken |
|
|
|
* @return mixed|\Redis|string |
|
|
|
* @throws \fast\FuncException |
|
|
|
* @throws FuncException |
|
|
|
*/ |
|
|
|
public function getAccessToken(){ |
|
|
|
public function getAccessToken(): string |
|
|
|
{ |
|
|
|
$access_token = $this->redis->get('wechat_access_token'); |
|
|
|
if($access_token){ |
|
|
|
if ($access_token) { |
|
|
|
return $access_token; |
|
|
|
} |
|
|
|
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appid}&secret={$this->appsecret}"; |
|
|
|
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appid}&secret={$this->appsecret}"; |
|
|
|
$http = new Http(); |
|
|
|
$res = $http::get($url); |
|
|
|
if($res['code'] != 200){ |
|
|
|
throw new \fast\FuncException($res['msg']); |
|
|
|
$res = $http::get($url); |
|
|
|
if ($res['code'] != 200) { |
|
|
|
throw new FuncException($res['msg']); |
|
|
|
} |
|
|
|
$data = json_decode($res['data'], true); |
|
|
|
if(isset($data['errcode']) && $data['errcode'] != 0){ |
|
|
|
throw new \fast\FuncException($data['errmsg']); |
|
|
|
if (isset($data['errcode']) && $data['errcode'] != 0) { |
|
|
|
throw new FuncException($data['errmsg']); |
|
|
|
} |
|
|
|
$this->redis->set('wechat_access_token', $data['access_token'], 7200); |
|
|
|
return $data['access_token']; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* |
|
|
|
* @return array |
|
|
|
* @throws FuncException |
|
|
|
*/ |
|
|
|
public function getJsSdkSignature(): array |
|
|
|
{ |
|
|
|
$timestamp = time(); |
|
|
|
|
|
|
|
public function wechatEvent(){ |
|
|
|
$nonceStr = generate_random_string(16); |
|
|
|
|
|
|
|
$data = file_get_contents("php://input"); |
|
|
|
$obj = simplexml_load_string($data,"SimpleXMLElement", LIBXML_NOCDATA); |
|
|
|
$this->log->info('消息', $obj->FromUserName); |
|
|
|
$jsapi_ticket = $this->getJsApiTicket(); |
|
|
|
|
|
|
|
$event = $obj->MsgType; |
|
|
|
switch ($event){ |
|
|
|
case 'event': |
|
|
|
$event = $obj->Event.'_'.$obj->MsgType; |
|
|
|
$this->$event($obj); |
|
|
|
break; |
|
|
|
} |
|
|
|
$ascllArr = [ |
|
|
|
'noncestr=' . $nonceStr, |
|
|
|
'jsapi_ticket=' . $jsapi_ticket, |
|
|
|
'timestamp=' . $timestamp, |
|
|
|
'url=' . 'http://mp.weixin.qq.com?params=value', |
|
|
|
]; |
|
|
|
|
|
|
|
sort($ascllArr); |
|
|
|
|
|
|
|
} |
|
|
|
$signature = sha1(implode('&', $ascllArr)); |
|
|
|
|
|
|
|
return [ |
|
|
|
'appId' => env('wechat.appid'), |
|
|
|
'timestamp' => $timestamp, |
|
|
|
'nonceStr' => $nonceStr, |
|
|
|
'signature' => $signature, |
|
|
|
]; |
|
|
|
} |
|
|
|
|
|
|
|
public function subscribe_event($obj){ |
|
|
|
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$this->getAccessToken()."&openid=".$obj->FromUserName."&lang=zh_CN"; |
|
|
|
$http = new Http(); |
|
|
|
$res = $http::get($url); |
|
|
|
if($res['code'] != 200){ |
|
|
|
throw new \fast\FuncException($res['msg']); |
|
|
|
} |
|
|
|
$data = json_decode($res['data'], true); |
|
|
|
if(isset($data['errcode']) && $data['errcode'] != 0){ |
|
|
|
throw new \fast\FuncException($data['errmsg']); |
|
|
|
} |
|
|
|
/** |
|
|
|
* |
|
|
|
* @throws FuncException |
|
|
|
*/ |
|
|
|
protected function getJsApiTicket() |
|
|
|
{ |
|
|
|
|
|
|
|
$user = WechatUser::where('openid', $data['openid'])->where('unionid', $data['unionid'])->where('is_deleted', 0)->find(); |
|
|
|
if($user){ |
|
|
|
$user->subscribe = 1; |
|
|
|
$user->subscribe_time = date('Y-m-d H:i:s', $data['subscribe_time']); |
|
|
|
$user->save(); |
|
|
|
return; |
|
|
|
} |
|
|
|
$access_token = $this->getAccessToken(); |
|
|
|
|
|
|
|
$user = [ |
|
|
|
'nickname' => $data['nickname'], |
|
|
|
'headimgurl' => $data['headimgurl'], |
|
|
|
'openid' => $data['openid'], |
|
|
|
'unionid' => $data['unionid'], |
|
|
|
'subscribe_time' => date('Y-m-d H:i:s', $data['subscribe_time']), |
|
|
|
'subscribe' => 1, |
|
|
|
]; |
|
|
|
(new WechatUser())->save($user); |
|
|
|
} |
|
|
|
$url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=' . $access_token . '&type=jsapi'; |
|
|
|
|
|
|
|
$res = Http::get($url); |
|
|
|
|
|
|
|
public function unsubscribe_event($obj){ |
|
|
|
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$this->getAccessToken()."&openid=".$obj->FromUserName."&lang=zh_CN"; |
|
|
|
$http = new Http(); |
|
|
|
$res = $http::get($url); |
|
|
|
if($res['code'] != 200){ |
|
|
|
throw new \fast\FuncException($res['msg']); |
|
|
|
} |
|
|
|
$data = json_decode($res['data'], true); |
|
|
|
if(isset($data['errcode']) && $data['errcode'] != 0){ |
|
|
|
throw new \fast\FuncException($data['errmsg']); |
|
|
|
if ($res['code'] != 200) { |
|
|
|
throw new FuncException($res['msg']); |
|
|
|
} |
|
|
|
|
|
|
|
$user = WechatUser::where('openid', $data['openid'])->where('unionid', $data['unionid'])->where('is_deleted', 0)->find(); |
|
|
|
if($user){ |
|
|
|
$user->subscribe = 0; |
|
|
|
$user->save(); |
|
|
|
} |
|
|
|
$data = json_decode($res['data'],true); |
|
|
|
return $data['ticket']; |
|
|
|
} |
|
|
|
} |
|
|
|
|