购物车

parent 0b40e1c4
......@@ -46,3 +46,5 @@ MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
PUBLIC_URL=http://www.semour.com
#商品服务网址
GOODS_INFO_URL=http://192.168.1.237:60014
......@@ -3,7 +3,10 @@
namespace App\Http\Controllers\Api;
use App\Http\Requests\UserRegister;
use App\Http\Services\CartService;
use App\Http\Services\InquiryService;
use App\Models\User;
use Facade\Ignition\Support\Packagist\Package;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Http\Request;
......@@ -17,25 +20,25 @@ use Illuminate\Support\Facades\Validator;
class CartApiController extends Controller
{
use ThrottlesLogins, RegistersUsers;
//添加购物车, items: {"goods_id":1166788996788323407,"goods_number":2}
public function addCart(Request $request)
{
$validator = Validator::make($request->all(), [
'email' => 'required|string|email|max:255',
'password' => 'required|string|min:8',
'items' => 'required|string',
], [
'password.min' => 'Password must be at least 8 characters long.'
'items.min' => 'items must be at least 1 characters long.'
]);
if ($validator->fails()) {
return $this->setError($validator->errors()->first());
}
$data = $request->only([
'items',
'data',
]);
$this->incrementLoginAttempts($request);
return $this->setError('Login failure');
$result = CartService::addCart($data, $request->user->user_id);
return !$result ? $this->setError('Add cart failed , please contact administrator'):$this->setSuccess('Add inquiry success');
}
......
......@@ -2,31 +2,76 @@
namespace App\Http\Services;
use App\Exceptions\InvalidRequestException;
use App\Models\Inquiry;
use App\Models\InquiryItems;
use App\Models\InquiryItemsModel;
use App\Models\InquiryModel;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Models\CartModel;
//购物车服务器层
class CartService
{
//添加购物车
public static function addCart($data, $user)
public static function addCart($data, $user_id)
{
return DB::transaction(function () use ($data, $user) {
$items = \Arr::get($data, 'item', []);
$inquiryItems = [];
try{
$con = DB::connection();
$con->beginTransaction();
$items = json_decode(\Arr::get($data, 'items', []),true);
$redis = Redis::connection();
$goodsInfoArr = ThirdService::getGoodsInfo(array_column($items,"goods_id"));
foreach ($items as $item) {
$inquiryItems[] = [
'user_id' => $user->user_id,
$goods_id = $item['goods_id'];
$temp = [
'user_id' => $user_id,
'goods_id' => $item['goods_id'],
'goods_number' => $item['goods_number'],
'create_goods_price' => $item['create_goods_price'],
'create_time' => time(),
'update_time' => time(),
];
$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"];
}
$checkHas = CartModel::where(["user_id"=>$user_id,"goods_id"=>$goods_id])->first();
if ($checkHas){ //存在累计库存
$temp["goods_number"] = $skuInfo["stock"] > ($item['goods_number']+$checkHas["goods_number"]) ? $item['goods_number']+$checkHas["goods_number"] : $skuInfo["stock"];
$temp["update_time"] = time();
$flag = CartModel::where(["user_id"=>$user_id,"goods_id"=>$goods_id])->update($temp);
if (!$flag){
return false;
}
}else{ //不存在插入购物车
$temp["goods_number"] = $skuInfo["stock"] > $item['goods_number'] ? $item['goods_number'] : $skuInfo["stock"];
$temp["create_time"] = time();
$temp["update_time"] = time();
$flag = CartModel::insertGetId($temp);
if (!$flag){
return false;
}
}
}
$con->commit();
return true;
}catch (\Exception $e){
$con->rollback();
throw new InvalidRequestException($e->getMessage().$e->getLine());
}
return InquiryItems::addInquiryItems($inquiryItems);
});
}
}
<?php
namespace App\Http\Services;
use App\Exceptions\InvalidRequestException;
use App\Http\Models\Order\OrderModel;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use SoapClient;
use App\Http\Services\SaleOrderDetailsService;
//调用第三方api接口服务层
class ThirdService
{
//获取商品详情
public static function getGoodsInfo($goods_ids){
$url = env('GOODS_INFO_URL', '') . '/synchronization';
$req_params['goods_id'] = implode(',', $goods_ids);
$response = Http::asForm()->post($url, $req_params);
if (request()->input("debug") == 1){
print_r($url);
print_r("<br/>");
print_r($req_params);
print_r("<br/>");
print_r($response->body());
print_r("<br/>");
die();
}
$res = json_decode($response->body(), true);
if (!$res || $res['errcode'] != 0) {
return [];
}
return $res['data'];
}
}
<?php
namespace App\Http\Models\Order;
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class CartModel extends Model
{
protected $primaryKey = 'cart_id';
protected $table = 'cart';
public $timestamps = false;
......@@ -21,5 +22,4 @@ class CartModel extends Model
}
}
......@@ -43,5 +43,7 @@ Route::middleware(['api', 'api.check'])->namespace('Api')->group(function () {
Route::get('country/list', 'CountryApiController@list');
Route::POST('cart/addCart', 'CartApiController@addCart'); //添加购物车
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment