我們在制作站點(diǎn)用戶權(quán)限auth
的過程中滞诺,通常需要統(tǒng)計站點(diǎn)的方法action
。如果人為的去統(tǒng)計action
的話环疼,效率會非常低习霹,所以我們需要寫一個自動采集action
的方法。
例:
action
表設(shè)計如下:
image.png
我們在頁面中點(diǎn)擊 刷新 按鈕炫隶,便可以將站點(diǎn)的方法action
進(jìn)行統(tǒng)計淋叶,更新、插入action數(shù)據(jù)表
image.png
代碼舉例:
(點(diǎn)擊刷新按鈕伪阶,觸發(fā)事件 --- 后端執(zhí)行refreshActionList()
煞檩,此處做忽略。)
刷新采集action
方法refreshActionList()
如下:
/**
* 刷新功能列表
* @return \think\response\Json
*/
public function refreshActionList()
{
//獲取action數(shù)據(jù)表中現(xiàn)有節(jié)點(diǎn)
$nodeUrl = Db::name('action')->column('action_url');
$node = new NodeService(); // 實(shí)例化采集服務(wù)---之后會詳細(xì)說明
$modules = $node->getModule(); //采集站點(diǎn)所有模塊
$i = 0;
$result = array();
$moArr = array('common','Common','services','Services','behavior','Behavior','facade','Facade','push','Push');//待過濾模塊組
foreach ($modules as $module) {
if(!in_array($module, $moArr)){ //過濾模塊
$all_controller = $node->getController($module); // 采集當(dāng)前模塊所有控制器
if(!$all_controller) continue;
foreach ($all_controller as $controller) {
$controller_name = $module.'/'.$controller;
$all_action = $node->getAction($controller_name); // 采集當(dāng)前控制器下所有 action
foreach ($all_action as $action) {
$str = $module.'/'.$controller.'/'.$action;
//如果當(dāng)前節(jié)點(diǎn)url不在數(shù)據(jù)庫中,說明是一個新節(jié)點(diǎn)需要添加到數(shù)據(jù)庫
if(!in_array($str, $nodeUrl)){
$result[$i]['action_url'] = $str;
$result[$i]['module'] = $module;
$i++;
}
}
}
}
}
$tran_result = true;
Db::startTrans();//開啟事務(wù)
try {// 異常處理
//將新節(jié)點(diǎn)批量插入到 action數(shù)據(jù)表 中
Db::name('action')->insertAll($result);
} catch (Exception $ex) {
$tran_result = false;
// 記錄日志
Log::record($ex->getMessage(), 'DEBUG');
}
if ($tran_result === false) {
Db::rollback();
$data['code'] = 0; //更新失敗
$data['msg'] = '刷新失敗';
Log::record("refresh:刷新失敗", 'operate');
} else {
Db::commit();
$data['code'] = 1; //更新成功
$data['msg'] = '刷新成功';
Log::record("refresh:刷新成功", 'operate');
}
return json($data);
}
在此我們將采集 model
栅贴、controller
斟湃、action
的方法封裝成服務(wù) NodeService.php
image.png
NodeService.php
代碼如下:
<?php
namespace app\services;
use think\facade\Env;
class NodeService
{
public function __construct(){
//echo 'action';
}
//獲取應(yīng)用下的模塊名稱
public function getModule(){
$dir = array();
define('APP_PATH', Env::get('app_path')); // 定義應(yīng)用目錄
$path = APP_PATH . '/*/';
$dir_name = glob($path); // glob() 函數(shù)返回匹配指定模式的文件名或目錄
foreach ($dir_name as $value){
array_push($dir, basename($value)); // basename() 函數(shù)返回路徑中的文件名部分。
}
return $dir;
}
//獲取所有控制器名稱
public function getController($module){
if(empty($module)) return false;
$module_path = APP_PATH . '/' . $module . '/controller/'; //控制器路徑
if(!is_dir($module_path)) return false;
$module_path .= '/*.php';
$ary_files = glob($module_path);
$files = array();
foreach ($ary_files as $file) {
if (is_dir($file)) {
continue;
}else {
$files[] = basename($file, '.php');
}
}
return $files;
}
//獲取所有方法名稱
public function getAction($controller){
if(empty($controller)) return false;
$con = controller($controller); // 實(shí)例化 controller
$functions = get_class_methods($con); //get_class_methods()返回由類的方法名組成的數(shù)組
$customer_functions = [];
//定義需排除的方法
$inherents_functions = array('_initialize','__construct','getActionName','isAjax','display','show','fetch','buildHtml','assign','theme','__set','get','__get','__isset','__call','error','success','ajaxReturn','redirect','__destruct', '_empty');
foreach ($functions as $func){
if(!in_array($func, $inherents_functions)){
$customer_functions[] = $func;
}
}
return $customer_functions;
}
}
至此:當(dāng)我們在頁面上點(diǎn)擊刷新按鈕檐薯,調(diào)用refreshActionList()
方法凝赛,便可以更新站點(diǎn)的所有 action
注:轉(zhuǎn)載請注明出處,尊重原創(chuàng)坛缕。歡迎大家來簡書關(guān)注筆者墓猎。也更加感謝大家的打賞與支持。謝謝赚楚!