This commit is contained in:
MeysamMoghaddam
2025-10-20 15:20:29 +03:30
parent ee17929da0
commit b30202b7bd
9 changed files with 298 additions and 138 deletions

View File

@@ -2,12 +2,13 @@
using FrontOffice.Main.Utilities;
using Microsoft.AspNetCore.Components;
using MudBlazor;
using System.Security.Cryptography;
namespace FrontOffice.Main.Pages;
public partial class Index
{
[Inject] private PackageContract.PackageContractClient PackageClient { get; set; } = default!;
[Inject] private MobileNumberEncryptor Encryptor { get; set; }
private string? _email;
private bool _isLoadingPackages;
private List<Pack> _packs = new();
@@ -15,6 +16,17 @@ public partial class Index
protected override async Task OnInitializedAsync()
{
await LoadPackagesAsync();
//string mobileNumber = "09387342688";
//// انکریپت کردن
//string encrypted = Encryptor.EncryptMobileNumber(mobileNumber);
//Console.WriteLine($"Encrypted: {encrypted}");
//// دیکریپت کردن برای تست
//string decrypted = Encryptor.DecryptMobileNumber(encrypted);
//Console.WriteLine($"Decrypted: {decrypted}");
}
private async Task LoadPackagesAsync()

View File

@@ -1,41 +1,62 @@
<div class="org-chart-container">
<div class="org-tree">
<div class="org-node root-node" id="root-node">
<div class="node-card">
<div class="node-avatar">
<MudAvatar Size="Size.Large">
<MudImage ObjectFit="ObjectFit.Cover"
ObjectPosition="ObjectPosition.Center"
Src="images/avatar1.jpg" />
</MudAvatar>
@if (_currentUser?.Children?.Any() == true)
{
<button class="expand-btn" @onclick="ToggleExpand" data-user-id="@_currentUser.Id">
<MudIcon Icon="@(_isExpanded ? Icons.Material.Filled.Remove : Icons.Material.Filled.Add)" Size="Size.Small" />
</button>
}
</div>
<div class="node-info">
<div class="node-name">@(_currentUser?.FirstName) @(_currentUser?.LastName)</div>
<div class="node-amounts">
<div class="personal-amount">
<span class="label">خرید شخصی:</span>
<span class="amount">@(_currentUser?.PersonalPurchase?.ToThousands().ToCurrencyUnitIRT() ?? "0 تومان")</span>
</div>
<div class="team-amount">
<span class="label">خرید تیمی:</span>
<span class="amount">@(_currentUser?.TeamPurchase?.ToThousands().ToCurrencyUnitIRT() ?? "0 تومان")</span>
@if (_currentUser != null)
{
<div class="org-chart-container">
<div class="org-tree">
<div class="org-node root-node" id="root-node">
<div class="node-card">
<div class="node-avatar">
@if (!string.IsNullOrWhiteSpace(_currentUser.Avatar))
{
<MudAvatar Size="Size.Large">
<MudImage ObjectFit="ObjectFit.Cover"
ObjectPosition="ObjectPosition.Center"
Src="@_currentUser.Avatar" />
</MudAvatar>
}
else
{
<MudAvatar Size="Size.Large" Color="Color.Primary" Variant="Variant.Outlined">
@(string.IsNullOrWhiteSpace(_currentUser.FirstName) ? "N" : _currentUser.FirstName.Substring(0, 1))
</MudAvatar>
}
@if (_currentUser?.Children?.Any() == true)
{
<button class="expand-btn" @onclick="ToggleExpand" data-user-id="@_currentUser.Id">
<MudIcon Icon="@(_isExpanded ? Icons.Material.Filled.Remove : Icons.Material.Filled.Add)" Size="Size.Small" />
</button>
}
</div>
<div class="node-info">
@if (!string.IsNullOrWhiteSpace(_currentUser.FirstName) || !string.IsNullOrWhiteSpace(_currentUser.LastName))
{
<div class="node-name">@_currentUser.FirstName @_currentUser.LastName</div>
}
else
{
<div class="node-name">@_currentUser.Mobile</div>
}
<div class="node-amounts">
<div class="personal-amount">
<span class="label">خرید شخصی:</span>
<span class="amount">@(_currentUser?.PersonalPurchase?.ToThousands().ToCurrencyUnitIRT() ?? "0 تومان")</span>
</div>
<div class="team-amount">
<span class="label">خرید تیمی:</span>
<span class="amount">@(_currentUser?.TeamPurchase?.ToThousands().ToCurrencyUnitIRT() ?? "0 تومان")</span>
</div>
</div>
</div>
</div>
</div>
</div>
@if (_isExpanded && _currentUser?.Children?.Any() == true)
{
<CascadingValue Value="this">
<OrganizationChartLevel Nodes="_currentUser.Children" Level="1" />
</CascadingValue>
}
@if (_isExpanded && _currentUser?.Children?.Any() == true)
{
<CascadingValue Value="this">
<OrganizationChartLevel Nodes="_currentUser.Children" Level="1" />
</CascadingValue>
}
</div>
</div>
</div>
}

View File

@@ -1,4 +1,7 @@
using static FrontOffice.Main.Pages.Profile.Components.OrganizationChartLevel;
using FrontOffice.BFF.User.Protobuf.Protos.User;
using Mapster;
using Microsoft.AspNetCore.Components;
using static FrontOffice.Main.Pages.Profile.Components.OrganizationChartLevel;
namespace FrontOffice.Main.Pages.Profile.Components;
public partial class OrganizationChart
@@ -6,112 +9,82 @@ public partial class OrganizationChart
private UserNode? _currentUser;
private bool _isExpanded;
[Inject] private UserContract.UserContractClient UserContract { get; set; } = default!;
private GetUserResponse _userProfile = new();
protected override async Task OnInitializedAsync()
{
await LoadCurrentUser();
}
private async Task LoadUserProfile()
{
try
{
_userProfile = await UserContract.GetUserAsync(request: new());
}
catch (Exception ex)
{
// Handle the case when user is not authenticated or API fails
_userProfile = new GetUserResponse();
}
StateHasChanged();
}
private async Task LoadCurrentUser()
{
await LoadUserProfile();
// Mock data - replace with actual API call
_currentUser = new UserNode
{
Id = 1,
FirstName = "علی",
LastName = "رضایی",
Avatar = "images/avatar1.jpg",
PersonalPurchase = 2500000,
TeamPurchase = 15000000,
Children = new List<UserNode>
{
new UserNode
{
Id = 2,
FirstName = "مریم",
LastName = "احمدی",
Avatar = "images/avatar2.jpg",
PersonalPurchase = 1800000,
TeamPurchase = 8500000,
Children = new List<UserNode>
{
new UserNode
{
Id = 5,
FirstName = "سارا",
LastName = "کریمی",
PersonalPurchase = 1200000,
TeamPurchase = 3200000,
Children = new List<UserNode>
{
new UserNode
{
Id = 8,
FirstName = "نازنین",
LastName = "رضایی",
PersonalPurchase = 950000,
TeamPurchase = 2100000
},
new UserNode
{
Id = 8,
FirstName = "فرزاد",
LastName = "رضایی",
PersonalPurchase = 950000,
TeamPurchase = 2100000
}
}
},
new UserNode
{
Id = 6,
FirstName = "امیر",
LastName = "حسینی",
PersonalPurchase = 950000,
TeamPurchase = 1800000
}
}
},
new UserNode
{
Id = 3,
FirstName = "حسن",
LastName = "کریمی",
Avatar = "images/avatar3.jpg",
PersonalPurchase = 2200000,
TeamPurchase = 9200000,
Children = new List<UserNode>
{
new UserNode
{
Id = 7,
FirstName = "فاطمه",
LastName = "رضایی",
PersonalPurchase = 1350000,
TeamPurchase = 4100000
},
new UserNode
{
Id = 7,
FirstName = "آرش",
LastName = "رضایی",
PersonalPurchase = 1350000,
TeamPurchase = 4100000
}
}
},
new UserNode
{
Id = 4,
FirstName = "زهرا",
LastName = "محمدی",
Avatar = "images/avatar4.jpg",
PersonalPurchase = 1950000,
TeamPurchase = 7800000
}
}
Id = _userProfile.Id,
FirstName = _userProfile.FirstName,
LastName = _userProfile.LastName,
Mobile = _userProfile.Mobile,
Avatar = _userProfile.AvatarPath,
PersonalPurchase = 0,
TeamPurchase = 0,
Children = await GetUserChildren(userId: _userProfile.Id)
};
}
private async Task<List<UserNode>?> GetUserChildren(long userId)
{
Console.WriteLine("OK0");
if (userId != 0)
{
Console.WriteLine("OK1");
var children = await UserContract.GetAllUserByFilterAsync(request: new()
{
Filter = new()
{
ParentId = userId,
}
});
if (children?.Models?.Any() == true)
{
Console.WriteLine("OK2");
var result = new List<UserNode>();
foreach (var item in children.Models)
{
var node = new UserNode
{
Id = item.Id,
FirstName = item.FirstName,
LastName = item.LastName,
Mobile = item.Mobile,
Avatar = item.AvatarPath,
ReferralCode = item.ReferralCode,
PersonalPurchase = 0,
TeamPurchase = 0,
Children = await GetUserChildren(userId: item.Id)
};
result.Add(node);
}
return result;
}
}
return null;
}
private void ToggleExpand()
{
_isExpanded = !_isExpanded;

View File

@@ -6,11 +6,22 @@
<div class="node-connector"></div>
<div class="node-card">
<div class="node-avatar">
<MudAvatar Size="@GetAvatarSize(Level)">
<MudImage ObjectFit="ObjectFit.Cover"
ObjectPosition="ObjectPosition.Center"
Src="@(string.IsNullOrEmpty(node.Avatar) ? "images/avatar1.jpg" : node.Avatar)" />
</MudAvatar>
@if (!string.IsNullOrWhiteSpace(node.Avatar))
{
<MudAvatar Size="@GetAvatarSize(Level)">
<MudImage ObjectFit="ObjectFit.Cover"
ObjectPosition="ObjectPosition.Center"
Src="@node.Avatar" />
</MudAvatar>
}
else
{
<MudAvatar Size="@GetAvatarSize(Level)"
Color="Color.Primary"
Variant="Variant.Outlined">
@(string.IsNullOrWhiteSpace(node.FirstName) ? "N" : node.FirstName.Substring(0, 1))
</MudAvatar>
}
@if (node.Children?.Any() == true)
{
<button class="expand-btn" @onclick="() => ToggleNodeExpand(node.Id)" data-user-id="@node.Id">
@@ -19,7 +30,14 @@
}
</div>
<div class="node-info">
<div class="node-name">@node.FirstName @node.LastName</div>
@if (!string.IsNullOrWhiteSpace(node.FirstName) || !string.IsNullOrWhiteSpace(node.LastName))
{
<div class="node-name">@node.FirstName @node.LastName</div>
}
else
{
<div class="node-name">@node.Mobile</div>
}
<div class="node-amounts">
<div class="personal-amount">
<span class="label">خرید شخصی:</span>

View File

@@ -40,6 +40,8 @@ public partial class OrganizationChartLevel
public long Id { get; set; }
public string? FirstName { get; set; }
public string? LastName { get; set; }
public string? Mobile { get; set; }
public string? ReferralCode { get; set; }
public string? Avatar { get; set; }
public long? PersonalPurchase { get; set; }
public long? TeamPurchase { get; set; }

View File

@@ -136,7 +136,7 @@ public partial class Index
private async Task ShareReferralCode()
{
var shareText = $"کد دعوت من در فرصت: {_userProfile.ReferralCode}\nبرای عضویت از این لینک استفاده کنید: {Navigation.BaseUri}?ref={_userProfile.ReferralCode}";
var shareText = $"کد دعوت من در فرصت: {_userProfile.ReferralCode}\nبرای عضویت از این لینک استفاده کنید:";
try
{

View File

@@ -25,6 +25,9 @@ ValidatorOptions.Global.LanguageManager = new CustomFluentValidationLanguageMana
var appSettings = builder.Configuration.Get<AppSettings>();
UrlUtility.DownloadUrl = appSettings.DownloadUrl;
builder.Services.Configure<EncryptionSettings>(builder.Configuration.GetSection("EncryptionSettings"));
builder.Services.AddSingleton<MobileNumberEncryptor>();
var app = builder.Build();
// Configure the HTTP request pipeline.
@@ -51,3 +54,8 @@ public class AppSettings
{
public string DownloadUrl { get; set; }
}
public class EncryptionSettings
{
public string Key { get; set; }
public string IV { get; set; }
}

View File

@@ -0,0 +1,122 @@
using Microsoft.AspNetCore.DataProtection.KeyManagement;
using System.Security.Cryptography;
using System.Text;
namespace FrontOffice.Main.Utilities;
public class MobileNumberEncryptor
{
private readonly string _key;
private readonly string _iv;
public MobileNumberEncryptor(IConfiguration configuration)
{
var encryptionSettings = configuration.GetSection("EncryptionSettings").Get<EncryptionSettings>();
_key = encryptionSettings?.Key ?? throw new ArgumentNullException("Encryption Key not found in configuration");
_iv = encryptionSettings?.IV ?? throw new ArgumentNullException("Encryption IV not found in configuration");
// اعتبارسنجی سایز
ValidateKeyAndIV();
}
public MobileNumberEncryptor(string key, string iv)
{
_key = key;
_iv = iv;
ValidateKeyAndIV();
}
private void ValidateKeyAndIV()
{
try
{
byte[] keyBytes = Convert.FromBase64String(_key);
byte[] ivBytes = Convert.FromBase64String(_iv);
if (keyBytes.Length != 32)
throw new ArgumentException("Key must be 32 bytes in Base64 format");
if (ivBytes.Length != 16)
throw new ArgumentException("IV must be 16 bytes in Base64 format");
}
catch (FormatException)
{
throw new ArgumentException("Key or IV is not valid Base64 string");
}
}
public string EncryptMobileNumber(string mobileNumber)
{
if (string.IsNullOrEmpty(mobileNumber))
throw new ArgumentException("Mobile number cannot be null or empty");
try
{
byte[] key = Convert.FromBase64String(_key);
byte[] iv = Convert.FromBase64String(_iv);
byte[] plainTextBytes = Encoding.UTF8.GetBytes(mobileNumber);
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = key;
aesAlg.IV = iv;
aesAlg.Mode = CipherMode.ECB;
aesAlg.Padding = PaddingMode.PKCS7;
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
csEncrypt.Write(plainTextBytes, 0, plainTextBytes.Length);
csEncrypt.FlushFinalBlock();
byte[] encryptedBytes = msEncrypt.ToArray();
return Convert.ToBase64String(encryptedBytes);
}
}
}
}
catch (Exception ex)
{
throw new Exception($"Encryption failed: {ex.Message}", ex);
}
}
public string DecryptMobileNumber(string encryptedMobileNumber)
{
if (string.IsNullOrEmpty(encryptedMobileNumber))
throw new ArgumentException("Encrypted mobile number cannot be null or empty");
try
{
byte[] key = Convert.FromBase64String(_key);
byte[] iv = Convert.FromBase64String(_iv);
byte[] cipherTextBytes = Convert.FromBase64String(encryptedMobileNumber);
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = key;
aesAlg.IV = iv;
aesAlg.Mode = CipherMode.ECB;
aesAlg.Padding = PaddingMode.PKCS7;
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msDecrypt = new MemoryStream(cipherTextBytes))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
return srDecrypt.ReadToEnd();
}
}
}
}
}
catch (Exception ex)
{
throw new Exception($"Decryption failed: {ex.Message}", ex);
}
}
}

View File

@@ -2,6 +2,10 @@
"GwUrl": "https://fogw.kbs1.ir",
//"GwUrl": "https://localhost:34781",
"DownloadUrl": "https://dl.afrino.co",
"EncryptionSettings": {
"Key": "kmcQ3XTmH4mrdh8VHziuscyf8LLYjG//Kyni81nH/0E=",
"IV": "1wyF3Tt142MOkCpIyCxh/g=="
},
"Logging": {
"LogLevel": {
"Default": "Information",