!108 【WIP】代码优化和添加hook(适配多商户)

* wip
* 文件管理器优化
* 文件管理器优化
* 后台订单管理和商品管理代码优化,提供更好的扩展性,便于多商家实现
* totalService
* 添加hook
* TotalService优化
* hook
* hook
* 购物车列表hook
This commit is contained in:
mengwb 2023-05-30 01:56:26 +00:00 committed by Edward Yang
parent 64cedc39d4
commit 8b711fa653
11 changed files with 73 additions and 45 deletions

View File

@ -126,7 +126,7 @@ class FileManagerController extends Controller
$savePath = $request->get('path');
$originName = $file->getClientOriginalName();
$filePath = $file->storeAs($savePath, $originName, 'catalog');
$filePath = (new FileManagerService)->uploadFile($file, $savePath, $originName);
return [
'name' => $originName,

View File

@ -13,11 +13,12 @@ namespace Beike\Admin\Services;
class FileManagerService
{
private $fileBasePath = '';
protected $fileBasePath = '';
protected $basePath = '';
public function __construct()
{
$this->fileBasePath = public_path('catalog');
$this->fileBasePath = public_path('catalog') . $this->basePath;
}
/**
@ -26,6 +27,7 @@ class FileManagerService
public function getDirectories($baseFolder = '/'): array
{
$currentBasePath = rtrim($this->fileBasePath . $baseFolder, '/');
$directories = glob("{$currentBasePath}/*", GLOB_ONLYDIR);
$result = [];
@ -114,7 +116,7 @@ class FileManagerService
*/
public function createDirectory($folderName)
{
$catalogFolderPath = "catalog/{$folderName}";
$catalogFolderPath = "catalog{$this->basePath}/{$folderName}";
$folderPath = public_path($catalogFolderPath);
if (is_dir($folderPath)) {
throw new \Exception(trans('admin/file_manager.directory_already_exist'));
@ -130,7 +132,7 @@ class FileManagerService
*/
public function deleteDirectoryOrFile($filePath)
{
$filePath = public_path("catalog/{$filePath}");
$filePath = public_path("catalog{$this->basePath}/{$filePath}");
if (is_dir($filePath)) {
$files = glob($filePath . '/*');
if ($files) {
@ -154,7 +156,7 @@ class FileManagerService
return;
}
foreach ($files as $file) {
$filePath = public_path("catalog/{$basePath}/$file");
$filePath = public_path("catalog{$this->basePath}/{$basePath}/$file");
if (file_exists($filePath)) {
@unlink($filePath);
}
@ -170,7 +172,7 @@ class FileManagerService
*/
public function updateName($originPath, $newPath)
{
$originPath = public_path("catalog/{$originPath}");
$originPath = public_path("catalog{$this->basePath}/{$originPath}");
if (! is_dir($originPath) && ! file_exists($originPath)) {
throw new \Exception(trans('admin/file_manager.target_not_exist'));
}
@ -182,6 +184,12 @@ class FileManagerService
@rename($originPath, $newPath);
}
public function uploadFile($file, $savePath, $originName)
{
$savePath = $this->basePath . $savePath;
return $file->storeAs($savePath, $originName, 'catalog');
}
/**
* 处理文件夹
*
@ -205,7 +213,7 @@ class FileManagerService
*/
private function hasSubFolders($folderPath): bool
{
$path = public_path("catalog/{$folderPath}");
$path = public_path("catalog{$this->basePath}/{$folderPath}");
$subFiles = glob($path . '/*');
foreach ($subFiles as $subFile) {
if (is_dir($subFile)) {

View File

@ -31,7 +31,7 @@ class OrderRepo
*/
public static function filterAll(array $filters = [])
{
$builder = self::getListBuilder($filters)->orderByDesc('created_at');
$builder = static::getListBuilder($filters)->orderByDesc('created_at');
return $builder->get();
}
@ -44,7 +44,7 @@ class OrderRepo
*/
public static function getListByCustomer($customer): LengthAwarePaginator
{
$builder = self::getListBuilder(['customer' => $customer])->orderByDesc('created_at');
$builder = static::getListBuilder(['customer' => $customer])->orderByDesc('created_at');
return $builder->paginate(perPage());
}
@ -56,7 +56,7 @@ class OrderRepo
*/
public static function getLatestOrders($customer, $limit)
{
return self::getListBuilder(['customer' => $customer])
return static::getListBuilder(['customer' => $customer])
->orderByDesc('created_at')
->take($limit)
->get();
@ -68,7 +68,7 @@ class OrderRepo
*/
public static function filterOrders(array $filters = []): LengthAwarePaginator
{
$builder = self::getListBuilder($filters)->orderByDesc('created_at');
$builder = static::getListBuilder($filters)->orderByDesc('created_at');
return $builder->paginate(perPage());
}
@ -266,6 +266,7 @@ class OrderRepo
OrderProductRepo::createOrderProducts($order, $carts['carts']);
OrderTotalRepo::createTotals($order, $totals);
hook_filter('repository.order.create.after', ['order' => $order, 'data' => $data]);
return $order;
}

View File

@ -55,7 +55,7 @@ class ProductRepo
*/
public static function getProductsByCategory($categoryId, $filterData)
{
$builder = self::getBuilder(array_merge(['category_id' => $categoryId, 'active' => 1], $filterData));
$builder = static::getBuilder(array_merge(['category_id' => $categoryId, 'active' => 1], $filterData));
return $builder->with('inCurrentWishlist')
->paginate($filterData['per_page'] ?? perPage())
@ -72,7 +72,7 @@ class ProductRepo
if (! $productIds) {
return ProductSimple::collection(new Collection());
}
$builder = self::getBuilder(['product_ids' => $productIds])->whereHas('masterSku');
$builder = static::getBuilder(['product_ids' => $productIds])->whereHas('masterSku');
$products = $builder->with('inCurrentWishlist')->get();
return ProductSimple::collection($products);
@ -220,7 +220,7 @@ class ProductRepo
public static function getFilterAttribute($data): array
{
$builder = self::getBuilder(array_diff_key($data, ['attr' => '', 'price' => '']))
$builder = static::getBuilder(array_diff_key($data, ['attr' => '', 'price' => '']))
->select(['pa.attribute_id', 'pa.attribute_value_id'])
->with(['attributes.attribute.description', 'attributes.attribute_value.description'])
->leftJoin('product_attributes as pa', 'pa.product_id', 'products.id')
@ -274,7 +274,7 @@ class ProductRepo
{
$selectPrice = $data['price'] ?? '-';
// unset($data['price']);
$builder = self::getBuilder(['category_id' => $data['category_id']])->leftJoin('product_skus as ps', 'products.id', 'ps.product_id')
$builder = static::getBuilder(['category_id' => $data['category_id']])->leftJoin('product_skus as ps', 'products.id', 'ps.product_id')
->where('ps.is_default', 1);
$min = $builder->min('ps.price');
$max = $builder->max('ps.price');
@ -293,7 +293,7 @@ class ProductRepo
public static function list($data = [])
{
return self::getBuilder($data)->paginate($data['per_page'] ?? 20);
return static::getBuilder($data)->paginate($data['per_page'] ?? 20);
}
public static function autocomplete($name)
@ -353,7 +353,7 @@ class ProductRepo
}
$items = [];
$products = self::getBuilder()->select('id')->get();
$products = static::getBuilder()->select('id')->get();
foreach ($products as $product) {
$items[$product->id] = [
'id' => $product->id,

View File

@ -152,7 +152,7 @@ class StateMachineService
];
}
return $result;
return hook_filter('service.state_machine.all_statuses', $result);
}
/**
@ -232,6 +232,8 @@ class StateMachineService
}
$this->{$function}($oldStatusCode, $status);
}
hook_filter('service.state_machine.change_status.after', ['order' => $order, 'status' => $status, 'comment' => $comment, 'notify' => $notify]);
}
/**
@ -242,11 +244,6 @@ class StateMachineService
*/
private function validStatusCode($statusCode)
{
if (! in_array($statusCode, self::ORDER_STATUS)) {
$statusCodeString = implode(', ', self::ORDER_STATUS);
throw new \Exception("Invalid order status, must be one of the '{$statusCodeString}'");
}
$orderId = $this->orderId;
$orderNumber = $this->order->number;
$currentStatusCode = $this->order->status;

View File

@ -50,7 +50,9 @@ class CartService
return $description && $product;
});
return CartDetail::collection($cartItems)->jsonSerialize();
$cartList = CartDetail::collection($cartItems)->jsonSerialize();
return hook_filter('service.cart.list', $cartList);
}
/**
@ -183,6 +185,6 @@ class CartService
'amount_format' => currency_format($amount),
];
return hook_filter('cart.data', $data);
return hook_filter('service.cart.data', $data);
}
}

View File

@ -232,7 +232,8 @@ class CheckoutService
$cartList = CartService::list($customer, true);
$carts = CartService::reloadData($cartList);
$totalService = (new TotalService($currentCart, $cartList));
$totalClass = hook_filter('service.checkout.total_service', 'Beike\Shop\Services\TotalService');
$totalService = (new $totalClass($currentCart, $cartList));
$this->totalService = $totalService;
$addresses = AddressRepo::listByCustomer($customer);
@ -268,7 +269,7 @@ class CheckoutService
'totals' => $totalService->getTotals($this),
];
return hook_filter('checkout.data', $data);
return hook_filter('service.checkout.data', $data);
}
public static function formatAddress($address)

View File

@ -17,16 +17,16 @@ use Illuminate\Support\Str;
class TotalService
{
private const TOTAL_CODES = [
protected const TOTAL_CODES = [
'subtotal',
'tax',
'shipping',
'customer_discount',
];
public Cart $currentCart;
protected Cart $currentCart;
public array $cartProducts;
protected array $cartProducts;
public array $taxes = [];
@ -34,7 +34,7 @@ class TotalService
public float $amount = 0;
public string $shippingMethod = '';
protected string|array $shippingMethod = '';
public function __construct($currentCart, $cartProducts)
{
@ -54,6 +54,11 @@ class TotalService
return $this;
}
public function getShippingMethod(): string
{
return $this->shippingMethod;
}
/**
* 获取税费数据
*
@ -132,4 +137,14 @@ class TotalService
return collect($carts)->sum('subtotal');
}
public function getCartProducts()
{
return $this->cartProducts;
}
public function getCurrentCart()
{
return $this->currentCart;
}
}

View File

@ -25,7 +25,7 @@ class ShippingService
public static function getTotal(CheckoutService $checkout): ?array
{
$totalService = $checkout->totalService;
$shippingMethod = $totalService->shippingMethod;
$shippingMethod = $totalService->getShippingMethod();
if (empty($shippingMethod)) {
return null;
}

24
resources/js/http.js vendored
View File

@ -3,7 +3,7 @@
* @link https://beikeshop.com
* @Author pu shuo <pushuo@guangda.work>
* @Date 2022-08-02 19:19:52
* @LastEditTime 2022-09-16 20:58:16
* @LastEditTime 2023-05-29 18:56:35
*/
window.axios = require('axios');
@ -25,8 +25,8 @@ export default {
* @param url 接口路由
* @returns {AxiosPromise<any>}
*/
get (url, params, {hmsg, hload}={}) {
return this.request('get', url, params = params, {hmsg, hload});
get (url, params, {hmsg, hload, base}={}) {
return this.request('get', url, params = params, {hmsg, hload, base});
},
/**
@ -37,8 +37,8 @@ export default {
* @returns {AxiosPromise<any>}
*/
post (url, params, {hmsg, hload}={}) {
return this.request('post', url, params, {hmsg, hload});
post (url, params, {hmsg, hload, base}={}) {
return this.request('post', url, params, {hmsg, hload, base});
},
/**
@ -48,8 +48,8 @@ export default {
* @returns {Promise}
*/
delete (url, params, {hmsg, hload}={}) {
return this.request('delete', url, params, {hmsg, hload});
delete (url, params, {hmsg, hload, base}={}) {
return this.request('delete', url, params, {hmsg, hload, base});
},
/**
@ -59,8 +59,8 @@ export default {
* @returns {Promise}
*/
put (url, params, {hmsg, hload}={}) {
return this.request('put', url, params, {hmsg, hload});
put (url, params, {hmsg, hload, base}={}) {
return this.request('put', url, params, {hmsg, hload, base});
},
@ -73,11 +73,15 @@ export default {
* @returns {Promise<any>}
*/
// 错误和失败信息都在这里进行处理,界面中调用的时候只处理正确数据即可
request(method, url, params = {}, {hmsg, hload} = {}) {
request(method, url, params = {}, {hmsg, hload, base} = {}) {
if (!hload) {
layer.load(2, {shade: [0.3,'#fff'] })
}
if (base) {
axios.defaults.baseURL = base;
}
return new Promise((resolve, reject) => {
axios({method: method, url: url, [method == 'get' ? 'params' : 'data']: params}).then((res) => {
if (res) {

View File

@ -88,8 +88,7 @@
<span class="rounded-circle bg-primary">{{ $carts['quantity'] }}</span>
</div>
<div class="card-body">
@hook('checkout.total.header')
@hookwrapper('checkout.products')
<div class="products-wrap">
@foreach ($carts['carts'] as $cart)
<div class="item">
@ -111,6 +110,7 @@
</div>
@endforeach
</div>
@endhookwrapper
<ul class="totals">
@foreach ($totals as $total)
<li><span>{{ $total['title'] }}</span><span>{{ $total['amount_format'] }}</span></li>