Skip to content

Commit

Permalink
Merge pull request #6 from myvas/release/3.1
Browse files Browse the repository at this point in the history
Release/3.1
  • Loading branch information
huangxiangyao authored Dec 13, 2019
2 parents b3d46c3 + f693df6 commit e625712
Show file tree
Hide file tree
Showing 29 changed files with 968 additions and 805 deletions.
Empty file removed .tgitconfig
Empty file.
3 changes: 3 additions & 0 deletions Weixin.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_", "_", "{E9754587-13F5-4E
NuGet.Config = NuGet.Config
README.md = README.md
EndProjectSection
ProjectSection(FolderGlobals) = preProject
D_5_4Myvas_4dev_4AspNetCore_4Myvas_1AspNetCore_1Weixin_4Myvas_1AspNetCore_1Weixin_4global_1json__JsonSchema =
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{73FCFCF4-3A1C-4D4D-939A-9CABDC2341DC}"
EndProject
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"sdk": {
"version": "2.2"
"version": "3.1"
}
}
5 changes: 2 additions & 3 deletions src/Weixin.Jssdk/Myvas.AspNetCore.Weixin.Jssdk.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<TargetFrameworks>netcoreapp3.1</TargetFrameworks>
<PackageId>Myvas.AspNetCore.Weixin.Jssdk</PackageId>
<Version>2.2.403.3</Version>
<Version>2.2.403</Version>
<Version>3.1.100.5</Version>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RootNamespace>Myvas.AspNetCore.Weixin.Jssdk</RootNamespace>
<PackageTags>myvas;aspnetcore;weixin;jssdk</PackageTags>
Expand Down
24 changes: 9 additions & 15 deletions src/Weixin/AccessToken/AccessTokenApi.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using Microsoft.AspNetCore.Http.Extensions;
using System.Net.Http;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Http.Extensions;
using System.Threading.Tasks;

namespace Myvas.AspNetCore.Weixin
{
/// <summary>
/// 获取微信凭证数据服务接口
/// </summary>
/// <remarks>
/// <see cref="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140183">获取access_token接口官方说明</see>
/// </remarks>
internal static class AccessTokenApi
/// <summary>
/// 获取微信凭证数据服务接口
/// </summary>
/// <remarks>
/// <see cref="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140183">获取access_token接口官方说明</see>
/// </remarks>
internal static class AccessTokenApi
{
private const string GetAccessTokenEndpoint = "https://api.weixin.qq.com/cgi-bin/token";

Expand Down Expand Up @@ -49,7 +43,7 @@ public static async Task<AccessTokenJson> GetTokenAsync(string appid, string sec
var requestUri = GetAccessTokenEndpoint + query.ToString();

var sResult = await new HttpClient().GetStringAsync(requestUri);
AccessTokenJson result = JsonConvert.DeserializeObject<AccessTokenJson>(sResult);
AccessTokenJson result = sResult.FromJson< AccessTokenJson>();
if (result.Succeeded)
return result;
else
Expand Down
15 changes: 6 additions & 9 deletions src/Weixin/AccessToken/WeixinAccessTokenOptions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;

namespace Myvas.AspNetCore.Weixin
{
public class WeixinAccessTokenOptions : WeixinOptions
{
}
}
namespace Myvas.AspNetCore.Weixin
{
public class WeixinAccessTokenOptions : WeixinOptions
{
}
}
21 changes: 10 additions & 11 deletions src/Weixin/Apis/Media/MediaApi.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
using System;
using System.IO;
using System.Text;
using Newtonsoft.Json;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

namespace Myvas.AspNetCore.Weixin
{

/// <summary>
/// 多媒体文件接口
/// </summary>
public static partial class MediaApi
/// <summary>
/// 多媒体文件接口
/// </summary>
public static partial class MediaApi
{
/// <summary>
/// 上传媒体文件。
Expand All @@ -38,7 +37,7 @@ public static async Task<MediaUploadResultJson> Upload(string accessToken, Uploa
byte[] responseData = await httpResponseMessage.Content.ReadAsByteArrayAsync();
string resp = Encoding.UTF8.GetString(responseData);

var json = JsonConvert.DeserializeObject<MediaUploadResultJson>(resp);
var json = resp.FromJson<MediaUploadResultJson>();

return json;
}
Expand All @@ -56,7 +55,7 @@ public static async Task<MaterialCountJson> GetMaterialCount(string accessToken)
throw new HttpRequestException("An error occured while retrieving the user profile.");
}

var payload = JObject.Parse(await response.Content.ReadAsStringAsync());
var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
int errorCode = WeixinApiHelper.GetErrorCode(payload);
if (errorCode != 0)
{
Expand Down Expand Up @@ -103,15 +102,15 @@ public static async Task<BatchGetMaterialsJson> BatchGetMaterials(string accessT
}

var responseString = await response.Content.ReadAsStringAsync();
var payload = JObject.Parse(responseString);
var payload = JsonDocument.Parse(responseString);
int errorCode = WeixinApiHelper.GetErrorCode(payload);
if (errorCode != 0)
{
var errorMessage = WeixinApiHelper.GetErrorMessage(payload);
//Logger.LogError($"The remote server returned an error while retrieving the user profile. {errorCode} {errorMessage}");
throw new Exception($"The remote server returned an error while retrieving the user profile. {errorCode} {errorMessage}");
}
var result = JsonConvert.DeserializeObject<BatchGetMaterialsJson>(responseString);
var result = responseString.FromJson<BatchGetMaterialsJson>();
return result;
}

Expand Down
19 changes: 8 additions & 11 deletions src/Weixin/Apis/Media/MediaApiHelper.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
using System.Text.Json;

namespace Myvas.AspNetCore.Weixin
{

public static class MediaApiHelper
public static class MediaApiHelper
{
public static string ExtractFileExtFromDisposition(string disposition)
{
Expand All @@ -22,16 +19,16 @@ public static string ExtractFileExtFromDisposition(string disposition)
/// <summary>
/// openid
/// </summary>
public static bool GetAllCounts(JObject payload, out int voiceCount, out int videoCount, out int imageCount, out int newsCount)
public static bool GetAllCounts(JsonDocument payload, out int voiceCount, out int videoCount, out int imageCount, out int newsCount)
{
if (payload == null)
{
throw new ArgumentNullException(nameof(payload));
}
voiceCount = payload.Value<int>("voice_count");
videoCount = payload.Value<int>("video_count");
imageCount = payload.Value<int>("image_count");
newsCount = payload.Value<int>("news_count");
voiceCount = payload.RootElement.GetInt32("voice_count");
videoCount = payload.RootElement.GetInt32("video_count");
imageCount = payload.RootElement.GetInt32("image_count");
newsCount = payload.RootElement.GetInt32("news_count");
return true;
}
}
Expand Down
17 changes: 5 additions & 12 deletions src/Weixin/Apis/Media/Models/MaterialCountJson.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
using System;
using System.IO;
using System.Text;
using Newtonsoft.Json;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Myvas.AspNetCore.Weixin
{
public class MaterialCountJson
{
[JsonProperty("video_count")]
[JsonPropertyName("video_count")]
public int VideoCount { get; set; }
[JsonProperty("voice_count")]
[JsonPropertyName("voice_count")]
public int VoiceCount { get; set; }
[JsonProperty("image_count")]
[JsonPropertyName("image_count")]
public int ImageCount { get; set; }
[JsonProperty("news_count")]
[JsonPropertyName("news_count")]
public int NewsCount { get; set; }
}
}
14 changes: 7 additions & 7 deletions src/Weixin/Apis/Media/WeixinApiHelper.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
using Newtonsoft.Json.Linq;
using System;
using System;
using System.Text.Json;

namespace Myvas.AspNetCore.Weixin
{
public static class WeixinApiHelper
public static class WeixinApiHelper
{
#region 错误时微信会返回含有两个字段的JSON数据包
/// <summary>
/// errcode
/// </summary>
public static int GetErrorCode(JObject payload)
public static int GetErrorCode(JsonDocument payload)
{
if (payload == null)
{
throw new ArgumentNullException(nameof(payload));
}
return payload.Value<int?>("errcode") ?? 0;
return payload.RootElement.GetInt32("errcode", 0);
}

/// <summary>
/// errmsg
/// </summary>
public static string GetErrorMessage(JObject payload)
public static string GetErrorMessage(JsonDocument payload)
{
if (payload == null)
{
throw new ArgumentNullException(nameof(payload));
}
return payload.Value<string>("errmsg");
return payload.GetString("errmsg");
}
#endregion
}
Expand Down
Loading

0 comments on commit e625712

Please sign in to comment.