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
8a10ec30
authored
Nov 09, 2022
by
SUDPTDUBLXEROFX\Administrator
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
no message
parent
e567922c
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
12 additions
and
10 deletions
app/Http/Controllers/Api/CartApiController.php
app/Http/Services/CartService.php
routes/api.php
app/Http/Controllers/Api/CartApiController.php
View file @
8a10ec30
...
@@ -21,7 +21,7 @@ class CartApiController extends Controller
...
@@ -21,7 +21,7 @@ class CartApiController extends Controller
{
{
//添加购物车, items: {"goods_id":1166788996788323407,"goods_number":2}
//添加购物车, items: {"goods_id":1166788996788323407,"goods_number":2}
public
function
add
Cart
(
Request
$request
)
public
function
save
Cart
(
Request
$request
)
{
{
$validator
=
Validator
::
make
(
$request
->
all
(),
[
$validator
=
Validator
::
make
(
$request
->
all
(),
[
'items'
=>
'required|string'
,
'items'
=>
'required|string'
,
...
@@ -37,13 +37,13 @@ class CartApiController extends Controller
...
@@ -37,13 +37,13 @@ class CartApiController extends Controller
'data'
,
'data'
,
]);
]);
$result
=
CartService
::
add
Cart
(
$data
,
$request
->
user
->
id
);
$result
=
CartService
::
save
Cart
(
$data
,
$request
->
user
->
id
);
return
!
$result
?
$this
->
setError
(
'Add cart failed , please contact administrator'
)
:
$this
->
setSuccess
(
'Add inquiry success'
);
return
!
$result
?
$this
->
setError
(
'Add cart failed , please contact administrator'
)
:
$this
->
setSuccess
(
'Add inquiry success'
);
}
}
//购物车列表
//购物车列表
public
function
cartLists
(){
public
function
cartLists
(
Request
$request
){
$result
=
CartService
::
cartLists
(
$request
->
user
->
id
);
}
}
...
...
app/Http/Services/CartService.php
View file @
8a10ec30
...
@@ -14,8 +14,8 @@ use App\Models\CartModel;
...
@@ -14,8 +14,8 @@ use App\Models\CartModel;
//购物车服务器层
//购物车服务器层
class
CartService
class
CartService
{
{
//添加购物车
//添加
或者更新
购物车
public
static
function
add
Cart
(
$data
,
$user_id
)
public
static
function
save
Cart
(
$data
,
$user_id
)
{
{
try
{
try
{
...
@@ -47,14 +47,14 @@ class CartService
...
@@ -47,14 +47,14 @@ class CartService
$checkHas
=
CartModel
::
where
([
"user_id"
=>
$user_id
,
"goods_id"
=>
$goods_id
,
"status"
=>
1
])
->
first
();
$checkHas
=
CartModel
::
where
([
"user_id"
=>
$user_id
,
"goods_id"
=>
$goods_id
,
"status"
=>
1
])
->
first
();
if
(
$checkHas
){
//存在累计库存
if
(
$checkHas
){
//存在累计库存
$temp
[
"
goods
_number"
]
=
$skuInfo
[
"stock"
]
>
(
$item
[
'buy_number'
]
+
$checkHas
[
"buy_number"
])
?
$item
[
'buy_number'
]
+
$checkHas
[
"buy_number"
]
:
$skuInfo
[
"stock"
];
$temp
[
"
buy
_number"
]
=
$skuInfo
[
"stock"
]
>
(
$item
[
'buy_number'
]
+
$checkHas
[
"buy_number"
])
?
$item
[
'buy_number'
]
+
$checkHas
[
"buy_number"
]
:
$skuInfo
[
"stock"
];
$temp
[
"update_time"
]
=
time
();
$temp
[
"update_time"
]
=
time
();
$flag
=
CartModel
::
where
([
"cart_id"
=>
$checkHas
[
"cart_id"
]])
->
update
(
$temp
);
$flag
=
CartModel
::
where
([
"cart_id"
=>
$checkHas
[
"cart_id"
]])
->
update
(
$temp
);
if
(
!
$flag
){
if
(
!
$flag
){
return
false
;
return
false
;
}
}
}
else
{
//不存在插入购物车
}
else
{
//不存在插入购物车
$temp
[
"
goods
_number"
]
=
$skuInfo
[
"stock"
]
>
$item
[
'buy_number'
]
?
$item
[
'buy_number'
]
:
$skuInfo
[
"stock"
];
$temp
[
"
buy
_number"
]
=
$skuInfo
[
"stock"
]
>
$item
[
'buy_number'
]
?
$item
[
'buy_number'
]
:
$skuInfo
[
"stock"
];
$temp
[
"create_time"
]
=
time
();
$temp
[
"create_time"
]
=
time
();
$temp
[
"update_time"
]
=
time
();
$temp
[
"update_time"
]
=
time
();
...
@@ -75,7 +75,7 @@ class CartService
...
@@ -75,7 +75,7 @@ class CartService
}
}
//刷新购物车并且返回列表
//刷新购物车并且返回列表
public
static
function
cart
UpdateGet
Lists
(
$user_id
){
public
static
function
cartLists
(
$user_id
){
//当前用户所有可用的购物车数据
//当前用户所有可用的购物车数据
$query
=
CartModel
::
where
([
'user_id'
=>
$user_id
,
"status"
=>
1
])
$query
=
CartModel
::
where
([
'user_id'
=>
$user_id
,
"status"
=>
1
])
...
@@ -92,6 +92,7 @@ class CartService
...
@@ -92,6 +92,7 @@ class CartService
$redis
=
Redis
::
connection
();
$redis
=
Redis
::
connection
();
$goodsInfoArr
=
ThirdService
::
getGoodsInfo
(
array_column
(
$result
,
"goods_id"
));
$goodsInfoArr
=
ThirdService
::
getGoodsInfo
(
array_column
(
$result
,
"goods_id"
));
$cartList
=
[];
foreach
(
$result
as
$item
)
{
foreach
(
$result
as
$item
)
{
$goods_id
=
$item
[
'goods_id'
];
$goods_id
=
$item
[
'goods_id'
];
...
...
routes/api.php
View file @
8a10ec30
...
@@ -43,7 +43,8 @@ Route::middleware(['api', 'api.check'])->namespace('Api')->group(function () {
...
@@ -43,7 +43,8 @@ Route::middleware(['api', 'api.check'])->namespace('Api')->group(function () {
Route
::
get
(
'country/list'
,
'CountryApiController@list'
);
Route
::
get
(
'country/list'
,
'CountryApiController@list'
);
Route
::
POST
(
'cart/addCart'
,
'CartApiController@addCart'
);
//添加购物车
Route
::
POST
(
'cart/saveCart'
,
'CartApiController@saveCart'
);
//添加或者更新购物车
Route
::
POST
(
'cart/cartLists'
,
'CartApiController@cartLists'
);
//购物车列表
});
});
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