You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
860 lines
36 KiB
860 lines
36 KiB
<?php
|
|
|
|
namespace app\api\controller;
|
|
|
|
use app\common\controller\Api;
|
|
use app\admin\model\Warehouse;
|
|
use app\admin\model\Goods;
|
|
use app\admin\model\Order as OrderModel;
|
|
use app\admin\model\order\Detail;
|
|
use app\admin\model\shopro\user\Address;
|
|
use app\admin\model\shopro\user\Coupon as UserCouponModel;
|
|
use app\common\service\UserService;
|
|
use app\common\service\OrderService;
|
|
use think\Config;
|
|
use app\common\model\User;
|
|
use addons\shopro\library\pay\PayService;
|
|
use think\Db;
|
|
use Yansongda\Pay\Exceptions\GatewayException;
|
|
use app\admin\model\transaction\Record;
|
|
use think\Log;
|
|
use addons\shopro\traits\CouponSend;
|
|
use think\Cache;
|
|
/**
|
|
* 首页接口
|
|
*/
|
|
class Order extends Api
|
|
{
|
|
use CouponSend;
|
|
protected $noNeedLogin = ['callback','test'];
|
|
protected $noNeedRight = ['*'];
|
|
const CACHE_TIME = 5;
|
|
/**
|
|
* 首页
|
|
*
|
|
*/
|
|
public function getWarehouseList()
|
|
{
|
|
$user = $this->auth->getUserinfo();
|
|
|
|
$list = model('\app\admin\model\Warehouse')->limit(1)->select();
|
|
if (!$list) {
|
|
$this->success('succ');
|
|
}
|
|
$time = time();
|
|
foreach ($list as $key => &$value) {
|
|
|
|
// if ($value['id'] != $user['warehouse_id']) {
|
|
// unset($list[$key]);
|
|
// continue;
|
|
// }
|
|
|
|
$value['is_allow_access'] = 0;
|
|
if ($user['pid'] > 0) {
|
|
$value['is_allow_access'] = 1;
|
|
}
|
|
if (in_array($user['id'], [61])) {
|
|
$value['status'] ='normal';
|
|
$value['is_allow_access'] = 1;
|
|
}
|
|
|
|
}
|
|
$this->success('succ', array_values($list));
|
|
}
|
|
/**
|
|
* 用户是否允许进入仓库
|
|
* [userIsAllowAccess description]
|
|
* @param [type] $user [description]
|
|
* @param [type] $warehouse [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function userIsAllowAccess($user, $warehouse, $goods, $time){
|
|
$advance_access_minute = $user['advance_access_minute'] * 60;
|
|
$is_allow_access = 0;
|
|
//是否在时间范围内
|
|
$start = strtotime($warehouse['start']);
|
|
$end = strtotime($warehouse['end']);
|
|
$start_date = date("Y-m-d H:i:s",$start);
|
|
$end_date = date("Y-m-d H:i:s", $end);
|
|
$code = $start."-".$end;
|
|
//仓库限制用户总抢购次数
|
|
if ($warehouse['limit_buy_num'] > 0) {
|
|
$count = Db::name('user_access_record')->where('user_id', $user['id'])->where('warehouse_id', $warehouse['id'])->where('code', $code)->count();
|
|
// var_dump($count);
|
|
// exit();
|
|
if ($count > $warehouse['limit_buy_num']) {
|
|
$is_allow_access = 0;
|
|
return $is_allow_access;
|
|
}
|
|
}
|
|
|
|
//提前进场
|
|
if ($advance_access_minute > 0 && $user['deadline'] > $time) {
|
|
|
|
if (($time + $advance_access_minute) >= $start && ($time + $advance_access_minute) <= $end) {
|
|
//提前进入次数
|
|
$count = Db::name('user_access_record')->where('user_id', $user['id'])->where('warehouse_id', $warehouse['id'])->where('code', $code)->where('is_advance', 1)->count();
|
|
if ($user['advance_access_num'] == 0 || $count < $user['advance_access_num']) {
|
|
$is_allow_access = 1;
|
|
return $is_allow_access;
|
|
}
|
|
}
|
|
}
|
|
|
|
//正常进场
|
|
if ($time >= $start && $time <= $end) {
|
|
//正常进入次数
|
|
$count = Db::name('user_access_record')->where('user_id', $user['id'])->where('warehouse_id', $warehouse['id'])->where('code', $code)->where('is_advance',0)->count();
|
|
if ($user['buy_goods_num_per_day'] == 0 || $count < $user['buy_goods_num_per_day']) {
|
|
$is_allow_access = 2;
|
|
return $is_allow_access;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
return $is_allow_access;
|
|
}
|
|
/**
|
|
* 限制购买次数
|
|
* [limitBuyNum description]
|
|
* @param [type] $user [description]
|
|
* @param [type] $warehouse [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function limitBuyNum($user, $warehouse){
|
|
|
|
$is_allow_access = 0;
|
|
//return $is_allow_access;
|
|
//是否在时间范围内
|
|
$start = strtotime($warehouse['start']);
|
|
$end = strtotime($warehouse['end']);
|
|
$start_date = date("Y-m-d H:i",$start);
|
|
$end_date = date("Y-m-d H:i", $end);
|
|
$code = $start_date."-".$end_date;
|
|
//提前下单次数(买单)
|
|
$advance_count = OrderModel::where('order_type',0)->where('user_id', $user['id'])->where('warehouse_id', $warehouse['id'])->where('code', $code)->where('is_advance', 1)->count();
|
|
if ($user['advance_access_num'] > 0 && $advance_count >= $user['advance_access_num']) {
|
|
$is_allow_access = 1;
|
|
return $is_allow_access;
|
|
}
|
|
//正常下单次数(买单)
|
|
$normal_count = OrderModel::where('order_type',0)->where('user_id', $user['id'])->where('warehouse_id', $warehouse['id'])->where('code', $code)->where('is_advance', 0)->count();
|
|
// var_dump($normal_count);
|
|
// exit();
|
|
if ($user['buy_goods_num_per_day'] > 0 && $normal_count >= $user['buy_goods_num_per_day']) {
|
|
$is_allow_access = 1;
|
|
return $is_allow_access;
|
|
}
|
|
$count = $advance_count + $normal_count;
|
|
//总下单次数
|
|
if ($warehouse['limit_buy_num'] > 0 && $count >= $warehouse['limit_buy_num']) {
|
|
$is_allow_access = 1;
|
|
return $is_allow_access;
|
|
}
|
|
|
|
//正常下单次数(买单)
|
|
// $start_date = date("Y-m-d H:i:s",$start);
|
|
// $end_date = date("Y-m-d H:i:s", $end);
|
|
// $code = $start."-".$end;
|
|
// //仓库限制用户总抢购次数
|
|
// $count = Db::name('user_access_record')->where('user_id', $user['id'])->where('warehouse_id', $warehouse['id'])->where('code', $code)->count();
|
|
|
|
// if ($warehouse['limit_buy_num'] > 0 && $count > $warehouse['limit_buy_num']) {
|
|
// $is_allow_access = 1;
|
|
// return $is_allow_access;
|
|
// }
|
|
|
|
// //提前进入次数
|
|
// $count = Db::name('user_access_record')->where('user_id', $user['id'])->where('warehouse_id', $warehouse['id'])->where('code', $code)->where('is_advance', 1)->count();
|
|
// if ($user['advance_access_num'] > 0 && $count > $user['advance_access_num']) {
|
|
// $is_allow_access = 1;
|
|
// return $is_allow_access;
|
|
// }
|
|
|
|
// //正常进入次数
|
|
// $count = Db::name('user_access_record')->where('user_id', $user['id'])->where('warehouse_id', $warehouse['id'])->where('code', $code)->where('is_advance',0)->count();
|
|
// if ($user['buy_goods_num_per_day'] > 0 && $count > $user['buy_goods_num_per_day']) {
|
|
// $is_allow_access = 1;
|
|
// return $is_allow_access;
|
|
// }
|
|
|
|
|
|
return $is_allow_access;
|
|
}
|
|
/**
|
|
* 获取购买时间
|
|
* [getBuyTime description]
|
|
* @param [type] &$warehouse [description]
|
|
* @param [type] $user [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function getBuyTime(&$warehouse, $user){
|
|
$advance_access_minute = $user['advance_access_minute'] * 60;
|
|
$time = strtotime($warehouse['start']) - $advance_access_minute;
|
|
|
|
$warehouse['start'] = date("H:i", $time);
|
|
}
|
|
/**
|
|
* 商品列表
|
|
* [getGoodsList description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function getGoodsList(){
|
|
$page = $this->request->post("page", 1);
|
|
$limit = $this->request->post("limit", 20);
|
|
$warehouse_id = $this->request->post("warehouse_id", 3);
|
|
$user = $this->auth->getUserinfo();
|
|
$warehouse = Warehouse::where('id', $warehouse_id)->find();
|
|
if (!$warehouse) {
|
|
$this->error("区域不存在");
|
|
}
|
|
$is_allow_access = $this->limitBuyNum($user, $warehouse);
|
|
if ($is_allow_access > 0 ) {
|
|
$this->error("抢购次数被限制");
|
|
}
|
|
$list = Db::name('order')->alias('order')
|
|
->join("order_detail order_detail","order_detail.order_id = order.id")
|
|
->join("goods goods","goods.id = order_detail.goods_id")
|
|
->field("order.id,order.user_id,order.order_amount,order.pay_amount,order.warehouse_id")
|
|
->where('order.warehouse_id', $warehouse_id)
|
|
->where('order.order_type',1)
|
|
->where('order.status', 3)
|
|
// ->where('goods.status', 'normal')
|
|
->where('order.user_id', "<>", $user['id'])
|
|
->group('order.id')
|
|
->order('goods.status', 'desc')
|
|
->order("order.id desc")
|
|
->paginate($limit, false, [])->each(function ($item, $key){
|
|
$detail = Detail::where('order_id', $item['id'])->find();
|
|
$goods = Goods::where('id', $detail['goods_id'])->find();
|
|
$goods['image'] = cdnurl($goods['image'], true);
|
|
$item['goods'] = $goods;
|
|
return $item;
|
|
});
|
|
// $list = OrderModel::where('warehouse_id', $warehouse_id)->where('order_type',1)->where('status', 3)->where('user_id', "<>", $user['id'])->field("id,user_id,order_amount,pay_amount,warehouse_id")->order('id', 'desc')->paginate($this->request->param('list_rows', 20))->each(function ($item, $key){
|
|
// $detail = Detail::where('order_id', $item['id'])->find();
|
|
// $goods = Goods::where('id', $detail['goods_id'])->find();
|
|
// $goods['image'] = cdnurl($goods['image'], true);
|
|
// $item['goods'] = $goods;
|
|
// return $item;
|
|
// });
|
|
$this->success("succ", $list);
|
|
}
|
|
/**
|
|
* 商品列表
|
|
* [getGoodsList description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function goodsDetail(){
|
|
$id = $this->request->get("id", 1);
|
|
$user = $this->auth->getUserinfo();
|
|
$order = OrderModel::where('id', $id)->where('order_type',1)->where('status', 3)->where('user_id', "<>", $user['id'])->field("id,user_id,order_amount,pay_amount,warehouse_id")->find();
|
|
if (!$order) {
|
|
$this->error("售卖商品不存在");
|
|
}
|
|
$detail = Detail::where('order_id', $order['id'])->find();
|
|
if (!$detail) {
|
|
$this->error("售卖商品不存在");
|
|
}
|
|
$goods = Goods::where('id', $detail['goods_id'])->find();
|
|
if (!$goods) {
|
|
$this->error("售卖商品不存在");
|
|
}
|
|
$warehouse = Warehouse::where('id', $order['warehouse_id'])->find();
|
|
$is_allow_access = $this->limitBuyNum($user, $warehouse);
|
|
if ($is_allow_access > 0 ) {
|
|
$this->error("抢购次数被限制");
|
|
}
|
|
$this->getBuyTime($warehouse, $user);
|
|
$owner = User::where('id', $goods['owner_id'])->field(['id', 'username', 'nickname', 'mobile', 'avatar', 'score','sign_image','sign_status','alipay_image','wechat_image','buy_goods_num_per_day','advance_access_num','bank_realname','bank_mobile','bank_no','bank_address','reason','warehouse_id','advance_access_minute','deadline','pid','total_commission_amount','available_commission_amount'])->find();
|
|
$goods['warehouse'] = $warehouse;
|
|
$goods['owner'] = $owner;
|
|
$goods['image'] = cdnurl($goods['image'], true);
|
|
|
|
//写进入记录
|
|
// $start = date("Y-m-d H:i", strtotime($warehouse['start']));
|
|
// $end = date("Y-m-d H:i", strtotime($warehouse['end']));
|
|
// $code = $start."-".$end;
|
|
// $info1 = Db::name('user_access_record')->where('warehouse_id', $goods['warehouse_id'])->where('user_id', $user['id'])->where('code', $code)->count();
|
|
// if (!$info1) {
|
|
// Db::name('user_access_record')->insert(['user_id' => $user['id'],'warehouse_id' => $goods['warehouse_id'],'code' => $code, "createtime"=>time(), 'is_advance' => $user['advance_access_minute'] > 0 ? 1 : 0]);
|
|
// }
|
|
$order['goods'] = $goods;
|
|
$this->success("succ", $order);
|
|
}
|
|
/**
|
|
* 创建订单
|
|
* [createOrder description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function createOrder(){
|
|
$address_id = $this->request->post("address_id", 0);
|
|
$seller_order_id = $this->request->post("seller_order_id", 0);
|
|
$user_id = $this->auth->id;
|
|
$user = $this->auth->getUserinfo();
|
|
$address = Address::where('id', $address_id)->where('user_id', $user_id)->find();
|
|
if (!$address) {
|
|
$this->error("收货地址不存在");
|
|
}
|
|
//通过订单去找商品
|
|
$order = OrderModel::where('id', $seller_order_id)->where('order_type',1)->where('status', 3)->where('user_id', "<>", $user['id'])->field("id,user_id,order_amount,pay_amount,warehouse_id")->find();
|
|
if (!$order) {
|
|
$this->error("售卖商品不存在");
|
|
}
|
|
$detail = Detail::where('order_id', $order['id'])->find();
|
|
if (!$detail) {
|
|
$this->error("售卖商品不存在");
|
|
}
|
|
$goods = Goods::where('id', $detail['goods_id'])->where('status','normal')->find();
|
|
if (!$goods) {
|
|
$this->error("售卖商品不存在");
|
|
}
|
|
|
|
$warehouse = Warehouse::where('id', $goods['warehouse_id'])->find();
|
|
$this->getBuyTime($warehouse, $user);
|
|
if (time() < strtotime($warehouse['start'])) {
|
|
$this->error("抢购时间还没到!");
|
|
}
|
|
if (time() > strtotime($warehouse['end'])) {
|
|
$this->error("抢购时间已结束!");
|
|
}
|
|
$is_allow_access = $this->limitBuyNum($user, $warehouse);
|
|
if ($is_allow_access > 0) {
|
|
$this->error("抢购次数被限制");
|
|
}
|
|
//使用redis锁,限制下单
|
|
$lock_key = $this->request->domain()."_createOrder_".$seller_order_id;
|
|
$res = Cache::store('redis')->setnx($lock_key, 1);
|
|
|
|
if (!$res) {
|
|
$this->error("您的手速太慢了,商品已被抢!");
|
|
}
|
|
Cache::store('redis')->expire($lock_key, self::CACHE_TIME);
|
|
//exit();
|
|
$order_amount = $order['pay_amount'];
|
|
$pay_amount = $order_amount;
|
|
$order_sn = "ZH".date("YmdHis").rand(1000,9999);
|
|
$start = date("Y-m-d H:i", strtotime($warehouse['start']));
|
|
$end = date("Y-m-d H:i", strtotime($warehouse['end']));
|
|
$code = $start."-".$end;
|
|
//创建订单
|
|
$orderData = [
|
|
"user_id" => $user_id,
|
|
"sell_order_id" => $seller_order_id,
|
|
"buyer_id" => $user_id,
|
|
"seller_id" => $goods['owner_id'],
|
|
"order_sn" => $order_sn,
|
|
"warehouse_id" => $goods['warehouse_id'],
|
|
"order_amount" => $order_amount,
|
|
"pay_amount" => $pay_amount,
|
|
"actual_amount" => -$pay_amount,
|
|
"coupon_price" => 0,
|
|
"coupon_id" => 0,
|
|
"consignee" => $address['consignee'],
|
|
"mobile" => $address['consignee'],
|
|
"province" => $address['province_name'],
|
|
"city" => $address['city_name'],
|
|
"district" => $address['district_name'],
|
|
"address" => $address['address'],
|
|
"is_advance" => $start > time() ? 1 : 0,
|
|
"code" => $code,
|
|
];
|
|
$order = OrderModel::create($orderData);
|
|
if ($order === false) {
|
|
$this->error("下单失败");
|
|
}
|
|
$orderDetail = [
|
|
"order_id" => $order->id,
|
|
"buyer_id" => $user_id,
|
|
"seller_id" => $goods['owner_id'],
|
|
"goods_id" => $goods['id'],
|
|
"goods_name" => $goods['name'],
|
|
"goods_image" => $goods['image'],
|
|
"goods_price" => $goods['price'],
|
|
"num" => 1,
|
|
];
|
|
$ret = Detail::create($orderDetail);
|
|
if ($ret === false) {
|
|
$this->error("下单失败");
|
|
}
|
|
//更新商品状态为下架
|
|
Goods::where('id', $goods['id'])->update(['status' => 'hidden', "updatetime" => time()]);
|
|
//分佣给买家上级
|
|
$site = Config::get("site");
|
|
$commission = bcmul($order_amount, $site['primary_distribution'] * 0.01, 2);
|
|
$obj = new UserService();
|
|
$obj->userCommission(0, $this->auth->getUserinfo(), $order_sn, $commission);
|
|
|
|
Cache::store('redis')->rm($lock_key);
|
|
|
|
$this->success("succ");
|
|
}
|
|
/**
|
|
* 卖方订单列表
|
|
* [getSellerOrderList description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function getSellerOrderList(){
|
|
$user_id = $this->auth->id;
|
|
$page = $this->request->get("page", 1);
|
|
$limit = $this->request->get("limit", 10);
|
|
$status = $this->request->get("status", "");
|
|
$where = [];
|
|
if ($status != "") {
|
|
$where['status'] = ['in', explode(",", $status)];
|
|
} else {
|
|
$where['status'] = ['in', [0,1,3,4,5]];
|
|
}
|
|
$list = OrderModel::where('user_id', $user_id)->where('order_type', 1)->where($where)->order('id', 'desc')->paginate($this->request->param('list_rows', $limit))->each(function ($item, $key){
|
|
$order_goods = Detail::where('order_id', $item['id'])->select();
|
|
if ($order_goods) {
|
|
foreach ($order_goods as $key => $value) {
|
|
$value['goods_image'] = cdnurl($value['goods_image'], true);
|
|
}
|
|
}
|
|
|
|
$warehouse = Warehouse::where('id', $item['warehouse_id'])->select();
|
|
$item['order_goods'] = $order_goods;
|
|
$item['warehouse'] = $warehouse;
|
|
return $item;
|
|
});
|
|
$this->success("succ", $list);
|
|
}
|
|
/**
|
|
* 买方订单列表
|
|
* [getBuyerOrderList description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function getBuyerOrderList(){
|
|
$user_id = $this->auth->id;
|
|
$page = $this->request->get("page", 1);
|
|
$limit = $this->request->get("limit", 10);
|
|
$status = $this->request->get("status", "");
|
|
$where = [];
|
|
if ($status != "") {
|
|
$where['status'] = ['in', explode(",", $status)];
|
|
} else {
|
|
$where['status'] = ['in', [-1,0,1,2,3,4,5]];
|
|
}
|
|
$list = OrderModel::where('buyer_id', $user_id)->where($where)->order('id', 'desc')->paginate($this->request->param('list_rows', $limit))->each(function ($item, $key){
|
|
$order_goods = Detail::where('order_id', $item['id'])->select();
|
|
if ($order_goods) {
|
|
foreach ($order_goods as $key => $value) {
|
|
$value['goods_image'] = cdnurl($value['goods_image'], true);
|
|
if ($item['status'] == 7) {
|
|
$value['goods_price'] = $value['commission_price'];
|
|
$item['pay_amount'] = $value['commission_price'];
|
|
$item['order_amount'] = $value['commission_price'];
|
|
}
|
|
}
|
|
}
|
|
|
|
$warehouse = Warehouse::where('id', $item['warehouse_id'])->find();
|
|
$item['order_goods'] = $order_goods;
|
|
$item['warehouse'] = $warehouse;
|
|
return $item;
|
|
});
|
|
$this->success("succ", $list);
|
|
}
|
|
/**
|
|
* 订单详情
|
|
* [orderDetail description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function orderDetail(){
|
|
$user_id = $this->auth->id;
|
|
$id = $this->request->get("id", 0);
|
|
|
|
$info = OrderModel::where('id', $id)->find();
|
|
if (!$info) {
|
|
$this->success("succ", $info);
|
|
}
|
|
$order_goods = Detail::where('order_id', $info['id'])->select();
|
|
if ($order_goods) {
|
|
foreach ($order_goods as $key => $value) {
|
|
$value['goods_image'] = cdnurl($value['goods_image'], true);
|
|
}
|
|
}
|
|
|
|
$warehouse = Warehouse::where('id', $info['warehouse_id'])->find();
|
|
$info['order_goods'] = $order_goods;
|
|
$info['warehouse'] = $warehouse;
|
|
$info['entrusted_ratio'] = Config::get("site.entrusted_ratio");
|
|
$info['entrustment_ratio'] = Config::get("site.entrustment_ratio");
|
|
$seller = User::where('id', $info['seller_id'])->field(['id', 'username', 'nickname', 'mobile', 'avatar', 'score','sign_image','sign_status','alipay_image','wechat_image','buy_goods_num_per_day','advance_access_num','bank_realname','bank_mobile','bank_no','bank_address','reason','warehouse_id','advance_access_minute','deadline','pid','total_commission_amount','available_commission_amount'])->find();
|
|
$info['seller'] = $seller;
|
|
$info['pay_voucher'] = cdnurl($info['pay_voucher'], true);
|
|
$this->success("succ", $info);
|
|
}
|
|
/**
|
|
* 取消订单(废弃)
|
|
* [cancelOrder description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function cancelOrder(){
|
|
$id = $this->request->post("id", 0);
|
|
$order = OrderModel::where('id', $id)->find();
|
|
if (!$order) {
|
|
$this->error("订单不存在");
|
|
}
|
|
$info = OrderModel::where('id', $id)->update(['status' => -1,'cancel_time' => time()]);
|
|
if ($info === false) {
|
|
$this->error("取消失败");
|
|
}
|
|
$detail = Detail::where("order_id", $id)->find();
|
|
//上架商品
|
|
Goods::where('id', $detail['goods_id'])->update(['status' => 'normal', "updatetime" => time()]);
|
|
//分佣给买家上级
|
|
$site = Config::get("site");
|
|
$commission = bcmul($order['order_amount'], $site['primary_distribution'] * 0.01, 2);
|
|
$obj = new UserService();
|
|
$obj->userCommission(1, $this->auth->getUserinfo(), $order['order_sn'], -$commission);
|
|
|
|
$this->success("succ", $info);
|
|
}
|
|
/**
|
|
* 上传支付凭证
|
|
* [uploadPayVoucher description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function orderPay(){
|
|
$pay_voucher = $this->request->post("pay_voucher", "");
|
|
$status = $this->request->post("status", "");
|
|
$pay_method = $this->request->post("pay_method", 0);
|
|
$id = $this->request->post("id", "");
|
|
$info = OrderModel::where('id', $id)->find();
|
|
if (!$info) {
|
|
$this->error("订单不存在");
|
|
}
|
|
$upData = ['updatetime' => time()];
|
|
if ($status == 1) {
|
|
$upData['pay_voucher'] = $pay_voucher;
|
|
$upData['pay_time'] = time();
|
|
$upData['pay_method'] = $pay_method;
|
|
$upData['status'] = $status;
|
|
|
|
$detail = Detail::where("order_id", $id)->find();
|
|
//更新该商品的最后一个已上架的订单为已完成
|
|
$single = Db::name('order')->alias('order')
|
|
->join('order_detail detail', 'detail.order_id = order.id', 'left')
|
|
->field("order.id")
|
|
->where("goods_id", $detail['goods_id'])
|
|
->where("status", 7)
|
|
->order("order.id desc")
|
|
->find();
|
|
if ($single) {
|
|
OrderModel::where('id', $single['id'])->update(['status' => 6, "updatetime" =>time()]);
|
|
}
|
|
} else {
|
|
$upData['status'] = $status;
|
|
}
|
|
|
|
$info = OrderModel::where('id', $id)->update($upData);
|
|
if ($info === false) {
|
|
$this->error("操作失败");
|
|
}
|
|
$this->success("succ", $info);
|
|
}
|
|
/**
|
|
* 商品上架
|
|
* [upGoods description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function payUpGoods(){
|
|
$info = get_addon_info('epay');
|
|
if(empty($info) || $info['state'] == 0){
|
|
$this->error('请到后台插件管理安装《微信支付宝整合》插件');
|
|
}
|
|
|
|
$order_id = $this->request->post("order_id", 0);
|
|
$coupon_id = $this->request->post("coupon_id", 0);
|
|
$use_commission = $this->request->post("use_commission", 0);
|
|
$num = $this->request->post("num", 1);
|
|
$user = $this->auth->getUserinfo();
|
|
$user_id = $user['id'];
|
|
|
|
$goods = Detail::where('order_id', $order_id)->find();
|
|
$order = OrderModel::where("id", $order_id)->where('status', 2)->find();
|
|
if (!$goods || !$order) {
|
|
$this->error("订单不存在");
|
|
}
|
|
|
|
//商品是否上架
|
|
$goods_info = Goods::where('id', $goods['goods_id'])->where('status', 'hidden')->find();
|
|
if (!$goods_info) {
|
|
$this->error("商品不存在");
|
|
}
|
|
//判断上架时间,开始前一小时,和结束前一小时,以及活动中不能上架
|
|
$time = time();
|
|
$warehouse = Warehouse::where('id', $goods_info['warehouse_id'])->find();
|
|
$start = strtotime($warehouse['start']);
|
|
$end = strtotime($warehouse['end']);
|
|
$after_activity_hour = $warehouse['after_activity_hour'] * 3600;
|
|
$before_activity_hour = $warehouse['before_activity_hour'] * 3600;
|
|
|
|
|
|
// if ((($start - $time) > 0 && (($start - $time) < $before_activity_hour)) || (($time - $end) > 0 && (($time - $end) < $after_activity_hour)) || ($time > $start && $time < $end)) {
|
|
// $this->error("该时间段不能上架");
|
|
// }
|
|
|
|
$order_amount = $goods['goods_price'] * $goods['num'] ;
|
|
$coupon_price = 0;
|
|
if ($coupon_id) {
|
|
$coupon = UserCouponModel::with('coupon')->where('id', $coupon_id)->where('user_id', $user_id)->find();
|
|
// if($order_amount < $coupon['enough']) {
|
|
// $this->error("不能使用优惠卷");
|
|
// }
|
|
if($coupon['use_time'] > 0) {
|
|
$this->error("优惠卷已使用");
|
|
}
|
|
if(strtotime($coupon['use_end_time']) < time()) {
|
|
$this->error("优惠卷已过期");
|
|
}
|
|
$coupon_price = $coupon['amount'] ?? 0;
|
|
}
|
|
OrderModel::where("id", $order_id)->update(['coupon_id' => $coupon_id, 'coupon_price' => $coupon_price]);
|
|
$pay_sn = "NO".date("YmdHis").rand(1000,9999);
|
|
$fee = bcmul($order_amount, Config::get("site.entrustment_ratio") * 0.01, 2);
|
|
$pay_amount = $fee - $coupon_price;
|
|
$pay_amount = $pay_amount > 0 ? $pay_amount : 0;
|
|
$pay_amount = round($pay_amount);//四舍五入取整数
|
|
$pay_method = "mp";
|
|
$order_data = [
|
|
'order_id' => $order_id,
|
|
'out_trade_no' => $pay_sn,
|
|
'total_amount' => $fee,
|
|
'coupon_amount' => $coupon_price,
|
|
'pay_amount' => $pay_amount,
|
|
'status' => 0,
|
|
'user_id' => $user_id,
|
|
'create_time' => time(),
|
|
];
|
|
$params = [
|
|
'amount' => $pay_amount,
|
|
'orderid' => $pay_sn,
|
|
'type' => "wechat",
|
|
'title'=> "商品支付",
|
|
'notifyurl' => $this->request->domain()."/api/order/callback",
|
|
'returnurl'=>"",
|
|
'method'=>$pay_method,
|
|
'openid' => $user['openid'],
|
|
];
|
|
$goods_price = bcmul($goods['goods_price'], (Config::get("site.entrusted_ratio") * 0.01 + 1), 2);
|
|
// var_dump($order_data);
|
|
// var_dump($params);
|
|
// exit();
|
|
//是否使用佣金
|
|
if ($use_commission) {
|
|
if ($pay_amount < $user['available_commission_amount']) {
|
|
$order_data['pay_source'] = 0;//佣金支付
|
|
$order_data['status'] = 1;//佣金支付
|
|
Db::name("pay_record")->insert($order_data);
|
|
//修改商品价格和归属人
|
|
Goods::where('id', $goods['goods_id'])->update(['price' => intval($goods_price), 'owner_id' => $user_id,"updatetime" => time(),'status'=>'normal']);
|
|
|
|
//写入用户收益
|
|
$profit = bcmul($goods['goods_price'], (Config::get("site.entrusted_ratio") - Config::get("site.entrustment_ratio")) * 0.01, 2);
|
|
$profit = $profit + $coupon_price + $pay_amount;
|
|
Record::create(["user_id" => $user_id, "type" => 2, "item_id" => $order_id,"amount"=> $profit]);
|
|
//修改用户可用佣金
|
|
User::where('id', $user_id)->update(['available_commission_amount' => $user['available_commission_amount'] - $pay_amount,"profit_amount" => $user['profit_amount'] + $profit, "updatetime" => time()]);
|
|
//更新订单信息
|
|
OrderModel::where("id", $order_id)->update(['status' => 6,'updatetime' => time()]);
|
|
Detail::where("order_id", $order_id)->update(['commission_price' => $goods_price, "fee" => $fee, "dikou_price" => $pay_amount]);
|
|
//使用优惠卷
|
|
if ($coupon_id) {
|
|
UserCouponModel::where('id', $coupon_id)->update(['use_order_id'=>$order_id, "use_time"=>time()]);
|
|
}
|
|
//新增卖单
|
|
$order_sn = "ZH".date("YmdHis").rand(1000,9999);
|
|
//创建订单
|
|
$orderData = [
|
|
"order_type" => 1,
|
|
"user_id" => $user_id,
|
|
"sell_order_id" => 0,
|
|
"buyer_id" => 0,
|
|
"seller_id" => $user_id,
|
|
"order_sn" => $order_sn,
|
|
"warehouse_id" => $order['warehouse_id'],
|
|
"order_amount" => intval($goods_price),
|
|
"pay_amount" => intval($goods_price),
|
|
"actual_amount" => intval($goods_price),
|
|
"coupon_price" => 0,
|
|
"coupon_id" => 0,
|
|
"status" => 3,
|
|
];
|
|
$order1 = OrderModel::create($orderData);
|
|
|
|
$goods1 = Goods::where('id', $goods['goods_id'])->find();
|
|
$orderDetail = [
|
|
"order_id" => $order1->id,
|
|
"buyer_id" => 0,
|
|
"seller_id" => $user_id,
|
|
"goods_id" => $goods1['id'],
|
|
"goods_name" => $goods1['name'],
|
|
"goods_image" => $goods1['image'],
|
|
"goods_price" => intval($goods_price),
|
|
"num" => 1,
|
|
];
|
|
$ret = Detail::create($orderDetail);
|
|
if ($ret === false) {
|
|
$this->error("下单失败");
|
|
}
|
|
$this->success('succ', ['pay_data' => 1]);
|
|
} else {
|
|
if ($user['available_commission_amount'] > 0) {
|
|
//写佣金支付
|
|
$order_data['pay_source'] = 0;//佣金支付
|
|
$order_data['pay_amount'] = $user['available_commission_amount'];
|
|
Db::name("pay_record")->insert($order_data);
|
|
//微信支付流水号
|
|
$pay_sn = "NO".date("YmdHis").rand(1000,9999);
|
|
}
|
|
//写微信支付
|
|
$order_data['pay_source'] = 1;//微信支付
|
|
$order_data['out_trade_no'] = $pay_sn;//微信支付
|
|
$order_data['pay_amount'] = $pay_amount - $user['available_commission_amount'];
|
|
Db::name("pay_record")->insert($order_data);
|
|
//修改微信支付信息
|
|
$params['amount'] = $order_data['pay_amount'];
|
|
$params['orderid'] = $pay_sn;
|
|
|
|
Detail::where("order_id", $order_id)->update(['commission_price' => $goods_price, "fee" => $fee, "dikou_price" => $user['available_commission_amount']]);
|
|
}
|
|
} else {
|
|
//写微信支付
|
|
$order_data['pay_source'] = 1;//微信支付
|
|
$order_data['out_trade_no'] = $pay_sn;//微信支付
|
|
$order_data['pay_amount'] = $pay_amount;
|
|
Db::name("pay_record")->insert($order_data);
|
|
//修改微信支付信息
|
|
$params['amount'] = $order_data['pay_amount'];
|
|
$params['orderid'] = $pay_sn;
|
|
|
|
Detail::where("order_id", $order_id)->update(['commission_price' => $goods_price, "fee" => $fee, "dikou_price" => 0]);
|
|
}
|
|
// echo "<pre>";
|
|
// print_r($params);
|
|
// exit();
|
|
//$params['amount'] = 0.01;
|
|
try {
|
|
$result = \addons\epay\library\Service::submitOrder($params);
|
|
}catch (GatewayException $e){
|
|
$this->error($e->getMessage());
|
|
}
|
|
|
|
$this->success('', ['pay_data' => $result]);
|
|
}
|
|
/**
|
|
* 支付回调
|
|
* [callback description]
|
|
* @return function [description]
|
|
*/
|
|
public function callback(){
|
|
$pay = \addons\epay\library\Service::checkNotify("wechat");
|
|
if (!$pay) {
|
|
echo '签名错误';
|
|
return;
|
|
}
|
|
$data = $pay->verify();
|
|
try {
|
|
Log::record("支付回调数据:".$data);
|
|
$out_trade_no = explode("_",$data['out_trade_no']);
|
|
if(empty($out_trade_no[0])){
|
|
return;
|
|
}
|
|
$order_no = $out_trade_no[0];
|
|
$pay_record = Db::name("pay_record")->where('out_trade_no', $order_no)->where('status', 0)->find();
|
|
if (!$pay_record) {
|
|
echo $pay->success();
|
|
exit();
|
|
}
|
|
$goods = Detail::where('order_id', $pay_record['order_id'])->find();
|
|
if (!$goods) {
|
|
echo $pay->success();
|
|
}
|
|
$user_id = $pay_record['user_id'];
|
|
$order_id = $pay_record['order_id'];
|
|
$user = User::where('id', $user_id)->find();
|
|
//修改支付记录
|
|
Db::name("pay_record")->where('order_id', $order_id)->update(['status'=>1, "update_time"=>time()]);
|
|
//修改商品价格和归属人
|
|
$goods_price = bcmul($goods['goods_price'], (Config::get("site.entrusted_ratio") * 0.01 + 1), 2);
|
|
Goods::where('id', $goods['goods_id'])->update(['price' => intval($goods_price), 'owner_id' => $user_id,"updatetime" => time(),"status"=>'normal']);
|
|
|
|
$order_info = OrderModel::where("id", $order_id)->find();
|
|
//写入用户收益
|
|
$profit = bcmul($goods['goods_price'], (Config::get("site.entrusted_ratio") - Config::get("site.entrustment_ratio")) * 0.01, 2);
|
|
$profit = $profit + $order_info['coupon_price'] + $goods['dikou_price'];
|
|
Record::create(["user_id" => $user_id, "type" => 2, "item_id" => $order_id,"amount"=> $profit ]);
|
|
//修改用户可用佣金
|
|
User::where('id', $user_id)->update(['available_commission_amount' => $user['available_commission_amount'] - $goods['dikou_price'],"profit_amount" => $user['profit_amount'] + $profit, "updatetime" => time()]);
|
|
//更新订单状态
|
|
OrderModel::where("id", $order_id)->update(['status' => 6,'updatetime' => time()]);
|
|
//使用优惠卷
|
|
if ($order_info['coupon_id']) {
|
|
UserCouponModel::where('id', $order_info['coupon_id'])->update(['use_order_id'=>$order_id, "use_time"=>time()]);
|
|
}
|
|
//新增卖单
|
|
$order_sn = "ZH".date("YmdHis").rand(1000,9999);
|
|
//创建订单
|
|
$orderData = [
|
|
"order_type" => 1,
|
|
"user_id" => $user_id,
|
|
"sell_order_id" => 0,
|
|
"buyer_id" => 0,
|
|
"seller_id" => $user_id,
|
|
"order_sn" => $order_sn,
|
|
"warehouse_id" => $order_info['warehouse_id'],
|
|
"order_amount" => intval($goods_price),
|
|
"pay_amount" => intval($goods_price),
|
|
"actual_amount" => intval($goods_price),
|
|
"coupon_price" => 0,
|
|
"coupon_id" => 0,
|
|
"status" => 3,
|
|
];
|
|
$order = OrderModel::create($orderData);
|
|
|
|
$goods1 = Goods::where('id', $goods['goods_id'])->find();
|
|
$orderDetail = [
|
|
"order_id" => $order->id,
|
|
"buyer_id" => 0,
|
|
"seller_id" => $user_id,
|
|
"goods_id" => $goods1['id'],
|
|
"goods_name" => $goods1['name'],
|
|
"goods_image" => $goods1['image'],
|
|
"goods_price" => intval($goods_price),
|
|
"num" => 1,
|
|
];
|
|
$ret = Detail::create($orderDetail);
|
|
if ($ret === false) {
|
|
$this->error("下单失败");
|
|
}
|
|
} catch (Exception $e) {
|
|
Log::record("支付回调报错:".$e->getMessage());
|
|
}
|
|
echo $pay->success();
|
|
exit();
|
|
}
|
|
|
|
|
|
public function test(){
|
|
$obj = new OrderService();
|
|
$obj->productPayRecord(3);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|