修复内容
This commit is contained in:
@@ -91,8 +91,11 @@ class Order extends ApiBase
|
||||
|
||||
$order = OrderLogic::add($this->user_id, $info['data'], $post);
|
||||
|
||||
if ($order['code'] == 0) {
|
||||
$this->_error($order['msg']);
|
||||
}
|
||||
|
||||
return $order;
|
||||
$this->_success('', $order['data']);
|
||||
}
|
||||
|
||||
|
||||
@@ -572,6 +575,7 @@ class Order extends ApiBase
|
||||
|
||||
public function putday()
|
||||
{
|
||||
$this->_error('该时间内无法自主预约,请联系客服');
|
||||
$post = $this->request->post();
|
||||
$hourLists = $post['data']['hourLists'] ?? [];
|
||||
$orderid = $post['data']['id'] ?? 0;
|
||||
|
||||
@@ -22,7 +22,8 @@ class Points extends ApiBase
|
||||
* @return mixed
|
||||
*/
|
||||
public function goodslist(){
|
||||
$lists = PointsLogic::goodslist();
|
||||
$clsssId = $this->request->get('classify_id');
|
||||
$lists = PointsLogic::goodslist($clsssId);
|
||||
$this->_success('获取数据成功',$lists);
|
||||
}
|
||||
/**
|
||||
@@ -38,16 +39,42 @@ class Points extends ApiBase
|
||||
}
|
||||
|
||||
public function goodsSub(){
|
||||
$data = $this->request->get();
|
||||
$data['user_id']= $this->user_id;
|
||||
$user_jf=UserLogic::getUserInfo($this->user_id);
|
||||
if($user_jf['user_integral']<$data['price']){
|
||||
$data = $this->request->post();
|
||||
|
||||
// 参数校验
|
||||
if(empty($data) || !is_array($data)){
|
||||
$this->_error('参数错误');
|
||||
}
|
||||
if(empty($data['id']) || empty($data['name']) || !isset($data['price'])){
|
||||
$this->_error('参数不完整');
|
||||
}
|
||||
|
||||
$data['user_id'] = $this->user_id;
|
||||
$user_jf = UserLogic::getUserInfo($this->user_id);
|
||||
|
||||
if($user_jf['user_integral'] < $data['price']){
|
||||
$this->_error('积分不足');
|
||||
}else{
|
||||
$data = PointsLogic::goodsSub($data);
|
||||
$this->_success('兑换成功',$data);
|
||||
$result = PointsLogic::goodsSub($data);
|
||||
if($result['code'] == 1){
|
||||
$this->_success('兑换成功');
|
||||
}else{
|
||||
$this->_error($result['msg']);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
//积分订单列表
|
||||
public function orderList()
|
||||
{
|
||||
$data = $this->request->post();
|
||||
// 参数校验
|
||||
if(empty($data) || !is_array($data)){
|
||||
$this->_error('参数错误');
|
||||
}
|
||||
$userId = $this->user_id;
|
||||
$state = isset($data['state']) ? $data['state'] : null;
|
||||
$result = PointsLogic::orderList($userId, $state);
|
||||
$this->_success('获取数据成功',$result);
|
||||
}
|
||||
}
|
||||
@@ -26,7 +26,7 @@ class Share extends ApiBase
|
||||
public function shareGoods()
|
||||
{
|
||||
$id = $this->request->get('id');
|
||||
$url = $this->request->get('url');
|
||||
$url = $this->request->get('page');
|
||||
$client = $this->client;
|
||||
if ($id && $url) {
|
||||
$result = ShareLogic::shareGoods($this->user_id, $id, $url, $client);
|
||||
|
||||
@@ -38,7 +38,7 @@ class GoodsLogic{
|
||||
$goods = new Goods();
|
||||
//品牌筛选
|
||||
if(isset($get['brand_id']) && $get['brand_id']) {
|
||||
$where[] = ['brand_id', '=', $get['brand_id']];
|
||||
$where[] = ['third_category_id', '=', $get['brand_id']];
|
||||
}
|
||||
//分类筛选
|
||||
if(isset($get['category_id']) && $get['category_id']){
|
||||
|
||||
@@ -491,8 +491,8 @@ class OrderLogic extends LogicBase
|
||||
// 拆分后每个订单的金额需要重新计算,这里先验证原始总金额
|
||||
// 实际拆分时每个订单会独立计算
|
||||
} else {
|
||||
if($user_money < $data['order_amount']){
|
||||
throw new Exception('账户余额不足');
|
||||
if($user_money < $data['order_amount']){
|
||||
throw new Exception('账户余额不足');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -556,10 +556,13 @@ class OrderLogic extends LogicBase
|
||||
|
||||
// 创建单个订单
|
||||
$single_goods_lists = $single_order_data['goods_lists'];
|
||||
if (empty($single_goods_lists)) {
|
||||
throw new Exception('商品信息获取失败');
|
||||
}
|
||||
$single_goods_id = $single_goods_lists[0]['goods_id'];
|
||||
|
||||
|
||||
$order = self::addOrder($user_id, $single_order_data, $order_source, $user_address, $single_goods_id);
|
||||
$order_id = $order['order_id'];
|
||||
$order_id = $order['order_id'];
|
||||
$order_ids[] = $order_id;
|
||||
|
||||
self::addOrderGoods($order_id, $single_goods_lists);
|
||||
@@ -590,6 +593,9 @@ class OrderLogic extends LogicBase
|
||||
}
|
||||
|
||||
$single_goods_lists = $single_order_data['goods_lists'];
|
||||
if (empty($single_goods_lists)) {
|
||||
throw new Exception('商品信息获取失败');
|
||||
}
|
||||
$single_goods_id = $single_goods_lists[0]['goods_id'];
|
||||
|
||||
$order = self::addOrder($user_id, $single_order_data, $order_source, $user_address, $single_goods_id);
|
||||
@@ -607,30 +613,36 @@ class OrderLogic extends LogicBase
|
||||
}
|
||||
} else {
|
||||
// 普通商品:保持原有逻辑
|
||||
if (empty($goods_lists)) {
|
||||
throw new Exception('商品信息获取失败');
|
||||
}
|
||||
$goods_id = $goods_lists[0]['goods_id'];
|
||||
|
||||
$order = self::addOrder($user_id, $data, $order_source, $user_address, $goods_id);
|
||||
$order_id = $order['order_id'];
|
||||
$order_ids[] = $order_id;
|
||||
|
||||
self::addOrderGoods($order_id, $goods_lists);
|
||||
self::addOrderAfter($order_id, $user_id, $type, $data);
|
||||
|
||||
//支付方式为余额支付,扣除余额,更新订单状态,支付状态
|
||||
if ($data['pay_way'] == Pay::BALANCE_PAY || $data['order_amount'] == 0) {
|
||||
PayNotifyLogic::handle('order', $order['order_sn'], []);
|
||||
}
|
||||
|
||||
self::addOrderGoods($order_id, $goods_lists);
|
||||
self::addOrderAfter($order_id, $user_id, $type, $data);
|
||||
|
||||
//支付方式为余额支付,扣除余额,更新订单状态,支付状态
|
||||
if ($data['pay_way'] == Pay::BALANCE_PAY || $data['order_amount'] == 0) {
|
||||
PayNotifyLogic::handle('order', $order['order_sn'], []);
|
||||
}
|
||||
|
||||
// 砍价订单处理
|
||||
if (isset($post['bargain_launch_id']) and $post['bargain_launch_id'] > 0) {
|
||||
$bargainLaunchModel = new BargainLaunch();
|
||||
$bargainLaunchModel->where(['id'=>(int)$post['bargain_launch_id']])
|
||||
->update(['order_id'=>$order_id, 'status'=>1]);
|
||||
// 砍价订单处理
|
||||
if (isset($post['bargain_launch_id']) and $post['bargain_launch_id'] > 0) {
|
||||
$bargainLaunchModel = new BargainLaunch();
|
||||
$bargainLaunchModel->where(['id'=>(int)$post['bargain_launch_id']])
|
||||
->update(['order_id'=>$order_id, 'status'=>1]);
|
||||
}
|
||||
}
|
||||
|
||||
Db::commit();
|
||||
return self::dataSuccess('', ['order_id' => $order_ids[0] ?? 0, 'order_ids' => $order_ids, 'type' => 'order']);
|
||||
if (empty($order_ids)) {
|
||||
throw new Exception('订单创建失败');
|
||||
}
|
||||
return self::dataSuccess('', ['order_id' => $order_ids[0], 'order_ids' => $order_ids, 'type' => 'order']);
|
||||
|
||||
} catch (Exception $e) {
|
||||
Db::rollback();
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
<?php
|
||||
namespace app\api\logic;
|
||||
use app\api\model\OrderPoints;
|
||||
use think\Db;
|
||||
use think\Exception;
|
||||
use app\common\server\UrlServer;
|
||||
@@ -9,13 +10,16 @@ class PointsLogic
|
||||
{
|
||||
//获取积分的分类
|
||||
public static function classify(){
|
||||
return Db::name('printer_type')->select();
|
||||
return Db::name('printer_type')->where('del',0)->select();
|
||||
}
|
||||
|
||||
|
||||
//获取到积分的商品列表
|
||||
public static function goodslist(){
|
||||
$where=[];
|
||||
public static function goodslist($clsssId){
|
||||
$where=[
|
||||
'brand_id'=>$clsssId,
|
||||
'del'=>0
|
||||
];
|
||||
$goods = Db::name('printer_goods')
|
||||
->where($where)
|
||||
->select();
|
||||
@@ -34,21 +38,83 @@ class PointsLogic
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 积分商品下单(扣减积分并生成订单)
|
||||
* @param array $data ['id','name','price','user_id','number'(可选)]
|
||||
* @return array
|
||||
*/
|
||||
public static function goodsSub($data){
|
||||
|
||||
$data=[
|
||||
'name' =>$data['name'],
|
||||
'goods_id' =>$data['id'],
|
||||
'price' =>$data['price'],
|
||||
'number' =>1,
|
||||
'user_id' =>$data['user_id'],
|
||||
'create_time'=>time()
|
||||
];
|
||||
$insert=Db::name('printer_order')->data($data)->insert();
|
||||
if($insert){
|
||||
$user=UserLogic::getUserInfo($data['user_id']);
|
||||
db::name('user')->where('id',$data['user_id'])->update(['user_integral'=>$user['user_integral']-$data['price']]);
|
||||
// Db::name('printer_goods')->where('')
|
||||
}
|
||||
if(empty($data) || !is_array($data)){
|
||||
return ['code'=>0,'msg'=>'参数错误'];
|
||||
}
|
||||
// 基础校验
|
||||
if(empty($data['id']) || empty($data['user_id']) || !isset($data['price'])){
|
||||
return ['code'=>0,'msg'=>'参数不完整'];
|
||||
}
|
||||
$number = isset($data['number']) && intval($data['number'])>0 ? intval($data['number']) : 1;
|
||||
$price = floatval($data['price']);
|
||||
$needIntegral = $price * $number;
|
||||
|
||||
Db::startTrans();
|
||||
try{
|
||||
// 加锁读取用户积分
|
||||
$user = Db::name('user')->where('id',$data['user_id'])->lock(true)->find();
|
||||
if(!$user){
|
||||
throw new Exception('用户不存在');
|
||||
}
|
||||
if($user['user_integral'] < $needIntegral){
|
||||
throw new Exception('积分不足');
|
||||
}
|
||||
|
||||
$orderData = [
|
||||
'name' => $data['name'] ?? '',
|
||||
'goods_id' => $data['id'],
|
||||
'price' => $price,
|
||||
'number' => $number,
|
||||
'user_id' => $data['user_id'],
|
||||
'create_time' => time(),
|
||||
'address_id' => $data['addressId'],
|
||||
'state' => 1
|
||||
];
|
||||
|
||||
$insert = Db::name('printer_order')->insert($orderData);
|
||||
if(!$insert){
|
||||
throw new Exception('下单失败');
|
||||
}
|
||||
|
||||
// 扣减积分
|
||||
Db::name('user')
|
||||
->where('id',$data['user_id'])
|
||||
->setDec('user_integral', $needIntegral);
|
||||
|
||||
Db::commit();
|
||||
return ['code'=>1,'msg'=>'兑换成功'];
|
||||
}catch (\Exception $e){
|
||||
Db::rollback();
|
||||
return ['code'=>0,'msg'=>$e->getMessage()];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//获取积分订单
|
||||
public static function orderList($userId, $state = null)
|
||||
{
|
||||
$order = new OrderPoints();
|
||||
$query = $order->with([
|
||||
'address' => function($query){
|
||||
$query->where('del',0)->field('id,address');
|
||||
},
|
||||
'pointsGoods' => function($query){
|
||||
$query->where('del',0)->field('id,images');
|
||||
}
|
||||
])->where('user_id', $userId);
|
||||
|
||||
// 如果传了state参数,才添加state条件,否则查询全部
|
||||
if($state !== null && $state !== ''){
|
||||
$query = $query->where('state', $state);
|
||||
}
|
||||
|
||||
$orderArray = $query->paginate(10);
|
||||
return $orderArray;
|
||||
}
|
||||
}
|
||||
@@ -77,9 +77,22 @@ class Goods extends Model{
|
||||
$this->setAttr('price',$goods_item[0]['price']);
|
||||
$this->setAttr('market_price',$goods_item[0]['market_price']);
|
||||
|
||||
//创建规格值ID到商品项ID的映射
|
||||
$spec_value_to_item = [];
|
||||
foreach ($goods_item as $item) {
|
||||
$spec_value_ids = $item['spec_value_ids'];
|
||||
// spec_value_ids 存储的是规格值ID,可能是一个或多个(逗号分隔)
|
||||
$spec_value_id_array = explode(',', $spec_value_ids);
|
||||
foreach ($spec_value_id_array as $spec_value_id) {
|
||||
$spec_value_to_item[$spec_value_id] = $item['id'];
|
||||
}
|
||||
}
|
||||
|
||||
//拼接规格
|
||||
foreach ($spec_value as $item){
|
||||
if(isset($spec[$item['spec_id']])){
|
||||
//添加对应的商品项ID
|
||||
$item['item_id'] = $spec_value_to_item[$item['id']] ?? 0;
|
||||
$spec[$item['spec_id']]['spec_value'][]= $item;
|
||||
}
|
||||
}
|
||||
|
||||
10
application/api/model/GoodsPoints.php
Normal file
10
application/api/model/GoodsPoints.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
class GoodsPoints extends Model
|
||||
{
|
||||
protected $name = 'printer_goods';
|
||||
}
|
||||
24
application/api/model/OrderPoints.php
Normal file
24
application/api/model/OrderPoints.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\model;
|
||||
|
||||
use think\Model;
|
||||
use think\model\relation\HasOne;
|
||||
|
||||
class OrderPoints extends Model
|
||||
{
|
||||
protected $name = 'printer_order';
|
||||
|
||||
|
||||
//关联地址
|
||||
public function address(): HasOne
|
||||
{
|
||||
return $this->hasOne(UserAddress::class, 'id', 'address_id');
|
||||
}
|
||||
|
||||
//关联积分商品
|
||||
public function pointsGoods(): HasOne
|
||||
{
|
||||
return $this->hasOne(GoodsPoints::class, 'id', 'goods_id');
|
||||
}
|
||||
}
|
||||
10
application/api/model/UserAddress.php
Normal file
10
application/api/model/UserAddress.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
class UserAddress extends Model
|
||||
{
|
||||
protected $name = 'user_address';
|
||||
}
|
||||
Reference in New Issue
Block a user