hwh
2024-06-21 158a01ee5fa6903111790c52bfb778f93f797028
Wms/Wms/Controllers/UpApiController.cs
@@ -15,6 +15,11 @@
using WMS.DAL;
using Microsoft.AspNetCore.Authorization;
using Utility;
using System.Collections.Generic;
using System.Reflection.Emit;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
namespace Wms.Controllers
{
@@ -464,24 +469,45 @@
        }
        [HttpGet]
        public IActionResult Demo3()
        [ServiceFilter(typeof(ApiResponseActionFilter))]
        public string Demo3()
        {
            var result = new ErpModel { Success = -1, Message = "" };
            try
            {
                var time = DateTimeOffset.Now.ToUnixTimeSeconds();
                result = new ErpModel { Success = 0, Message = time.ToString() };
            string jsonParams = "{\"param1\": \"value1\", \"param2\": \"value2\"}";
                return Ok(result);
            }
            catch (Exception e)
            {
                result.Message = e.Message;
                return Ok(result);
            }
            // 生成时间戳(Unix 时间戳)
            var timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString();
            // 生成签名
            var signature = GenerateSignature(jsonParams);
            // 输出签名
            Console.WriteLine("Generated Signature: " + signature);
            return signature;
        }
        private static string GenerateSignature(string jsonParams)
        {
            var appKey = "90170307d4184844ac2a26b431f79980";
            // 将 JSON 字符串转换为 JObject
            JObject jObject = JObject.Parse(jsonParams);
            // 过滤掉数组类型的属性
            var filteredProperties = jObject.Properties()
                .Where(p => p.Value.Type != JTokenType.Array)
                .ToDictionary(p => p.Name, p => p.Value.ToString());
            // 添加时间戳(Unix 时间戳)
            var timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString();
            filteredProperties.Add("timestamp", timestamp);
            filteredProperties.Add("appKey", appKey);
            // 构建待签名字符串
            var signatureBaseString = string.Join("&", filteredProperties.OrderBy(p => p.Key).Select(p => p.Key + "=" + p.Value));
            // 计算 MD5 值
            var computedSignature = Md5Tools.CalcMd5(signatureBaseString);
            return computedSignature;
        }
        #endregion
    }
}