发票管理apiadmin
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.
 
 
 

139 lines
4.1 KiB

<?php
declare (strict_types = 1);
namespace app\service\wechat;
use app\model\WechatUser;
use app\service\BaseService;
use fast\Http;
class WechatService extends BaseService
{
protected $token = "";
protected $appid = "";
protected $appsecret = "";
protected $redis = null;
public function __construct()
{
parent::__construct();
if($token = env('wechat.token')){
$this->token = $token;
}
$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){
$data = array($this->token, $timestamp, $nonce);
sort($data, SORT_STRING);
$str = sha1(implode( $data ));
if($str == $signature){
return true;
}
return false;
}
/**
* 获取accessToken
* @return mixed|\Redis|string
* @throws \fast\FuncException
*/
public function getAccessToken(){
$access_token = $this->redis->get('wechat_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}";
$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']);
}
$this->redis->set('wechat_access_token', $data['access_token'], 7200);
return $data['access_token'];
}
public function wechatEvent(){
$data = file_get_contents("php://input");
$obj = simplexml_load_string($data,"SimpleXMLElement", LIBXML_NOCDATA);
$this->log->info('消息', $obj->FromUserName);
$event = $obj->MsgType;
switch ($event){
case 'event':
$event = $obj->Event.'_'.$obj->MsgType;
$this->$event($obj);
break;
}
}
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']);
}
$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;
}
$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);
}
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']);
}
$user = WechatUser::where('openid', $data['openid'])->where('unionid', $data['unionid'])->where('is_deleted', 0)->find();
if($user){
$user->subscribe = 0;
$user->save();
}
}
}