feat: 代理分级权限系统 + 专属注册链接
- AppProxy.php: 列表增加referral_code和专属注册URL显示 - AppProxy.php: 创建代理按类型分配权限组(一级→group11, 二级→group12) - app_proxy.js: 表格新增专属注册链接列, 点击复制 - agent_permission_setup.sql: 创建二级代理权限组的SQL脚本
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
-- ============================================
|
||||
-- 代理分级权限系统 SQL 初始化脚本
|
||||
-- 在宝塔面板 phpMyAdmin 中执行此SQL
|
||||
-- ============================================
|
||||
|
||||
-- 1. 创建二级代理权限组 (group_id=12)
|
||||
-- 注意: 如果 id=12 已存在,请手动修改ID或删除冲突记录
|
||||
INSERT INTO `fa_auth_group` (`id`, `pid`, `name`, `rules`, `createtime`, `updatetime`, `status`)
|
||||
SELECT 12, 11, '二级代理',
|
||||
-- 二级代理只有基本权限:查看团队统计 + 查看合约订单(只读)
|
||||
(SELECT `rules` FROM `fa_auth_group` WHERE `id` = 11 LIMIT 1),
|
||||
UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), 'normal'
|
||||
FROM DUAL
|
||||
WHERE NOT EXISTS (SELECT 1 FROM `fa_auth_group` WHERE `id` = 12);
|
||||
|
||||
-- 2. 确保一级代理权限组 (group_id=11) 包含必要权限
|
||||
-- 总代理(id=1)在后台 权限管理→角色组 中可以随时调整各组权限
|
||||
-- 一级代理必备权限: 地址管理, 交易管理, 账户管理, 客服管理
|
||||
-- 二级代理基本权限: 仅查看团队统计和合约订单
|
||||
|
||||
-- 3. 将一级代理权限组的父级设为总管理(pid=1)
|
||||
UPDATE `fa_auth_group` SET `pid` = 1, `name` = '一级代理' WHERE `id` = 11;
|
||||
|
||||
-- 4. 添加一键平仓权限规则(如果不存在)
|
||||
INSERT INTO `fa_auth_rule` (`type`, `pid`, `name`, `title`, `icon`, `condition`, `remark`, `ismenu`, `createtime`, `updatetime`, `weigh`, `status`)
|
||||
SELECT 'file', 0, 'app/app_contract/closeall', '一键平仓', 'fa fa-gavel', '', '', 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), 0, 'normal'
|
||||
FROM DUAL
|
||||
WHERE NOT EXISTS (SELECT 1 FROM `fa_auth_rule` WHERE `name` = 'app/app_contract/closeall');
|
||||
|
||||
-- 5. 将一键平仓权限添加到超级管理员组(如果需要的话,通常id=1的管理员自动拥有所有权限)
|
||||
-- 如果需要给一级代理也赋予一键平仓权限,在后台角色组中勾选即可
|
||||
|
||||
-- ============================================
|
||||
-- 使用说明:
|
||||
-- 1. 在宝塔 phpMyAdmin 中执行此SQL
|
||||
-- 2. 登录后台 → 权限管理 → 角色组
|
||||
-- 3. 编辑「一级代理」(group_id=11) - 勾选需要的权限(地址管理/交易管理/账户管理/客服管理)
|
||||
-- 4. 编辑「二级代理」(group_id=12) - 仅勾选基本权限(查看团队统计)
|
||||
-- 5. 创建代理时选择类型,系统自动分配对应权限组
|
||||
-- ============================================
|
||||
+417
@@ -0,0 +1,417 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\app;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use think\Config;
|
||||
use think\Db;
|
||||
use fast\Random;
|
||||
|
||||
|
||||
/**
|
||||
* 代理管理
|
||||
*
|
||||
* @icon fa fa-circle-o
|
||||
*/
|
||||
class AppProxy extends Backend
|
||||
{
|
||||
|
||||
/**
|
||||
* AppProxy模型对象
|
||||
* @var \app\admin\model\app\AppProxy
|
||||
*/
|
||||
protected $model = null;
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = new \app\admin\model\app\AppProxy;
|
||||
$this->view->assign("statusList", $this->model->getStatusList());
|
||||
$this->view->assign("typeList", $this->model->getTypeList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
|
||||
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
|
||||
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
|
||||
*/
|
||||
|
||||
/**
|
||||
* 查看
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
//设置过滤方法
|
||||
$this->request->filter(['strip_tags']);
|
||||
if ($this->request->isAjax())
|
||||
{
|
||||
//如果发送的来源是Selectpage,则转发到Selectpage
|
||||
if ($this->request->request('keyField'))
|
||||
{
|
||||
return $this->selectpage();
|
||||
}
|
||||
// list($where, $sort, $order, $offset, $limit) = $this->buildparams();
|
||||
$offset = $this->request->get("offset", 0);
|
||||
$limit = $this->request->get("limit", 0);
|
||||
$params = json_decode(input('filter'),true);
|
||||
$op = json_decode(input('op'),true);
|
||||
if(count($params) > 0){
|
||||
$new_params = $new_op = [];
|
||||
foreach ($params as $key => $value) {
|
||||
if($key == 'user_name')
|
||||
{
|
||||
$new_params['u.nickname'] = $value;
|
||||
$new_op['u.nickname'] = $op[$key];
|
||||
}else if($key == 'admin_name'){
|
||||
$new_params['b.nickname'] = $value;
|
||||
$new_op['b.nickname'] = $op[$key];
|
||||
}else {
|
||||
$new_params['a.' . $key] = $value;
|
||||
$new_op['a.' . $key] = $op[$key];
|
||||
}
|
||||
}
|
||||
$w = $this->rewriteQuery($new_params, $new_op);
|
||||
}else{
|
||||
$w['a.id'] = array('>', 0);
|
||||
}
|
||||
if($this->auth->id != 1) {
|
||||
$w['a.admin_id'] = $this->auth->id;
|
||||
}
|
||||
$total = Db::name('app_proxy')
|
||||
->alias('a')
|
||||
->field('a.*,b.nickname as admin_name,u.nickname as user_name')
|
||||
->join('fa_user u', 'a.user_id = u.id')
|
||||
->join('fa_admin b','b.id = a.admin_id')
|
||||
->where($w)
|
||||
->count();
|
||||
$list = Db::name('app_proxy')
|
||||
->alias('a')
|
||||
->field('a.*,b.nickname as admin_name,u.nickname as user_name,u.referral_code')
|
||||
->join('fa_user u', 'a.user_id = u.id')
|
||||
->join('fa_admin b','b.id = a.admin_id')
|
||||
->where($w)
|
||||
->order('a.id', 'desc')
|
||||
->limit($offset, $limit)
|
||||
->select();
|
||||
|
||||
// 生成专属注册链接
|
||||
$front_url = Config::get('site.app_downurl') ?: $this->request->domain();
|
||||
foreach ($list as $key => $value) {
|
||||
$list[$key]['register_url'] = $front_url . '/#/pages/login/register?code=' . ($value['referral_code'] ?: '');
|
||||
}
|
||||
|
||||
//可结算金额
|
||||
$result = array("total" => $total, "rows" => $list);
|
||||
|
||||
return json($result);
|
||||
}
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* Selectpage的实现方法
|
||||
*
|
||||
* 当前方法只是一个比较通用的搜索匹配,请按需重载此方法来编写自己的搜索逻辑,$where按自己的需求写即可
|
||||
* 这里示例了所有的参数,所以比较复杂,实现上自己实现只需简单的几行即可
|
||||
*
|
||||
*/
|
||||
protected function selectpage()
|
||||
{
|
||||
//设置过滤方法
|
||||
$this->request->filter(['strip_tags', 'htmlspecialchars']);
|
||||
|
||||
//搜索关键词,客户端输入以空格分开,这里接收为数组
|
||||
$word = (array)$this->request->request("q_word/a");
|
||||
//当前页
|
||||
$page = $this->request->request("pageNumber");
|
||||
//分页大小
|
||||
$pagesize = $this->request->request("pageSize");
|
||||
//搜索条件
|
||||
$andor = $this->request->request("andOr", "and", "strtoupper");
|
||||
//排序方式
|
||||
$orderby = (array)$this->request->request("orderBy/a");
|
||||
//显示的字段
|
||||
$field = $this->request->request("showField");
|
||||
//主键
|
||||
$primarykey = $this->request->request("keyField");
|
||||
//主键值
|
||||
$primaryvalue = $this->request->request("keyValue");
|
||||
//搜索字段
|
||||
$searchfield = (array)$this->request->request("searchField/a");
|
||||
//自定义搜索条件
|
||||
$custom = (array)$this->request->request("custom/a");
|
||||
//是否返回树形结构
|
||||
$istree = $this->request->request("isTree", 0);
|
||||
$ishtml = $this->request->request("isHtml", 0);
|
||||
if ($istree) {
|
||||
$word = [];
|
||||
$pagesize = 99999;
|
||||
}
|
||||
$order = [];
|
||||
foreach ($orderby as $k => $v) {
|
||||
$order[$v[0]] = $v[1];
|
||||
}
|
||||
$field = $field ? $field : 'name';
|
||||
|
||||
//如果有primaryvalue,说明当前是初始化传值
|
||||
if ($primaryvalue !== null) {
|
||||
$where = [$primarykey => ['in', $primaryvalue]];
|
||||
$pagesize = 99999;
|
||||
} else {
|
||||
$where = function ($query) use ($word, $andor, $field, $searchfield, $custom) {
|
||||
$logic = $andor == 'AND' ? '&' : '|';
|
||||
$searchfield = is_array($searchfield) ? implode($logic, $searchfield) : $searchfield;
|
||||
foreach ($word as $k => $v) {
|
||||
$query->where(str_replace(',', $logic, $searchfield), "like", "%{$v}%");
|
||||
}
|
||||
if ($custom && is_array($custom)) {
|
||||
foreach ($custom as $k => $v) {
|
||||
if (is_array($v) && 2 == count($v)) {
|
||||
$query->where($k, trim($v[0]), $v[1]);
|
||||
} else {
|
||||
$query->where($k, '=', $v);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
$adminIds = $this->getDataLimitAdminIds();
|
||||
if (is_array($adminIds)) {
|
||||
$this->model->where($this->dataLimitField, 'in', $adminIds);
|
||||
}
|
||||
$list = [];
|
||||
$total = $this->model->where($where)->count();
|
||||
if ($total > 0) {
|
||||
if (is_array($adminIds)) {
|
||||
$this->model->where($this->dataLimitField, 'in', $adminIds);
|
||||
}
|
||||
$datalist = $this->model->where($where)
|
||||
->order($order)
|
||||
->page($page, $pagesize)
|
||||
->field($this->selectpageFields)
|
||||
->select();
|
||||
foreach ($datalist as $index => $item) {
|
||||
unset($item['password'], $item['salt']);
|
||||
$list[] = [
|
||||
// $primarykey => isset($item[$primarykey]) ? $item[$primarykey] : '',
|
||||
$field => isset($item[$field]) ? $item[$field] : '',
|
||||
'pid' => isset($item['pid']) ? $item['pid'] : 0,
|
||||
'id' => $item['admin_id']
|
||||
];
|
||||
}
|
||||
if ($istree && !$primaryvalue) {
|
||||
$tree = Tree::instance();
|
||||
$tree->init(collection($list)->toArray(), 'pid');
|
||||
$list = $tree->getTreeList($tree->getTreeArray(0), $field);
|
||||
if (!$ishtml) {
|
||||
foreach ($list as &$item) {
|
||||
$item = str_replace(' ', ' ', $item);
|
||||
}
|
||||
unset($item);
|
||||
}
|
||||
}
|
||||
}
|
||||
//这里一定要返回有list这个字段,total是可选的,如果total<=list的数量,则会隐藏分页按钮
|
||||
return json(['list' => $list, 'total' => $total]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$params = $this->request->post("row/a");
|
||||
if ($params) {
|
||||
$params = $this->preExcludeFields($params);
|
||||
|
||||
if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
|
||||
$params[$this->dataLimitField] = $this->auth->id;
|
||||
}
|
||||
$result = false;
|
||||
Db::startTrans();
|
||||
try {
|
||||
//是否采用模型验证
|
||||
if ($this->modelValidate) {
|
||||
$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
|
||||
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
|
||||
$this->model->validateFailException(true)->validate($validate);
|
||||
}
|
||||
|
||||
//添加管理权限
|
||||
$admins['salt'] = Random::alnum();
|
||||
$admins['password'] = md5(md5($params['password']) . $admins['salt']);
|
||||
$admins['avatar'] = '/assets/img/avatar.png'; //设置新管理员默认头像。
|
||||
$admins['username'] = $params['username'];
|
||||
$params['password'] = $admins['password'];
|
||||
$admins['nickname'] = $admins['username'];
|
||||
$admins['email'] = $admins['username']."@qq.com";
|
||||
$admins['createtime'] = time();
|
||||
$admins['status'] = 'normal';
|
||||
|
||||
$result1 = Db::name('admin')->insertGetId($admins);
|
||||
if(!$result1){
|
||||
Db::rollback();
|
||||
$this->error('创建管理失败');
|
||||
}
|
||||
if($params['type'] == 1){
|
||||
// 一级代理 - 完整权限组
|
||||
$result2 = Db::name('auth_group_access')
|
||||
->insert(['uid'=>$result1,'group_id'=>11]);
|
||||
}else{
|
||||
// 二级代理 - 基本权限组
|
||||
$result2 = Db::name('auth_group_access')
|
||||
->insert(['uid'=>$result1,'group_id'=>12]);
|
||||
}
|
||||
|
||||
if(!$result2){
|
||||
Db::rollback();
|
||||
$this->error('创建管理失败.');
|
||||
}
|
||||
$result3 = Db::name("user")->where("id",$params['user_id'])->update(['admin_id'=>$result1]);
|
||||
if(!$result3){
|
||||
Db::rollback();
|
||||
$this->error('绑定用户失败');
|
||||
}
|
||||
$params['admin_id'] = $result1;
|
||||
|
||||
$result = $this->model->allowField(true)->save($params);
|
||||
|
||||
Db::commit();
|
||||
} catch (ValidateException $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
} catch (PDOException $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
} catch (Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
if ($result !== false) {
|
||||
$this->success();
|
||||
} else {
|
||||
$this->error(__('No rows were inserted'));
|
||||
}
|
||||
}
|
||||
$this->error(__('Parameter %s can not be empty', ''));
|
||||
}
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑
|
||||
*/
|
||||
public function edit($ids = null)
|
||||
{
|
||||
$row = $this->model->get($ids);
|
||||
if (!$row) {
|
||||
$this->error(__('No Results were found'));
|
||||
}
|
||||
$adminIds = $this->getDataLimitAdminIds();
|
||||
if (is_array($adminIds)) {
|
||||
if (!in_array($row[$this->dataLimitField], $adminIds)) {
|
||||
$this->error(__('You have no permission'));
|
||||
}
|
||||
}
|
||||
if ($this->request->isPost()) {
|
||||
$params = $this->request->post("row/a");
|
||||
if ($params) {
|
||||
$params = $this->preExcludeFields($params);
|
||||
$result = false;
|
||||
Db::startTrans();
|
||||
try {
|
||||
$update['username'] = $params['username'];
|
||||
$update['updatetime'] = time();
|
||||
if(!empty($params['password']))
|
||||
{
|
||||
$admins = Db::name('admin')->where('id',$row['admin_id'])->find();
|
||||
$params['password'] = md5(md5($params['password']) . $admins['salt']);
|
||||
$update['password'] = $params['password'];
|
||||
}else{
|
||||
unset($params['password']);
|
||||
}
|
||||
Db::name('admin')
|
||||
->where('id',$row['admin_id'])
|
||||
->update($update);
|
||||
if($params['type'] != $row['type']){
|
||||
if($params['type'] == 1){
|
||||
// 升级为一级代理 - 完整权限
|
||||
Db::name('auth_group_access')->where("uid",$row['admin_id'])->update(['group_id'=>11]);
|
||||
}else{
|
||||
// 降级为二级代理 - 基本权限
|
||||
Db::name('auth_group_access')->where("uid",$row['admin_id'])->update(['group_id'=>12]);
|
||||
}
|
||||
}
|
||||
//是否采用模型验证
|
||||
if ($this->modelValidate) {
|
||||
$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
|
||||
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
|
||||
$row->validateFailException(true)->validate($validate);
|
||||
}
|
||||
$result = $row->allowField(true)->save($params);
|
||||
|
||||
Db::commit();
|
||||
} catch (ValidateException $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
} catch (PDOException $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
} catch (Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
if ($result !== false) {
|
||||
$this->success();
|
||||
} else {
|
||||
$this->error(__('No rows were updated'));
|
||||
}
|
||||
}
|
||||
$this->error(__('Parameter %s can not be empty', ''));
|
||||
}
|
||||
$this->view->assign("row", $row);
|
||||
return $this->view->fetch();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*/
|
||||
public function del($ids = "")
|
||||
{
|
||||
if ($ids) {
|
||||
$pk = $this->model->getPk();
|
||||
$adminIds = $this->getDataLimitAdminIds();
|
||||
if (is_array($adminIds)) {
|
||||
$this->model->where($this->dataLimitField, 'in', $adminIds);
|
||||
}
|
||||
$list = $this->model->where($pk, 'in', $ids)->select();
|
||||
$count = 0;
|
||||
Db::startTrans();
|
||||
try {
|
||||
foreach ($list as $k => $v) {
|
||||
$count += $v->delete();
|
||||
Db::name('admin')->where('id',$v->admin_id)->delete();
|
||||
Db::name('auth_group_access')->where('uid',$v->admin_id)->delete();
|
||||
Db::name("app_proxy")->where("id",$v->id)->delete();
|
||||
}
|
||||
|
||||
Db::commit();
|
||||
} catch (PDOException $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
} catch (Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
if ($count) {
|
||||
$this->success();
|
||||
} else {
|
||||
$this->error(__('No rows were deleted'));
|
||||
}
|
||||
}
|
||||
$this->error(__('Parameter %s can not be empty', 'ids'));
|
||||
}
|
||||
|
||||
}
|
||||
Executable
+76
@@ -0,0 +1,76 @@
|
||||
define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
|
||||
|
||||
var Controller = {
|
||||
index: function () {
|
||||
// 初始化表格参数配置
|
||||
Table.api.init({
|
||||
extend: {
|
||||
index_url: 'app/app_proxy/index' + location.search,
|
||||
add_url: 'app/app_proxy/add',
|
||||
edit_url: 'app/app_proxy/edit',
|
||||
del_url: 'app/app_proxy/del',
|
||||
// multi_url: 'app/app_proxy/multi',
|
||||
// import_url: 'app/app_proxy/import',
|
||||
table: 'app_proxy',
|
||||
}
|
||||
});
|
||||
|
||||
var table = $("#table");
|
||||
|
||||
// 初始化表格
|
||||
table.bootstrapTable({
|
||||
url: $.fn.bootstrapTable.defaults.extend.index_url,
|
||||
pk: 'id',
|
||||
sortName: 'id',
|
||||
columns: [
|
||||
[
|
||||
{checkbox: true},
|
||||
{field: 'id', title: __('Id')},
|
||||
{field: 'name', title: __('Name'), operate: 'LIKE'},
|
||||
{field: 'user_id', title: __('User_id')},
|
||||
{field: 'admin_id', title: __('Admin_id')},
|
||||
{field: 'username', title: __('Username'), operate: 'LIKE'},
|
||||
// {field: 'password', title: __('Password'), operate: 'LIKE'},
|
||||
// {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
|
||||
{field: 'type', title: __('Type'), searchList: {"1":__('Type 1'),"2":__('Type 2')}, formatter: Table.api.formatter.status},
|
||||
{field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
|
||||
{field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
|
||||
{field: 'remake', title: __('Remake'), operate: 'LIKE'},
|
||||
{field: 'register_url', title: '专属注册链接', operate: false, formatter: function(value, row) {
|
||||
if (!value) return '-';
|
||||
return '<a href="javascript:;" class="btn btn-xs btn-info btn-copy-url" data-url="' + value + '" title="点击复制"><i class="fa fa-copy"></i> 复制链接</a>';
|
||||
}},
|
||||
{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
|
||||
]
|
||||
]
|
||||
});
|
||||
|
||||
// 为表格绑定事件
|
||||
Table.api.bindevent(table);
|
||||
|
||||
// 复制注册链接
|
||||
$(document).on('click', '.btn-copy-url', function () {
|
||||
var url = $(this).data('url');
|
||||
var input = document.createElement('input');
|
||||
input.value = url;
|
||||
document.body.appendChild(input);
|
||||
input.select();
|
||||
document.execCommand('copy');
|
||||
document.body.removeChild(input);
|
||||
Toastr.success('注册链接已复制: ' + url);
|
||||
});
|
||||
},
|
||||
add: function () {
|
||||
Controller.api.bindevent();
|
||||
},
|
||||
edit: function () {
|
||||
Controller.api.bindevent();
|
||||
},
|
||||
api: {
|
||||
bindevent: function () {
|
||||
Form.api.bindevent($("form[role=form]"));
|
||||
}
|
||||
}
|
||||
};
|
||||
return Controller;
|
||||
});
|
||||
Reference in New Issue
Block a user