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
98699b1f
authored
Nov 08, 2022
by
SUDPTDUBLXEROFX\Administrator
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
购物车
parent
8638708a
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
70 additions
and
5 deletions
app/Http/Controllers/Api/CartApiController.php
app/Http/Services/CartService.php
app/Models/CartModel.php
app/Http/Controllers/Api/CartApiController.php
View file @
98699b1f
...
@@ -37,9 +37,14 @@ class CartApiController extends Controller
...
@@ -37,9 +37,14 @@ class CartApiController extends Controller
'data'
,
'data'
,
]);
]);
$result
=
CartService
::
addCart
(
$data
,
$request
->
user
->
user_
id
);
$result
=
CartService
::
addCart
(
$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
(){
}
}
}
app/Http/Services/CartService.php
View file @
98699b1f
...
@@ -44,17 +44,17 @@ class CartService
...
@@ -44,17 +44,17 @@ class CartService
$temp
[
"raw_brand_name"
]
=
$digikeyArr
[
"raw_brand_name"
];
$temp
[
"raw_brand_name"
]
=
$digikeyArr
[
"raw_brand_name"
];
}
}
$checkHas
=
CartModel
::
where
([
"user_id"
=>
$user_id
,
"goods_id"
=>
$goods_id
])
->
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
[
'
goods_number'
]
+
$checkHas
[
"goods_number"
])
?
$item
[
'goods_number'
]
+
$checkHas
[
"goods
_number"
]
:
$skuInfo
[
"stock"
];
$temp
[
"goods_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
([
"
user_id"
=>
$user_id
,
"goods_id"
=>
$goods_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
[
'
goods_number'
]
?
$item
[
'goods
_number'
]
:
$skuInfo
[
"stock"
];
$temp
[
"goods_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
();
...
@@ -74,4 +74,62 @@ class CartService
...
@@ -74,4 +74,62 @@ class CartService
}
}
}
}
//刷新购物车并且返回列表
public
static
function
cartUpdateGetLists
(
$user_id
){
//当前用户所有可用的购物车数据
$query
=
CartModel
::
where
([
'user_id'
=>
$user_id
,
"status"
=>
1
])
->
orderBy
(
'cart_id'
,
'desc'
);
$result
=
$query
->
get
()
->
toArray
();
if
(
!
$result
){
return
false
;
}
try
{
$con
=
DB
::
connection
();
$con
->
beginTransaction
();
$redis
=
Redis
::
connection
();
$goodsInfoArr
=
ThirdService
::
getGoodsInfo
(
array_column
(
$result
,
"goods_id"
));
foreach
(
$result
as
$item
)
{
$goods_id
=
$item
[
'goods_id'
];
$temp
=
[
'user_id'
=>
$user_id
,
'goods_id'
=>
$item
[
'goods_id'
],
];
$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"
];
}
$temp
[
"buy_number"
]
=
$skuInfo
[
"stock"
]
>
$item
[
'buy_number'
]
?
$item
[
'buy_number'
]
:
$skuInfo
[
"stock"
];
if
(
$temp
[
"buy_number"
]
==
0
){
$temp
[
"status"
]
=
CartModel
::
STATUS_NO
;
}
$temp
[
"update_time"
]
=
time
();
$flag
=
CartModel
::
where
([
"cart_id"
=>
$item
[
"cart_id"
]])
->
update
(
$temp
);
if
(
!
$flag
){
return
false
;
}
}
$con
->
commit
();
return
true
;
}
catch
(
\Exception
$e
){
$con
->
rollback
();
throw
new
InvalidRequestException
(
$e
->
getMessage
()
.
$e
->
getLine
());
}
}
}
}
app/Models/CartModel.php
View file @
98699b1f
...
@@ -10,6 +10,8 @@ class CartModel extends Model
...
@@ -10,6 +10,8 @@ class CartModel extends Model
protected
$table
=
'cart'
;
protected
$table
=
'cart'
;
public
$timestamps
=
false
;
public
$timestamps
=
false
;
const
STATUS_NO
=
-
1
;
//状态禁用
const
STATUS_YES
=
1
;
//状态启动
//查询购物车数据
//查询购物车数据
public
static
function
getCartInfo
(
$user_id
)
public
static
function
getCartInfo
(
$user_id
)
...
...
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