Kaynağa Gözat

信访信件提交成功

lqq 8 yıl önce
ebeveyn
işleme
19fefcda7e

+ 6 - 4
Winsoft.GOV.XF.WebApi.WXCore/Controllers/BundleController.cs

@@ -93,7 +93,7 @@ namespace Winsoft.GOV.XF.WebApi.WXCore.Controllers
             BundleAccessToken a = JsonConvert.DeserializeObject<BundleAccessToken>(data);
             if (a == null)
                 return BadRequest("非法请求");
-            string multimediaURL = _bundlesService.GenerateShareURL(b, a);
+            string multimediaURL = _bundlesService.GenerateShareURL(b, a, HttpContext.Request.Cookies["BaseUrl"]);
             if (!String.IsNullOrEmpty(multimediaURL))
                 return Ok(multimediaURL);
             return BadRequest("无权限发布");
@@ -122,7 +122,7 @@ namespace Winsoft.GOV.XF.WebApi.WXCore.Controllers
         [Produces(typeof(string))]
         [WeixinInternalRequest("访问被拒绝,请通过微信客户端访问!", "nofilter")]
         [WXOAuthCheck(appId: null, oauthCallbackUrl: "api/Auth")]
-        public async Task<IActionResult> Post([FromBody]Bundle value)
+        public async Task<IActionResult> Post([FromBody] Bundle value)
         {
             foreach(Asset a in value.Assets)
             {
@@ -138,11 +138,13 @@ namespace Winsoft.GOV.XF.WebApi.WXCore.Controllers
             value.County_id = "004038694b4540c4b218cb22d011e10e";
             value.Unit_id = "2014101416995276299";
             value.OpenID = HttpContext.Session.GetString("OpenId");
-            if (await _xfApiService.PostLetter(value, e => failsMessage = e))
+            await _bundlesService.Add(value);
+            if (await _xfApiService.PostLetter(value, HttpContext.Request.Cookies["BaseUrl"], e => failsMessage = e))
             {
-                await _bundlesService.Add(value);
+                
                 return Ok();
             }
+            await _bundlesService.Delete(value);
             return BadRequest(failsMessage);
         }
 

+ 2 - 2
Winsoft.GOV.XF.WebApi.WXCore/Controllers/HomeController.cs

@@ -33,8 +33,8 @@ namespace Winsoft.GOV.XF.WebApi.WXCore.Controllers
             
             if (u != null)
                 HttpContext.Response.Cookies.Append("User", JsonConvert.SerializeObject(u));
-            HttpContext.Response.Cookies.Append("BaseUrl", HttpContext.Request.GetAbsoluteUri() + "");
-            return Redirect(HttpContext.Request.GetAbsoluteUri()+ "/dist/web12345/index.html");
+            HttpContext.Response.Cookies.Append("BaseUrl", HttpContext.Request.GetAbsoluteUri());
+            return Redirect(HttpContext.Request.GetAbsoluteUri()+ "dist/web12345/index.html");
         }
 
 

+ 66 - 39
Winsoft.GOV.XF.WebApi.WXCore/Helpers/Utilities.cs

@@ -38,68 +38,95 @@ namespace Winsoft.GOV.XF.WebApi.WXCore.Helpers
         public static string AESEncrypt(this string input, string key)
         {
 
-            var encryptKey = Encoding.UTF8.GetBytes(key);
+                byte[] bytesToBeEncrypted = Encoding.UTF8.GetBytes(input);
+                byte[] passwordBytes = Encoding.UTF8.GetBytes(key);
 
-            using (var aesAlg = Aes.Create())
-            {
-                using (var encryptor = aesAlg.CreateEncryptor(encryptKey, aesAlg.IV))
-                {
-                    using (var msEncrypt = new MemoryStream())
-                    {
-                        using (var csEncrypt = new CryptoStream(msEncrypt, encryptor,
-                            CryptoStreamMode.Write))
+                passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
+
+                byte[] bytesEncrypted = AESEncryptBytes(bytesToBeEncrypted, passwordBytes);
+
+                string result = Convert.ToBase64String(bytesEncrypted);
+
+                return result;
+        }
 
-                        using (var swEncrypt = new StreamWriter(csEncrypt))
-                        {
-                            swEncrypt.Write(input);
-                        }
+        private static byte[] AESEncryptBytes(byte[] bytesToBeEncrypted, byte[] passwordBytes)
+        {
+            byte[] encryptedBytes = null;
 
-                        var iv = aesAlg.IV;
+            var saltBytes = new byte[9] { 13, 34, 27, 67, 189, 255, 104, 219, 122 };
 
-                        var decryptedContent = msEncrypt.ToArray();
+            using (var ms = new MemoryStream())
+            {
+                using (var AES = new RijndaelManaged())
+                {
+                    AES.KeySize = 256;
+                    AES.BlockSize = 128;
 
-                        var result = new byte[iv.Length + decryptedContent.Length];
+                    var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
+                    AES.Key = key.GetBytes(32);
+                    AES.IV = key.GetBytes(16);
 
-                        Buffer.BlockCopy(iv, 0, result, 0, iv.Length);
-                        Buffer.BlockCopy(decryptedContent, 0, result,
-                            iv.Length, decryptedContent.Length);
+                    AES.Mode = CipherMode.CBC;
 
-                        return Convert.ToBase64String(result);
+                    using (var cs = new CryptoStream(ms, AES.CreateEncryptor(),
+                        CryptoStreamMode.Write))
+                    {
+                        cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
+                        cs.Close();
                     }
+
+                    encryptedBytes = ms.ToArray();
                 }
             }
+
+            return encryptedBytes;
         }
         public static string AESDecrypt(this string input, string key)
         {
-            var fullCipher = Convert.FromBase64String(input);
+            byte[] bytesToBeDecrypted = Convert.FromBase64String(input);
+
+            byte[] passwordBytes = Encoding.UTF8.GetBytes(key);
+
+            passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
+
+            byte[] bytesDecrypted = AESDecryptBytes(bytesToBeDecrypted, passwordBytes);
+
+            string result = Encoding.UTF8.GetString(bytesDecrypted);
+
+            return result;
+        }
 
-            var iv = new byte[16];
-            var cipher = new byte[16];
+        public static byte[] AESDecryptBytes(byte[] bytesToBeDecrypted, byte[] passwordBytes)
+        {
+            byte[] decryptedBytes = null;
 
-            Buffer.BlockCopy(fullCipher, 0, iv, 0, iv.Length);
-            Buffer.BlockCopy(fullCipher, iv.Length, cipher, 0, iv.Length);
-            var decryptKey = Encoding.UTF8.GetBytes(key);
+            var saltBytes = new byte[9] { 13, 34, 27, 67, 189, 255, 104, 219, 122 };
 
-            using (var aesAlg = Aes.Create())
+            using (var ms = new MemoryStream())
             {
-                using (var decryptor = aesAlg.CreateDecryptor(decryptKey, iv))
+                using (var AES = new RijndaelManaged())
                 {
-                    string result;
-                    using (var msDecrypt = new MemoryStream(cipher))
+                    AES.KeySize = 256;
+                    AES.BlockSize = 128;
+
+                    var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
+                    AES.Key = key.GetBytes(32);
+                    AES.IV = key.GetBytes(16);
+
+                    AES.Mode = CipherMode.CBC;
+
+                    using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
                     {
-                        using (var csDecrypt = new CryptoStream(msDecrypt,
-                            decryptor, CryptoStreamMode.Read))
-                        {
-                            using (var srDecrypt = new StreamReader(csDecrypt))
-                            {
-                                result = srDecrypt.ReadToEnd();
-                            }
-                        }
+                        cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
+                        cs.Close();
                     }
 
-                    return result;
+                    decryptedBytes = ms.ToArray();
                 }
             }
+
+            return decryptedBytes;
         }
 
         /// <summary>

+ 1 - 6
Winsoft.GOV.XF.WebApi.WXCore/Models/Bundle.cs

@@ -33,7 +33,7 @@ namespace Winsoft.GOV.XF.WebApi.WXCore.Models
         public string SearchCode { get; set; }
         [JsonConverter(typeof(ChinaDateTimeConverter))]
         public DateTime CreateDate { get; set; }
-
+        [JsonIgnore]
         public string OpenID { get; set; }
 
         public Dictionary<string, string> ToDictionary()
@@ -52,11 +52,6 @@ namespace Winsoft.GOV.XF.WebApi.WXCore.Models
             d.Add("ly", "2");
             return d;
         }
-
-        public override string ToString()
-        {
-            
-        }
     }
     //丽水市 331100000000
     //开发区 331101000000

+ 20 - 14
Winsoft.GOV.XF.WebApi.WXCore/Services/BundlesService.cs

@@ -13,11 +13,10 @@ using Newtonsoft.Json;
 namespace Winsoft.GOV.XF.WebApi.WXCore.Services
 {
     public class BundlesService : BaseService
-    {
-        HttpContext _httpContext;
-        public BundlesService(XFDbContext context, ILoggerFactory loggerFactory, HttpContext httpContext):base(context, loggerFactory)
+    {       
+        public BundlesService(XFDbContext context, ILoggerFactory loggerFactory):base(context, loggerFactory)
         {
-            _httpContext = httpContext;
+            
         }
         public async Task<IEnumerable<Bundle>> GetByUserId(int userId)
         {
@@ -35,9 +34,10 @@ namespace Winsoft.GOV.XF.WebApi.WXCore.Services
             return null;
         }
 
-        public string GetURL(Bundle b)
+        public string GetURL(Bundle b, string absoluteUri)
         {
-            string s = _httpContext.Request.GetAbsoluteUri() + "/dist/web12345-pageView/index.html#!/pageView?bid={0}&m={1}";
+            //_httpContext.Request.GetAbsoluteUri()
+            string s = absoluteUri + "/dist/web12345-pageView/index.html#!/pageView?bid={0}&m={1}";
             s = String.Format(s, b.Id.ToString(), b.Id.ToString().AESEncrypt(b.Key));
             return s;
         }
@@ -64,6 +64,14 @@ namespace Winsoft.GOV.XF.WebApi.WXCore.Services
             }
             return String.Empty;
         }
+        public async Task Delete(Bundle b)
+        {
+            if (b != null)
+            {
+                _context.Bundles.Remove(b);
+                await _context.SaveChangesAsync();
+            }
+        }
 
         public void Update(Bundle b)
         {
@@ -73,27 +81,25 @@ namespace Winsoft.GOV.XF.WebApi.WXCore.Services
             }
         }
 
-        public string GenerateBaseURL(Bundle b)
+        public string GenerateBaseURL(Bundle b, string absoluteUri)
         {
             BundleAccessToken a = new BundleAccessToken()
             {
                 Expire = -1,
                 BundleId = b.Id
             };
-            string multimediaURL = string.Format("{0}://{1}/multimedia?bundleId={2}&ciphertext={3}",
-                _httpContext.Request.Scheme,
-                _httpContext.Request.Host.Value, b.Id, JsonConvert.SerializeObject(a).AESEncrypt(b.Key));
+            string multimediaURL = string.Format("{0}dist/multimedia/index.html?bundleId={1}&ciphertext={2}",
+                absoluteUri, b.Id, JsonConvert.SerializeObject(a).AESEncrypt(b.Key));
             return multimediaURL;
         }
 
-        public string GenerateShareURL(Bundle b, BundleAccessToken a)
+        public string GenerateShareURL(Bundle b, BundleAccessToken a, string absoluteUri)
         {
             if (a.Expire != -1)
             {
                 a.Expire = DateTime.Now.AddDays(1).ToTimeStamp();
-                string multimediaURL = string.Format("{0}://{1}/multimedia?bundleId={2}&data={3}",
-                    _httpContext.Request.Scheme,
-                    _httpContext.Request.Host.Value, b.Id, JsonConvert.SerializeObject(a).AESEncrypt(b.Key));
+                string multimediaURL = string.Format("{0}dist/multimedia/index.html?bundleId={1}&data={2}",
+                    absoluteUri, b.Id, JsonConvert.SerializeObject(a).AESEncrypt(b.Key));
                 return multimediaURL;
             }
             return String.Empty;

+ 4 - 4
Winsoft.GOV.XF.WebApi.WXCore/Services/XFApiService.cs

@@ -28,16 +28,16 @@ namespace Winsoft.GOV.XF.WebApi.WXCore.Services
         /// <param name="b">信件内容</param>
         /// <param name="onFails">失败回调</param>
         /// <returns></returns>
-        public async Task<bool> PostLetter(Bundle b, OnFails onFails)
+        public async Task<bool> PostLetter(Bundle b, string absoluteUri, OnFails onFails)
         {
             string d = b.Describe;
             if (b.Assets.Count() > 0)
-                d += "(此信件内容含有其他多媒体信息,详细请复制网址到浏览器打开:" + _bundlesService.GenerateBaseURL(b) + ")";
+                d += "(此信件内容含有其他多媒体信息,详细请复制网址到浏览器打开:" + _bundlesService.GenerateBaseURL(b, absoluteUri) + ")";
             string s = "advice={0};type={1};name={2};phone={3};is_public={4};content={5};title={6};county_id={7};unit_id={8};sswz=8;ly=2; user_id={9}";
-            s = String.Format(s, b.Advice, b.Type.ToString(), b.Name, b.Mobile, b.IsPublic.ToString(), b.Describe, b.Title, b.County_id, b.Unit_id, b.OpenID);
+            s = String.Format(s, b.Advice, b.Type.ToString(), b.Name, b.Mobile, b.IsPublic.ToString(), d, b.Title, b.County_id, b.Unit_id, b.OpenID);
             
 
-            XFApiResult rObj = await HttpPostAsync<XFApiResult>("http://118.178.118.50/test1/letter/submitLetter;", b.ToString(), onFails);
+            XFApiResult rObj = await HttpPostAsync<XFApiResult>("http://118.178.118.50/test1/letter/submitLetter;", s, onFails);
 
             if (rObj == null)
                 return false;

BIN
Winsoft.GOV.XF.WebApi.WXCore/wwwroot/images/2017/1212/20171212203628776.png


BIN
Winsoft.GOV.XF.WebApi.WXCore/wwwroot/images/2017/1212/20171212204003076.png