diff --git a/SolarPower/Controllers/MyBaseController.cs b/SolarPower/Controllers/MyBaseController.cs index c479007..31afcb1 100644 --- a/SolarPower/Controllers/MyBaseController.cs +++ b/SolarPower/Controllers/MyBaseController.cs @@ -88,24 +88,38 @@ namespace SolarPower.Controllers myUser = userRepository.GetMyUserInfoByAccount(myAccount); myUser.Company = companyRepository.GetMyCompanyInfoById(myUser.CompanyId); - //判斷該檔案是否存在 - if (!string.IsNullOrEmpty(myUser.Company.Logo)) + //判斷該使用者是否有個人專屬Logo + if (!string.IsNullOrEmpty(myUser.Logo)) { - var fullFilePath = Directory.GetCurrentDirectory() + "/wwwroot/" + Path.Combine("upload", "company_logo", myUser.Company.Logo); - if (!System.IO.File.Exists(fullFilePath)) + var fullFilePath = Directory.GetCurrentDirectory() + "/wwwroot/" + Path.Combine("upload", "company_logo", myUser.Logo); + if (System.IO.File.Exists(fullFilePath)) { - myUser.Company.Logo = "/img/logo.png"; - } - else - { - myUser.Company.Logo = "/" + Path.Combine("upload", "company_logo", myUser.Company.Logo); + myUser.Logo = "/" + Path.Combine("upload", "company_logo", myUser.Logo); } } else { - myUser.Company.Logo = "/img/logo.png"; + //判斷該檔案是否存在 + if (!string.IsNullOrEmpty(myUser.Company.Logo)) + { + var fullFilePath = Directory.GetCurrentDirectory() + "/wwwroot/" + Path.Combine("upload", "company_logo", myUser.Company.Logo); + if (!System.IO.File.Exists(fullFilePath)) + { + myUser.Company.Logo = "/img/logo.png"; + } + else + { + myUser.Company.Logo = "/" + Path.Combine("upload", "company_logo", myUser.Company.Logo); + } + } + else + { + myUser.Company.Logo = "/img/logo.png"; + } } + + myUser.Role = roleRepository.GetMyRoleInfoById(myUser.RoleId); List auth_arr = new List(); diff --git a/SolarPower/Controllers/UserController.cs b/SolarPower/Controllers/UserController.cs index dacaa37..834c68e 100644 --- a/SolarPower/Controllers/UserController.cs +++ b/SolarPower/Controllers/UserController.cs @@ -13,6 +13,7 @@ using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; +using System.IO; using System.Linq; using System.Threading.Tasks; @@ -26,6 +27,8 @@ namespace SolarPower.Controllers private readonly IPowerStationRepository powerStationRepository; private readonly IRoleRepository roleRepository; private string logoPath = "/upload/company_logo/"; + private string logoSaveAsPath = ""; + public UserController(IUserRepository userRepository, ISendEmailService sendEmailService, IPowerStationRepository powerStationRepository, @@ -35,6 +38,8 @@ namespace SolarPower.Controllers this.sendEmailService = sendEmailService; this.powerStationRepository = powerStationRepository; this.roleRepository = roleRepository; + + logoSaveAsPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "upload", "company_logo"); } public IActionResult Index() @@ -320,6 +325,8 @@ namespace SolarPower.Controllers var newPassword = edFunction.GetSHA256Encryption(random_password); + + user = new User() { CompanyId = post.CompanyId, @@ -344,7 +351,37 @@ namespace SolarPower.Controllers "CreatedBy", }; - await userRepository.AddAsync(user, properties); + var id = await userRepository.AddOneAsync(user, properties); + + #region 處理公司Logo圖片 + if (post.LogoFile != null) + { + var split = post.LogoFile.FileName.Split("."); + var fileName = "user_" + id + "." + split[split.Length - 1]; + + var fullPath = Path.Combine(logoSaveAsPath, fileName); + + using (var stream = new FileStream(fullPath, FileMode.Create)) + { + post.LogoFile.CopyTo(stream); + } + + UpdateUser updateUser = new UpdateUser() + { + Id = id, + Logo = fileName + }; + + properties = new List() + { + "Id", + "Logo" + }; + + await userRepository.UpdatePersonInfo(updateUser, properties); + } + #endregion + var sendSubject = "新增帳號成功"; var sendContent = $"您的新密碼為:{random_password}"; @@ -365,7 +402,7 @@ namespace SolarPower.Controllers #region 修改使用者 UpdateUser update = new UpdateUser() { - Id = post.Id, + Id = user.Id, Name = post.Name, Email = post.Email, Phone = post.Phone, @@ -386,6 +423,35 @@ namespace SolarPower.Controllers await userRepository.UpdatePersonInfo(update, properties); + #region 處理公司Logo圖片 + if (post.LogoFile != null) + { + var split = post.LogoFile.FileName.Split("."); + var fileName = "user_" + user.Id + "." + split[split.Length - 1]; + + var fullPath = Path.Combine(logoSaveAsPath, fileName); + + using (var stream = new FileStream(fullPath, FileMode.Create)) + { + post.LogoFile.CopyTo(stream); + } + + UpdateUser updateUser = new UpdateUser() + { + Id = user.Id, + Logo = fileName + }; + + properties = new List() + { + "Id", + "Logo" + }; + + await userRepository.UpdatePersonInfo(updateUser, properties); + } + #endregion + apiResult.Code = "0000"; apiResult.Msg = "儲存成功"; #endregion diff --git a/SolarPower/DBSchema/solar_power_schema.sql b/SolarPower/DBSchema/solar_power_schema.sql index 8f74a13..9f5f6f6 100644 --- a/SolarPower/DBSchema/solar_power_schema.sql +++ b/SolarPower/DBSchema/solar_power_schema.sql @@ -2199,6 +2199,10 @@ ALTER TABLE `operation_firm` ADD COLUMN `TaxIDNumber` VARCHAR(8) NULL DEFAULT NULL COMMENT '統一編號' AFTER `Email`, ADD COLUMN `Remark` VARCHAR(255) NULL DEFAULT NULL COMMENT '備註' AFTER `TaxIDNumber`; +-- 使用者加入個人公司logo 20210911 +ALTER TABLE `user` + ADD COLUMN `Logo` VARCHAR(100) NULL DEFAULT NULL COMMENT '公司logo' COLLATE 'utf8mb4_unicode_ci' AFTER `Email`; + /*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */; /*!40014 SET FOREIGN_KEY_CHECKS=IFNULL(@OLD_FOREIGN_KEY_CHECKS, 1) */; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; diff --git a/SolarPower/Models/MyBaseModel.cs b/SolarPower/Models/MyBaseModel.cs index b176946..179e2f1 100644 --- a/SolarPower/Models/MyBaseModel.cs +++ b/SolarPower/Models/MyBaseModel.cs @@ -49,6 +49,7 @@ namespace SolarPower.Models public int CompanyId { get; set; } //公司編號 public int RoleId { get; set; } //角色編號 public string Email { get; set; } + public string Logo { get; set; } public MyCompany Company { get; set; } //公司資訊 public MyRole Role { get; set; } //角色資訊 public List myPowerStationGroupByCities { get; set; } diff --git a/SolarPower/Models/User.cs b/SolarPower/Models/User.cs index 61ac577..2d8de94 100644 --- a/SolarPower/Models/User.cs +++ b/SolarPower/Models/User.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.AspNetCore.Http; +using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -38,6 +39,7 @@ namespace SolarPower.Models.User public string Phone { get; set; } //手機 public int RoleId { get; set; } //角色編號 public string Tel { get; set; } //市話 + public string Logo { get; set; } } /// @@ -87,6 +89,7 @@ namespace SolarPower.Models.User public string Email { get; set; } //信箱 public string Phone { get; set; } //手機 public int RoleId { get; set; } //角色編號 + public string Logo { get; set; } } /// @@ -129,6 +132,7 @@ namespace SolarPower.Models.User public string Account { get; set; } //帳號 public int RoleId { get; set; } //角色編號 public string Phone { get; set; } //手機 + public IFormFile LogoFile { get; set; } //個人logo } public class UserSelectItemList diff --git a/SolarPower/Views/PowerStation/PowerStationEdit.cshtml b/SolarPower/Views/PowerStation/PowerStationEdit.cshtml index 09d0aed..38a9bf5 100644 --- a/SolarPower/Views/PowerStation/PowerStationEdit.cshtml +++ b/SolarPower/Views/PowerStation/PowerStationEdit.cshtml @@ -2726,14 +2726,21 @@ myDropzone.processQueue(); }); - myDropzone.on("sending", function (file, xhr, data) { - if ((countPowerStationImage + myDropzone.files.length) > 5) { + + myDropzone.on("sendingmultiple", function (file, xhr, data) { + temp_count = countPowerStationImage + myDropzone.files.length; + if (temp_count > 5) { toast_warning("圖片總數量不可超過 5 張"); - myDropzone.removeFile(file); + file.forEach(function (item) { + myDropzone.removeFile(item); + }); return; - } else { + } + else { data.append("PowerStationId", stationId); - data.append("StationImages", file); + file.forEach(function (item) { + data.append("StationImages", item); + }); } }); @@ -2865,8 +2872,8 @@ url: "/PowerStation/SavePowerStationSingleLine", acceptedFiles: "image/*", autoProcessQueue: false, - parallelUploads: 1, - maxFiles: 1, + parallelUploads: 5, + maxFiles: 5, addRemoveLinks: true, uploadMultiple: true, dictRemoveFile: "移除", @@ -2877,14 +2884,20 @@ myDropzone.processQueue(); }); - myDropzone.on("sending", function (file, xhr, data) { - if ((countPowerStationSingleLine + myDropzone.files.length) > 1) { - toast_warning("請先刪除原本圖片"); - myDropzone.removeFile(file); + myDropzone.on("sendingmultiple", function (file, xhr, data) { + temp_count = countPowerStationSingleLine + myDropzone.files.length; + if (temp_count > 5) { + toast_warning("圖片總數量不可超過 5 張"); + file.forEach(function (item) { + myDropzone.removeFile(item); + }); return; - } else { + } + else { data.append("PowerStationId", stationId); - data.append("SingleLineImages", file); + file.forEach(function (item) { + data.append("SingleLineImages", item); + }); } }); @@ -2934,7 +2947,6 @@ Swal.fire({ title: "刪除", text: "你確定是否刪除此筆資料?", - type: "warning", icon: 'warning', showCancelButton: true, confirmButtonText: "是", @@ -2945,7 +2957,8 @@ var url = "/PowerStation/DeletePowerStationSingleLine"; var send_data = { - Id: selectedImageId + SelectedId: selectedImageId, + PowerStationId: stationId } $.post(url, send_data, function (rel) { diff --git a/SolarPower/Views/Shared/_Layout.cshtml b/SolarPower/Views/Shared/_Layout.cshtml index 2e6e05c..731544d 100644 --- a/SolarPower/Views/Shared/_Layout.cshtml +++ b/SolarPower/Views/Shared/_Layout.cshtml @@ -109,7 +109,11 @@