微信菜单

This commit is contained in:
JaguarJack 2020-06-26 16:27:20 +08:00
parent ce71b10d1c
commit a232f555c3
14 changed files with 515 additions and 3 deletions

View File

@ -0,0 +1,10 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------

View File

@ -0,0 +1,86 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\wechat\controller;
use catchAdmin\wechat\repository\WechatMenusRepository;
use catcher\base\CatchController;
use catcher\base\CatchRequest;
use catcher\CatchResponse;
class Menus extends CatchController
{
protected $menus;
public function __construct(WechatMenusRepository $repository)
{
$this->menus = $repository;
}
public function index()
{
return CatchResponse::success($this->menus->all());
}
/**
* 保存
*
* @time 2020年06月26日
* @param CatchRequest $request
* @return \think\response\Json
*/
public function save(CatchRequest $request)
{
return CatchResponse::success($this->menus->storeBy($request->param()));
}
/**
* 更新
*
* @time 2020年06月26日
* @param $id
* @param CatchRequest $request
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @return \think\response\Json
*/
public function update($id, CatchRequest $request)
{
return CatchResponse::success($this->menus->updateBy($id, $request->param()));
}
/**
* 删除
*
* @time 2020年06月26日
* @param $id
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @return \think\response\Json
*/
public function delete($id)
{
return CatchResponse::success($this->menus->deleteBy($id));
}
/**
* 同步
*
* @time 2020年06月26日
* @return \think\response\Json
* @throws \Exception
*/
public function sync()
{
return CatchResponse::success($this->menus->sync());
}
}

View File

@ -0,0 +1,42 @@
<?php
use think\migration\Migrator;
use think\migration\db\Column;
use Phinx\Db\Adapter\MysqlAdapter;
class WechatMaterial extends Migrator
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
$table = $this->table('wechat_material', ['engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci', 'comment' => '微信素材' ,'id' => 'id','signed' => true ,'primary_key' => ['id']]);
$table->addColumn('tag_id', 'integer', ['limit' => MysqlAdapter::INT_SMALL,'null' => true,'signed' => false,'comment' => '微信 tagId',])
->addColumn('name', 'string', ['limit' => 30,'null' => true,'comment' => '标签名称',])
->addColumn('fans_amount', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '粉丝数量',])
->addColumn('created_at', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '创建时间',])
->addColumn('updated_at', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '更新时间',])
->addColumn('deleted_at', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '软删除',])
->addIndex(['tag_id'], ['unique' => true,'name' => 'unique_tag_id'])
->create();
}
}

View File

@ -0,0 +1,46 @@
<?php
use think\migration\Migrator;
use think\migration\db\Column;
use Phinx\Db\Adapter\MysqlAdapter;
class WechatMenus extends Migrator
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
$table = $this->table('wechat_menus', ['engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci', 'comment' => '微信菜单' ,'id' => 'id','signed' => true ,'primary_key' => ['id']]);
$table->addColumn('name', 'string', ['limit' => 30,'null' => true,'comment' => '菜单名称',])
->addColumn('parent_id', 'integer', ['limit' => MysqlAdapter::INT_SMALL,'null' => false,'default' => 0,'signed' => true,'comment' => '父级ID',])
->addColumn('type', 'string', ['null' => false, 'limit' => 100, 'comment' => '类型',])
->addColumn('key', 'string', ['null' => false, 'limit' => 30, 'comment' => 'key',])
->addColumn('url', 'string', ['default' => '', 'limit' => 255, 'comment' => 'view 类型 url 链接',])
->addColumn('appid', 'string', ['default' => '', 'limit' => 100, 'comment' => '小程序 appid',])
->addColumn('pagepath', 'string', ['default' => '', 'limit' => 255, 'comment' => '小程序页面',])
->addColumn('media_id', 'string', ['default' => '', 'limit' => 100, 'comment' => '调用新增永久素材接口返回的合法media_id'])
->addColumn('created_at', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '创建时间',])
->addColumn('updated_at', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '更新时间',])
->addColumn('deleted_at', 'integer', ['limit' => MysqlAdapter::INT_REGULAR,'null' => false,'default' => 0,'signed' => true,'comment' => '软删除',])
->create();
}
}

View File

@ -0,0 +1,10 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------

View File

@ -0,0 +1,10 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------

View File

@ -0,0 +1,10 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------

View File

@ -0,0 +1,10 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------

View File

@ -0,0 +1,10 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------

View File

@ -0,0 +1,10 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------

View File

@ -0,0 +1,39 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\wechat\model;
use think\Model;
use catcher\traits\db\BaseOptionsTrait;
class WechatMenus extends Model
{
use BaseOptionsTrait;
protected $name = 'wechat_menus';
protected $field = [
'id', //
'name', // 菜单名称
'parent_id', // 父级ID
'type', // 类型
'key', // key
'url', // view 类型 url 链接
'appid', // 小程序 appid
'pagepath', // 小程序页面
'media_id', // 调用新增永久素材接口返回的合法media_id
'created_at', // 创建时间
'updated_at', // 更新时间
'deleted_at', // 软删除
];
}

View File

@ -0,0 +1,227 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\wechat\repository;
use catchAdmin\permissions\middleware\PermissionsMiddleware;
use catchAdmin\wechat\model\WechatMenus;
use catcher\base\CatchRepository;
use catcher\exceptions\FailedException;
use catcher\library\WeChat;
use catcher\Tree;
use catcher\Utils;
class WechatMenusRepository extends CatchRepository
{
protected $menus;
public function __construct(WechatMenus $menus)
{
$this->menus = $menus;
}
protected function model()
{
return $this->menus;
}
public function all()
{
$menus = $this->menus->select();
return Tree::done($menus->toArray());
}
/**
* 新增
*
* @time 2020年06月26日
* @param array $data
* @return mixed
*/
public function storeBy(array $data)
{
$parentId = $data['parent_id'] ?? 0;
$this->checkMenuNum($parentId);
$data['parent_id'] = $parentId;
$data['key'] = $data['type'] . '_' . rand(10000, 999999);
if (parent::storeBy($data)) {
return $this->syncToWechat();
}// TODO: Change the autogenerated stub
throw new FailedException('新增失败');
}
/**
* 更新
*
* @time 2020年06月26日
* @param int $id
* @param array $data
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @return bool
*/
public function updateBy(int $id, array $data)
{
if (parent::updateBy($id, $data)) {
return $this->syncToWechat();
}
throw new FailedException('更新失败');
}
/**
* 删除失败
*
* @time 2020年06月26日
* @param int $id
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @return bool
*/
public function deleteBy(int $id)
{
$menu = $this->findBy($id);
// 父级菜单
if (!$menu->parent_id) {
if ($this->menus->where('parent_id', $id)->count()) {
throw new FailedException('请先删除子级菜单');
}
}
if (parent::deleteBy($id)) {
return $this->syncToWechat();
}
throw new FailedException('删除失败');
}
/**
* 同步
*
* @time 2020年06月26日
* @throws \Exception
* @return bool
*/
public function sync()
{
// 同步前先删除
$this->menus->where('id', '>', 0)->delete();
$menus = WeChat::officialAccount()->menu->list()['menu']['button'];
foreach ($menus as $menu) {
$id = $this->menus->createBy($this->menuToLocal($menu));
if (!empty($menu['sub_button'])) {
foreach ($menu['sub_button'] as $button) {
$button['parent_id'] = $id;
$this->menus->createBy($this->menuToLocal($button));
}
}
}
return true;
}
/**
* 同步到表
*
* @time 2020年06月26日
* @param $menu
* @return array
*/
protected function menuToLocal($menu)
{
$data = [
'parent_id' => $menu['parent_id'] ?? 0,
'type' => $menu['type'] ?? '',
'name' => $menu['name'],
'key' => $menu['key'] ?? '',
'created_at' => \time(),
'updated_at' => \time(),
];
if (isset($menu['type'])) {
if ($menu['type'] == 'view') {
$data['url'] = $menu['url'];
return $data;
} elseif ($menu['type'] == 'miniprogram') {
$data['url'] = $menu['url'] ?? '';
$data['appid'] = $menu['appid'];
$data['pagepath'] = $menu['pagepath'];
return $data;
}
}
return $data;
}
/**
* 同步到微信
*
* @time 2020年06月26日
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @return bool
*/
protected function syncToWechat()
{
$menus = $this->menus->field([
'id', 'parent_id', 'key', 'name', 'type', 'url', 'appid', 'pagepath', 'media_id'
])->select()->toArray();
foreach ($menus as &$menu) {
if ($menu['type'] == 'view') {
unset($menu['appid'], $menu['media_id'], $menu['pagepath']);
} elseif ($menu['type'] == 'miniprogram') {
unset($menu['media_id']);
} else {
unset($menu['url'], $menu['appid'], $menu['pagepath'], $menu['media_id']);
}
}
$wechatMenus = Tree::done($menus, 0, 'parent_id', 'sub_button');
WeChat::throw(WeChat::officialAccount()->menu->create($wechatMenus));
return true;
}
/**
* check menus number
*
* @time 2020年06月26日
* @param $parentId
* @return void
*/
protected function checkMenuNum($parentId)
{
// 父级别分类
if (!$parentId) {
if ($this->menus->where('parent_id', 0)->count() >= 3) {
throw new FailedException('只支持三个一级菜单');
}
} else {
if ($this->menus->where('parent_id', $parentId)->count() >= 5) {
throw new FailedException('只支持五个二级菜单');
}
}
}
}

View File

@ -25,5 +25,10 @@ $router->group('wechat', function () use ($router){
}); });
// 消息 // 消息
$router->rule('message', '\catchAdmin\wechat\controller\Message@done', 'GET|POST'); $router->rule('message', '\catchAdmin\wechat\controller\Message@done', 'GET|POST');
// 微信菜单
$router->group('official/menus', function () use ($router){
$router->resource('', '\catchAdmin\wechat\controller\Menus');
$router->post('sync', '\catchAdmin\wechat\controller\Menus@sync');
});
})->middleware('auth'); })->middleware('auth');

View File

@ -3,9 +3,6 @@ declare (strict_types = 1);
namespace catcher\event; namespace catcher\event;
use catchAdmin\permissions\PermissionsMiddleware;
use catchAdmin\user\AuthTokenMiddleware;
use catcher\CatchAdmin;
use think\App; use think\App;
use think\Route; use think\Route;