Browse Source

Update pages to support "check-create-order"

pull/190/head
gdlcf88 4 years ago
parent
commit
f738f80a65
  1. 1
      plugins/Baskets/src/EasyAbp.EShop.Plugins.Baskets.Domain.Shared/EasyAbp/EShop/Plugins/Baskets/BasketsErrorCodes.cs
  2. 1
      plugins/Baskets/src/EasyAbp.EShop.Plugins.Baskets.Domain.Shared/EasyAbp/EShop/Plugins/Baskets/Localization/en.json
  3. 1
      plugins/Baskets/src/EasyAbp.EShop.Plugins.Baskets.Domain.Shared/EasyAbp/EShop/Plugins/Baskets/Localization/zh-Hans.json
  4. 1
      plugins/Baskets/src/EasyAbp.EShop.Plugins.Baskets.Domain.Shared/EasyAbp/EShop/Plugins/Baskets/Localization/zh-Hant.json
  5. 4
      plugins/Baskets/src/EasyAbp.EShop.Plugins.Baskets.Web/EShopPluginsBasketsWebModule.cs
  6. 1
      plugins/Baskets/src/EasyAbp.EShop.Plugins.Baskets.Web/EasyAbp.EShop.Plugins.Baskets.Web.csproj
  7. 41
      plugins/Baskets/src/EasyAbp.EShop.Plugins.Baskets.Web/Pages/EShop/Plugins/Baskets/BasketItems/BasketItem/CreateModal.cshtml
  8. 40
      plugins/Baskets/src/EasyAbp.EShop.Plugins.Baskets.Web/Pages/EShop/Plugins/Baskets/BasketItems/BasketItem/CreateModal.cshtml.cs
  9. 52
      plugins/Baskets/src/EasyAbp.EShop.Plugins.Baskets.Web/Pages/EShop/Plugins/Baskets/BasketItems/BasketItem/EditModal.cshtml
  10. 42
      plugins/Baskets/src/EasyAbp.EShop.Plugins.Baskets.Web/Pages/EShop/Plugins/Baskets/BasketItems/BasketItem/EditModal.cshtml.cs
  11. 29
      plugins/Baskets/src/EasyAbp.EShop.Plugins.Baskets.Web/Pages/EShop/Plugins/Baskets/BasketItems/BasketItem/index.js

1
plugins/Baskets/src/EasyAbp.EShop.Plugins.Baskets.Domain.Shared/EasyAbp/EShop/Plugins/Baskets/BasketsErrorCodes.cs

@ -2,6 +2,7 @@
{
public static class BasketsErrorCodes
{
public const string CheckCreateOrderFailed = "EasyAbp.EShop.Plugins.Baskets:CheckCreateOrderFailed";
public const string ProductSkuNotFound = "EasyAbp.EShop.Plugins.Baskets:ProductSkuNotFound";
}
}

1
plugins/Baskets/src/EasyAbp.EShop.Plugins.Baskets.Domain.Shared/EasyAbp/EShop/Plugins/Baskets/Localization/en.json

@ -27,6 +27,7 @@
"EditBasketItem": "Edit",
"BasketItemDeletionConfirmationMessage": "Are you sure to delete the basket item {0}?",
"SuccessfullyDeleted": "Successfully deleted",
"EasyAbp.EShop.Plugins.Baskets:CheckCreateOrderFailed": "You are not allowed to purchase the specified item. Reason: {reason}",
"EasyAbp.EShop.Plugins.Baskets:ProductSkuNotFound": "Product {productId} (SKU: {productSkuId}) not found."
}
}

1
plugins/Baskets/src/EasyAbp.EShop.Plugins.Baskets.Domain.Shared/EasyAbp/EShop/Plugins/Baskets/Localization/zh-Hans.json

@ -27,6 +27,7 @@
"EditBasketItem": "编辑",
"BasketItemDeletionConfirmationMessage": "确认删除购物车项 {0}?",
"SuccessfullyDeleted": "删除成功",
"EasyAbp.EShop.Plugins.Baskets:CheckCreateOrderFailed": "您无法购买指定商品,原因:{reason}",
"EasyAbp.EShop.Plugins.Baskets:ProductSkuNotFound": "商品{productId}(SKU: {productSkuId})未找到"
}
}

1
plugins/Baskets/src/EasyAbp.EShop.Plugins.Baskets.Domain.Shared/EasyAbp/EShop/Plugins/Baskets/Localization/zh-Hant.json

@ -27,6 +27,7 @@
"EditBasketItem": "編輯",
"BasketItemDeletionConfirmationMessage": "確認刪除購物車項 {0}?",
"SuccessfullyDeleted": "刪除成功",
"EasyAbp.EShop.Plugins.Baskets:CheckCreateOrderFailed": "您無法購買指定商品,原因:{reason}",
"EasyAbp.EShop.Plugins.Baskets:ProductSkuNotFound": "商品{productId}(SKU: {productSkuId})未找到"
}
}

4
plugins/Baskets/src/EasyAbp.EShop.Plugins.Baskets.Web/EShopPluginsBasketsWebModule.cs

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc.RazorPages;
using EasyAbp.EShop.Orders;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.DependencyInjection;
using EasyAbp.EShop.Plugins.Baskets.Localization;
using EasyAbp.EShop.Plugins.Baskets.Web.Menus;
@ -12,6 +13,7 @@ using Volo.Abp.VirtualFileSystem;
namespace EasyAbp.EShop.Plugins.Baskets.Web
{
[DependsOn(
typeof(EShopOrdersApplicationContractsModule),
typeof(EShopPluginsBasketsApplicationContractsModule),
typeof(AbpAspNetCoreMvcUiThemeSharedModule),
typeof(AbpAutoMapperModule)

1
plugins/Baskets/src/EasyAbp.EShop.Plugins.Baskets.Web/EasyAbp.EShop.Plugins.Baskets.Web.csproj

@ -17,6 +17,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\modules\EasyAbp.EShop.Orders\src\EasyAbp.EShop.Orders.Application.Contracts\EasyAbp.EShop.Orders.Application.Contracts.csproj" />
<ProjectReference Include="..\EasyAbp.EShop.Plugins.Baskets.Application.Contracts\EasyAbp.EShop.Plugins.Baskets.Application.Contracts.csproj" />
</ItemGroup>

41
plugins/Baskets/src/EasyAbp.EShop.Plugins.Baskets.Web/Pages/EShop/Plugins/Baskets/BasketItems/BasketItem/CreateModal.cshtml

@ -1,6 +1,6 @@
@page
@using EasyAbp.EShop.Plugins.Baskets
@using EasyAbp.EShop.Plugins.Baskets.Localization
@using EasyAbp.EShop.Plugins.Baskets.Permissions
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Mvc.Localization
@using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Modal
@ -25,16 +25,15 @@
<script>
var localStorageItemKey = localStorageItemKey || "EShopBasket:" + basketName;
var l = abp.localization.getResource('EasyAbpEShopPluginsBaskets');
$(document).ready(function() {
if (@(Model.ServerSide ? "true" : "false")) return;
var $form = $("form");
$form.off('submit');
$form.on('submit', function(e){
e.preventDefault();
var service = easyAbp.eShop.plugins.baskets.basketItems.basketItem;
service.generateClientSideData({ items: [ $form.serializeFormToObject().viewModel ] }, {
function generateClientSideData(formObj) {
var basketItemService = easyAbp.eShop.plugins.baskets.basketItems.basketItem;
basketItemService.generateClientSideData({ items: [ formObj.viewModel ] }, {
success: function (responseText, statusText, xhr, form) {
var basketItems = JSON.parse(localStorage.getItem(localStorageItemKey)) || [];
basketItems = pushOrUpdateBasketItem(basketItems, responseText.items[0])
@ -48,6 +47,34 @@
})
}
});
}
var $form = $("form");
$form.off('submit');
$form.on('submit', function(e){
e.preventDefault();
var orderService = easyAbp.eShop.orders.orders.order;
var formObj = $form.serializeFormToObject();
if (abp.currentUser.isAuthenticated) {
orderService.checkCreate({
storeId: formObj.viewModel.storeId,
orderLines: [{
productId: formObj.viewModel.productId,
productSkuId: formObj.viewModel.productSkuId,
quantity: formObj.viewModel.quantity
}]
}, {
success: function (responseText, statusText, xhr, form) {
if (responseText.canCreate) {
generateClientSideData(formObj);
} else {
abp.message.error(l('@BasketsErrorCodes.CheckCreateOrderFailed', responseText.reason));
}
}
});
} else {
generateClientSideData(formObj);
}
});
});

40
plugins/Baskets/src/EasyAbp.EShop.Plugins.Baskets.Web/Pages/EShop/Plugins/Baskets/BasketItems/BasketItem/CreateModal.cshtml.cs

@ -1,10 +1,14 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using EasyAbp.EShop.Orders.Orders;
using EasyAbp.EShop.Orders.Orders.Dtos;
using Microsoft.AspNetCore.Mvc;
using EasyAbp.EShop.Plugins.Baskets.BasketItems;
using EasyAbp.EShop.Plugins.Baskets.BasketItems.Dtos;
using EasyAbp.EShop.Plugins.Baskets.Permissions;
using EasyAbp.EShop.Plugins.Baskets.Web.Pages.EShop.Plugins.Baskets.BasketItems.BasketItem.ViewModels;
using Microsoft.AspNetCore.Authorization;
using Volo.Abp;
using Volo.Abp.Settings;
namespace EasyAbp.EShop.Plugins.Baskets.Web.Pages.EShop.Plugins.Baskets.BasketItems.BasketItem
@ -13,14 +17,18 @@ namespace EasyAbp.EShop.Plugins.Baskets.Web.Pages.EShop.Plugins.Baskets.BasketIt
{
[BindProperty]
public CreateBasketItemViewModel ViewModel { get; set; } = new();
public bool ServerSide { get; set; }
private readonly IBasketItemAppService _service;
private readonly IOrderAppService _orderAppService;
private readonly IBasketItemAppService _basketItemAppService;
public CreateModalModel(IBasketItemAppService service)
public CreateModalModel(
IOrderAppService orderAppService,
IBasketItemAppService basketItemAppService)
{
_service = service;
_orderAppService = orderAppService;
_basketItemAppService = basketItemAppService;
}
public virtual async Task OnGetAsync()
@ -32,7 +40,29 @@ namespace EasyAbp.EShop.Plugins.Baskets.Web.Pages.EShop.Plugins.Baskets.BasketIt
public virtual async Task<IActionResult> OnPostAsync()
{
var dto = ObjectMapper.Map<CreateBasketItemViewModel, CreateBasketItemDto>(ViewModel);
await _service.CreateAsync(dto);
var checkCreateOrderResult = await _orderAppService.CheckCreateAsync(new CheckCreateOrderInput
{
StoreId = ViewModel.StoreId,
OrderLines = new List<CreateOrderLineDto>
{
new()
{
ProductId = ViewModel.ProductId,
ProductSkuId = ViewModel.ProductSkuId,
Quantity = ViewModel.Quantity
}
}
});
if (!checkCreateOrderResult.CanCreate)
{
throw new BusinessException(BasketsErrorCodes.CheckCreateOrderFailed)
.WithData("reason", checkCreateOrderResult.Reason);
}
await _basketItemAppService.CreateAsync(dto);
return NoContent();
}
}

52
plugins/Baskets/src/EasyAbp.EShop.Plugins.Baskets.Web/Pages/EShop/Plugins/Baskets/BasketItems/BasketItem/EditModal.cshtml

@ -1,8 +1,9 @@
@page
@using EasyAbp.EShop.Plugins.Baskets
@using EasyAbp.EShop.Plugins.Baskets.Localization
@using EasyAbp.EShop.Plugins.Baskets.Permissions
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Mvc.Localization
@using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form
@using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Modal
@using Volo.Abp.Settings
@inject IHtmlLocalizer<BasketsResource> L
@ -28,19 +29,11 @@
$(document).ready(function() {
if (@(Model.ServerSide ? "true" : "false")) return;
var basketItems = JSON.parse(localStorage.getItem(localStorageItemKey)) || [];
var index = basketItems.findIndex(x => x.id === '@Model.Id');
$('#ViewModel_Quantity').val(basketItems[index].quantity);
var $form = $("form");
$form.off('submit');
$form.on('submit', function(e){
e.preventDefault();
var service = easyAbp.eShop.plugins.baskets.basketItems.basketItem;
basketItems[index].quantity = parseInt($form.serializeFormToObject().viewModel.quantity);
service.generateClientSideData({ items: [ basketItems[index] ] }, {
function generateClientSideData(basketItems) {
var basketItemService = easyAbp.eShop.plugins.baskets.basketItems.basketItem;
basketItemService.generateClientSideData({ items: [ basketItems[index] ] }, {
success: function (responseText, statusText, xhr, form) {
localStorage.setItem(localStorageItemKey, JSON.stringify(basketItems));
$form.trigger('abp-ajax-success',
@ -52,6 +45,39 @@
})
}
});
}
var basketItems = JSON.parse(localStorage.getItem(localStorageItemKey)) || [];
var index = basketItems.findIndex(x => x.id === '@Model.Id');
$('#ViewModel_Quantity').val(basketItems[index].quantity);
var $form = $("form");
$form.off('submit');
$form.on('submit', function(e){
e.preventDefault();
var orderService = easyAbp.eShop.orders.orders.order;
basketItems[index].quantity = parseInt($form.serializeFormToObject().viewModel.quantity);
if (abp.currentUser.isAuthenticated) {
orderService.checkCreate({
storeId: basketItems[index].storeId,
orderLines: [{
productId: basketItems[index].productId,
productSkuId: basketItems[index].productSkuId,
quantity: basketItems[index].quantity
}]
}, {
success: function (responseText, statusText, xhr, form) {
if (responseText.canCreate) {
generateClientSideData(basketItems);
} else {
abp.message.error(l('@BasketsErrorCodes.CheckCreateOrderFailed', responseText.reason));
}
}
});
} else {
generateClientSideData(basketItems);
}
});
});
</script>

42
plugins/Baskets/src/EasyAbp.EShop.Plugins.Baskets.Web/Pages/EShop/Plugins/Baskets/BasketItems/BasketItem/EditModal.cshtml.cs

@ -1,12 +1,16 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using EasyAbp.EShop.Orders.Orders;
using EasyAbp.EShop.Orders.Orders.Dtos;
using Microsoft.AspNetCore.Mvc;
using EasyAbp.EShop.Plugins.Baskets.BasketItems;
using EasyAbp.EShop.Plugins.Baskets.BasketItems.Dtos;
using EasyAbp.EShop.Plugins.Baskets.Permissions;
using EasyAbp.EShop.Plugins.Baskets.Web.Pages.EShop.Plugins.Baskets.BasketItems.BasketItem.ViewModels;
using Microsoft.AspNetCore.Authorization;
using Volo.Abp;
using Volo.Abp.Settings;
namespace EasyAbp.EShop.Plugins.Baskets.Web.Pages.EShop.Plugins.Baskets.BasketItems.BasketItem
@ -27,11 +31,15 @@ namespace EasyAbp.EShop.Plugins.Baskets.Web.Pages.EShop.Plugins.Baskets.BasketIt
[BindProperty]
public EditBasketItemViewModel ViewModel { get; set; }
private readonly IBasketItemAppService _service;
private readonly IOrderAppService _orderAppService;
private readonly IBasketItemAppService _basketItemAppService;
public EditModalModel(IBasketItemAppService service)
public EditModalModel(
IOrderAppService orderAppService,
IBasketItemAppService basketItemAppService)
{
_service = service;
_orderAppService = orderAppService;
_basketItemAppService = basketItemAppService;
}
public virtual async Task OnGetAsync()
@ -44,14 +52,38 @@ namespace EasyAbp.EShop.Plugins.Baskets.Web.Pages.EShop.Plugins.Baskets.BasketIt
return;
}
var dto = await _service.GetAsync(Id);
var dto = await _basketItemAppService.GetAsync(Id);
ViewModel = ObjectMapper.Map<BasketItemDto, EditBasketItemViewModel>(dto);
}
public virtual async Task<IActionResult> OnPostAsync()
{
var item = await _basketItemAppService.GetAsync(Id);
var dto = ObjectMapper.Map<EditBasketItemViewModel, UpdateBasketItemDto>(ViewModel);
await _service.UpdateAsync(Id, dto);
var checkCreateOrderResult = await _orderAppService.CheckCreateAsync(new CheckCreateOrderInput
{
StoreId = item.StoreId,
OrderLines = new List<CreateOrderLineDto>
{
new()
{
ProductId = item.ProductId,
ProductSkuId = item.ProductSkuId,
Quantity = ViewModel.Quantity
}
}
});
if (!checkCreateOrderResult.CanCreate)
{
throw new BusinessException(BasketsErrorCodes.CheckCreateOrderFailed)
.WithData("reason", checkCreateOrderResult.Reason);
}
await _basketItemAppService.UpdateAsync(Id, dto);
return NoContent();
}
}

29
plugins/Baskets/src/EasyAbp.EShop.Plugins.Baskets.Web/Pages/EShop/Plugins/Baskets/BasketItems/BasketItem/index.js

@ -120,15 +120,30 @@ $(function () {
function createManyServerSideBasketItems(items, autoReloadDataTable = true) {
var item = items.shift();
service.create(item, {
success: function () {
if (items.length > 0) {
createManyServerSideBasketItems(items);
} else if (autoReloadDataTable) {
dataTable.ajax.reload();
var orderService = easyAbp.eShop.orders.orders.order;
orderService.checkCreate({
storeId: item.storeId,
orderLines: [{
productId: item.productId,
productSkuId: item.productSkuId,
quantity: item.quantity
}]
}, {
success: function (responseText, statusText, xhr, form) {
console.log(responseText);
if (responseText.canCreate) {
service.create(item, {
success: function () {
if (items.length > 0) {
createManyServerSideBasketItems(items);
} else if (autoReloadDataTable) {
dataTable.ajax.reload();
}
}
})
}
}
})
});
}
$('#NewBasketItemButton').click(function (e) {

Loading…
Cancel
Save