- 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: 新闻自动采集脚本
306 lines
11 KiB
PHP
Executable File
306 lines
11 KiB
PHP
Executable File
<?php
|
||
|
||
namespace app\api\controller;
|
||
|
||
use app\common\controller\Api;
|
||
use think\Db;
|
||
use think\Config;
|
||
/**
|
||
* 投资接口
|
||
*/
|
||
class Product extends Api
|
||
{
|
||
|
||
|
||
// 无需登录的接口,*表示全部
|
||
protected $noNeedLogin = ['*'];
|
||
// 无需鉴权的接口,*表示全部
|
||
protected $noNeedRight = ['*'];
|
||
|
||
/**
|
||
* 投资首页
|
||
*/
|
||
public function index_list()
|
||
{
|
||
$list = Db::name("app_product")->field("id,name,logo_image,min_num,max_num,fast_rixi,day,rixi_json")
|
||
->where("status",1)
|
||
->order("weigh desc")
|
||
->select();
|
||
foreach ($list as $key => $value) {
|
||
$rixi_json = json_decode($value['rixi_json'],true);
|
||
$rixi = min($rixi_json)."%-".max($rixi_json)."%";
|
||
$value['logo_image'] = Config::get("site.image_url").$value['logo_image'];
|
||
$value['rixi'] = $rixi;
|
||
unset($value['rixi_json']);
|
||
$list[$key] = $value;
|
||
}
|
||
$this->success("ok",$list);
|
||
}
|
||
|
||
/**
|
||
* 投资详情
|
||
*/
|
||
public function product_con()
|
||
{
|
||
$user_id = $this->auth->id;
|
||
$id = $this->request->post("id");
|
||
$product = Db::name("app_product")->field("id,name,logo_image,min_num,max_num,fast_rixi,day,rixi_json,msg,curr_id")
|
||
->where("id",$id)
|
||
->where("status",1)
|
||
->find();
|
||
if(!$product){
|
||
$this->error(__("产品不存在或已下架"));
|
||
}
|
||
$curr = Db::name("app_currency")->where("id",$product['curr_id'])->find();
|
||
$product['curr_name'] = $curr['name'];
|
||
$product['money_trx'] = Db::name("app_currency_user")->where("user_id",$user_id)->where("curr_id",$product['curr_id'])->value("num");
|
||
//我的投资
|
||
// $my_num = Db::name("app_product_user")->where("user_id",$user_id)->where("status",1)->sum("num");
|
||
$rixi_json = json_decode($product['rixi_json'],true);
|
||
$rixi = min($rixi_json);
|
||
$rixi_arr = [];
|
||
foreach ($rixi_json as $key => $value) {
|
||
$rixi_arr[] = array(
|
||
"key" => $key,
|
||
"value" => $value,
|
||
);
|
||
}
|
||
$product['rixi'] = $rixi;
|
||
$product['rixi_arr'] = $rixi_arr;
|
||
unset($product['rixi_json']);
|
||
unset($product['fast_rixi']);
|
||
$this->success("ok",$product);
|
||
}
|
||
|
||
/**
|
||
* 立即投资
|
||
*/
|
||
public function product_buy()
|
||
{
|
||
$user_id = $this->auth->id;
|
||
$id = $this->request->post("id");
|
||
$num = $this->request->post("num");
|
||
$pay_pwd = $this->request->post("pay_pwd");
|
||
//判断交易密码
|
||
if(strtoupper(md5(strtoupper(md5($pay_pwd.'skund')))) != $this->auth->pay_pwd)
|
||
{
|
||
$this->error(__('交易密码错误'));
|
||
}
|
||
$product = Db::name("app_product")->field("id,name,logo_image,min_num,max_num,fast_rixi,day,rixi_json,curr_id")
|
||
->where("id",$id)
|
||
->where("status",1)
|
||
->find();
|
||
if(!$product){
|
||
$this->error(__("产品不存在或已下架"));
|
||
}
|
||
if($num < $product['min_num']){
|
||
$this->error(__("最低投资数量:").$product['min_num']);
|
||
}
|
||
if($num > $product['max_num']){
|
||
$this->error(__("最高投资数量:").$product['max_num']);
|
||
}
|
||
$trx_curr = Db::name("app_currency_user")->where("user_id",$user_id)->where("curr_id",$product['curr_id'])->find();
|
||
if($trx_curr['num'] < $num){
|
||
$this->error(__("余额不足"));
|
||
}
|
||
//redis防重复点击
|
||
$symbol = "product_buy" . $this->auth->id;
|
||
$submited = pushRedis($symbol);
|
||
if (!$submited) {
|
||
$this->error(__("操作频繁"));
|
||
}
|
||
//我的投资
|
||
// $my_num = Db::name("app_product_user")->where("user_id",$user_id)->where("status",1)->sum("num");
|
||
$rixi_json = json_decode($product['rixi_json'],true);
|
||
$rixi = min($rixi_json);
|
||
foreach ($rixi_json as $key => $value) {
|
||
if($num >= $key){
|
||
$rixi = $value;
|
||
}
|
||
}
|
||
|
||
$data = array(
|
||
"user_id" => $user_id,
|
||
"product_id" => $id,
|
||
"curr_id" => $product['curr_id'],
|
||
"rixi" => $rixi,
|
||
"num" => $num,
|
||
"day" => $product['day'],
|
||
"alr_day" => 0,
|
||
"alr_num" => 0,
|
||
"status" => 1,
|
||
"addtime" => time(),
|
||
"lasttime" => time(),
|
||
);
|
||
$detailed_data = array(
|
||
"user_id" => $user_id,
|
||
"curr_id" => $trx_curr['curr_id'],
|
||
"price" => $num,
|
||
"cart" => 2,
|
||
"type" => 11,
|
||
"description" => "产品投资",
|
||
"createtime" => time(),
|
||
"before_num" => $trx_curr['num'],
|
||
"after_num" => $trx_curr['num'] - $num,
|
||
);
|
||
|
||
Db::startTrans();
|
||
try {
|
||
Db::name("app_product_user")->insert($data);
|
||
Db::name("app_detailed")->insert($detailed_data);
|
||
Db::name("app_currency_user")->where("id",$trx_curr['id'])->setDec("num",$num);
|
||
Db::commit();
|
||
$this->success( __("提交成功") );
|
||
} catch (Exception $e) {
|
||
Db::rollback();
|
||
lopRedis($symbol);
|
||
$this->error( __( "网络错误,请稍后再试!" ) );
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 投资记录
|
||
*/
|
||
public function product_user()
|
||
{
|
||
$user_id = $this->auth->id;
|
||
$list = Db::name("app_product_user a")->field("a.id,b.name,a.num,a.rixi,a.day,a.addtime,a.status,c.name as curr_name")
|
||
->join("app_product b","a.product_id=b.id","left")
|
||
->join("app_currency c","a.curr_id=c.id","left")
|
||
->where("a.user_id",$user_id)
|
||
->order("a.id desc")
|
||
->select();
|
||
foreach ($list as $key => $value) {
|
||
$value['addtime'] = date("Y-m-d H:i",$value['addtime']);
|
||
$list[$key] = $value;
|
||
}
|
||
$this->success("ok",$list);
|
||
}
|
||
|
||
/**
|
||
* 投资记录-详情
|
||
*/
|
||
public function product_user_con()
|
||
{
|
||
$user_id = $this->auth->id;
|
||
$id = $this->request->post("id");
|
||
$product = Db::name("app_product_user")->field("id,alr_day,alr_num,curr_id")->where("user_id",$user_id)->where("id",$id)->find();
|
||
if(empty($product)){
|
||
$this->error(__("产品不存在或已下架"));
|
||
}
|
||
$product['curr_name'] = Db::name("app_currency")->where("id",$product['curr_id'])->value("name");
|
||
$this->success("ok",$product);
|
||
}
|
||
|
||
/**
|
||
* 产品-释放记录
|
||
*/
|
||
public function product_user_log()
|
||
{
|
||
$user_id = $this->auth->id;
|
||
$id = $this->request->post("id");
|
||
// $detail_log = Db::name("app_detailed")->field("price,createtime")
|
||
// ->where("user_id",$user_id)
|
||
// ->where("type",4)
|
||
// ->where("pro_id",$id)
|
||
// ->order("id desc")
|
||
// ->paginate(20, false, ['query' => request()->param()]);
|
||
$detail_log = Db::name("app_product_log")->field("num as price,addtime as createtime")
|
||
->where("pro_id",$id)
|
||
->order("id desc")
|
||
->paginate(20, false, ['query' => request()->param()]);
|
||
foreach ($detail_log as $key => $value) {
|
||
$value['createtime'] = date("Y-m-d H:i",$value['createtime']);
|
||
$detail_log[$key] = $value;
|
||
}
|
||
$this->success("ok",$detail_log);
|
||
}
|
||
|
||
/**
|
||
* 每日日息产生
|
||
*/
|
||
public function set_rixi()
|
||
{
|
||
$product = Db::name("app_product_user a")->field("a.*,b.exchange,u.path")
|
||
->join("app_currency b","a.curr_id=b.id","left")
|
||
->join("user u","a.user_id=u.id","left")
|
||
->where("a.status",1)
|
||
->where("a.lasttime","<", time()-86400)
|
||
->order("a.lasttime asc")
|
||
->limit(20)
|
||
->select();
|
||
if(empty($product)){
|
||
echo "暂无产品";exit;
|
||
}
|
||
foreach ($product as $key => $value) {
|
||
if($value['alr_day'] >= $value['day']){
|
||
Db::name("app_product_user")->where("id",$value['id'])->update(['status'=>0]);
|
||
continue;
|
||
}
|
||
$detail_log = [];
|
||
Db::startTrans();
|
||
try {
|
||
//释放对应币种
|
||
$sf_num = sprintf("%.8f",$value['rixi']*$value['num']/100);
|
||
//应释放USDT量
|
||
// $usdt_num = sprintf("%.8f",$sf_num*$value['exchange']);
|
||
$update = array(
|
||
"alr_day" => $value['alr_day']+1,
|
||
"alr_num" => $value['alr_num']+$sf_num,
|
||
"lasttime" => time(),
|
||
);
|
||
$pro_log = array(
|
||
"pro_id" => $value['id'],
|
||
"num" => $sf_num,
|
||
"addtime" => time(),
|
||
);
|
||
Db::name("app_product_log")->insert($pro_log);
|
||
if($update['alr_day']>=$value['day']){
|
||
$update['status'] = 0;
|
||
//退还本金
|
||
$user_trx = Db::name('app_currency_user')->where("user_id",$value['user_id'])->where("curr_id",$value['curr_id'])->find();
|
||
$detail_log[] = array(
|
||
"user_id" => $user_trx['user_id'],
|
||
"curr_id" => $user_trx['curr_id'],
|
||
"price" => $value['num'],
|
||
"cart" => 1,
|
||
"type" => 11,
|
||
"description" => "理财本金退还",
|
||
"createtime" => time(),
|
||
"notice" => "产品ID:".$value['id'],
|
||
"before_num" => $user_trx['num'],
|
||
"after_num" => $user_trx['num']+$value['num'],
|
||
"pro_id" => 0,
|
||
);
|
||
//一次性反息
|
||
$detail_log[] = array(
|
||
"user_id" => $user_trx['user_id'],
|
||
"curr_id" => $user_trx['curr_id'],
|
||
"price" => $update['alr_num'],
|
||
"cart" => 1,
|
||
"type" => 11,
|
||
"description" => "理财利息",
|
||
"createtime" => time(),
|
||
"notice" => "产品ID:".$value['id'],
|
||
"before_num" => $user_trx['num'],
|
||
"after_num" => $user_trx['num']+$value['num']+$update['alr_num'],
|
||
"pro_id" => 0,
|
||
);
|
||
Db::name('app_currency_user')->where("id",$user_trx['id'])->update(['num'=>$user_trx['num']+$value['num']+$update['alr_num']]);
|
||
}
|
||
if($detail_log){
|
||
Db::name("app_detailed")->insertAll($detail_log);
|
||
}
|
||
Db::name("app_product_user")->where("id",$value['id'])->update($update);
|
||
Db::commit();
|
||
} catch (Exception $e) {
|
||
Db::rollback();
|
||
echo '数据插入失败';
|
||
}
|
||
echo $value['id']."--";
|
||
}
|
||
echo "success";
|
||
}
|
||
|
||
}
|