Skip to content

Commit

Permalink
wechat base build
Browse files Browse the repository at this point in the history
  • Loading branch information
hdliyu committed Oct 16, 2020
1 parent 55691ac commit 726c2cc
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .phpstorm.meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

/**
* PhpStorm Meta file, to provide autocomplete information for PhpStorm
* Generated on 2020-10-15 21:05:19.
* Generated on 2020-10-16 15:31:50.
*
* @author Barry vd. Heuvel <[email protected]>
* @see https://github.com/barryvdh/laravel-ide-helper
Expand Down
2 changes: 1 addition & 1 deletion _ide_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/**
* A helper file for Laravel, to provide autocomplete information to your IDE
* Generated for Laravel 7.15.0 on 2020-10-15 21:05:18.
* Generated for Laravel 7.15.0 on 2020-10-16 15:31:42.
*
* This file should not be included in your code, only analyzed by your IDE!
*
Expand Down
23 changes: 23 additions & 0 deletions app/Http/Controllers/Wechat/SubscribeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Http\Controllers\Wechat;

use App\Http\Controllers\Controller;
use Hdliyu\Wechat\Message;
use Hdliyu\Wechat\Wechat;
use Illuminate\Http\Request;
use Log;

class SubscribeController extends Controller
{
public function handle(Request $request,Wechat $wechat,Message $message)
{
if($message->isSubscribe()){
return $message->text('感谢关注');
}
// return $message->text('你好');
/* return $message->news([
['title'=>'n你好1','description'=>'xxx','picurl'=>'http://front.wxnet.vip/xite/images/top/20170617104248203.png','url'=>'https://www.baidu.com'],
]);*/
}
}
14 changes: 0 additions & 14 deletions app/Http/Controllers/WechatController.php

This file was deleted.

3 changes: 1 addition & 2 deletions app/Http/Middleware/VerifyCsrfToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class VerifyCsrfToken extends Middleware
* @var array
*/
protected $except = [
//
'wechat/*'
'wechat'
];
}
File renamed without changes.
4 changes: 3 additions & 1 deletion config/hdliyu.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
return [
'wechat'=>[
'token'=>'hdliyu666'
'token'=>'hdliyu666',
'appID'=>'wxb6de5a7b5c78ba5d',
'appsecret'=>'4c0a4d74be90e8051c197a26a2dd84ed'
]
];
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('wechat','WechatController@handle');
Route::any('wechat','Wechat\SubscribeController@handle');

Route::get('/', 'HomeController@entry')->name('home')->middleware(['front']);

Expand Down
9 changes: 9 additions & 0 deletions wechat/src/Message.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace Hdliyu\Wechat;

use Hdliyu\Wechat\Message\MsgType;
use Hdliyu\Wechat\Message\Send;

class Message extends Wechat {
use Send,MsgType;
}
40 changes: 40 additions & 0 deletions wechat/src/Message/MsgType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
namespace Hdliyu\Wechat\Message;

trait MsgType{

public function isText()
{
return $this->MsgType == 'text';
}

public function isImage()
{
return $this->MsgType == 'image';
}

public function isVoice()
{
return $this->MsgType == 'voice';
}

public function isVideo()
{
return $this->MsgType == 'video';
}

public function isMusic()
{
return $this->MsgType == 'music';
}

public function isSubscribe()
{
return $this->MsgType == 'event' && $this->Event == 'subscribe';
}

public function isUnsubscribe()
{
return $this->MsgType == 'event' && $this->Event == 'unsubscribe';
}
}
49 changes: 49 additions & 0 deletions wechat/src/Message/Send.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
namespace Hdliyu\Wechat\Message;


trait Send{

public function text($content)
{
$xml=<<<php
<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>
php;
return sprintf($xml,$this->FromUserName,$this->ToUserName,time(),$content);
}

public function news(array $data)
{
$xml=<<<xml
<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>%s</ArticleCount>
<Articles>
%s
</Articles>
</xml>
xml;
$news = '';
$articleXml = <<<php
<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>
php;
foreach ($data as $article){
$news.=sprintf($articleXml,$article['title'],$article['description'],$article['picurl'],$article['url']);
}
return sprintf($xml,$this->FromUserName,$this->ToUserName,time(),count($data),$news);
}
}
27 changes: 17 additions & 10 deletions wechat/src/Wechat.php
Original file line number Diff line number Diff line change
@@ -1,38 +1,45 @@
<?php
namespace Hdliyu\Wechat;

use Log;

class Wechat{
public $message;
protected $message;

public function __construct()
{
$this->bind();
$this->message = $this->getMessage();
\Log::info($this->message);
$this->getMessage();
}

protected function getMessage()
public function getMessage()
{
$message = (array)simplexml_load_string(file_get_contents('php://input'));
return $message;
$content = file_get_contents('php://input');
if($content){
return $this->message = simplexml_load_string($content);
}
}

public function __get($name)
{
return $this->message->$name??null;
}

protected function bind()
{
if(isset($_GET['ignature'],$_GET['timestamp'],$_GET['nonce'],$_GET['echostr'])){
$signature = $_GET['ignature'];
if(isset($_GET['signature'],$_GET['timestamp'],$_GET['nonce'],$_GET['echostr'])){
$signature = $_GET['signature'];
$timestamp = $_GET['timestamp'];
$nonce = $_GET['nonce'];
$token = config('hdliyu.wechat.token');
$tmpArr = [$token, $timestamp, $nonce];
sort($tmpArr, SORT_STRING);
if( sha1(implode( $tmpArr )) == $signature ){
die($_GET['echostr']);
}else{
return false;
}
return false;
}

}

}

0 comments on commit 726c2cc

Please sign in to comment.