schangxiang@126.com
2025-04-02 c33261384f7abd2c974aa729d322c033e0414da2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using iWare.Authorize.Dto;
using Newtonsoft.Json;
using ProjectCode;
 
namespace iWare.Authorize
{
    /// <summary>
    /// 授权帮助类
    /// </summary>
    public class AuthorizeHelper
    {
        /// <summary>
        /// 验证授权
        /// </summary>
        /// <returns></returns>
        public static AuthorizeFunRetEntity ValidateLicense(ValidateLicenseInput input)
        {
            try
            {
                //1、获取mac地址
                var macAddresses = EnDecode.GetAllMacAddresses();
                //2、找到 授权文件
                string baseDir = AppDomain.CurrentDomain.BaseDirectory;
                DirectoryInfo theFolder = new DirectoryInfo(baseDir + @"\\AuthorizeFile\\lic");
                //遍历文件,读取第一个文件
                var files = theFolder.GetFiles();
                if (files.Length == 0)
                {
                    return AuthorizeFunRetEntity.Fail("未找到授权文件");
                }
 
                var firstFile = files.First();
                //3、读取 永久授权
                string rst = EnDecode.ReadAndValidateLicenseFile(firstFile.FullName);
                if (!string.IsNullOrEmpty(rst))
                {
                    AuthorizeFunRetEntity newRet = JsonConvert.DeserializeObject<AuthorizeFunRetEntity>(rst);
                    if (EnDecode.ValidatePermanentLicense(firstFile.FullName))
                    {
                        return AuthorizeFunRetEntity.Success("成功", newRet);
                    }
                    else
                        return ValidateLicenseForDev(firstFile.FullName, input, newRet);
                }
                else
                {
                    return AuthorizeFunRetEntity.Fail("无效的授权文件");
                }
            }
            catch (Exception ex)
            {
                return AuthorizeFunRetEntity.Fail("异常:" + ex.Message);
            }
 
        }
 
        /// <summary>
        /// 验证开发授权
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        private static AuthorizeFunRetEntity ValidateLicenseForDev(string filePath, ValidateLicenseInput input, AuthorizeFunRetEntity newRet)
        {
            //读取 开发授权
            var val = EnDecode.ValidateDevLicense(filePath);
            if (val.IsValid)
            {
                if (input != null && input.WaringDays != null)
                {
                    int waringDays = (int)input.WaringDays;
                    if (waringDays >= val.RemainingDays)
                    {
                        return AuthorizeFunRetEntity.Success("成功", val.RemainingDays, $"授权剩余天数{val.RemainingDays}小于{waringDays}天");
                    }
                }
                return AuthorizeFunRetEntity.Success("成功", val.RemainingDays, newRet);
            }
            else
                return AuthorizeFunRetEntity.Fail("授权失败", val.RemainingDays, newRet);
 
        }
 
    }
}