- 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: 新闻自动采集脚本
1203 lines
44 KiB
PHP
Executable File
1203 lines
44 KiB
PHP
Executable File
<?php
|
|
|
|
namespace app\api\controller;
|
|
|
|
use app\common\controller\Api;
|
|
use think\Config;
|
|
use think\Db;
|
|
/**
|
|
* C2C交易
|
|
*/
|
|
class Marketotc extends Api
|
|
{
|
|
|
|
// 无需登录的接口,*表示全部
|
|
protected $noNeedLogin = [''];
|
|
// 无需鉴权的接口,*表示全部
|
|
protected $noNeedRight = ['*'];
|
|
|
|
/**
|
|
* 查询自己是否有订单再交易中
|
|
*/
|
|
public function get_order_status()
|
|
{
|
|
$user_id = $this->auth->id;
|
|
//查询自己是否有订单再交易中
|
|
$w['user_id|relid'] = array("eq",$user_id);
|
|
$w['status'] = array("in","1,2,3,6,7");
|
|
$my_order = Db::name("app_market_user")->field("id,status")->where($w)->find();
|
|
$data = array(
|
|
"wait_order" => $my_order,
|
|
);
|
|
$this->success("ok",$data);
|
|
}
|
|
|
|
/**
|
|
* 获取国家法币
|
|
*/
|
|
public function get_country()
|
|
{
|
|
$country = Db::name("app_market_country")->where("status",1)->select();
|
|
foreach ($country as $key => $value) {
|
|
$value['pay_image'] = Config::get("site.image_url").$value['pay_image'];
|
|
$country[$key] = $value;
|
|
}
|
|
$this->success("ok",$country);
|
|
}
|
|
|
|
/**
|
|
* 获取可交易币种
|
|
*/
|
|
public function get_curr()
|
|
{
|
|
$country = Db::name("app_currency")->field("id,name")->where("is_otc",1)->select();
|
|
$this->success("ok",$country);
|
|
}
|
|
|
|
/**
|
|
* 获取已绑定收款方式
|
|
*/
|
|
public function get_collection()
|
|
{
|
|
$payment = $this->request->post("payment",1); //收款方式:1=银行卡
|
|
$user_id = $this->auth->id;
|
|
$collection = Db::name("app_collection")->field("id,username,card_num,bank_name")->where("user_id",$user_id)->select();
|
|
$this->success("ok",$collection);
|
|
}
|
|
|
|
/**
|
|
* 快捷卖出
|
|
*/
|
|
public function quick_sell()
|
|
{
|
|
$user_id = $this->auth->id;
|
|
$num = $this->request->post("num");
|
|
$country_id = $this->request->post("country_id",1); //国家ID
|
|
$curr_id = $this->request->post("curr_id",1); //币种ID
|
|
$price = $this->request->post("price");//单价
|
|
$min_price = $this->request->post("min_price");//最低金额
|
|
$max_price = $this->request->post("max_price");//最高金额
|
|
$pay_pwd = $this->request->post("pay_pwd");
|
|
$payment = $this->request->post("payment",1); //收款方式:1=银行卡
|
|
$collection_id = $this->request->post("collection_id",1); //收款账户
|
|
|
|
if(!$num || !is_numeric($num) || $num<=0){
|
|
$this->error(__("数量错误"));
|
|
}
|
|
if(!$price || !is_numeric($num) || $num<=0){
|
|
$this->error(__("金额输入错误"));
|
|
}
|
|
if(!$min_price || !is_numeric($min_price) || $min_price<=0){
|
|
$this->error(__("金额输入错误"));
|
|
}
|
|
if(!$max_price || !is_numeric($max_price) || $max_price<=0){
|
|
$this->error(__("金额输入错误"));
|
|
}
|
|
//实名认证判断
|
|
if(!controller("Common")->get_auth(2)){
|
|
$this->error(__("请先完成实名认证"),['jump'=>1]);
|
|
}
|
|
$collection = Db::name("app_collection")->where("id",$collection_id)->where("user_id",$user_id)->find();
|
|
if(empty($collection)){
|
|
$this->error(__("请选择收款账户"));
|
|
}
|
|
//判断交易密码
|
|
if(strtoupper(md5(strtoupper(md5($pay_pwd.'skund')))) != $this->auth->pay_pwd)
|
|
{
|
|
$this->error(__('交易密码错误'));
|
|
}
|
|
$country = Db::name("app_market_country")->where("id",$country_id)->where("status",1)->find();
|
|
if(empty($country)){
|
|
$this->error(__("暂不支持此法币"));
|
|
}
|
|
$curr = Db::name("app_currency")->where("id",$curr_id)->find();
|
|
$min_hang = $curr['min_hang'];
|
|
$max_hang = $curr['max_hang'];
|
|
if($num<$min_hang){
|
|
$this->error(__("最低挂单数量为").$min_hang);
|
|
}
|
|
if($num>$max_hang){
|
|
$this->error(__("最高挂单数量为").$max_hang);
|
|
}
|
|
if(!in_array($payment, [1])){
|
|
$this->error(__("选择的收款方式有误"));
|
|
}
|
|
|
|
//redis防重复点击
|
|
$symbol = "quick_sell" . $this->auth->id;
|
|
$submited = pushRedis($symbol);
|
|
if (!$submited) {
|
|
$this->error(__("操作频繁"));
|
|
}
|
|
|
|
//手续费
|
|
$otc_free = Config::get("site.otc_free");
|
|
$free = sprintf("%.6f",$otc_free*$num/100);
|
|
$count_num = $free+$num;
|
|
$user_curr = Db::name("app_currency_user")->where("user_id",$user_id)->where("curr_id",$curr_id)->find();
|
|
if($user_curr['num_fb']<$count_num){
|
|
lopRedis($symbol);
|
|
$this->error(__("余额不足"));
|
|
}
|
|
|
|
$data = array(
|
|
"user_id" => $user_id,
|
|
"country_id" => $country_id,
|
|
"curr_id" => $curr_id,
|
|
"num" => $num,
|
|
"residue_num" => $num,
|
|
"free" => $free,
|
|
"price" => $price,
|
|
"type" => 2,
|
|
"min_price" => $min_price,
|
|
"max_price" => $max_price,
|
|
"payment" => $payment,
|
|
"collection_id" => $collection_id,
|
|
"status" => 1,
|
|
"createtime" => time(),
|
|
);
|
|
$detail_data[] = array(
|
|
"user_id" => $user_id,
|
|
"curr_id" => $curr_id,
|
|
"price" => $count_num,
|
|
"cart" => 2,
|
|
"type" => 2,
|
|
"description" => "C2C挂卖",
|
|
"createtime" => time(),
|
|
"before_num" => $user_curr['num_fb'],
|
|
"after_num" => $user_curr['num_fb'] - $count_num,
|
|
);
|
|
Db::startTrans();
|
|
try {
|
|
Db::name("app_market")->insert($data);
|
|
Db::name("app_detailed_fb")->insertAll($detail_data);
|
|
Db::name("app_currency_user")->where("id",$user_curr['id'])->setDec(['num_fb'=>$user_curr['num_fb'] - $count_num]);
|
|
Db::commit();
|
|
lopRedis($symbol);
|
|
$this->success(__("发布成功"));
|
|
} catch (Exception $e) {
|
|
Db::rollback();
|
|
lopRedis($symbol);
|
|
$this->error(__("发布失败"));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 快捷买入
|
|
*/
|
|
public function quick_buy()
|
|
{
|
|
$user_id = $this->auth->id;
|
|
$num = $this->request->post("num");
|
|
$country_id = $this->request->post("country_id",1); //国家ID
|
|
$curr_id = $this->request->post("curr_id",1); //币种ID
|
|
$price = $this->request->post("price");//单价
|
|
$min_price = $this->request->post("min_price");//最低金额
|
|
$max_price = $this->request->post("max_price");//最高金额
|
|
$pay_pwd = $this->request->post("pay_pwd");
|
|
$payment = $this->request->post("payment",1); //支付方式:1=银行卡
|
|
|
|
if(!$num || !is_numeric($num) || $num<=0){
|
|
$this->error(__("数量错误"));
|
|
}
|
|
if(!$price || !is_numeric($num) || $num<=0){
|
|
$this->error(__("金额输入错误"));
|
|
}
|
|
if(!$min_price || !is_numeric($min_price) || $min_price<=0){
|
|
$this->error(__("金额输入错误"));
|
|
}
|
|
if(!$max_price || !is_numeric($max_price) || $max_price<=0){
|
|
$this->error(__("金额输入错误"));
|
|
}
|
|
//实名认证判断
|
|
if(!controller("Common")->get_auth(2)){
|
|
$this->error(__("请先完成实名认证"),['jump'=>1]);
|
|
}
|
|
//判断交易密码
|
|
if(strtoupper(md5(strtoupper(md5($pay_pwd.'skund')))) != $this->auth->pay_pwd)
|
|
{
|
|
$this->error(__('交易密码错误'));
|
|
}
|
|
$country = Db::name("app_market_country")->where("id",$country_id)->where("status",1)->find();
|
|
if(empty($country)){
|
|
$this->error(__("暂不支持此法币"));
|
|
}
|
|
$curr = Db::name("app_currency")->where("id",$curr_id)->find();
|
|
$min_hang = $curr['min_hang'];
|
|
$max_hang = $curr['max_hang'];
|
|
if($num<$min_hang){
|
|
$this->error(__("最低挂单数量为").$min_hang);
|
|
}
|
|
if($num>$max_hang){
|
|
$this->error(__("最高挂单数量为").$max_hang);
|
|
}
|
|
if(!in_array($payment, [1])){
|
|
$this->error(__("选择的支付方式有误"));
|
|
}
|
|
|
|
// redis防重复点击
|
|
$symbol = "quick_buy" . $this->auth->id;
|
|
$submited = pushRedis($symbol);
|
|
if (!$submited) {
|
|
$this->error(__("操作频繁"));
|
|
}
|
|
|
|
//手续费
|
|
$otc_free = Config::get("site.otc_free");
|
|
$free = sprintf("%.6f",$otc_free*$num/100);
|
|
$user_curr = Db::name("app_currency_user")->where("user_id",$user_id)->where("curr_id",$curr_id)->find();
|
|
if($user_curr['num_fb']<$free){
|
|
lopRedis($symbol);
|
|
$this->error(__("手续费不足"));
|
|
}
|
|
$data = array(
|
|
"user_id" => $user_id,
|
|
"country_id" => $country_id,
|
|
"curr_id" => $curr_id,
|
|
"num" => $num,
|
|
"residue_num" => $num,
|
|
"free" => $free,
|
|
"price" => $price,
|
|
"type" => 1,
|
|
"min_price" => $min_price,
|
|
"max_price" => $max_price,
|
|
"payment" => $payment,
|
|
"status" => 1,
|
|
"createtime" => time(),
|
|
);
|
|
$detail_data[] = array(
|
|
"user_id" => $user_id,
|
|
"curr_id" => $curr_id,
|
|
"price" => $free,
|
|
"cart" => 2,
|
|
"type" => 2,
|
|
"description" => "手续费扣除",
|
|
"createtime" => time(),
|
|
"before_num" => $user_curr['num_fb'],
|
|
"after_num" => $user_curr['num_fb'] - $free,
|
|
);
|
|
Db::startTrans();
|
|
try {
|
|
Db::name("app_market")->insert($data);
|
|
Db::name("app_detailed_fb")->insertAll($detail_data);
|
|
Db::name("app_currency_user")->where("id",$user_curr['id'])->update(['num_fb'=>$user_curr['num_fb'] - $free]);
|
|
Db::commit();
|
|
lopRedis($symbol);
|
|
$this->success(__("发布成功"));
|
|
} catch (Exception $e) {
|
|
Db::rollback();
|
|
lopRedis($symbol);
|
|
$this->error(__("发布失败"));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 卖出/买入列表
|
|
*/
|
|
public function market_list()
|
|
{
|
|
$user_id = $this->auth->id;
|
|
$type = $this->request->post("type",2);//2=我要买 1=我要卖
|
|
$country_id = $this->request->post("country_id",1); //国家ID
|
|
$curr_id = $this->request->post("curr_id",1); //币种ID
|
|
$payment = $this->request->post("payment",""); //交易方式
|
|
$price = $this->request->post("price",1); //金额
|
|
if($type == 1){
|
|
$order = "a.price desc";
|
|
}else{
|
|
$order = "a.price asc";
|
|
}
|
|
$w['a.status'] = array("eq",1);
|
|
$w['a.type'] = array("eq",$type);
|
|
if(!$country_id) $country_id = 1;
|
|
if(!$curr_id)$curr_id = 1;
|
|
$w['a.curr_id'] = array("eq",$curr_id);
|
|
$w['a.country_id'] = array("eq",$country_id);
|
|
if($payment){
|
|
$w['a.payment'] = array("eq",$payment);
|
|
}
|
|
if($price){
|
|
$w['a.min_price'] = array("<=",$price);
|
|
$w['a.max_price'] = array(">=",$price);
|
|
}
|
|
$curr = Db::name("app_currency")->where("id",$curr_id)->find();
|
|
$user_curr = Db::name("app_currency_user")->where("user_id",$user_id)->where("curr_id",$curr_id)->find();
|
|
$market = Db::name("app_market a")->field("a.id,a.user_id,a.residue_num,a.price,a.min_price,a.max_price,a.payment,b.nickname,b.avatar,a.type,c.symbol")
|
|
->join("user b","a.user_id=b.id","left")
|
|
->join("app_market_country c","a.country_id=c.id","left")
|
|
->where($w)
|
|
->order($order)
|
|
->paginate(10, false, ['query' => request()->param()]);
|
|
foreach ($market as $key => $value) {
|
|
$value['curr_name'] = $curr['name'];
|
|
$value['avatar'] = Config::get("site.image_url").$value['avatar'];
|
|
$value['count_price'] = sprintf("%.2f",$value['residue_num']*$value['price']);
|
|
//成交量计算
|
|
$succ_num = Db::name("app_market_user")->where("relid",$value['user_id'])->count();
|
|
//用户余额
|
|
$value['money_fb'] = sprintf("%.2f",$user_curr['num_fb']*$value['price']);
|
|
$value['money_num'] = $user_curr['num_fb'];
|
|
$value['succ_num'] = $succ_num;
|
|
if($value['user_id'] == $user_id){
|
|
$value['is_me'] = true;
|
|
}else{
|
|
$value['is_me'] = false;
|
|
}
|
|
$market[$key] = $value;
|
|
}
|
|
$this->success("ok",$market);
|
|
}
|
|
|
|
/**
|
|
* 挂单买入
|
|
*/
|
|
public function order_buy()
|
|
{
|
|
$user_id = $this->auth->id;
|
|
$id = $this->request->post("id");
|
|
$buy_type = $this->request->post("buy_type",1);//购买类型 1=金额购买,2=数量购买
|
|
$num = $this->request->post("num");//数量
|
|
|
|
if(!is_numeric($num) || $num<=1){
|
|
$this->error(__("输入有误"));
|
|
}
|
|
$market = Db::name("app_market")->where("id",$id)->find();
|
|
if(empty($market)){
|
|
$this->error(__("订单不存在"));
|
|
}
|
|
if($market['user_id'] == $user_id){
|
|
$this->error(__("不可交易自己订单"));
|
|
}
|
|
if($market['status'] != 1){
|
|
$this->error(__("该订单已被交易"));
|
|
}
|
|
if($market['type'] == 1){
|
|
$this->error(__("订单错误"));
|
|
}
|
|
$pay_pwd = $this->request->post("pay_pwd");
|
|
//判断交易密码
|
|
if(strtoupper(md5(strtoupper(md5($pay_pwd.'skund')))) != $this->auth->pay_pwd)
|
|
{
|
|
$this->error(__('交易密码错误'));
|
|
}
|
|
//实名认证判断
|
|
if(!controller("Common")->get_auth(2)){
|
|
$this->error(__("请先完成实名认证"),['jump'=>1]);
|
|
}
|
|
|
|
$user_market = Db::name("app_market_user")->where("user_id",$user_id)->where("status","in","1,2,3")->find();
|
|
if($user_market){
|
|
$this->error(__("您有一笔订单正在交易中,请先完成"));
|
|
}
|
|
|
|
//当天取消3次禁止交易
|
|
$today = strtotime(date("Y-m-d"), time());
|
|
$quxiao_log = Db::name("app_market_cancel_log")->where("user_id",$user_id)->where("createtime",">",$today)->count();
|
|
if($quxiao_log >= 3){
|
|
$this->error(__("交易限制"));
|
|
}
|
|
if($buy_type == 1){
|
|
$buy_price = $num;
|
|
$buy_num = sprintf("%.6f",$buy_price/$market['price']);
|
|
}else{
|
|
$buy_price = sprintf("%.2f",$num*$market['price']);
|
|
$buy_num = $num;
|
|
}
|
|
|
|
if($market['residue_num'] < $buy_num){
|
|
$this->error(__("订单数量不足"));
|
|
}
|
|
if(($buy_price < $market['min_price'] && $market['residue_num']> sprintf("%.6f",$market['min_price']/$market['price'])) || $buy_price > $market['max_price']){
|
|
$this->error(__("限额").$market['min_price']." - ".$market['max_price']);
|
|
}
|
|
|
|
//redis防重复点击
|
|
$symbol = "order_sell" . $this->auth->id;
|
|
$submited = pushRedis($symbol);
|
|
if (!$submited) {
|
|
$this->error(__("操作频繁"));
|
|
}
|
|
$free = 0;
|
|
|
|
$order_num = rand(10000, 99999).time();
|
|
$data = array(
|
|
"user_id" => $user_id,
|
|
"order_num" => $order_num,
|
|
"order_id" => $market['id'],
|
|
"collection_id" => $market['collection_id'],
|
|
"type" => 1,
|
|
"relid" => $market['user_id'],
|
|
"num" => $buy_num,
|
|
"free_num" => $free,
|
|
"price" => $buy_price,
|
|
"status" => 1,
|
|
"add_time" => time(),
|
|
);
|
|
$market_update = array(
|
|
"status" => 2,
|
|
"updatetime" => time(),
|
|
);
|
|
|
|
Db::startTrans();
|
|
try {
|
|
$id_s = Db::name("app_market_user")->insertGetId($data);
|
|
Db::name("app_market")->where("id",$id)->update($market_update);
|
|
Db::commit();
|
|
$this->success(__("下单成功"),['id'=>$id_s]);
|
|
} catch (Exception $e) {
|
|
Db::rollback();
|
|
lopRedis($symbol);
|
|
$this->error(__("下单失败"));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 挂单卖出
|
|
*/
|
|
public function order_sell()
|
|
{
|
|
$user_id = $this->auth->id;
|
|
$id = $this->request->post("id");
|
|
$buy_type = $this->request->post("buy_type",1);//购买类型 1=金额购买,2=数量购买
|
|
$num = $this->request->post("num");//数量
|
|
$collection_id = $this->request->post("collection_id",1); //收款账户
|
|
|
|
if(!is_numeric($num) || $num<1){
|
|
$this->error(__("输入有误"));
|
|
}
|
|
//实名认证判断
|
|
if(!controller("Common")->get_auth(2)){
|
|
$this->error(__("请先完成实名认证"),['jump'=>1]);
|
|
}
|
|
$market = Db::name("app_market")->where("id",$id)->find();
|
|
|
|
if(empty($market)){
|
|
$this->error(__("订单不存在"));
|
|
}
|
|
if($market['user_id'] == $user_id){
|
|
$this->error(__("不可交易自己订单"));
|
|
}
|
|
if($market['type'] != 1){
|
|
$this->error(__("订单错误"));
|
|
}
|
|
if($market['status'] != 1){
|
|
$this->error(__("该订单已被交易"));
|
|
}
|
|
$pay_pwd = $this->request->post("pay_pwd");
|
|
//判断密码
|
|
if(strtoupper(md5(strtoupper(md5($pay_pwd.'skund')))) != $this->auth->pay_pwd)
|
|
{
|
|
$this->error(__('交易密码错误'));
|
|
}
|
|
$collection = Db::name("app_collection")->where("id",$collection_id)->where("user_id",$user_id)->find();
|
|
if(empty($collection)){
|
|
$this->error(__("请选择收款账户"));
|
|
}
|
|
|
|
//交易中查询
|
|
$user_market = Db::name("app_market_user")->where("user_id",$user_id)->where("status","in","1,2,3")->find();
|
|
if($user_market){
|
|
$this->error(__("您有一笔订单正在交易中,请先完成"));
|
|
}
|
|
|
|
//当天取消3次禁止交易
|
|
$today = strtotime(date("Y-m-d"), time());
|
|
$quxiao_log = Db::name("app_market_cancel_log")->where("user_id",$user_id)->where("createtime",">",$today)->count();
|
|
if($quxiao_log >= 3){
|
|
$this->error(__("交易限制"));
|
|
}
|
|
if($buy_type == 1){
|
|
$buy_price = $num;
|
|
$buy_num = sprintf("%.6f",$buy_price/$market['price']);
|
|
}else{
|
|
$buy_price = sprintf("%.2f",$num*$market['price']);
|
|
$buy_num = $num;
|
|
}
|
|
|
|
if($market['residue_num'] < $buy_num){
|
|
$this->error(__("订单数量不足"));
|
|
}
|
|
if($buy_price < $market['min_price'] || $buy_price > $market['max_price']){
|
|
$this->error(__("限额").$market['min_price']." - ".$market['max_price']);
|
|
}
|
|
|
|
//redis防重复点击
|
|
$symbol = "order_sell" . $this->auth->id;
|
|
$submited = pushRedis($symbol);
|
|
if (!$submited) {
|
|
$this->error(__("操作频繁"));
|
|
}
|
|
|
|
//
|
|
$user_curr = Db::name("app_currency_user")->where("user_id",$user_id)->where("curr_id",$market['curr_id'])->find();
|
|
if($user_curr['num_fb']<$buy_num){
|
|
$this->error(__("余额不足"));
|
|
}
|
|
$detail_data = array(
|
|
"user_id" => $user_id,
|
|
"curr_id" => $market['curr_id'],
|
|
"price" => $buy_num,
|
|
"cart" => 2,
|
|
"type" => 2,
|
|
"description" => "货币出售",
|
|
"createtime" => time(),
|
|
"before_num" => $user_curr['num_fb'],
|
|
"after_num" => $user_curr['num_fb'] - $buy_num,
|
|
);
|
|
|
|
$order_num = rand(10000, 99999).time();
|
|
$data = array(
|
|
"user_id" => $user_id,
|
|
"order_num" => $order_num,
|
|
"order_id" => $market['id'],
|
|
"collection_id" => $collection['id'],
|
|
"type" => 2,
|
|
"relid" => $market['user_id'],
|
|
"num" => $buy_num,
|
|
"price" => $buy_price,
|
|
"status" => 1,
|
|
"add_time" => time(),
|
|
);
|
|
$market_update = array(
|
|
"status" => 2,
|
|
"updatetime" => time(),
|
|
);
|
|
|
|
Db::startTrans();
|
|
try {
|
|
Db::name("app_detailed_fb")->insert($detail_data);
|
|
Db::name("app_currency_user")->where("id",$user_curr['id'])->update(['num_fb' => $user_curr['num_fb'] - $buy_num]);
|
|
|
|
$id_s = Db::name("app_market_user")->insertGetId($data);
|
|
Db::name("app_market")->where("id",$id)->update($market_update);
|
|
Db::commit();
|
|
$sell_user = Db::name("user")->where("id",$market['user_id'])->find();
|
|
if($sell_user['email']){
|
|
// $send_data = array("num"=>$params['num'],"curr_name"=>$curr['name'],"address"=>$params['address']);
|
|
controller("Ems")->api_send($sell_user['email'], "daifukuan");
|
|
}
|
|
$this->success(__("下单成功"),['id'=>$id_s]);
|
|
} catch (Exception $e) {
|
|
Db::rollback();
|
|
lopRedis($symbol);
|
|
$this->error(__("下单失败"));
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* 我的发布记录
|
|
*/
|
|
public function my_fb_list()
|
|
{
|
|
$user_id = $this->auth->id;
|
|
$market = Db::name("app_market a")->field("a.id,a.num,a.residue_num,a.price,a.type,a.min_price,a.max_price,a.payment,a.status,a.createtime,b.symbol,c.name as curr_name")
|
|
->join("app_market_country b","a.country_id=b.id","left")
|
|
->join("app_currency c","a.curr_id=c.id","left")
|
|
->where("user_id",$user_id)
|
|
->order("id desc")
|
|
->paginate(20, false, ['query' => request()->param()]);
|
|
foreach ($market as $key => $value) {
|
|
$value['createtime'] = date("Y-m-d H:i:s",$value['createtime']);
|
|
$market[$key] = $value;
|
|
}
|
|
$this->success("ok",$market);
|
|
}
|
|
|
|
/**
|
|
* 订单撤回
|
|
*/
|
|
public function order_withdraw()
|
|
{
|
|
$user_id = $this->auth->id;
|
|
$id = $this->request->post("id");
|
|
$pay_pwd = $this->request->post("pay_pwd");
|
|
|
|
//判断密码
|
|
if(strtoupper(md5(strtoupper(md5($pay_pwd.'skund')))) != $this->auth->pay_pwd)
|
|
{
|
|
$this->error(__('交易密码错误'));
|
|
}
|
|
//redis防重复点击
|
|
$symbol = "order_withdraw" . $this->auth->id;
|
|
$submited = pushRedis($symbol);
|
|
if (!$submited) {
|
|
$this->error(__("操作频繁"));
|
|
}
|
|
|
|
|
|
$order = Db::name("app_market")->where("id",$id)->find();
|
|
if(empty($order)){
|
|
lopRedis($symbol);
|
|
$this->error(__("订单不存在"));
|
|
}
|
|
if($order['user_id'] != $user_id){
|
|
lopRedis($symbol);
|
|
$this->error(__("非法操作"));
|
|
}
|
|
if($order['status'] != 1){
|
|
lopRedis($symbol);
|
|
$this->error(__("该订单不可撤回"));
|
|
}
|
|
|
|
$market_update = array(
|
|
"status" => 4,
|
|
"updatetime" => time(),
|
|
);
|
|
|
|
Db::startTrans();
|
|
try {
|
|
Db::name("app_market")->where("id",$order['id'])->update($market_update);//修改挂卖记录
|
|
if($order['type'] == 2){
|
|
$user_curr = Db::name("app_currency_user")->where("user_id",$order['user_id'])->where("curr_id",$order['curr_id'])->find();
|
|
if($order['free'] > 0){
|
|
$residue_num = $order['residue_num'];
|
|
}else{
|
|
$residue_num = $order['residue_num'];
|
|
}
|
|
$detail_data = array(
|
|
"user_id" => $user_curr['user_id'],
|
|
"curr_id" => $user_curr['curr_id'],
|
|
"price" => $residue_num,
|
|
"cart" => 1,
|
|
"type" => 2,
|
|
"description" => "挂卖单撤回",
|
|
"createtime" => time(),
|
|
"before_num" => $user_curr['num_fb'],
|
|
"after_num" => $user_curr['num_fb'] + $residue_num,
|
|
);
|
|
Db::name("app_detailed_fb")->insert($detail_data);
|
|
Db::name("app_currency_user")->where("id",$user_curr['id'])->update(['num'=>$user_curr['num_fb'] + $residue_num]);
|
|
}else{
|
|
if($order['free'] > 0){
|
|
// $user_curr = Db::name("app_currency_user")->where("user_id",$order['user_id'])->where("curr_id",$order['curr_id'])->find();
|
|
// $detail_data = array(
|
|
// "user_id" => $user_curr['user_id'],
|
|
// "curr_id" => $user_curr['curr_id'],
|
|
// "price" => $order['free'],
|
|
// "cart" => 1,
|
|
// "type" => 2,
|
|
// "description" => "手续费退还",
|
|
// "createtime" => time(),
|
|
// "before_num" => $user_curr['num_fb'],
|
|
// "after_num" => $user_curr['num_fb'] + $order['free'],
|
|
// );
|
|
// Db::name("app_detailed_fb")->insert($detail_data);
|
|
// Db::name("app_currency_user")->where("id",$user_curr['id'])->update(['num'=>$user_curr['num_fb'] + $order['free']]);
|
|
}
|
|
}
|
|
|
|
Db::commit();
|
|
$this->success(__("撤回成功"));
|
|
} catch (Exception $e) {
|
|
Db::rollback();
|
|
lopRedis($symbol);
|
|
$this->error(__("撤回失败"));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 我的订单列表
|
|
*/
|
|
public function order_list()
|
|
{
|
|
$user_id = $this->auth->id;
|
|
$type = $this->request->post("type");//1=买入,2=卖出
|
|
$status = $this->request->post("status",0);//状态:1=待付款,2=已付款,3=申诉中,4=已完成,5=已取消
|
|
if($type == 1){
|
|
$w = "((a.user_id = ".$user_id." AND a.type=1) OR (a.relid = ".$user_id." AND a.type=2))";
|
|
}elseif($type == 2){
|
|
$w = "((a.user_id = ".$user_id." AND a.type=2) OR (a.relid = ".$user_id." AND a.type=1))";
|
|
}else{
|
|
$w = "(a.user_id = ".$user_id." OR a.relid = ".$user_id.")";
|
|
}
|
|
if($status){
|
|
$w .= " AND a.status=".$status;
|
|
}
|
|
$user_market = Db::name("app_market_user a")
|
|
->field("a.id,a.num,a.price as count_price,b.price,a.status,a.add_time,b.payment,a.type,a.relid,a.user_id,b.type as types,c.name,d.curr_name as fb_name,d.symbol")
|
|
->join("app_market b","a.order_id=b.id","left")
|
|
->join("app_currency c","b.curr_id=c.id","left")
|
|
->join("app_market_country d","b.country_id=d.id","left")
|
|
->where($w)
|
|
->order("a.id desc")
|
|
->paginate(20, false, ['query' => request()->param()]);
|
|
foreach ($user_market as $key => $value) {
|
|
if($value['user_id'] == $user_id){
|
|
$user = Db::name("user")->where("id",$value['relid'])->find();
|
|
$value['nickname'] = $user['nickname'];
|
|
$value['avatar'] = Config::get("site.image_url").$user['avatar'];
|
|
}else{
|
|
$user = Db::name("user")->where("id",$value['user_id'])->find();
|
|
$value['nickname'] = $user['nickname'];
|
|
$value['avatar'] = Config::get("site.image_url").$user['avatar'];
|
|
$value['type'] =$value['types'];
|
|
}
|
|
$value['num'] = floatval($value['num']);
|
|
$value['add_time'] = date("Y-m-d H:i",$value['add_time']);
|
|
unset($value['types']);
|
|
unset($value['relid']);
|
|
$user_market[$key] = $value;
|
|
}
|
|
|
|
$this->success("ok",$user_market);
|
|
}
|
|
|
|
/**
|
|
* 订单详情
|
|
*/
|
|
public function content_order()
|
|
{
|
|
$user_id = $this->auth->id;
|
|
$id = $this->request->post("id");
|
|
|
|
$user_market = Db::name("app_market_user a")
|
|
->field("a.id,a.user_id,a.relid,a.order_num,a.type,a.num,a.price as count_price,b.price,a.status,"
|
|
. "b.payment,a.add_time,b.type as type_m,a.order_id,c.symbol,d.name as curr_name,a.pay_time,a.voucher_image,a.collection_id")
|
|
->join("app_market b","a.order_id=b.id","left")
|
|
->join("app_market_country c","b.country_id=c.id","left")
|
|
->join("app_currency d","b.curr_id=d.id","left")
|
|
->where("a.id",$id)
|
|
->where("a.user_id|a.relid",$user_id)
|
|
->find();
|
|
if(empty($user_market)){
|
|
$this->error(__("订单不存在"));
|
|
}
|
|
$user_market['num'] = floatval($user_market['num']);
|
|
|
|
//收款信息
|
|
$collection = Db::name("app_collection")->where("id",$user_market['collection_id'])->find();
|
|
|
|
if(empty($collection)){
|
|
$this->error(__("订单错误"));
|
|
}
|
|
|
|
if($user_market['user_id'] == $user_id){
|
|
$user = Db::name("user")->where("id",$user_market['relid'])->find();
|
|
$auth = Db::name("app_auth")->where("user_id",$user['id'])->find();
|
|
}else{
|
|
$user = Db::name("user")->where("id",$user_market['user_id'])->find();
|
|
$auth = Db::name("app_auth")->where("user_id",$user['id'])->find();
|
|
$user_market['type'] = $user_market['type_m'];
|
|
}
|
|
unset($user_market['type_m']);
|
|
|
|
$user_market['avatar'] = Config::get("site.image_url").$user['avatar'];
|
|
$user_market['nickname'] = $user['nickname'];
|
|
$user_market['mobile'] = $auth['mobile'];
|
|
$user_market['name'] = $auth['name'];
|
|
$user_market['collection'] = $collection;
|
|
|
|
//待付款倒计时
|
|
if($user_market['status'] == 1){
|
|
$market_count = Config::get("site.market_time");
|
|
$day_time = $user_market['add_time']+$market_count*60 - time();
|
|
$day_time = $day_time>0 ? $day_time : 0;
|
|
$d = floor($day_time/86400);
|
|
$h = floor(($day_time-86400*$d)/(3600));
|
|
$i = floor(($day_time-86400*$d-$h*3600)/60);
|
|
$s = floor($day_time-86400*$d-$h*3600-60*$i);
|
|
$user_market['h'] = $h;
|
|
$user_market['i'] = $i;
|
|
$user_market['s'] = $s;
|
|
}
|
|
$user_market['add_time'] = date("Y-m-d H:i:s",$user_market['add_time']);
|
|
|
|
//已付款倒计时
|
|
if($user_market['status'] == 2){
|
|
$market_count = Config::get("site.market_time");
|
|
$day_time = $user_market['pay_time']+$market_count*60 - time();
|
|
$day_time = $day_time>0 ? $day_time : 0;
|
|
$d = floor($day_time/86400);
|
|
$h = floor(($day_time-86400*$d)/(3600));
|
|
$i = floor(($day_time-86400*$d-$h*3600)/60);
|
|
$s = floor($day_time-86400*$d-$h*3600-60*$i);
|
|
$user_market['h'] = $h;
|
|
$user_market['i'] = $i;
|
|
$user_market['s'] = $s;
|
|
}
|
|
$user_market['pay_time'] = date("Y-m-d H:i:s",$user_market['pay_time']);
|
|
|
|
//支付凭证
|
|
if($user_market['voucher_image']){
|
|
$voucher_image = explode(",", $user_market['voucher_image']);
|
|
foreach ($voucher_image as $key => $value) {
|
|
$value = Config::get("site.image_url").$value;
|
|
$voucher_image[$key] = $value;
|
|
}
|
|
$user_market['voucher_image'] = $voucher_image;
|
|
}
|
|
|
|
//申述内容
|
|
if($user_market['status'] == 3){
|
|
$appeal = Db::name("app_market_appeal")->field("id,content,annex_images,type")->where("order_id",$id)->find();
|
|
$user_market['appeal_id'] = $appeal['id'];
|
|
if($appeal['annex_images']){
|
|
$images = explode(",", $appeal['annex_images']);
|
|
foreach ($images as $key => $value) {
|
|
$value = Config::get("site.image_url").$value;
|
|
$images[$key] = $value;
|
|
}
|
|
$appeal['annex_images'] = $images;
|
|
}
|
|
$appeal_data = array(
|
|
"content" => $appeal['content'],
|
|
"annex_images" => $appeal['annex_images'],
|
|
"type" => $appeal['type'],
|
|
);
|
|
$user_market['appeal_arr'] = $appeal_data;
|
|
}
|
|
|
|
$this->success("ok",$user_market);
|
|
}
|
|
|
|
/**
|
|
* 取消订单
|
|
*/
|
|
public function order_cancel()
|
|
{
|
|
$user_id = $this->auth->id;
|
|
$id = $this->request->post("id");
|
|
|
|
$w['id'] = array("eq",$id);
|
|
$w["user_id|relid"] = array("eq",$user_id);
|
|
$user_market = Db::name("app_market_user")->where($w)->find();
|
|
|
|
if(empty($user_market)){
|
|
$this->error(__("订单不存在"));
|
|
}
|
|
if($user_market['status'] != 1){
|
|
$this->error(__("已付款不可取消"));
|
|
}
|
|
|
|
$todaye = strtotime(date("Y-m-d", time()));
|
|
$cancel_num = Db::name("app_market_cancel_log")->where("user_id",$user_id)->where("createtime",">",$todaye)->count();
|
|
$cleal_num = Config::get("site.cleal_num");
|
|
|
|
//redis防重复点击
|
|
$symbol = "order_cancel" . $this->auth->id;
|
|
$submited = pushRedis($symbol);
|
|
if (!$submited) {
|
|
$this->error(__("操作频繁"));
|
|
}
|
|
|
|
//买家可取消
|
|
if($user_market['type'] == 2){
|
|
if($user_id != $user_market['relid']){
|
|
lopRedis($symbol);
|
|
$this->error(__("非法请求"));
|
|
}
|
|
}else{
|
|
if($user_id != $user_market['user_id']){
|
|
lopRedis($symbol);
|
|
$this->error(__("非法请求"));
|
|
}
|
|
}
|
|
|
|
if($cancel_num>=$cleal_num){
|
|
lopRedis($symbol);
|
|
$this->error(__("今日取消次数已达上线"));
|
|
}
|
|
$update = array(
|
|
"cancel_time" => time(),
|
|
"status" => 5,
|
|
);
|
|
$market_update = array(
|
|
"status" => 1,
|
|
"updatetime" => time(),
|
|
);
|
|
$market = Db::name("app_market")->where("id",$user_market['order_id'])->find();
|
|
|
|
Db::startTrans();
|
|
try {
|
|
if($user_market['type'] == 2){
|
|
$user_curr = Db::name("app_currency_user")->where("user_id",$user_market['user_id'])->where("curr_id",$market['curr_id'])->find();
|
|
$count_num = $user_market['num']+$user_market['free_num'];
|
|
$detail_data = array(
|
|
"user_id" => $user_market['user_id'],
|
|
"curr_id" => $market['curr_id'],
|
|
"price" => $count_num,
|
|
"cart" => 1,
|
|
"type" => 2,
|
|
"description" => "取消订单",
|
|
"createtime" => time(),
|
|
"before_num" => $user_curr['num_fb'],
|
|
"after_num" => $user_curr['num_fb']+$count_num,
|
|
);
|
|
Db::name("app_detailed_fb")->insert($detail_data);
|
|
Db::name("app_currency_user")->where("id",$user_curr['id'])->update(['num_fb' => $user_curr['num_fb']+$count_num]);
|
|
}
|
|
//插入记录
|
|
$cancel_data = array(
|
|
"user_id" => $user_id,
|
|
"type" => 1,
|
|
"description" => "取消订单",
|
|
"createtime" => time(),
|
|
);
|
|
Db::name("app_market_cancel_log")->insert($cancel_data);
|
|
Db::name("app_market_user")->where("id",$id)->update($update);
|
|
Db::name("app_market")->where("id",$user_market['order_id'])->update($market_update);
|
|
Db::commit();
|
|
lopRedis($symbol);
|
|
$this->success(__("取消成功"));
|
|
} catch (Exception $e) {
|
|
Db::rollback();
|
|
lopRedis($symbol);
|
|
$this->error(__("网络错误"));
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* 上传支付凭证
|
|
*/
|
|
public function order_pay()
|
|
{
|
|
$user_id = $this->auth->id;
|
|
$id = $this->request->post("id");
|
|
$voucher_image = $this->request->post("voucher_image");
|
|
|
|
$user_market = Db::name("app_market_user")->where("id",$id)->find();
|
|
|
|
if(empty($user_market)){
|
|
$this->error(__("订单不存在"));
|
|
}
|
|
if(!$voucher_image){
|
|
$this->error(__("请上传支付凭证"));
|
|
}
|
|
if($user_market['status'] != 1){
|
|
$this->error(__("请勿重复操作"));
|
|
}
|
|
if($user_market['type'] == 1 && $user_market['user_id'] != $user_id){
|
|
$this->error(__("非法操作"));
|
|
}
|
|
if($user_market['type'] == 2 && $user_market['relid'] != $user_id){
|
|
$this->error(__("非法操作"));
|
|
}
|
|
$update = array(
|
|
"pay_time" => time(),
|
|
"status" => 2,
|
|
"voucher_image" => $voucher_image,
|
|
);
|
|
Db::startTrans();
|
|
try {
|
|
Db::name("app_market_user")->where("id",$id)->update($update);
|
|
Db::commit();
|
|
if($user_market['type'] == 1){
|
|
$sell_user_id = $user_market['relid'];
|
|
}else{
|
|
$sell_user_id = $user_market['user_id'];
|
|
}
|
|
$sell_user = Db::name("user")->where("id",$sell_user_id)->find();
|
|
if($sell_user['email']){
|
|
$send_data = array("nickname"=> $this->auth->nickname);
|
|
controller("Ems")->api_send($sell_user['email'], "daiqueren",$send_data);
|
|
}
|
|
$this->success("ok");
|
|
} catch (Exception $e) {
|
|
Db::rollback();
|
|
$this->error(__("网络错误"));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 确认收款 放币
|
|
*/
|
|
public function order_succ()
|
|
{
|
|
$user_id = $this->auth->id;
|
|
$id = $this->request->post("id");
|
|
|
|
//redis防重复点击
|
|
$symbol = "order_succ" . $this->auth->id;
|
|
$submited = pushRedis($symbol);
|
|
if (!$submited) {
|
|
$this->error(__("操作频繁"));
|
|
}
|
|
|
|
$user_market = Db::name("app_market_user a")->field("a.*,b.residue_num,b.free,b.curr_id")
|
|
->join("app_market b","a.order_id=b.id","left")
|
|
->where("a.id",$id)
|
|
->where("a.user_id|relid",$user_id)
|
|
->find();
|
|
if(empty($user_market)){
|
|
lopRedis($symbol);
|
|
$this->error(__("订单不存在"));
|
|
}
|
|
if($user_market['status'] != 2){
|
|
lopRedis($symbol);
|
|
$this->error(__("网络连接失败"));
|
|
}
|
|
if(($user_market['relid'] == $user_id && $user_market['type'] != 1) || ($user_market['user_id'] == $user_id && $user_market['type'] != 2)){
|
|
lopRedis($symbol);
|
|
$this->error(__("非法操作"));
|
|
}
|
|
$update = array(
|
|
"succ_time" => time(),
|
|
"status" => 4,
|
|
);
|
|
if($user_market['type'] == 1){
|
|
$buy_user_id = $user_market['user_id'];
|
|
}else{
|
|
$buy_user_id = $user_market['relid'];
|
|
}
|
|
$buy_user_curr = Db::name("app_currency_user")->where("user_id",$buy_user_id)->where("curr_id",$user_market['curr_id'])->find();
|
|
$detail_data[] = array(
|
|
"user_id" => $buy_user_id,
|
|
"curr_id" => 1,
|
|
"price" => $user_market['num'],
|
|
"cart" => 1,
|
|
"type" => 2,
|
|
"description" => "C2C购买交易",
|
|
"createtime" => time(),
|
|
"before_num" => $buy_user_curr['num_fb'],
|
|
"after_num" => $buy_user_curr['num_fb'] + $user_market['num'],
|
|
);
|
|
|
|
Db::startTrans();
|
|
try {
|
|
|
|
Db::name("app_market_user")->where("id",$id)->update($update);
|
|
Db::name("app_detailed_fb")->insertAll($detail_data);
|
|
Db::name("app_currency_user")->where("id",$buy_user_curr['id'])->setInc("num_fb",$user_market['num']);
|
|
if($user_market['residue_num']>$user_market['num']){
|
|
$market_update = array(
|
|
"residue_num" => $user_market['residue_num']-$user_market['num'],
|
|
"status" => 1,
|
|
"updatetime" => time(),
|
|
);
|
|
}else{
|
|
$market_update = array(
|
|
"residue_num" => 0,
|
|
"status" => 3,
|
|
"endtime" => time(),
|
|
);
|
|
}
|
|
Db::name("app_market")->where("id",$user_market['order_id'])->update($market_update);
|
|
Db::commit();
|
|
lopRedis($symbol);
|
|
$this->success(__("交易成功"));
|
|
} catch (Exception $e) {
|
|
Db::rollback();
|
|
lopRedis($symbol);
|
|
$this->error(__("网络错误"));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 申述
|
|
*/
|
|
public function shenshu()
|
|
{
|
|
$user_id = $this->auth->id;
|
|
$id = $this->request->post("id");
|
|
$content = $this->request->post("content");
|
|
$annex_image = $this->request->post("annex_image");
|
|
|
|
$user_market = Db::name("app_market_user")->where("id",$id)->find();
|
|
|
|
if(empty($user_market)){
|
|
$this->error(__("订单不存在"));
|
|
}
|
|
if(!$content){
|
|
$this->error(__("请输入申述内容"));
|
|
}
|
|
if(!$annex_image){
|
|
$this->error(__("请上传凭证附件"));
|
|
}
|
|
if($user_market['status'] != 2){
|
|
$this->error(__("此订单不可申述"));
|
|
}
|
|
if($user_market['relid'] != $user_id && $user_market['user_id'] != $user_id){
|
|
$this->error(__("非法操作"));
|
|
}
|
|
if(($user_market['relid'] == $user_id && $user_market['type'] == 1) || ($user_market['user_id'] == $user_id && $user_market['type'] == 2)){
|
|
//卖方申述
|
|
$type = 1;
|
|
}else{
|
|
//买方申述
|
|
$type = 2;
|
|
}
|
|
$update = array(
|
|
"status" => 3,
|
|
);
|
|
$appeal_data = array(
|
|
"user_id" => $user_id,
|
|
"order_id" => $id,
|
|
"type" => $type,
|
|
"content" => $content,
|
|
"annex_images" => $annex_image,
|
|
"victory" => $type,
|
|
"createtime" => time(),
|
|
);
|
|
Db::startTrans();
|
|
try {
|
|
Db::name("app_market_user")->where("id",$id)->update($update);
|
|
Db::name("app_market_appeal")->insert($appeal_data);
|
|
Db::commit();
|
|
$this->success(__("申述成功"));
|
|
} catch (Exception $e) {
|
|
Db::rollback();
|
|
$this->error(__("网络错误"));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 补充申述
|
|
*/
|
|
public function replenish()
|
|
{
|
|
$user_id = $this->auth->id;
|
|
$appeal_id = $this->request->post("appeal_id");
|
|
$content = $this->request->post("content");
|
|
$annex_image = $this->request->post("annex_image");
|
|
|
|
$appeal = Db::name("app_market_appeal")->where("id",$appeal_id)->find();
|
|
$user_market = Db::name("app_market_user")->where("id",$appeal['order_id'])->find();
|
|
|
|
if(empty($appeal)){
|
|
$this->error(__("申述不存在"));
|
|
}
|
|
if(empty($user_market)){
|
|
$this->error(__("订单不存在"));
|
|
}
|
|
if(!$content){
|
|
$this->error(__("请输入申述内容"));
|
|
}
|
|
if(!$annex_image){
|
|
$this->error(__("请上传凭证附件"));
|
|
}
|
|
if($user_market['status'] != 3){
|
|
$this->error(__("该申述订单已被处理"));
|
|
}
|
|
if($user_market['relid'] != $user_id && $user_market['user_id'] != $user_id){
|
|
$this->error(__("非法操作"));
|
|
}
|
|
if($user_market['relid'] == $user_id){
|
|
//买方申述
|
|
$type = 2;
|
|
}else{
|
|
//卖方申述
|
|
$type = 1;
|
|
}
|
|
$appeal_data = array(
|
|
"appeal_id" => $appeal_id,
|
|
"user_id" => $user_id,
|
|
"type" => $type,
|
|
"content" => $content,
|
|
"images" => $annex_image,
|
|
"addtime" => time(),
|
|
);
|
|
Db::startTrans();
|
|
try {
|
|
Db::name("app_market_appeal_log")->insert($appeal_data);
|
|
Db::commit();
|
|
$this->success(__("提交成功"));
|
|
} catch (Exception $e) {
|
|
Db::rollback();
|
|
$this->error(__("网络错误"));
|
|
}
|
|
}
|
|
}
|