-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d1cc1b6
Showing
13 changed files
with
658 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.DS_Store | ||
phpunit.phar | ||
/vendor | ||
composer.phar | ||
composer.lock | ||
*.project | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 Jens Segers | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
laravel-admin 微信公众号管理插件 | ||
====== | ||
|
||
目前实现了简单的自定义菜单和自定义关键字回复功能 | ||
|
||
#### 安装 | ||
`composer require phpcxy/wechat-manager` | ||
|
||
#### 发布资源 | ||
``` | ||
php artisan vendor:publish --tag=wechat-manager-migrations | ||
``` | ||
|
||
#### 数据迁移 | ||
|
||
``` | ||
php artisan migrate | ||
``` | ||
|
||
#### 发布菜单 | ||
``` | ||
php artisan admin:import wechat-manager | ||
``` | ||
|
||
#### 配置微信 | ||
在.env写入以下配置 | ||
``` | ||
WECHAT_OFFICIAL_ACCOUNT_APPID=公众号appid | ||
WECHAT_OFFICIAL_ACCOUNT_SECRET=公众号app secret | ||
WECHAT_OFFICIAL_ACCOUNT_TOKEN=token | ||
WECHAT_OFFICIAL_ACCOUNT_AES_KEY= | ||
``` | ||
|
||
#### 使用说明 | ||
|
||
0. 使用[laravel-wechat](https://github.com/overtrue/laravel-wechat)调用微信SDK,具体使用请查看文档 | ||
|
||
1. 菜单使用了[model-tree](http://laravel-admin.org/docs/zh/model-tree)来管理,编辑好菜单后点击`发布菜单`按钮进行发布 | ||
|
||
2. 获取指定关键字的回复 | ||
``` | ||
$text = WechatManager::getReply('keyword'); | ||
``` | ||
即可返回该关键字设置的回复信息,如果该关键字有多个回复则会随机获取一个返回。 | ||
|
||
使用laravel-wechat的话,可以在微信消息服务端那里这样使用 | ||
``` | ||
/** | ||
* 接收微信消息和事件 | ||
* @param Request $request | ||
* @return | ||
*/ | ||
public function server(Request $request) | ||
{ | ||
$wechat = app('wechat.official_account'); | ||
$wechat->server->push(function($message) { | ||
$type = $message['MsgType']; | ||
switch ($type) { | ||
case 'text': | ||
$content = $message['Content']; | ||
$reply = WechatManager::getReply($content); | ||
if ($reply) { | ||
return $reply; | ||
} | ||
break; | ||
case 'event': | ||
// 菜单的点击回复使用了CLICK事件,所以需要在事件这里获取下回复内容 | ||
if ($message['Event'] === 'CLICK') { | ||
$reply = WechatManager::getReply($message['EventKey'], 'menu'); | ||
if ($reply) { | ||
return $reply; | ||
} | ||
} | ||
break; | ||
default: | ||
return 'hello world'; | ||
break; | ||
} | ||
}); | ||
return $this->wechat->server->serve(); | ||
} | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"name": "phpcxy/wechat-manager", | ||
"description": "wechat manager extension for laravel-admin", | ||
"type": "library", | ||
"keywords": ["laravel-admin", "extension", "wechat"], | ||
"homepage": "https://github.com/phpcxy/wechat-manager", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "phpcxy", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": ">=7.0.0", | ||
"encore/laravel-admin": "^1.7.3", | ||
"overtrue/laravel-wechat": "~4.0" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "~6.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Phpcxy\\WechatManager\\": "src/" | ||
} | ||
}, | ||
"extra": { | ||
"laravel": { | ||
"providers": [ | ||
"Phpcxy\\WechatManager\\WechatManagerServiceProvider" | ||
] | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
database/migrations/2019_07_23_141906_create_wechat_menu_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class CreateWechatMenuTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('admin_wechat_menu', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->string('title'); | ||
$table->unsignedInteger('parent_id')->default(0); | ||
$table->unsignedInteger('order')->default(0); | ||
$table->string('type', 16)->nullable(); | ||
$table->string('key', 16)->nullable(); | ||
$table->text('value')->nullable(); | ||
$table->timestamps(); | ||
}); | ||
|
||
Schema::create('admin_wechat_reply', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->string('key'); | ||
$table->text('value'); | ||
$table->string('source', 16); | ||
$table->string('type', 16); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('admin_wechat_menu'); | ||
Schema::dropIfExists('admin_wechat_reply'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
use Phpcxy\WechatManager\Http\Controllers\AdminWechatMenuController; | ||
use Phpcxy\WechatManager\Http\Controllers\AdminWechatReplyController; | ||
|
||
Route::resource('wechat/menu', AdminWechatMenuController::class); | ||
Route::resource('wechat/reply', AdminWechatReplyController::class); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
|
||
namespace Phpcxy\WechatManager\Http\Controllers; | ||
|
||
use Encore\Admin\Controllers\AdminController; | ||
use Encore\Admin\Form; | ||
use Encore\Admin\Layout\Content; | ||
use Encore\Admin\Tree\Tools; | ||
use Illuminate\Support\Str; | ||
use Phpcxy\WechatManager\Models\AdminWechatMenu; | ||
use Phpcxy\WechatManager\Models\AdminWechatReply; | ||
use Phpcxy\WechatManager\Tools\ApplyMenu; | ||
|
||
class AdminWechatMenuController extends AdminController | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $types; | ||
|
||
/** | ||
* Title for current resource. | ||
* | ||
* @var string | ||
*/ | ||
protected $title = '微信自定义菜单'; | ||
|
||
public function __construct() | ||
{ | ||
$this->types = [ | ||
'view' => '链接', | ||
'text' => '文字', | ||
'click' => '事件' | ||
]; | ||
} | ||
|
||
public function index(Content $content) | ||
{ | ||
$content->header('微信自定义菜单'); | ||
$content->body(AdminWechatMenu::tree(function($tree) { | ||
$tree->tools(function (Tools $tools) { | ||
$tools->add(new ApplyMenu()); | ||
}); | ||
|
||
$tree->branch(function ($branch) { | ||
return "{$branch['title']} <span class='label label-success'>{$this->types[$branch['type']]}</span>"; | ||
}); | ||
})); | ||
|
||
return $content; | ||
} | ||
|
||
/** | ||
* Redirect to edit page. | ||
* | ||
* @param int $id | ||
* | ||
* @return \Illuminate\Http\RedirectResponse | ||
*/ | ||
public function show($id) | ||
{ | ||
return redirect()->route('menu.edit', ['id' => $id]); | ||
} | ||
|
||
/** | ||
* Make a form builder. | ||
* | ||
* @return Form | ||
*/ | ||
protected function form() | ||
{ | ||
$form = new Form(new AdminWechatMenu); | ||
$form->text('title', __('名称'))->rules('required'); | ||
$form->select('parent_id', '父级菜单')->options(AdminWechatMenu::selectOptions()) | ||
->rules('required'); | ||
|
||
$form->select('type', '类型')->options($this->types)->help("如果是一级菜单下还有二级菜单,无需选择") | ||
->rules(function($form) { | ||
if (request()->get('parent_id') > 0) { | ||
return 'required'; | ||
} | ||
}); | ||
|
||
$form->textarea('value', '内容')->help("如果是一级菜单下还有二级菜单,无需填写") | ||
->rules(function($form) { | ||
if (request()->get('type')) { | ||
return 'required'; | ||
} | ||
}); | ||
|
||
// 表单回调 | ||
$form->saved(function (Form $form) { | ||
if ($form->type == 'text') { | ||
if (!$form->model()->key) { | ||
$form->model()->key = 'M_' . Str::random(6); | ||
$form->model()->save(); | ||
} | ||
|
||
$reply = AdminWechatReply::where('key', $form->model()->key)->first(); | ||
if ($reply) { | ||
$reply->value = $form->value; | ||
$reply->save(); | ||
} else { | ||
AdminWechatReply::create([ | ||
'key' => $form->model()->key, | ||
'source' => 'menu', | ||
'type' => 'text', | ||
'value' => $form->value | ||
]); | ||
} | ||
} | ||
}); | ||
|
||
return $form; | ||
} | ||
} |
Oops, something went wrong.