502 lines
25 KiB
PHP
Executable File
502 lines
25 KiB
PHP
Executable File
<?php
|
||
// +----------------------------------------------------------------------
|
||
// | likeshop开源商城系统
|
||
// +----------------------------------------------------------------------
|
||
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
||
// | gitee下载:https://gitee.com/likeshop_gitee
|
||
// | github下载:https://github.com/likeshop-github
|
||
// | 访问官网:https://www.likeshop.cn
|
||
// | 访问社区:https://home.likeshop.cn
|
||
// | 访问手册:http://doc.likeshop.cn
|
||
// | 微信公众号:likeshop技术社区
|
||
// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
|
||
// | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
|
||
// | 禁止对系统程序代码以任何目的,任何形式的再发布
|
||
// | likeshop团队版权所有并拥有最终解释权
|
||
// +----------------------------------------------------------------------
|
||
// | author: likeshop.cn.team
|
||
// +----------------------------------------------------------------------
|
||
namespace app\index\controller;
|
||
|
||
|
||
|
||
use app\common\basics\IndexBase;
|
||
use app\common\server\ConfigServer;
|
||
use think\facade\Db;
|
||
use app\api\logic\BillionsLogic;
|
||
use app\common\server\WeChatPayServer;
|
||
use app\common\server\WeChatServer;
|
||
class Index extends IndexBase
|
||
{
|
||
public function index()
|
||
{
|
||
$template = app()->getRootPath() . 'public/pc/index.html';
|
||
if (is_mobile()) {
|
||
$template = app()->getRootPath() . 'public/mobile/index.html';
|
||
}
|
||
if (file_exists($template)) {
|
||
return view($template);
|
||
}
|
||
return '';
|
||
}
|
||
|
||
|
||
public function app()
|
||
{
|
||
if (strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone') || strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')) {
|
||
$url = ConfigServer::get('app', 'line_ios', '');
|
||
} else {
|
||
$url = ConfigServer::get('app', 'line_android', '');
|
||
}
|
||
|
||
if (empty($url)) {
|
||
echo '未设置app下载链接';
|
||
exit;
|
||
}
|
||
if (!preg_match("/^http(s)?:\\/\\/.+/", $url)) {
|
||
$url = "http://".$url;
|
||
}
|
||
|
||
$this->redirect($url);
|
||
}
|
||
|
||
public function localReturn(){
|
||
$re = $_REQUEST;
|
||
$data['content'] = $re;
|
||
$data['dtime'] = date("Y-m-d H:i:s");
|
||
Db::name("local_return")->save($data);
|
||
if (gettype($re) == "string") {
|
||
$data['content'] = $re;
|
||
$data['dtime'] = date("Y-m-d H:i:s");
|
||
Db::name("local_return")->save($data);
|
||
} else {
|
||
$data['content'] = var_export($re, true);
|
||
$data['dtime'] = date("Y-m-d H:i:s");
|
||
Db::name("local_return")->save($data);
|
||
}
|
||
}
|
||
public function billions(){
|
||
|
||
$result = Db::name("user_billions_surplus")
|
||
->where("residue_price",">","0")
|
||
// ->where("user_start_price",">","0")
|
||
->find();
|
||
// var_dump($result);die;
|
||
$r = array();
|
||
$i = 0;
|
||
if($result != null){
|
||
$rst = Db::name("user_billions")
|
||
->where("billions_id","<=",$result['billions_id'])
|
||
->where("state",0)
|
||
->select()->toArray();
|
||
// print_r("<pre>");
|
||
// var_dump($rst);
|
||
$rst1 = Db::name("user_billions_surplus_log")->field("user_billion_id")
|
||
->where("surplus_id",$result['surplus_id'])
|
||
->select()->toArray();
|
||
$a = array();
|
||
foreach($rst1 as $key=>$value){
|
||
$a[] = $value['user_billion_id'];
|
||
}
|
||
// print_r("<pre>");
|
||
// var_dump($result['surplus_id']);
|
||
// var_dump($a);
|
||
|
||
foreach($rst as $key=>$value){
|
||
$b = array_search($value['billions_id'],$a);
|
||
// var_dump($b);
|
||
// var_dump($value['billions_id']);
|
||
if($b===false){
|
||
$r[$i]['billions_id'] = $value['billions_id'];
|
||
$r[$i]['surplus_id'] = $result['surplus_id'];
|
||
$r[$i]['user_id'] = $value['user_id'];
|
||
$r[$i]['order_price'] = $value['order_price'];
|
||
$r[$i]['subsidy_price'] = $value['subsidy_price'];
|
||
$r[$i]['withdrawal_amount'] = $value['withdrawal_amount'];
|
||
$r[$i]['available_amount'] = $value['available_amount'];
|
||
$i = $i+1;
|
||
if($i == 1000){
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
// print_r("<pre>");
|
||
// var_dump($r);die;
|
||
// foreach($rst as $key=>$value){
|
||
// $rs = Db::name("user_billions_surplus_log")
|
||
// ->field("surplus_id")
|
||
// ->where("user_billion_id",$value['billions_id'])
|
||
// ->where("surplus_id",$result['surplus_id'])
|
||
// ->find();
|
||
// if($rs == null){
|
||
// $r[$i]['billions_id'] = $value['billions_id'];
|
||
// $r[$i]['surplus_id'] = $rs['surplus_id'];
|
||
// $r[$i]['user_id'] = $value['user_id'];
|
||
// $r[$i]['order_price'] = $value['order_price'];
|
||
// $r[$i]['subsidy_price'] = $value['subsidy_price'];
|
||
// $i = $i+1;
|
||
// if($i == 1000){
|
||
// break;
|
||
// }
|
||
// }
|
||
// }
|
||
// print_r("<pre>");
|
||
// var_dump($r);die;
|
||
// die;
|
||
// $r = Db::name("user_billions")->alias("a")
|
||
// ->field("a.billions_id,b.surplus_id,a.user_id,a.order_price,a.subsidy_price")
|
||
// ->join("user_billions_surplus_log b","a.billions_id = b.billions_id","left")
|
||
// ->where("a.billions_id","<=",$result['billions_id'])
|
||
// ->where("b.surplus_id IS NULL")
|
||
// ->where("a.state",0)
|
||
// ->limit(1000)
|
||
// ->select()->toArray();
|
||
if(count($r)>0){
|
||
foreach($r as $key=>$value){
|
||
$ranking = BillionsLogic::getRanking($value['billions_id']);
|
||
$data['price'] = 0;
|
||
if($ranking <= $result['user_start_num']){
|
||
if($result['user_start_price'] >0){
|
||
if($result['user_start_avg_price'] <= $result['user_start_price']){
|
||
$dec_price = $data['price'] = $result['user_start_avg_price'];
|
||
}else{
|
||
$dec_price = $data['price'] = $result['user_start_price'];
|
||
}
|
||
Db::name("user_billions_surplus")
|
||
->where("surplus_id",$result['surplus_id'])
|
||
->dec("user_start_price",$dec_price)
|
||
->update();
|
||
}
|
||
}else{
|
||
if($result['user_price'] <= $result['residue_price']){
|
||
$dec_price = $data['price'] = $result['user_price'];
|
||
}else{
|
||
$dec_price = $data['price'] = $result['residue_price'];
|
||
}
|
||
Db::name("user_billions_surplus")
|
||
->where("surplus_id",$result['surplus_id'])
|
||
->dec("residue_price",$dec_price)
|
||
->update();
|
||
}
|
||
$data['surplus_id'] = $result['surplus_id'];
|
||
$data['user_id'] = $value['user_id'];
|
||
$data['billions_id'] = $result['billions_id'];
|
||
$data['user_billion_id'] = $value['billions_id'];
|
||
$data['dtime'] = date("Y-m-d H:i:s");
|
||
Db::name("user_billions_surplus_log")->save($data);
|
||
if($value['order_price'] <= $data['price'] + $value['subsidy_price']){
|
||
// 查询用户总余额账户
|
||
$first_user_all_account = Db::name("subsidy_withdrawal")->where("user_id",$value['user_id'])->find();
|
||
if($first_user_all_account != null){
|
||
// 判断账户是否可用
|
||
if($first_user_all_account['state'] == 0 || $first_user_all_account['state'] == 2){
|
||
return -2;
|
||
}
|
||
$o = round($value['order_price'] - $value['available_amount'],8);
|
||
// 更新主表
|
||
$first_update_subsidy_withdrawal = Db::name("subsidy_withdrawal")
|
||
->where("user_id",$value['user_id'])
|
||
->inc("total_amount",$o)
|
||
->update();
|
||
if(!$first_update_subsidy_withdrawal){
|
||
return -4;
|
||
}
|
||
$dat['subsidy_withdrawal_id'] = $first_user_all_account['id'];
|
||
$dat['user_id'] = $value['user_id'];
|
||
$dat['billions_id'] = $value['billions_id'];
|
||
$dat['type'] = 1;
|
||
$dat['money'] = $o;
|
||
$dat['available_amount'] = $value['available_amount'];
|
||
$dat['create_time'] = date("Y-m-d H:i:s");
|
||
// 插入提现记录
|
||
$first_save_record = Db::name("subsidy_withdrawal_record")->save($dat);
|
||
if ($first_save_record) {
|
||
$send = array(
|
||
'user_id' => $value['user_id'],
|
||
'money' => $o
|
||
);
|
||
sendBillionsSubsidySMS($send);
|
||
}
|
||
|
||
if(!$first_save_record){
|
||
return -5;
|
||
}
|
||
}else{
|
||
$d['user_id'] = $value['user_id'];
|
||
$d['total_amount'] = round($value['order_price'] - $value['available_amount'],8);
|
||
$first_update_subsidy_withdrawal = Db::name("subsidy_withdrawal")->insertGetId($d);
|
||
if(!$first_update_subsidy_withdrawal){
|
||
return -4;
|
||
}
|
||
$dat['subsidy_withdrawal_id'] = $first_update_subsidy_withdrawal;
|
||
$dat['user_id'] = $value['user_id'];
|
||
$dat['billions_id'] = $value['billions_id'];
|
||
$dat['type'] = 1;
|
||
$dat['available_amount'] = $value['available_amount'];
|
||
$dat['money'] = $d['total_amount'];
|
||
$dat['create_time'] = date("Y-m-d H:i:s");
|
||
// 插入提现记录
|
||
$first_save_record = Db::name("subsidy_withdrawal_record")->save($dat);
|
||
if ($first_save_record) {
|
||
$send = array(
|
||
'user_id' => $value['user_id'],
|
||
'money' => $d['total_amount']
|
||
);
|
||
sendBillionsSubsidySMS($send);
|
||
}
|
||
|
||
if(!$first_save_record){
|
||
return -5;
|
||
}
|
||
}
|
||
// 更新排队状态
|
||
$first_update_user_billions = Db::name("user_billions")->where("billions_id",$value['billions_id'])->update(['subsidy_price'=> $value['order_price'],"state"=>2]);
|
||
if(!$first_update_user_billions){
|
||
return -3;
|
||
}
|
||
}else{
|
||
$subsidy_price_sum = round($value['subsidy_price']+$data['price'],8);
|
||
$datd['subsidy_price'] = $subsidy_price_sum;
|
||
$datd['withdrawal_amount'] = round($value['withdrawal_amount']+$data['price'],8);
|
||
Db::name("user_billions")->where("billions_id",$value['billions_id'])->update($datd);
|
||
}
|
||
}
|
||
}else{
|
||
if($result['user_start_price']>0){
|
||
$rets = Db::name("user_billions")->where("state",0)->limit(1)->select()->toArray();
|
||
if($rets[0]['order_price'] <= $result['user_start_price'] + $rets[0]['subsidy_price']){
|
||
$p = $result['user_start_price'] + $rets[0]['subsidy_price'] - $rets[0]['order_price'];
|
||
Db::name("user_billions_surplus")
|
||
->where("surplus_id",$result['surplus_id'])
|
||
// ->dec("user_start_price",0)
|
||
->update(['user_start_price'=>$p]);
|
||
// 查询用户总余额账户
|
||
$first_user_all_account = Db::name("subsidy_withdrawal")
|
||
->where("user_id",$rets[0]['user_id'])
|
||
->find();
|
||
if($first_user_all_account != null){
|
||
// 判断账户是否可用
|
||
if($first_user_all_account['state'] == 0 || $first_user_all_account['state'] == 2){
|
||
return -2;
|
||
}
|
||
$o = round($rets[0]['order_price'] - $rets[0]['available_amount'],8);
|
||
// 更新主表
|
||
$first_update_subsidy_withdrawal = Db::name("subsidy_withdrawal")
|
||
->where("user_id",$rets[0]['user_id'])
|
||
->inc("total_amount",$o)
|
||
->update();
|
||
if(!$first_update_subsidy_withdrawal){
|
||
return -4;
|
||
}
|
||
$dat['subsidy_withdrawal_id'] = $first_user_all_account['id'];
|
||
$dat['user_id'] = $rets[0]['user_id'];
|
||
$dat['billions_id'] = $rets[0]['billions_id'];
|
||
$dat['type'] = 1;
|
||
$dat['available_amount'] = $rets[0]['available_amount'];
|
||
$dat['money'] = $rets[0]['order_price'];
|
||
$dat['create_time'] = date("Y-m-d H:i:s");
|
||
// 插入提现记录
|
||
$first_save_record = Db::name("subsidy_withdrawal_record")->save($dat);
|
||
if ($first_save_record) {
|
||
$send = array(
|
||
'user_id' => $rets[0]['user_id'],
|
||
'money' => $o
|
||
);
|
||
sendBillionsSubsidySMS($send);
|
||
}
|
||
|
||
if(!$first_save_record){
|
||
return -5;
|
||
}
|
||
}else{
|
||
$d['user_id'] = $rets[0]['user_id'];
|
||
$d['total_amount'] = round($rets[0]['order_price']-$rets[0]['available_amount'],8);
|
||
$first_update_subsidy_withdrawal = Db::name("subsidy_withdrawal")->insertGetId($d);
|
||
if(!$first_update_subsidy_withdrawal){
|
||
return -4;
|
||
}
|
||
$dat['subsidy_withdrawal_id'] = $first_update_subsidy_withdrawal;
|
||
$dat['user_id'] = $rets[0]['user_id'];
|
||
$dat['billions_id'] = $rets[0]['billions_id'];
|
||
$dat['type'] = 1;
|
||
$dat['available_amount'] = $rets[0]['available_amount'];
|
||
$dat['money'] = $d['total_amount'];
|
||
$dat['create_time'] = date("Y-m-d H:i:s");
|
||
// 插入提现记录
|
||
$first_save_record = Db::name("subsidy_withdrawal_record")->save($dat);
|
||
if ($first_save_record) {
|
||
$send = array(
|
||
'user_id' => $rets[0]['user_id'],
|
||
'money' => $d['total_amount']
|
||
);
|
||
sendBillionsSubsidySMS($send);
|
||
}
|
||
|
||
if(!$first_save_record){
|
||
return -5;
|
||
}
|
||
}
|
||
// 更新排队状态
|
||
$first_update_user_billions = Db::name("user_billions")
|
||
->where("billions_id",$rets[0]['billions_id'])
|
||
->update(['subsidy_price'=> $rets[0]['order_price'],"state"=>2]);
|
||
if(!$first_update_user_billions){
|
||
return -3;
|
||
}
|
||
}else{
|
||
Db::name("user_billions_surplus")
|
||
->where("surplus_id",$result['surplus_id'])
|
||
->dec("user_start_price",0)
|
||
->update();
|
||
$subsidy_price_sum = round($rets[0]['subsidy_price']+$result['user_start_price'],8);
|
||
$datd['subsidy_price'] = $subsidy_price_sum;
|
||
$datd['withdrawal_amount'] = round($rets[0]['withdrawal_amount']+$result['user_start_price'],8);
|
||
|
||
Db::name("user_billions")->where("billions_id",$rets[0]['billions_id'])->update($datd);
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
if($result['residue_price']>0){
|
||
$rets = Db::name("user_billions")->where("state",0)->limit(1)->select()->toArray();
|
||
if($rets[0]['order_price'] <= $result['residue_price'] + $rets[0]['subsidy_price']){
|
||
$p = $result['residue_price'] + $rets[0]['subsidy_price'] - $rets[0]['order_price'];
|
||
Db::name("user_billions_surplus")
|
||
->where("surplus_id",$result['surplus_id'])
|
||
// ->dec("user_start_price",0)
|
||
->update(['residue_price'=>$p]);
|
||
// 查询用户总余额账户
|
||
$first_user_all_account = Db::name("subsidy_withdrawal")->where("user_id",$rets[0]['user_id'])->find();
|
||
if($first_user_all_account != null){
|
||
// 判断账户是否可用
|
||
if($first_user_all_account['state'] == 0 || $first_user_all_account['state'] == 2){
|
||
return -2;
|
||
}
|
||
$o = round($rets[0]['order_price'] - $rets[0]['available_amount'],8);
|
||
// 更新主表
|
||
$first_update_subsidy_withdrawal = Db::name("subsidy_withdrawal")
|
||
->where("user_id",$rets[0]['user_id'])
|
||
->inc("total_amount",$o)->update();
|
||
if(!$first_update_subsidy_withdrawal){
|
||
return -4;
|
||
}
|
||
$dat['subsidy_withdrawal_id'] = $first_user_all_account['id'];
|
||
$dat['user_id'] = $rets[0]['user_id'];
|
||
$dat['billions_id'] = $rets[0]['billions_id'];
|
||
$dat['type'] = 1;
|
||
$dat['money'] = $o;
|
||
$dat['create_time'] = date("Y-m-d H:i:s");
|
||
// 插入提现记录
|
||
$first_save_record = Db::name("subsidy_withdrawal_record")->save($dat);
|
||
if ($first_save_record) {
|
||
$send = array(
|
||
'user_id' => $rets[0]['user_id'],
|
||
'money' => $o
|
||
);
|
||
sendBillionsSubsidySMS($send);
|
||
}
|
||
|
||
if(!$first_save_record){
|
||
return -5;
|
||
}
|
||
}else{
|
||
$d['user_id'] = $rets[0]['user_id'];
|
||
$d['total_amount'] = round($rets[0]['order_price']-$rets[0]['available_amount'],8);
|
||
$first_update_subsidy_withdrawal = Db::name("subsidy_withdrawal")->insertGetId($d);
|
||
if(!$first_update_subsidy_withdrawal){
|
||
return -4;
|
||
}
|
||
$dat['subsidy_withdrawal_id'] = $first_update_subsidy_withdrawal;
|
||
$dat['user_id'] = $rets[0]['user_id'];
|
||
$dat['billions_id'] = $rets[0]['billions_id'];
|
||
$dat['type'] = 1;
|
||
$dat['money'] = $d['total_amount'];
|
||
$dat['available_amount'] = $rets[0]['available_amount'];
|
||
$dat['create_time'] = date("Y-m-d H:i:s");
|
||
// 插入提现记录
|
||
$first_save_record = Db::name("subsidy_withdrawal_record")->save($dat);
|
||
if ($first_save_record) {
|
||
$send = array(
|
||
'user_id' => $rets[0]['user_id'],
|
||
'money' => $d['total_amount']
|
||
);
|
||
sendBillionsSubsidySMS($send);
|
||
}
|
||
|
||
if(!$first_save_record){
|
||
return -5;
|
||
}
|
||
}
|
||
// 更新排队状态
|
||
$first_update_user_billions = Db::name("user_billions")->where("billions_id",$rets[0]['billions_id'])->update(['subsidy_price'=> $rets[0]['order_price'],"state"=>2]);
|
||
if(!$first_update_user_billions){
|
||
return -3;
|
||
}
|
||
}else{
|
||
Db::name("user_billions_surplus")
|
||
->where("surplus_id",$result['surplus_id'])
|
||
->update(["residue_price"=>0]);
|
||
|
||
$subsidy_price_sum = round($result['residue_price']+$rets[0]['subsidy_price'],8);
|
||
$datd['subsidy_price'] = $subsidy_price_sum;
|
||
$datd['withdrawal_amount'] = round($rets[0]['withdrawal_amount']+$result['residue_price'],8);
|
||
Db::name("user_billions")->where("billions_id",$rets[0]['billions_id'])->update($datd);
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
print_r("<pre>");
|
||
var_dump($r);
|
||
// foreach($r as $key=>$value){
|
||
// var_dump($value['billions_id']);
|
||
// $data['name'] = $value['billions_id'];
|
||
// Db::name("user_billions_test")->save($data);
|
||
|
||
// }
|
||
|
||
// var_dump($r);
|
||
}
|
||
|
||
}
|
||
// 退款
|
||
public function retund(){die();
|
||
$config = WeChatServer::getPayConfigBySource(1)['config'];
|
||
if (empty($config)) {
|
||
throw new Exception('请联系管理员设置微信相关配置!');
|
||
}
|
||
if (!isset($config['cert_path']) || !isset($config['key_path'])) {
|
||
throw new Exception('请联系管理员设置微信证书!');
|
||
}
|
||
if (!file_exists($config['cert_path']) || !file_exists($config['key_path'])) {
|
||
throw new Exception('微信证书不存在,请联系管理员!');
|
||
}
|
||
|
||
$result = WeChatPayServer::refund($config, [
|
||
'transaction_id' => "4200002417202409298999399108",
|
||
'refund_sn' => "202409291431067057",
|
||
'total_fee' => intval(strval(650 * 100)),//订单金额,单位为分
|
||
'refund_fee' => intval(strval(650 * 100)),//退款金额
|
||
]);
|
||
var_dump($result);
|
||
if (isset($result['return_code']) && $result['return_code'] == 'FAIL') {
|
||
throw new Exception($result['return_msg']);
|
||
}
|
||
if (isset($result['err_code_des'])) {
|
||
throw new Exception($result['err_code_des']);
|
||
}
|
||
if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
|
||
//更新退款日志记录
|
||
$dat['wechat_refund_id'] = $result['refund_id'] ?? 0;
|
||
$dat['refund_msg'] = json_encode($result, JSON_UNESCAPED_UNICODE);
|
||
$dat['dtime'] = date("Y-m-d H:i:s");
|
||
Db::name("local_pay_return")->save($dat);
|
||
} else {
|
||
throw new Exception('微信支付退款失败');
|
||
}
|
||
}
|
||
}
|