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
56298307
authored
Nov 10, 2022
by
杨树贤
Browse files
Options
_('Browse Files')
Download
Plain Diff
修复冲突
parents
1d880627
1b9e07f6
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
51 additions
and
10 deletions
app/Http/Controllers/Api/CartApiController.php
app/Http/Services/CartService.php
app/helpers.php
routes/api.php
app/Http/Controllers/Api/CartApiController.php
View file @
56298307
...
...
@@ -21,7 +21,7 @@ class CartApiController extends Controller
{
//添加购物车, items: {"goods_id":1166788996788323407,"goods_number":2}
public
function
add
Cart
(
Request
$request
)
public
function
save
Cart
(
Request
$request
)
{
$validator
=
Validator
::
make
(
$request
->
all
(),
[
'items'
=>
'required|string'
,
...
...
@@ -37,13 +37,13 @@ class CartApiController extends Controller
'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'
);
}
//购物车列表
public
function
cartLists
(){
public
function
cartLists
(
Request
$request
){
$result
=
CartService
::
cartLists
(
$request
->
user
->
id
);
}
...
...
app/Http/Services/CartService.php
View file @
56298307
...
...
@@ -14,8 +14,8 @@ use App\Models\CartModel;
//购物车服务器层
class
CartService
{
//添加购物车
public
static
function
add
Cart
(
$data
,
$user_id
)
//添加
或者更新
购物车
public
static
function
save
Cart
(
$data
,
$user_id
)
{
try
{
...
...
@@ -47,14 +47,14 @@ class CartService
$checkHas
=
CartModel
::
where
([
"user_id"
=>
$user_id
,
"goods_id"
=>
$goods_id
,
"status"
=>
1
])
->
first
();
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
();
$flag
=
CartModel
::
where
([
"cart_id"
=>
$checkHas
[
"cart_id"
]])
->
update
(
$temp
);
if
(
!
$flag
){
return
false
;
}
}
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
[
"update_time"
]
=
time
();
...
...
@@ -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
])
...
...
@@ -92,6 +92,7 @@ class CartService
$redis
=
Redis
::
connection
();
$goodsInfoArr
=
ThirdService
::
getGoodsInfo
(
array_column
(
$result
,
"goods_id"
));
$cartList
=
[];
foreach
(
$result
as
$item
)
{
$goods_id
=
$item
[
'goods_id'
];
...
...
app/helpers.php
View file @
56298307
...
...
@@ -22,3 +22,42 @@ if (!function_exists('generate_letters')) {
return
$letters
;
}
}
/*
* 反爬虫用html标签替换数字,不包括“.”
* $number 数字串
*/
function
numberToHtml
(
$number
){
$arr
=
[
[
'asfgdqwer'
,
'asfgdtyhg'
,
'asfgdpolk'
,
'asfgdpoqw'
],
[
'asfgdrfdf'
,
'asfgderfd'
,
'asfgdwdsa'
,
'asfgdpoer'
],
[
'asfgdasde'
,
'asfgdqwsz'
,
'asfgdrtgd'
,
'asfgdpovv'
],
[
'asfgdwsxc'
,
'asfgdwsxz'
,
'asfgdrfvb'
,
'asfgdpoee'
],
[
'asfgdqazs'
,
'asfgdqasd'
,
'asfgdqwag'
,
'asfgdpogh'
],
[
'asfgdrtyh'
,
'asfgdyutr'
,
'asfgdeews'
,
'asfgdpotg'
],
[
'asfgdpluj'
,
'asfgdikjf'
,
'asfgdesgj'
,
'asfgdpfff'
],
[
'asfgdtrdb'
,
'asfgdiksf'
,
'asfgdsgkp'
,
'asfgdprty'
],
[
'asfgdpehl'
,
'asfgdstgb'
,
'asfgderll'
,
'asfgdpokf'
],
[
'asfgdpehg'
,
'asfgdstgf'
,
'asfgderlf'
,
'asfgdpogk'
]
];
if
(
empty
(
$arr
)){
return
$number
;
}
$len
=
strlen
(
$number
);
if
(
$len
>
0
){
$str
=
''
;
for
(
$i
=
0
;
$i
<
$len
;
$i
++
){
$num
=
substr
(
$number
,
$i
,
1
);
if
(
preg_match
(
'/\d/'
,
$num
)){
$index
=
rand
(
0
,
3
);
$class
=
$arr
[
$num
][
$index
];
$other_class
=
strRandom
(
$class
);
$num
=
'<font class="'
.
$class
.
$other_class
.
'"></font>'
;
}
$str
.=
$num
;
}
return
$str
;
}
else
{
return
$number
;
}
}
routes/api.php
View file @
56298307
...
...
@@ -43,7 +43,8 @@ Route::middleware(['api', 'api.check'])->namespace('Api')->group(function () {
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