zhengyuxuan
2025-04-02 ebd007c8948a0c1cf73b86656aeb71278917c2b3
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using LifePayment.Domain.Shared;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Caching;
using ZeroD.Util;
 
namespace LifePayment.Domain
{
    public class WxManager : IWxManager
    {
        private readonly WxMiniAppOption _wxOptions;
        private readonly InformationOption _informationOption;
        private readonly WxMatchMakingMiniAppOption _wxMatchMakingMiniAppOption;
        private readonly ICommonManager _commonManager;
        private readonly IHttpClientFactory _httpClientFactory;
        private readonly IDistributedCache<string> _distributedCache;
 
        public WxManager(
        ICommonManager commonManager,
        IHttpClientFactory httpClientFactory,
        IDistributedCache<string> distributedCache,
        IOptionsMonitor<WxMiniAppOption> wxOptionsMonitor,
        IOptionsMonitor<InformationOption> informationOptionMonitor,
        IOptionsMonitor<WxMatchMakingMiniAppOption> wxMatchMakingMiniAppOptionMonitor)
        {
            _commonManager = commonManager;
            _distributedCache = distributedCache;
            _httpClientFactory = httpClientFactory;
            _wxOptions = wxOptionsMonitor.CurrentValue;
            _informationOption = informationOptionMonitor.CurrentValue;
            _wxMatchMakingMiniAppOption = wxMatchMakingMiniAppOptionMonitor.CurrentValue;
        }
 
        public async Task<string> GetWxMiniAccessToken()
        {
            var result = await _distributedCache.GetAsync(LifePaymentConstant.小程序Token缓存key);
            if (result != null)
            {
                return result;
            }
 
            var client = _httpClientFactory.CreateClient(LifePaymentConstant.WeChatHttpClientName);
            var url = $"{_wxOptions.Url}{string.Format(LifePaymentConstant.微信小程序获取Token, _wxOptions.AppId, _wxOptions.Secret)}";
            var responseMessage = await client.GetAsync(url);
            var str = await responseMessage.Content.ReadAsStringAsync();
            var res = JsonConvert.DeserializeObject<WxMiniTokenInfo>(str);
            if (res == null || res.AccessToken.IsNullOrEmpty() || res.ExpiresIn == default)
            {
                throw new UserFriendlyException("获取小程序token失败");
            }
 
            await _distributedCache.SetAsync(LifePaymentConstant.小程序Token缓存key, res.AccessToken, new DistributedCacheEntryOptions
            {
                AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(res.ExpiresIn - 360),
            });
            return res.AccessToken;
        }
 
        public async Task<string> GetWxCgiAccessToken()
        {
            var result = await _distributedCache.GetAsync(LifePaymentConstant.微信公众号Token缓存key);
            if (result != null)
            {
                return result;
            }
 
            var client = _httpClientFactory.CreateClient(LifePaymentConstant.WeChatHttpClientName);
            var url = $"{_informationOption.RequestUrl}{string.Format(LifePaymentConstant.微信小程序获取Token, _informationOption.AppId, _informationOption.AppSecret)}";
            var responseMessage = await client.GetAsync(url);
            var str = await responseMessage.Content.ReadAsStringAsync();
            var res = JsonConvert.DeserializeObject<WxMiniTokenInfo>(str);
            if (res == null || res.AccessToken.IsNullOrEmpty() || res.ExpiresIn == default)
            {
                throw new UserFriendlyException("获取小程序token失败");
            }
 
            await _distributedCache.SetAsync(LifePaymentConstant.微信公众号Token缓存key, res.AccessToken, new DistributedCacheEntryOptions
            {
                AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(res.ExpiresIn - 360),
            });
            return res.AccessToken;
        }
 
        public async Task<string> CreateQrCodeUrl(string name, CreateQrCodeInput input)
        {
            var res = await CreateQrCode(input);
            var reslut = _commonManager.GetOssUrl(LifePaymentConstant.扫码登录二维码路径, name, res, "png");
            return reslut;
        }
 
        public async Task<byte[]> CreateQrCode(CreateQrCodeInput input)
        {
            var token = await GetWxMiniAccessToken();
            string url = $"{_informationOption.RequestUrl}{string.Format(LifePaymentConstant.微信小程序创建二维码, token)}"; ;
            return await _commonManager.PostAsync<byte[]>(url, new
            {
                page = input.Page,
                scene = input.Scene,
                env_version = input.EnvVersion,
                width = input.Width,
                check_path = input.CheckPath,
            });
        }
 
        public async Task<WxMiniAppIndentity> GetWxIndentity(string code, WxMiniAppEnum wxMiniApp)
        {
            var appInfo = GetAppInfoByEnum(wxMiniApp);
            var url = $"{appInfo.Url}{string.Format(LifePaymentConstant.微信小程序身份会话信息, appInfo.AppId, appInfo.Secret, code)}";
            var result = await _commonManager.PostAsync<WxMiniAppIndentity>(url, null);
            return result;
        }
 
 
        public async Task<WxMiniAppIndentity> GetWxOauth2AccessToken(string code)
        {
            var url = $"{_informationOption.RequestUrl}{string.Format(LifePaymentConstant.微信公众号获取access_token, _informationOption.AppId, _informationOption.AppSecret, code)}";
            var result = await _commonManager.PostAsync<WxMiniAppIndentity>(url, null);
            return result;
        }
 
        public async Task<GetCgiWritingResponse> GetWxPublishWriting()
        {
            var token = await GetWxCgiAccessToken();
            var input = new GetCgiWritingInput();
            input.AccessToken = token;
            var url = $"{_wxOptions.Url}{string.Format(LifePaymentConstant.微信接口获取公众号文章, token)}";
            var requestContent = new StringContent(JsonString.Object2Json(input), Encoding.UTF8, "application/json");
            var client = _httpClientFactory.CreateClient(LifePaymentConstant.WeChatHttpClientName);
            var responseMessage = await client.PostAsync(url, requestContent);
            var str = await responseMessage.Content.ReadAsStringAsync();
            var res = JsonConvert.DeserializeObject<GetCgiWritingResponse>(str);
            return res;
        }
 
        public string GetPhoneNumber(string encryptedDataStr, string iv, string sessionKey)
        {
            return GetUserInfo(encryptedDataStr, iv, sessionKey)?.PhoneNumber;
        }
 
        public WxMiniAppUserInfo GetUserInfo(string encryptedDataStr, string iv, string sessionKey)
        {
            var result = string.Empty;
            RijndaelManaged rijalg = new RijndaelManaged();
            rijalg.KeySize = 128;
            rijalg.Padding = PaddingMode.PKCS7;
            rijalg.Mode = CipherMode.CBC;
            rijalg.Key = Convert.FromBase64String(sessionKey);
            rijalg.IV = Convert.FromBase64String(iv);
            byte[] encryptedData = Convert.FromBase64String(encryptedDataStr);
            ICryptoTransform decryptor = rijalg.CreateDecryptor(rijalg.Key, rijalg.IV);
            using (MemoryStream msDecrypt = new MemoryStream(encryptedData))
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                    {
                        result = srDecrypt.ReadToEnd();
                    }
                }
            }
 
            return JsonConvert.DeserializeObject<WxMiniAppUserInfo>(result);
        }
 
        private (string Url, string AppId, string Secret) GetAppInfoByEnum(WxMiniAppEnum wxMiniApp)
        {
            return wxMiniApp switch
            {
                WxMiniAppEnum.行业资讯 => (_wxOptions.Url, _wxOptions.AppId, _wxOptions.Secret),
                WxMiniAppEnum.人单合一 => (_wxMatchMakingMiniAppOption.Url, _wxMatchMakingMiniAppOption.AppId, _wxMatchMakingMiniAppOption.Secret),
                WxMiniAppEnum.生活缴费 => (_wxMatchMakingMiniAppOption.Url, _wxMatchMakingMiniAppOption.AppId, _wxMatchMakingMiniAppOption.Secret),
                _ => throw new UserFriendlyException("存在未实现的WxMiniAppEnum"),
            };
        }
    }
}