Files
li 1b24994e74 feat: 后端全量更新 - 含所有本次需求
- Contract.php: 返回合约账户余额(balance_contract)
- My.php: 地址管理增加BTC/ETH
- AppContract.php: 一键平仓(closeall)
- AppProxy.php: 代理专属注册链接 + 分级权限(L1/L2)
- site.php: 手续费减半(0.018→0.009)
- agent_permission_setup.sql: 代理权限SQL
- crypto_news_crawler.py: 新闻自动采集脚本
2026-03-30 20:16:32 +08:00

418 lines
16 KiB
PHP
Executable File

<?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('&nbsp;', ' ', $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'));
}
}