<?php
namespace app\admin\traits;
use think\facade\Session;
use think\Db;
// 按钮操作类
trait BtnTrait {
    /**
     * 显示删除按钮
     * @param string $route 操作路径,格式:控制器/方法
     * @param int $id 数据ID
    */
    public function deleteButton($route, $id)
    {
        // 检测参数
        if (empty($id) || empty($route)) {
            return '';
        }
        $bool = $this->getUserAuth($route);
        if (!$bool) {
            return '';
        }
        return "<a class='btn btn-flat btn-warning btn-xs list_del del-btn' id='del-btn' data-id='".$id."'><i class='fa fa-trash-o'></i> 删除 </a>";
    }
    
    
    /**
     * 显示查看按钮
     * @param string $route 操作路径,格式:控制器/方法
     * @param int $id 数据ID
    */
    public function readButton($route, $id)
    {
        // 检测参数
        if (empty($id) || empty($route)) {
            return '';
        }
        $bool = $this->getUserAuth($route);
        if (!$bool) {
            return '';
        }
        $url = url($route, ['id'=>$id]);
        return "<a class='btn btn-flat btn-info btn-xs read-btn' href='".$url."'><i class='fa fa-edit'></i> 查看</a>";
    }
    
    
    public function sendButton($route, $id)
    {
        // 检测参数
        if (empty($id) || empty($route)) {
            return '';
        }
        $bool = $this->getUserAuth($route);
        if (!$bool) {
            return '';
        }
        $url = url($route);
        return "<a class='btn btn-flat btn-info btn-xs read-btn bg-green color-palette sendbutton' style='background-color: #1E9FFF!important;' href='".$url."' data-id='".$id."'><i class='fa fa-paper-plane'></i> 推送 </a>";
    }
    
    
    /**
     * 批量删除按钮
     * @param string $route 操作路径,格式:控制器/方法
    */
    public function batchDelButton($route) 
    {
        // 检测参数
        if (empty($route)) {
            return '';
        }
        $bool = $this->getUserAuth($route);
        if (!$bool) {
            return '';
        }
        return "<input class='btn btn-flat btn-warning m_10 delete_select' id='delete_select' type='button' value='批量删除'>";
    }
    
    /**
     * 批量推送按钮
     * @param string $route 操作路径,格式:控制器/方法
    */
    public function batchSendButton($route) 
    {
        // 检测参数
        if (empty($route)) {
            return '';
        }
        $bool = $this->getUserAuth($route);
        if (!$bool) {
            return '';
        }
        return "<input class='btn btn-flat btn-success m_10 send_select' id='send_select' type='button' value='批量推送'>";
    }
    
    
    /**
     * 全部删除按钮
     * @param string $route 操作路径,格式:控制器/方法
    */
    public function clearButton($route) 
    {
        // 检测参数
        if (empty($route)) {
            return '';
        }
        $bool = $this->getUserAuth($route);
        if (!$bool) {
            return '';
        }
        return "<input class='btn btn-flat btn-danger m_10 delete_all' type='button' value='清空所有'>";
    }
    
    
    /**
     * 编辑按钮
     * @param string $route 操作路径,格式:控制器/方法
     * @param int $id 数据ID
    */
    public function editButton($route, $id)
    {
        // 检测参数
        if (empty($id) || empty($route)) {
            return '';
        }
        $bool = $this->getUserAuth($route);
        if (!$bool) {
            return '';
        }
        $url = url($route, ['id'=>$id]);
        return "<a class='btn btn-flat btn-info btn-xs edit-btn' href='".$url."'><i class='fa fa-edit'></i> 编辑 </a>";
    }
    
    
    
    /**
     * 导入按钮
     * @param string $route 操作路径,格式:控制器/方法
    */
    public function importButton($route)
    {
        // 检测参数
        if (empty($route)) {
            return '';
        }
        $bool = $this->getUserAuth($route);
        if (!$bool) {
            return '';
        }
        return "<input class='btn btn-flat btn-info m_10 import' id='import' type='button' value='导入'>";
    }
    
    /**
     * 下载模板按钮
     * @param string $route 操作路径,格式:控制器/方法
    */
    public function downloadMbButton($route)
    {
        // 检测参数
        if (empty($route)) {
            return '';
        }
        $bool = $this->getUserAuth($route);
        if (!$bool) {
            return '';
        }
        return "<input class='btn btn-flat btn-warning m_10 download_mb' id='download_mb'  type='button' value='下载模板'>";
    }
    
    /**
     * 添加按钮
     * @param string $route 操作路径,格式:控制器/方法
    */
    public function addButton($route) 
    {
        // 检测参数
        if (empty($route)) {
            return '';
        }
        $bool = $this->getUserAuth($route);
        if (!$bool) {
            return '';
        }
        return "<a class='btn btn-flat btn-success m_10 f_r' href='".url($route)."'><i class='fa fa-plus m-r-10'></i>添 加</a>";
    }
    
    
    /**
     * 修改权限按钮
     * @param string $route 操作路径,格式:控制器/方法
     * @param int $id 数据ID
    */
    public function authButton($route, $id) 
    {
        // 检测参数
        if (empty($id) || empty($route)) {
            return '';
        }
        $bool = $this->getUserAuth($route);
        if (!$bool) {
            return '';
        }
        $url = url($route, ['id'=>$id]);
        return "<a class='btn btn-flat btn-danger btn-xs auth' id='auth' href='".$url."'><i class='fa fa-edit'></i> 权限 </a>";
    }
    
    /**
     * 隐藏列内容(检测用户是否有权限,有则显示,没有则隐藏)
     * @param string $url 操作路径,格式:控制器/方法
     * @return bool true表示显示列内容,false表示隐藏列内容
     */
    public function isHiddenField($url)
    {
        // 检测参数
        if (empty($url)) {
            return false;
        }
        $bool = $this->getUserAuth($url);
        if (!$bool) {
            return false;
        }
        return true;
    }
    
        /**
     * 添加按钮2
     * @param string $url 操作路径,格式:控制器/方法
     * @param int $id 数据ID
     */
    public function addButton2($url, $id)
    {
        // 检测参数
        if (empty($id) || empty($url)) {
            return '';
        }
        $bool = $this->getUserAuth($url);
        if (!$bool) {
            return '';
        }
        $url = url($url, ['id' => $id]);
        return "<a class='btn btn-flat btn-success btn-xs add-button2' href='".$url."'><i class='fa fa-plus'></i> 添加 </a>";
    }
    
        /**
     * 排序按钮
     * @param string $url 操作路径,格式:控制器/方法
     * @param int $id 数据ID
     * @param string $sort_value 排序值
     * @return string 按钮字符串
     */
    public function sortButton($url, $id, $sort_value)
    {
        // 检测参数
        if (empty($url) || empty($id)) {
            return '';
        }
        $bool = $this->getUserAuth($url);
        if (!$bool) {
            return '';
        }
        return "<input class='form-control input-sm w_40 m_auto sort-button' type='text' value='".$sort_value."' data-id='".$id."'>";
    }
    
        /**
     * 显示或隐藏操作按钮
     * @param string $url 操作路径,格式:控制器/方法
     * @param int $id 数据ID
     * @param string $status_value 状态值,1为显示,0为隐藏
     * @param string $filed_name 选择器值,用作CSS选择器,方便获取
     * @return string 按钮字符串
     */
    public function showAndHiddenButton($url, $id, $status_value, $filed_name)
    {
        // 检测参数
        if (empty($url) || empty($id) || empty($filed_name)) {
            return '';
        }
        $bool = $this->getUserAuth($url);
        if (!$bool) {
            return '';
        }
        if ($status_value == 1) {
            return "<a class='state_ok ".$filed_name."' data-id='".$id."'><i class='fa fa-fw fa-check-square'></i></a>";
        } else {
            return "<a class='state_no ".$filed_name."' data-id='".$id."'><i class='fa fa-fw fa-check-square'></i></a>";
        }
    }
    
    /**
     * 导出按钮
     * @param string $url 操作路径,格式:控制器/方法
     */
    public function exportButton($url)
    {
        // 检测参数
        if (empty($url)) {
            return '';
        }
        $bool = $this->getUserAuth($url);
        if (!$bool) {
            return '';
        }
        $url = url($url);
        return "<a class='btn btn-flat btn-danger m_10 f_r export-button' id='export-button' href='".$url."'><i class='fa fa-download m-r-10'></i>导出</a>";
    }
    
    
    public function viewButton() {
        return '<button onclick="view()">查看</button>';
    }
    
    // 检查用户是否具有权限
    public function getUserAuth($route)
    {
        // 检测用户ID
        $admin_id = Session::get('admin.id');
        if (empty($admin_id)) {
            return false;
        }
        // 检查用户所在的组
        $group_id = Db::name('auth_group_access')->where('uid', $admin_id)->value('group_id');
        if (empty($group_id)) {
            return false;
        }
        // 获取操作路径(即控制器/方法)的ID
        $auth_id = Db::name('auth_rule')->where('name', $route)->value('id');
        if (empty($auth_id)) {
            return false;
        }
        // 获取这个用户组所具有的权限
        $auth_ids = Db::name('auth_group')->where('id', $group_id)->value('rules');
        if (empty($auth_ids)) {
            return false;
        }
        // 比较权限
        $auth_ids_arr = explode(',',$auth_ids);
        if (in_array($auth_id, $auth_ids_arr)) {
            return true;
        }
        return false;
    }
}traits按钮
相关推荐
- 
                            生成图片from PIL import Image, ImageColor, ImageDraw, ImageFont, ImageFilterdef create_image_with_text(size, color, text, font_path, font_size, text_color, shadow_color, output_path): """ Create a new image of specified size and color with centered text that has a border and shadow. :param size: A tuple con 查看详情
- 
                            获取指定目录下的所有图片信息1 获取指定目录下的所有图片信息// 获取指定目录下的所有图片信息 public function getImagesInfo($directory) { $images = []; // 创建递归目录迭代器 $iterator = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::LEAVES_ONLY ); // 遍历目录中的每个文件 foreach ( 查看详情
- 
                            Thinkphp各版本的PHP要求ThinkPHP 8.0:运行环境要求PHP8.0+,兼容PHP8.3ThinkPHP 6.1:运行环境要求PHP7.2+,兼容PHP8.1ThinkPHP 6.0:运行环境要求PHP7.2+,兼容PHP8.1ThinkPHP 5.1:运行环境要求PHP5.6+,兼容PHP8.0ThinkPHP 5.0:运行环境要求PHP5.4+,兼容PHP7.3 查看详情
 智享笔记
								    智享笔记								 
                             
                             
                             
                            