From 809e5b44e59fecc89ba4cbe9aaa81e3b90c43f7c Mon Sep 17 00:00:00 2001 From: li Date: Mon, 30 Mar 2026 15:15:17 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=90=8E=E5=8F=B0=E4=B8=80=E9=94=AE?= =?UTF-8?q?=E5=B9=B3=E4=BB=93=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - AppContract.php: 新增closeall()方法, 遍历所有status=1持仓订单按当前价平仓 - index.html: 工具栏新增一键平仓按钮 - app_contract.js: 按钮事件, 二次确认后POST请求, 刷新表格 - 非超管只能平自己团队的仓位 --- .../admin/controller/app/AppContract.php | 261 ++++++++++++++++++ .../admin/view/app/app_contract/index.html | 46 +++ public/assets/js/backend/app/app_contract.js | 95 +++++++ 3 files changed, 402 insertions(+) create mode 100755 application/admin/controller/app/AppContract.php create mode 100755 application/admin/view/app/app_contract/index.html create mode 100755 public/assets/js/backend/app/app_contract.js diff --git a/application/admin/controller/app/AppContract.php b/application/admin/controller/app/AppContract.php new file mode 100755 index 0000000..9861089 --- /dev/null +++ b/application/admin/controller/app/AppContract.php @@ -0,0 +1,261 @@ +model = new \app\admin\model\app\AppContract; + $this->view->assign("typeList", $this->model->getTypeList()); + $this->view->assign("statusList", $this->model->getStatusList()); + } + + /** + * 一键平仓 - 关闭所有持仓中的合约订单 + */ + public function closeall() + { + if (!$this->request->isPost()) { + $this->error('请求方式错误'); + } + $admin_id = $this->auth->id; + + // 构建查询条件 + $where = ['a.status' => '1']; + + // 非超级管理员只能平仓自己团队的订单 + if ($admin_id > 1) { + $user = Db::name("user")->where("admin_id", $admin_id)->find(); + if ($user) { + $where['u.path'] = ['like', $user['path'] . '%']; + } + } + + // 查询所有持仓中的合约 + $contracts = Db::name('app_contract') + ->alias('a') + ->field('a.*,b.close,b.symbol') + ->join('app_rate b', 'a.coin_id = b.id', 'left') + ->join('fa_user u', 'a.user_id = u.id') + ->where($where) + ->select(); + + if (empty($contracts)) { + $this->error('没有持仓中的订单'); + } + + $shizhi = Config::get('site.shizhi'); + $success_count = 0; + $fail_count = 0; + + foreach ($contracts as $contract) { + Db::startTrans(); + try { + $pc_price = $contract['close']; + $diffnum = $pc_price - $contract['price']; + + // 计算收益 + if ($contract['type'] == 'more') { + $income = $diffnum * $contract['num'] * $shizhi; + } else { + if ($diffnum < 0) { + $income = abs($diffnum) * $contract['num'] * $shizhi; + } else { + $income = 0 - $diffnum * $contract['num'] * $shizhi; + } + } + $income = sprintf('%.6f', $income); + + // 获取用户钱包 + $currency = Db::name('app_currency_user') + ->field('num,id') + ->where('user_id', $contract['user_id']) + ->where('curr_id', 1)->find(); + + if (!$currency) { + $fail_count++; + Db::rollback(); + continue; + } + + // 记录资金明细 + $detailed = [ + 'user_id' => $contract['user_id'], + 'curr_id' => 1, + 'price' => abs($income), + 'cart' => $income > 0 ? '1' : '2', + 'type' => '6', + 'serial_no' => time() . mt_rand(1000, 9999), + 'order_result' => 'result', + 'description' => $income > 0 ? '合约交易平仓盈利' : '合约交易平仓亏损', + 'createtime' => time(), + 'notice' => '管理员一键平仓', + 'before_num' => $currency['num'], + 'after_num' => $currency['num'] + $income, + ]; + + $detaileds = [$detailed]; + $detaileds[] = [ + 'user_id' => $contract['user_id'], + 'curr_id' => 1, + 'price' => $contract['promise_fee'], + 'cart' => '1', + 'type' => '6', + 'serial_no' => time() . mt_rand(1000, 9999), + 'order_result' => 'result', + 'description' => '合约交易保证金退还', + 'createtime' => time(), + 'notice' => '管理员一键平仓', + 'before_num' => $detailed['after_num'], + 'after_num' => $detailed['after_num'] + $contract['promise_fee'], + ]; + + $lostnum = sprintf("%.6f", ($currency['num'] + $income + $contract['promise_fee'])); + + // 更新钱包余额 + Db::name('app_currency_user') + ->where('id', $currency['id']) + ->update(['num' => $lostnum, 'updatetime' => time()]); + + // 更新订单状态 + Db::name('app_contract') + ->where('id', $contract['id']) + ->update(['status' => '2', 'pc_price' => $pc_price, 'pctime' => time(), 'income' => $income]); + + // 写入资金明细 + Db::name('app_detailed')->insertAll($detaileds); + + Db::commit(); + $success_count++; + } catch (\Exception $e) { + Db::rollback(); + $fail_count++; + } + } + + $this->success("一键平仓完成:成功 {$success_count} 笔,失败 {$fail_count} 笔"); + } + + public function import() + { + parent::import(); + } + + /** + * 默认生成的控制器所继承的父类中有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 == 'mobile') + { + $new_params['u.' . $key] = $value; + $new_op['u.' . $key] = $op[$key]; + }else if($key == 'user_name'){ + $new_params['u.nickname'] = $value; + $new_op['u.nickname'] = $op[$key]; + }elseif ($key == 'daili_name') { + $new_params['e.nickname'] = $value; + $new_op['e.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); + } + + $admin_id = $this->auth->id; + if($admin_id > 1){ + $user = Db::name("user")->where("admin_id",$admin_id)->find(); + if($user){ + $w['u.path'] = array("like",$user['path']."%"); + } + } + + $total = Db::name('app_contract') + ->alias('a') + ->field('a.*,u.nickname as user_name,e.nickname as daili_name') + ->join('fa_user u', 'a.user_id = u.id') + ->join('admin e', 'u.p_admin_id = e.id' , 'left') + ->where($w) + ->count(); + $list = Db::name('app_contract') + ->alias('a') + ->field('a.*,u.nickname as user_name,b.coinname,b.symbol,b.close,e.nickname as daili_name') + ->join('app_rate b','a.coin_id = b.id','left') + ->join('fa_user u', 'a.user_id = u.id') + ->join('admin e', 'u.p_admin_id = e.id' , 'left') + ->where($w) + ->order('a.id', 'desc') + ->limit($offset, $limit) + ->select(); + foreach ($list as $key=>$value) + { + if($value['status'] == '1') { + $diffnum = $value['close'] - $value['price']; + if ($value['type'] == 'more') { + //计算收益 + $income = $diffnum / 100 * $value['num'] * $value['multiple'] * Config::get('site.shizhi'); + } else { + if ($diffnum < 0) { + $income = abs($diffnum) / 100 * $value['num'] * $value['multiple'] * Config::get('site.shizhi'); + } else { + $income = 0 - $diffnum / 100 * $value['num'] * $value['multiple'] * Config::get('site.shizhi'); + } + } + $value['income'] = sprintf('%.6f', $income); + $list[$key] = $value; + } + } + + $result = array("total" => $total, "rows" => $list); + + return json($result); + } + return $this->view->fetch(); + } + +} diff --git a/application/admin/view/app/app_contract/index.html b/application/admin/view/app/app_contract/index.html new file mode 100755 index 0000000..b9d0acf --- /dev/null +++ b/application/admin/view/app/app_contract/index.html @@ -0,0 +1,46 @@ +
+ +
+ {:build_heading(null,FALSE)} + +
+ + +
+ +
+
diff --git a/public/assets/js/backend/app/app_contract.js b/public/assets/js/backend/app/app_contract.js new file mode 100755 index 0000000..e7269fa --- /dev/null +++ b/public/assets/js/backend/app/app_contract.js @@ -0,0 +1,95 @@ +define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) { + + var Controller = { + index: function () { + // 初始化表格参数配置 + Table.api.init({ + extend: { + index_url: 'app/app_contract/index' + location.search, + add_url: 'app/app_contract/add', + edit_url: 'app/app_contract/edit', + del_url: 'app/app_contract/del', +// multi_url: 'app/app_contract/multi', +// import_url: 'app/app_contract/import', + table: 'app_contract', + } + }); + + var table = $("#table"); + + // 初始化表格 + table.bootstrapTable({ + url: $.fn.bootstrapTable.defaults.extend.index_url, + pk: 'id', + sortName: 'id', + columns: [ + [ + {checkbox: true}, + {field: 'id', title: __('Id')}, + {field: 'daili_name', title: __('所属代理'), operate: 'LIKE'}, + {field: 'user_id', title: __('用户ID')}, + {field: 'user_name', title: __('用户')}, + {field: 'coin_id', title: __('Coin_id')}, + {field: 'symbol', title: __('Symbol'), operate: 'LIKE'}, + {field: 'type', title: __('Type'), searchList: {"more":__('Type more'),"free":__('Type free')}, formatter: Table.api.formatter.normal}, + {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3')}, formatter: Table.api.formatter.status}, + {field: 'price', title: __('Price'), operate:'BETWEEN'}, + {field: 'pc_price', title: __('Pc_price'), operate:'BETWEEN'}, + {field: 'num', title: __('Num')}, + {field: 'multiple', title: __('Multiple')}, + {field: 'promise_fee', title: __('Promise_fee'), operate:'BETWEEN'}, + {field: 'fee', title: __('Fee'), operate:'BETWEEN'}, + {field: 'zy_price', title: __('Zy_price'), operate:'BETWEEN'}, + {field: 'zs_price', title: __('Zs_price'), operate:'BETWEEN'}, + {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: 'pctime', title: __('Pctime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime}, + {field: 'income', title: __('Income'), operate:'BETWEEN'}, + {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate} + ] + ] + }); + + // 为表格绑定事件 + Table.api.bindevent(table); + + // 一键平仓按钮事件 + $(document).on('click', '.btn-closeall', function () { + Layer.confirm('确定要一键平仓所有持仓中的合约订单吗?此操作不可撤销!', { + icon: 0, + title: '一键平仓确认' + }, function (index) { + Layer.close(index); + $.ajax({ + url: 'app/app_contract/closeall', + type: 'POST', + dataType: 'json', + success: function (ret) { + if (ret.code === 1) { + Layer.alert(ret.msg, {icon: 1}); + table.bootstrapTable('refresh'); + } else { + Layer.alert(ret.msg, {icon: 2}); + } + }, + error: function () { + Layer.alert('请求失败', {icon: 2}); + } + }); + }); + }); + }, + add: function () { + Controller.api.bindevent(); + }, + edit: function () { + Controller.api.bindevent(); + }, + api: { + bindevent: function () { + Form.api.bindevent($("form[role=form]")); + } + } + }; + return Controller; +}); \ No newline at end of file