## 知識點:
1、準備工作
2、添加規則
3、規則排序
4、規則刪除
5、編輯規則
[TOC]
## 一、準備工作
### 1、build建Authrule模型
簡單
### 2、引入類庫
use think\Controller;
use think\Db;
use util\Tree;
use app\admin\model\Authrule as AuthruleModel;
### 3、操作
~~~
class Authrule extends Common {
public function index($tab = 1, $id = 0){
$authruleArray = AuthruleModel::order('id')->select();
foreach ($authruleArray as $key => $value) {
$authruleList[] = $value->toArray(); //對象轉數組
}
$tree = new Tree();
$tree->tree($authruleList,'id','pid','title');
$authrule = $tree->getArray();
$this->assign('authrule',$authrule);
return view();
}
}
~~~
### 4、數據表不存在
解決方法
use think\Model;
~~~
class Authrule extends Model {
//當前模型對應的數據表名
protected $table = 'sfox_auth_rule';
}
~~~
### 5、樹型菜單
~~~
use util\Tree;
$authruleArray = AuthruleModel::order('id')->select();
foreach ($authruleArray as $key => $value) {
$authruleList[] = $value->toArray(); //對象轉數組
}
$tree = new Tree();
$tree->tree($authruleList,'id','pid','title');
$authrule = $tree->getArray();
$this->assign('authrule',$authrule);
~~~
tree有四個參數
參一:數組
參二:當前ID
參三:父ID
參數:中文名稱
### 6、模型
~~~
<?php
namespace app\admin\model;
use think\Model;
class Authrule extends Model {
//當前模型對應的數據表名
protected $table = 'sfox_auth_rule';
}
~~~
### 7、新增pid、child、listorder字段
pid 父ID,默認值0,無符號
child 是否子孫,tinyint 長度1,默認值0,無符號
listorder 排序smallint,默認值0,無符號
## 二、添加規則
### 1、模板
片段:
~~~
<option value="{$vo.id}" {$vo.id ? '' : 'disabled'} {if condition="input('pid',0) eq $vo.id"}selected{/if}>{$vo.title}</option>
~~~
~~~
<div class="form-group">
<label class="col-sm-2 control-label">上級規則</label>
<div class="col-sm-10">
<select class="form-control m-b" name="pid">
<option value="0" selected>≡ 頂級 ≡</option>
{volist name="authrule" id="vo"}
<option value="{$vo.id}" {$vo.id ? '' : 'disabled'} {if condition="input('pid',0) eq $vo.id"}selected{/if}>{$vo.title}</option>
{/volist}
</select>
</div>
</div>
~~~
### 2、添加
~~~
public function add($tab = 1){
if(request()->isPost()){
$authruleModel = new AuthruleModel;
if($authruleModel->allowField(true)->save(input('post.'))){
return success('新規則添加成功!',url('index',['tab'=>1]));
}else{
return error('規則添加失敗!',url('index',['tab'=>$tab]));
}
}
}
~~~
## 三、規則排序
~~~
public function index($tab = 1, $id = 0){
if(request()->isPost()){
foreach (input('post.listorder/a') as $key => $value) {
Db::name('auth_rule')->where('id',$key)->update(['listorder'=>$value]);
}
return success('排序更新成功!',url('index',['tab'=>$tab]));
}else{
$authruleArray = AuthruleModel::order('id')->select();
foreach ($authruleArray as $key => $value) {
$authruleList[] = $value->toArray(); //對象轉數組
}
$tree = new Tree();
$tree->tree($authruleList,'id','pid','title');
$authrule = $tree->getArray();
$this->assign('authrule',$authrule);
// 編輯規則
if( 3 == $tab ){
// 獲取所要編輯菜單的信息
$info = Db::name('auth_rule')->where('id',$id)->find();
if($info!=null && is_array($info)){
$this->assign('info',$info);
}
}
}
return view();
}
~~~
## 四、規則刪除
~~~
public function delete($id = 0){
if(Db::name('auth_rule')->where('id', $id)->delete()){
return success('刪除成功!',url('index',['tab'=>1]));
}else{
return error('刪除失敗!',url('index',['tab'=>1]));
}
}
~~~
## 五、編輯規則
### 1、獲取所要編輯菜單的信息
~~~
public function index($tab = 1, $id = 0){
if(request()->isPost()){
foreach (input('post.listorder/a') as $key => $value) {
Db::name('auth_rule')->where('id',$key)->update(['listorder'=>$value]);
}
return success('排序更新成功!',url('index',['tab'=>$tab]));
}else{
$authruleArray = AuthruleModel::order('id')->select();
foreach ($authruleArray as $key => $value) {
$authruleList[] = $value->toArray(); //對象轉數組
}
$tree = new Tree();
$tree->tree($authruleList,'id','pid','title');
$authrule = $tree->getArray();
$this->assign('authrule',$authrule);
// 編輯規則
if( 3 == $tab ){
// 獲取所要編輯菜單的信息
$info = Db::name('auth_rule')->where('id',$id)->find();
if($info!=null && is_array($info)){
$this->assign('info',$info);
}
}
}
return view();
}
~~~
### 2、編輯操作
~~~
public function edit($tab = 1, $id = 0){
if(request()->isPost()){
$authrule_form = input('post.');
//判斷child狀態
if(input('post.child')==null){
$authrule_form['child'] = 0;
$count = Db::name('auth_rule')->where('pid',$id)->count();
if($count){
return error('該規則擁有子項,無法取消勾選!');
}
}
// 編輯規則
$authruleModel = new AuthruleModel;
if($authruleModel->allowField(true)->isUpdate()->save($authrule_form)){
return success('規則編輯成功!',url('index',['tab'=>1]));
}else{
return error('規則信息未修改或編輯失敗!');
}
}
}
~~~
- Layer無刷新不跳轉彈框提示信息
- 整合ThinkPHP+實用代碼
- TP整合Layer插件實現無刷新
- 自定義助手函數
- 添加信息失敗后不跳轉
- 三種無限級分類
- TP常用代碼
- 自定義公共函數
- TP模型管理專題
- TP模型管理之添加模型
- sfox_newmodel.sql
- TP模型管理之刪除模型
- TP模型管理之編輯模型
- TP模型管理之字段添加
- sfox_newmodel.sql_edit
- layer_hplus.js_edit
- TP模型管理之字段刪除
- TP模型管理之字段編輯
- TP模型管理之預覽模型
- TP模型管理之公共函數
- layer_hplus.js_修訂一
- TP模型管理之預覽模型靜態頁
- 后臺內容管理系統
- 分類樹顯示
- 內容列表顯示
- 信息發布
- 編輯信息
- layer_hplus.js
- myJs第一版
- myJs第二版
- myJs第三版
- myJs第四版
- TP5插件用法
- Datatables
- WebUploader
- bootstrap-fileinput
- UEditor
- 簡單調用
- 路徑問題
- 跨域多圖上傳
- 跨域單圖上傳
- UEditor圖片跨域上傳解決方案
- 定制工具欄圖標
- ajaxFileUpload
- LayUI
- 圖片上傳
- layui分頁
- 搜索頁
- 搜索優化及刪除
- Uploadify
- TP5前端應用
- 靜態首頁
- 前臺首頁功能實現
- 自定義標簽庫
- 前臺模板繼承應用
- 首頁自定義標簽改進
- 文章內容頁
- 自定義標簽改進
- 自定義標簽修正
- 圖片等比例自動縮放
- 后臺權限管理
- 角色管理
- 規則管理
- 權限設置
- 會員管理
- 權限管理
- 前臺登錄注冊功能
- 注冊登錄
- 阿里大于手機注冊
- 阿里大于升級阿里云短信服務
- 自動登錄完成
- PHP異位或加密實現自動登陸
- 微信開發
- 分享接口
- 靜態頁面實現微信分享
- 動態頁微信分享
- 頁面靜態化
- 1-全站靜態化前期配置
- 2-鏈接地址靜態化
- TP5常用片段代碼
- 加載靜態資源路徑與常量
- thinkphp5預定義常量
- 刪除某文件夾的內容
- 解壓插件包
- 異步提交插件
- 其他功能
- 背景音樂
- 手機訪問PC網站自動跳轉到手機網站代碼
- 手機微信音樂MP3播放器
- 后盾之網頁背景音樂
- 播放器寬度自適應
- 前臺首頁數據調用
- 視頻列表
- 搜索分頁
- H5解決蘋果(IOS)不能自動播放音樂
- 清空緩存
- 文件處理常識
- 刪除路徑下的所有文件夾和文件
- 一鍵清空緩存
- 評論留言
- 格式化時間
- 替換微博內容的URL地址@用戶與表情
- PHP正則理解
- jQuery評論插件
- TP空操作
- TP路由
- 跨域訪問
- 設置請其頭允許跨域請求
- 模板前臺判斷手機訪問跳轉手機網址代碼
- PHP遍歷一個文件夾下所有文件和子文件夾
- PHP獲取視頻的第一幀與時長
- TP5數據庫
- 鏈式操作原理
- update替換字段部分內容
- 后臺開發
- 后臺登錄頁居中顯示
- TP5自帶驗證碼
- JS & JQuery專題
- 二級城市聯動菜單
- 模板引擎
- 混合模板編譯
- 黃永成TP微博開發
- 消息推送
- memcache安裝
- 插件開發
- 插件介紹
- 插件鉤子
- 淺談初步理解鉤子
- 插件鉤子(hooks)分析
- 插件鉤子簡單理解
- 控制器調用插件
- 鉤子通用處理函數
- 插件基類代碼
- 插件測試代碼
- 淺談鉤子與插件
- 技術綜合
- 常用代碼
- PHP
- 56個PHP開發常用代碼片段(上)
- 56個PHP 開發常用代碼片段(中)
- 56個PHP 開發常用代碼片段(下)
- sublime text安裝自動補全注釋的插件
- 影音視頻開發
- 視頻
- H5視頻直播掃盲
- 音樂
- 語音
- PHP實現語音播報功能
- MUI
- 窗體操作