Commit 334e1170 by 肖康

Merge branch 'dev/ver/1.0.0' of http://git.ichunt.net/semour/semour_web into dev/ver/1.0.0

parents 1a88b631 98699b1f
......@@ -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
......@@ -2,9 +2,10 @@
/public/hot
/public/storage
/storage/*.key
/.idea/
/.idea
.idea
/vendor/_laravel_idea
/vendor/_laravel_idea/
#/vendor
#.env
.env.backup
......
......@@ -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,30 @@ 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',
]);
$result = CartService::addCart($data, $request->user->id);
return !$result ? $this->setError('Add cart failed , please contact administrator'):$this->setSuccess('Add inquiry success');
}
$this->incrementLoginAttempts($request);
//购物车列表
public function cartLists(){
return $this->setError('Login failure');
}
......
<?php
namespace App\Http\Controllers\Api;
use App\Http\Requests\InquirySave;
use App\Http\Services\CountryService;
use App\Http\Services\InquiryService;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class CountryApiController extends Controller
{
public function list(Request $request)
{
$data = CountryService::getCountryList();
return $this->setSuccessData($data['data'], $data['total']);
}
}
......@@ -2,11 +2,89 @@
namespace App\Http\Controllers\Api;
use App\Http\Requests\UserAddressSave;
use App\Http\Services\UserAddressService;
use App\Models\UserAddressModel;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;
class UserAddressApiController extends Controller
{
public function list(Request $request)
{
$userId = $request->user->id;
$data = UserAddressService::getUserAddressList($userId);
return $this->setSuccessData($data['data'], $data['total']);
}
public function detail(Request $request)
{
$addressId = $request->input('address_id');
$data = UserAddressService::getUserAddressDetail($addressId);
return $this->setSuccessData($data);
}
public function add(UserAddressSave $request)
{
$address = $request->only([
'email',
'phone',
'country',
'address_type',
'last_name',
'first_name',
'company_name',
'detail_address',
'city',
'province',
'post_code'
]);
$user = $request->user;
$result = UserAddressService::addUserAddress($address, $user);
if (!$result) {
return $this->setError('Add user address failed');
}
return $this->setSuccess('Add user address success');
}
public function update(UserAddressSave $request)
{
$address = $request->only([
'address_id',
'email',
'phone',
'country',
'address_type',
'last_name',
'first_name',
'company_name',
'detail_address',
'city',
'province',
'post_code'
]);
$user = $request->user;
$result = UserAddressService::updateUserAddress($address, $user);
if (!$result) {
return $this->setError('Update user address failed');
}
return $this->setSuccess('Update user address success');
}
public function delete(Request $request)
{
$addressId = $request->input('address_id');
$result = UserAddressModel::where('address_id',$addressId)->delete();
if (!$result) {
return $this->setError('Delete user address failed');
}
return $this->setSuccess('Delete user address success');
}
}
......@@ -2,6 +2,7 @@
namespace App\Http\Controllers\Api;
use App\Http\Requests\UserUpdate;
use App\Models\UserModel;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
......@@ -30,7 +31,7 @@ class UserApiController extends Controller
return $this->setSuccessData($user);
}
public function update(Request $request)
public function update(UserUpdate $request)
{
$data = $request->only([
'account_properties',
......@@ -38,15 +39,14 @@ class UserApiController extends Controller
'first_name',
'last_name',
]);
$id = $request->user->id;
$data['update_time'] = time();
$result = UserModel::where('id', $id)->update($data);
if (!$result) {
return $this->setError('Save user info failed');
return $this->setError('Update user info failed');
}
return $this->setSuccess('Save user info success');
return $this->setSuccess('Update user info success');
}
public function changePhone(Request $request)
......@@ -81,7 +81,8 @@ class UserApiController extends Controller
$email = $request->input('email');
$validator = Validator::make($request->all(), [
'email' => 'required|email',
'password' => 'required|max:100'
'password' => 'required|max:100',
'email_code' => 'required'
]);
if ($validator->fails()) {
......
......@@ -4,10 +4,10 @@ namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserAddressesController extends Controller
class UserAddressController extends Controller
{
public function index(Request $request)
{
return view('user_addresses.index');
return view('user_address.index');
}
}
......@@ -40,11 +40,7 @@ class BaseRequest extends FormRequest
public function __get($key)
{
if ($key == 'user') {
return request()->get('user');
}
if ($key == 'perms') {
return request()->get('perms');
return request()->user;
}
}
......
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UserAddressSave extends BaseRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$rules = [
'email' => 'required|email',
'phone' => 'required|max:20',
'country' => 'required|max:64',
'province' => 'required|max:64',
'city' => 'required|max:64',
'last_name' => 'required|max:50',
'first_name' => 'required|max:50',
'company_name' => 'required|max:50',
'detail_address' => 'required|max:255',
'post_code' => 'required|max:10',
];
if (request()->routeIs('user_address.update')) {
$rules['address_id'] = 'required';
}
return $rules;
}
}
......@@ -27,11 +27,11 @@ class UserRegister extends BaseRequest
[
'email' => 'required|string|email|max:100',
'password' => 'required|string|min:8|confirmed',
'first_name' => 'required|max:100',
'last_name' => 'required|max:100',
'email_code' => 'required|max:100',
'first_name' => 'required|max:50',
'last_name' => 'required|max:50',
'email_code' => 'required|max:10',
'phone' => 'required|max:100',
'company_name' => 'required|max:100',
'company_name' => 'required|max:50',
]
];
}
......
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UserUpdate extends BaseRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'account_properties' => 'required|in:1,2',
'company_name' => 'required|max:50',
'first_name' => 'required|max:50',
'last_name' => 'required|max:50',
];
}
}
......@@ -2,31 +2,134 @@
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'],
];
$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,"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["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["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());
}
}
//刷新购物车并且返回列表
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'],
'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"];
}
$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;
}
}
return InquiryItems::addInquiryItems($inquiryItems);
});
$con->commit();
return true;
}catch (\Exception $e){
$con->rollback();
throw new InvalidRequestException($e->getMessage().$e->getLine());
}
}
}
<?php
namespace App\Http\Services;
use App\Models\CountryModel;
class CountryService
{
public static function getCountryList()
{
$result = CountryModel::paginate(10)->toArray();
return $result;
}
}
<?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\Services;
use App\Models\UserAddressModel;
use Illuminate\Support\Facades\DB;
class UserAddressService
{
public static function addUserAddress($address, $user)
{
return DB::transaction(function () use ($address, $user) {
$address['user_id'] = $user->id;
$address['create_time'] = time();
return UserAddressModel::insert($address);
});
}
public static function updateUserAddress($address, $user)
{
return DB::transaction(function () use ($address, $user) {
$address['user_id'] = $user->id;
$address['create_time'] = time();
return UserAddressModel::where('address_id',$address['address_id'])->update($address);
});
}
public static function getUserAddressDetail($addressId)
{
$address = UserAddressModel::select(['*'])->where('address_id', $addressId)
->first()->toArray();
return $address;
}
public static function getUserAddressList($userId)
{
$query = UserAddressModel::where('user_id', $userId)->orderBy('address_id', 'desc');
$result = $query->paginate(10)->toArray();
foreach ($result['data'] as &$item) {
$item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
}
unset($item);
return $result;
}
}
<?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;
const STATUS_NO = -1; //状态禁用
const STATUS_YES = 1; //状态启动
//查询购物车数据
public static function getCartInfo($user_id)
......@@ -21,5 +24,4 @@ class CartModel extends Model
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class CountryModel extends Model
{
protected $table = 'country';
public $timestamps = false;
}
......@@ -8,6 +8,8 @@ class UserAddressModel extends Model
{
protected $table = 'user_address';
public $timestamps = false;
//
protected $fillable = [
];
......
......@@ -27,9 +27,23 @@ Route::middleware(['api', 'api.check'])->namespace('Api')->group(function () {
Route::POST('auth/send_email_code', 'AuthApiController@sendEmailCode');
Route::get('user/info', 'UserApiController@info');
Route::POST('user/update', 'UserApiController@update');
Route::POST('user/change_phone', 'UserApiController@changePhone');
Route::POST('user/change_email', 'UserApiController@changeEmail');
Route::POST('inquiry/add', 'InquiryApiController@add');
Route::GET('inquiry/list', 'InquiryApiController@list');
Route::GET('inquiry/detail', 'InquiryApiController@detail');
Route::get('user_address/list', 'UserAddressApiController@list');
Route::get('user_address/detail', 'UserAddressApiController@detail')->name('user_address.detail');
Route::POST('user_address/add', 'UserAddressApiController@add')->name('user_address.add');
Route::POST('user_address/update', 'UserAddressApiController@update')->name('user_address.update');
Route::POST('user_address/delete', 'UserAddressApiController@delete')->name('user_address.delete');
Route::get('country/list', 'CountryApiController@list');
Route::POST('cart/addCart', 'CartApiController@addCart'); //添加购物车
Route::ANY('inquiry/add', 'InquiryApiController@add');
Route::ANY('inquiry/list', 'InquiryApiController@list');
Route::ANY('inquiry/detail', 'InquiryApiController@detail');
});
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