Skip to content

Commit d1cc1b6

Browse files
committed
first commit
0 parents  commit d1cc1b6

13 files changed

+658
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.DS_Store
2+
phpunit.phar
3+
/vendor
4+
composer.phar
5+
composer.lock
6+
*.project
7+
.idea/

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Jens Segers
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
laravel-admin 微信公众号管理插件
2+
======
3+
4+
目前实现了简单的自定义菜单和自定义关键字回复功能
5+
6+
#### 安装
7+
`composer require phpcxy/wechat-manager`
8+
9+
#### 发布资源
10+
```
11+
php artisan vendor:publish --tag=wechat-manager-migrations
12+
```
13+
14+
#### 数据迁移
15+
16+
```
17+
php artisan migrate
18+
```
19+
20+
#### 发布菜单
21+
```
22+
php artisan admin:import wechat-manager
23+
```
24+
25+
#### 配置微信
26+
在.env写入以下配置
27+
```
28+
WECHAT_OFFICIAL_ACCOUNT_APPID=公众号appid
29+
WECHAT_OFFICIAL_ACCOUNT_SECRET=公众号app secret
30+
WECHAT_OFFICIAL_ACCOUNT_TOKEN=token
31+
WECHAT_OFFICIAL_ACCOUNT_AES_KEY=
32+
```
33+
34+
#### 使用说明
35+
36+
0. 使用[laravel-wechat](https://github.com/overtrue/laravel-wechat)调用微信SDK,具体使用请查看文档
37+
38+
1. 菜单使用了[model-tree](http://laravel-admin.org/docs/zh/model-tree)来管理,编辑好菜单后点击`发布菜单`按钮进行发布
39+
40+
2. 获取指定关键字的回复
41+
```
42+
$text = WechatManager::getReply('keyword');
43+
```
44+
即可返回该关键字设置的回复信息,如果该关键字有多个回复则会随机获取一个返回。
45+
46+
使用laravel-wechat的话,可以在微信消息服务端那里这样使用
47+
```
48+
49+
/**
50+
* 接收微信消息和事件
51+
* @param Request $request
52+
* @return
53+
*/
54+
public function server(Request $request)
55+
{
56+
$wechat = app('wechat.official_account');
57+
$wechat->server->push(function($message) {
58+
59+
$type = $message['MsgType'];
60+
61+
switch ($type) {
62+
case 'text':
63+
$content = $message['Content'];
64+
$reply = WechatManager::getReply($content);
65+
if ($reply) {
66+
return $reply;
67+
}
68+
break;
69+
case 'event':
70+
// 菜单的点击回复使用了CLICK事件,所以需要在事件这里获取下回复内容
71+
if ($message['Event'] === 'CLICK') {
72+
$reply = WechatManager::getReply($message['EventKey'], 'menu');
73+
if ($reply) {
74+
return $reply;
75+
}
76+
}
77+
78+
break;
79+
80+
default:
81+
return 'hello world';
82+
break;
83+
84+
}
85+
86+
});
87+
88+
return $this->wechat->server->serve();
89+
}
90+
91+
```
92+

composer.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "phpcxy/wechat-manager",
3+
"description": "wechat manager extension for laravel-admin",
4+
"type": "library",
5+
"keywords": ["laravel-admin", "extension", "wechat"],
6+
"homepage": "https://github.com/phpcxy/wechat-manager",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "phpcxy",
11+
"email": "[email protected]"
12+
}
13+
],
14+
"require": {
15+
"php": ">=7.0.0",
16+
"encore/laravel-admin": "^1.7.3",
17+
"overtrue/laravel-wechat": "~4.0"
18+
},
19+
"require-dev": {
20+
"phpunit/phpunit": "~6.0"
21+
},
22+
"autoload": {
23+
"psr-4": {
24+
"Phpcxy\\WechatManager\\": "src/"
25+
}
26+
},
27+
"extra": {
28+
"laravel": {
29+
"providers": [
30+
"Phpcxy\\WechatManager\\WechatManagerServiceProvider"
31+
]
32+
}
33+
}
34+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class CreateWechatMenuTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('admin_wechat_menu', function (Blueprint $table) {
17+
$table->increments('id');
18+
$table->string('title');
19+
$table->unsignedInteger('parent_id')->default(0);
20+
$table->unsignedInteger('order')->default(0);
21+
$table->string('type', 16)->nullable();
22+
$table->string('key', 16)->nullable();
23+
$table->text('value')->nullable();
24+
$table->timestamps();
25+
});
26+
27+
Schema::create('admin_wechat_reply', function (Blueprint $table) {
28+
$table->increments('id');
29+
$table->string('key');
30+
$table->text('value');
31+
$table->string('source', 16);
32+
$table->string('type', 16);
33+
$table->timestamps();
34+
});
35+
}
36+
37+
/**
38+
* Reverse the migrations.
39+
*
40+
* @return void
41+
*/
42+
public function down()
43+
{
44+
Schema::dropIfExists('admin_wechat_menu');
45+
Schema::dropIfExists('admin_wechat_reply');
46+
}
47+
}

routes/web.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
use Phpcxy\WechatManager\Http\Controllers\AdminWechatMenuController;
4+
use Phpcxy\WechatManager\Http\Controllers\AdminWechatReplyController;
5+
6+
Route::resource('wechat/menu', AdminWechatMenuController::class);
7+
Route::resource('wechat/reply', AdminWechatReplyController::class);
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
namespace Phpcxy\WechatManager\Http\Controllers;
4+
5+
use Encore\Admin\Controllers\AdminController;
6+
use Encore\Admin\Form;
7+
use Encore\Admin\Layout\Content;
8+
use Encore\Admin\Tree\Tools;
9+
use Illuminate\Support\Str;
10+
use Phpcxy\WechatManager\Models\AdminWechatMenu;
11+
use Phpcxy\WechatManager\Models\AdminWechatReply;
12+
use Phpcxy\WechatManager\Tools\ApplyMenu;
13+
14+
class AdminWechatMenuController extends AdminController
15+
{
16+
/**
17+
* @var array
18+
*/
19+
private $types;
20+
21+
/**
22+
* Title for current resource.
23+
*
24+
* @var string
25+
*/
26+
protected $title = '微信自定义菜单';
27+
28+
public function __construct()
29+
{
30+
$this->types = [
31+
'view' => '链接',
32+
'text' => '文字',
33+
'click' => '事件'
34+
];
35+
}
36+
37+
public function index(Content $content)
38+
{
39+
$content->header('微信自定义菜单');
40+
$content->body(AdminWechatMenu::tree(function($tree) {
41+
$tree->tools(function (Tools $tools) {
42+
$tools->add(new ApplyMenu());
43+
});
44+
45+
$tree->branch(function ($branch) {
46+
return "{$branch['title']} <span class='label label-success'>{$this->types[$branch['type']]}</span>";
47+
});
48+
}));
49+
50+
return $content;
51+
}
52+
53+
/**
54+
* Redirect to edit page.
55+
*
56+
* @param int $id
57+
*
58+
* @return \Illuminate\Http\RedirectResponse
59+
*/
60+
public function show($id)
61+
{
62+
return redirect()->route('menu.edit', ['id' => $id]);
63+
}
64+
65+
/**
66+
* Make a form builder.
67+
*
68+
* @return Form
69+
*/
70+
protected function form()
71+
{
72+
$form = new Form(new AdminWechatMenu);
73+
$form->text('title', __('名称'))->rules('required');
74+
$form->select('parent_id', '父级菜单')->options(AdminWechatMenu::selectOptions())
75+
->rules('required');
76+
77+
$form->select('type', '类型')->options($this->types)->help("如果是一级菜单下还有二级菜单,无需选择")
78+
->rules(function($form) {
79+
if (request()->get('parent_id') > 0) {
80+
return 'required';
81+
}
82+
});
83+
84+
$form->textarea('value', '内容')->help("如果是一级菜单下还有二级菜单,无需填写")
85+
->rules(function($form) {
86+
if (request()->get('type')) {
87+
return 'required';
88+
}
89+
});
90+
91+
// 表单回调
92+
$form->saved(function (Form $form) {
93+
if ($form->type == 'text') {
94+
if (!$form->model()->key) {
95+
$form->model()->key = 'M_' . Str::random(6);
96+
$form->model()->save();
97+
}
98+
99+
$reply = AdminWechatReply::where('key', $form->model()->key)->first();
100+
if ($reply) {
101+
$reply->value = $form->value;
102+
$reply->save();
103+
} else {
104+
AdminWechatReply::create([
105+
'key' => $form->model()->key,
106+
'source' => 'menu',
107+
'type' => 'text',
108+
'value' => $form->value
109+
]);
110+
}
111+
}
112+
});
113+
114+
return $form;
115+
}
116+
}

0 commit comments

Comments
 (0)