Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
semour
/
semour_web
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
5004580e
authored
Nov 21, 2022
by
SUDPTDUBLXEROFX\Administrator
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
购物车
parent
309264ef
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
107 additions
and
4 deletions
app/Http/Controllers/Api/CartApiController.php
app/Http/Services/CartService.php
app/Http/Controllers/Api/CartApiController.php
View file @
5004580e
...
...
@@ -2,6 +2,7 @@
namespace
App\Http\Controllers\Api
;
use
App\Http\ApiHelper\ApiCode
;
use
App\Http\Requests\UserRegister
;
use
App\Http\Services\CartService
;
use
App\Http\Services\InquiryService
;
...
...
@@ -19,11 +20,27 @@ use Illuminate\Support\Facades\Validator;
//购物车控制器
class
CartApiController
extends
Controller
{
//添加购物车, items: {"goods_id":1166788996788323407,"goods_number":2}
//单条添加或者更新购物车
public
function
saveCart
(
Request
$request
)
{
$validator
=
Validator
::
make
(
$request
->
all
(),
[
'goods_id'
=>
'required|string'
,
'buy_number'
=>
'required|string'
,
'cart_id'
=>
'string'
,
]);
if
(
$validator
->
fails
())
{
return
$this
->
setError
(
$validator
->
errors
()
->
first
());
}
$result
=
CartService
::
saveCart
(
$request
->
user
->
id
,
$request
->
user
->
gid
,
$request
->
input
(
"goods_id"
,
0
),
$request
->
input
(
"buy_number"
,
0
));
return
!
$result
?
$this
->
setError
(
'Add cart failed , please contact administrator'
)
:
$this
->
setSuccess
(
'success'
,
ApiCode
::
API_CODE_SUCCESS
,
$result
);
}
//批量添加购物车, items: {"goods_id":1166788996788323407,"goods_number":2}
public
function
saveCartBatch
(
Request
$request
)
{
$validator
=
Validator
::
make
(
$request
->
all
(),
[
'items'
=>
'required|string'
,
],
[
'items.min'
=>
'items must be at least 1 characters long.'
...
...
app/Http/Services/CartService.php
View file @
5004580e
...
...
@@ -17,8 +17,93 @@ class CartService
static
$day_filter
=
[
"工作日"
,
"个工作日"
,
"日"
];
static
$week_filter
=
[
"周"
];
//添加或者更新购物车
public
static
function
saveCart
(
$data
,
$user_id
=
""
,
$gid
=
""
)
//添加或者更新购物车(单条)
public
static
function
saveCart
(
$user_id
=
""
,
$gid
=
""
,
$goods_id
=
0
,
$buy_number
=
0
)
{
try
{
$con
=
DB
::
connection
();
$con
->
beginTransaction
();
$redis
=
Redis
::
connection
();
$goodsInfoArr
=
ThirdService
::
getGoodsInfo
([
$goods_id
]);
$temp
=
[
'goods_id'
=>
$goods_id
,
'update_time'
=>
time
(),
];
if
(
$user_id
){
$temp
[
"user_id"
]
=
$user_id
;
}
else
{
$temp
[
"gid"
]
=
$gid
;
}
$skuInfo
=
$goodsInfoArr
[
$goods_id
];
//sku库存
$digikeyInfo
=
$redis
->
hget
(
"sku_raw_map"
,
$goods_id
);
//digikey 编码
if
(
$digikeyInfo
){
$digikeyArr
=
json_decode
(
$digikeyInfo
,
true
);
$temp
[
"raw_goods_sn"
]
=
$digikeyArr
[
"raw_goods_id"
];
$temp
[
"raw_goods_packing"
]
=
$digikeyArr
[
"pack"
];
$temp
[
"raw_brand_name"
]
=
$digikeyArr
[
"raw_brand_name"
];
}
//购买时价格
$buy_price
=
0
;
$ladder_price
=
data_get
(
$skuInfo
,
"ladder_price"
);
if
(
$ladder_price
){
//购买价格
foreach
(
$ladder_price
as
$a
=>
$b
){
if
(
$buy_number
>=
$b
[
"purchases"
]){
$buy_price
=
$b
[
"price_us"
];
}
else
{
break
;
}
}
}
$temp
[
"buy_price"
]
=
$buy_price
;
$checkHas
=
CartModel
::
where
([
"user_id"
=>
$user_id
,
"goods_id"
=>
$goods_id
,
"status"
=>
1
])
->
first
();
$temp
[
"buy_number"
]
=
$skuInfo
[
"stock"
]
>
$buy_number
?
$buy_number
:
$skuInfo
[
"stock"
];
if
(
$checkHas
){
$cart_id
=
$checkHas
[
"cart_id"
];
$temp
[
"update_time"
]
=
time
();
$temp
[
"status"
]
=
$buy_number
>
0
?
CartModel
::
status_yes
:
CartModel
::
status_no
;
$flag
=
CartModel
::
where
([
"cart_id"
=>
$checkHas
[
"cart_id"
]])
->
update
(
$temp
);
if
(
!
$flag
){
return
false
;
}
}
else
{
//不存在插入购物车
$temp
[
"status"
]
=
$buy_number
>
0
?
CartModel
::
status_yes
:
CartModel
::
status_no
;
$temp
[
"create_time"
]
=
time
();
$temp
[
"update_time"
]
=
time
();
$cart_id
=
CartModel
::
insertGetId
(
$temp
);
if
(
!
$cart_id
){
return
false
;
}
}
#返回更新详情
$con
->
commit
();
$te
=
[
"cart_id"
=>
$cart_id
,
"goods_id"
=>
$goods_id
,
"buy_price"
=>
$buy_price
,
"buy_number"
=>
$buy_number
,
"ext_price"
=>
round
(
$buy_price
*
$buy_number
,
2
),
"stock"
=>
$skuInfo
[
"stock"
],
];
return
$te
;
}
catch
(
\Exception
$e
){
$con
->
rollback
();
throw
new
InvalidRequestException
(
$e
->
getMessage
()
.
$e
->
getLine
());
}
}
//添加或者更新购物车(批量)
public
static
function
saveCartBatch
(
$data
,
$user_id
=
""
,
$gid
=
""
)
{
try
{
...
...
@@ -180,6 +265,7 @@ class CartService
//格式化数据
$skuInfo
=
self
::
dullDataInfo
(
$skuInfo
);
$cartList
[]
=
[
"cart_id"
=>
$v
[
"cart_id"
],
//cart_id
"goods_id"
=>
$skuInfo
[
"goods_id"
],
//sku_id
"goods_name"
=>
$skuInfo
[
"goods_name"
],
//型号名称
"status_cn"
=>
\Arr
::
get
(
CartModel
::
$status
,
$v
[
"status"
]),
//型号名称
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment