From d39fa25af0398d4c2a8bc71cba5c936214cce500 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=AE=97=E7=A5=9E?= Date: Sun, 10 Sep 2023 19:17:57 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E4=B8=80=E4=B8=AANotebook=E7=9A=84sample?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Samples/All/notebook/README.md | 7 + Samples/All/notebook/sample.ipynb | 249 ++++++++++++++++++++++++++++++ Samples/All/notebook/senparc.json | 21 +++ 3 files changed, 277 insertions(+) create mode 100644 Samples/All/notebook/README.md create mode 100644 Samples/All/notebook/sample.ipynb create mode 100644 Samples/All/notebook/senparc.json diff --git a/Samples/All/notebook/README.md b/Samples/All/notebook/README.md new file mode 100644 index 0000000000..eb11bf110d --- /dev/null +++ b/Samples/All/notebook/README.md @@ -0,0 +1,7 @@ +# 使用说明 +1. 安装`Visual Studio Code` +2. 安装并配置扩展`Polyglot Notebooks` +3. 修改`senparc.json`配置文件的内容 +4. 打开`sample.ipynb`文件修改`注册盛派SDK`节中的json文件的绝对路径 +5. 视个人需求调整代码 +6. Enjoy!!! \ No newline at end of file diff --git a/Samples/All/notebook/sample.ipynb b/Samples/All/notebook/sample.ipynb new file mode 100644 index 0000000000..4ef1622914 --- /dev/null +++ b/Samples/All/notebook/sample.ipynb @@ -0,0 +1,249 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 引用Nuget包" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "dotnet_interactive": { + "language": "csharp" + }, + "polyglot_notebook": { + "kernelName": "csharp" + }, + "vscode": { + "languageId": "polyglot-notebook" + } + }, + "outputs": [ + { + "data": { + "text/html": [ + "
Installed Packages
  • Microsoft.Extensions.Caching.Memory, 6.0.0
  • Microsoft.Extensions.Configuration.Json, 6.0.0
  • Senparc.Weixin.Cache.Redis, 2.15.13
  • Senparc.Weixin.MP, 16.19.3
  • Senparc.Weixin.WxOpen, 3.16.3
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "#r \"nuget:Senparc.Weixin.WxOpen\"\n", + "#r \"nuget:Senparc.Weixin.Cache.Redis\"\n", + "#r \"nuget:Senparc.Weixin.MP\"\n", + "#r \"nuget:Microsoft.Extensions.Caching.Memory,6.0.0\"\n", + "#r \"nuget:Microsoft.Extensions.Configuration.Json,6.0.0\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 引用命名空间" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "dotnet_interactive": { + "language": "csharp" + }, + "polyglot_notebook": { + "kernelName": "csharp" + }, + "vscode": { + "languageId": "polyglot-notebook" + } + }, + "outputs": [], + "source": [ + "using Senparc.CO2NET;\n", + "using Senparc.CO2NET.RegisterServices;\n", + "using Senparc.Weixin.Cache.Redis;\n", + "using Senparc.Weixin.Entities;\n", + "using Senparc.Weixin;\n", + "using Senparc.Weixin.MP;\n", + "using Senparc.Weixin.WxOpen;\n", + "using Microsoft.Extensions.DependencyInjection;\n", + "using Microsoft.Extensions.Configuration;\n", + "using Senparc.Weixin.WxOpen.AdvancedAPIs.Template;\n", + "using Senparc.Weixin.MP.AdvancedAPIs.TemplateMessage;" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 注册盛派SDK" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "dotnet_interactive": { + "language": "csharp" + }, + "polyglot_notebook": { + "kernelName": "csharp" + }, + "vscode": { + "languageId": "polyglot-notebook" + } + }, + "outputs": [], + "source": [ + "// 关闭盛派APM\n", + "Senparc.CO2NET.APM.Config.EnableAPM = false;\n", + "// 不抛出异常\n", + "Senparc.Weixin.Config.ThrownWhenJsonResultFaild = false;\n", + "\n", + "var configBuilder = new ConfigurationBuilder();\n", + "// 此处需要使用配置文件的绝对路径\n", + "configBuilder.AddJsonFile(\"/Volumes/SSD/Source/PythonProjects/senparc.json\", false, false);\n", + "\n", + "var config = configBuilder.Build();\n", + "\n", + "var senparcSetting = new SenparcSetting();\n", + "var senparcWeixinSetting = new SenparcWeixinSetting();\n", + "\n", + "config.GetSection(\"SenparcSetting\").Bind(senparcSetting);\n", + "config.GetSection(\"SenparcWeixinSetting\").Bind(senparcWeixinSetting);\n", + "\n", + "var services = new ServiceCollection();\n", + "services.AddSenparcGlobalServices(config);//Senparc.CO2NET 全局注册\n", + "\n", + "IRegisterService register = RegisterService.Start(senparcSetting)\n", + " .UseSenparcGlobal();\n", + "\n", + "var redisConfigurationStr = senparcSetting.Cache_Redis_Configuration;\n", + "Senparc.CO2NET.Cache.Redis.Register.SetConfigurationOption(redisConfigurationStr);\n", + "//键值对缓存策略(推荐)\n", + "Senparc.CO2NET.Cache.Redis.Register.UseKeyValueRedisNow();\n", + "\n", + "register.UseSenparcWeixinCacheRedis();\n", + "\n", + "register.UseSenparcWeixin(senparcWeixinSetting)\n", + " .RegisterMpAccount(senparcWeixinSetting, \"梧州珍宝巴士\")\n", + " .RegisterWxOpenAccount(senparcWeixinSetting, \"梧州公交\");" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 发送统一服务消息" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "dotnet_interactive": { + "language": "csharp" + }, + "polyglot_notebook": { + "kernelName": "csharp" + }, + "vscode": { + "languageId": "polyglot-notebook" + } + }, + "outputs": [ + { + "data": { + "text/html": [ + "
WxJsonResult:{errcode:'0',errcode_name:'请求成功',errmsg:'ok'}
errcode请求成功
ErrorCodeValue
0
errmsg
ok
P2PData
<null>
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "public static readonly string WxOpenAppId = Senparc.Weixin.Config.SenparcWeixinSetting.WxOpenAppId;// 与微信小程序后台的AppId设置保持一致,区分大小写。\n", + "public static readonly string WxOpenAppSecret = Senparc.Weixin.Config.SenparcWeixinSetting.WxOpenAppSecret;// 与微信小程序账号后台的AppId设置保持一致,区分大小写。\n", + "public static readonly string WeixinAppId = Senparc.Weixin.Config.SenparcWeixinSetting.WeixinAppId;// 微信公众号AppId\n", + "private const string TemplateId = \"OJ-cY-IJhkw8SF_l0OZK_Cy5cKGN-uqhMiSXLxCaON8\";// 模板Id\n", + "const string openid = \"oMr9M5SoFzyGqjHm2pvUImj1w0BQ\";// 接收测试消息的openid\n", + "\n", + "UniformSendData sendData = new(openid,\n", + " new Mp_Template_Msg(WeixinAppId, TemplateId, \"https://mp.weixin.qq.com/s/ObnyNhpOIjij7zgS1RIvaA\",\n", + " new Miniprogram_Page(WxOpenAppId, string.Empty),\n", + " new\n", + " {\n", + " keyword1 = new TemplateDataItem(\"3075309865\"),\n", + " keyword2 = new TemplateDataItem(\"2023-09-10 17:02:22\"),\n", + " keyword3 = new TemplateDataItem(\"49路\"),\n", + " keyword4 = new TemplateDataItem(\"010678\"),\n", + " keyword5 = new TemplateDataItem(\"2.00\")\n", + " }));\n", + "\n", + "var result = TemplateApi.UniformSend(WxOpenAppId, sendData);\n", + "// 查看返回结果\n", + "result.Display();" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": ".NET (C#)", + "language": "C#", + "name": ".net-csharp" + }, + "language_info": { + "name": "python" + }, + "orig_nbformat": 4, + "polyglot_notebook": { + "kernelInfo": { + "defaultKernelName": "csharp", + "items": [ + { + "aliases": [], + "name": "csharp" + } + ] + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Samples/All/notebook/senparc.json b/Samples/All/notebook/senparc.json new file mode 100644 index 0000000000..bc79b8f4a9 --- /dev/null +++ b/Samples/All/notebook/senparc.json @@ -0,0 +1,21 @@ +{ + //CO2NET 设置 + "SenparcSetting": { + //以下为 CO2NET 的 SenparcSetting 全局配置,请勿修改 key,勿删除任何项 + + "IsDebug": false, + "DefaultCacheNamespace": "WeChatCache", + + //分布式缓存 + "Cache_Redis_Configuration": "127.0.0.1,password=[password],connectTimeout=1000,connectRetry=2,syncTimeout=10000,defaultDatabase=3", //密码及其他配置 + "SenparcUnionAgentKey": "WeChat" //SenparcUnionAgentKey + }, + "SenparcWeixinSetting": { + "Token": "[Token]", + "EncodingAESKey": "[EncodingAESKey]", + "WeixinAppId": "[WeixinAppId]", + "WeixinAppSecret": "[WeixinAppSecret]", + "WxOpenAppId": "[WxOpenAppId]", + "WxOpenAppSecret": "[WxOpenAppSecret]" + } +} From e1206a34377253acedc0c98bf0651cd25bfba869 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=AE=97=E7=A5=9E?= Date: Thu, 21 Sep 2023 08:40:14 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E6=9B=B4=E6=96=B0notebook=20sample?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Samples/All/notebook/sample.ipynb | 120 +++++++++++++++++++++++++++--- 1 file changed, 111 insertions(+), 9 deletions(-) diff --git a/Samples/All/notebook/sample.ipynb b/Samples/All/notebook/sample.ipynb index 4ef1622914..3e66c9fc4d 100644 --- a/Samples/All/notebook/sample.ipynb +++ b/Samples/All/notebook/sample.ipynb @@ -9,7 +9,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 1, "metadata": { "dotnet_interactive": { "language": "csharp" @@ -49,7 +49,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 2, "metadata": { "dotnet_interactive": { "language": "csharp" @@ -76,6 +76,34 @@ "using Senparc.Weixin.MP.AdvancedAPIs.TemplateMessage;" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 配置文件" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "dotnet_interactive": { + "language": "csharp" + }, + "polyglot_notebook": { + "kernelName": "csharp" + }, + "vscode": { + "languageId": "polyglot-notebook" + } + }, + "outputs": [], + "source": [ + "var configBuilder = new ConfigurationBuilder();\n", + "// 此处需要使用配置文件的绝对路径\n", + "configBuilder.AddJsonFile(@\"D:\\PythonProjects\\senparc.json\", false, false);" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -85,7 +113,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 4, "metadata": { "dotnet_interactive": { "language": "csharp" @@ -104,10 +132,6 @@ "// 不抛出异常\n", "Senparc.Weixin.Config.ThrownWhenJsonResultFaild = false;\n", "\n", - "var configBuilder = new ConfigurationBuilder();\n", - "// 此处需要使用配置文件的绝对路径\n", - "configBuilder.AddJsonFile(\"/Volumes/SSD/Source/PythonProjects/senparc.json\", false, false);\n", - "\n", "var config = configBuilder.Build();\n", "\n", "var senparcSetting = new SenparcSetting();\n", @@ -143,7 +167,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 5, "metadata": { "dotnet_interactive": { "language": "csharp" @@ -159,7 +183,7 @@ { "data": { "text/html": [ - "
WxJsonResult:{errcode:'0',errcode_name:'请求成功',errmsg:'ok'}
errcode请求成功
ErrorCodeValue
0
errmsg
ok
P2PData
<null>
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "var result = Senparc.Weixin.MP.AdvancedAPIs.TemplateApi.SendTemplateMessage(WeixinAppId, openid, TemplateId,\n", + " \"https://mp.weixin.qq.com/s/ObnyNhpOIjij7zgS1RIvaA\",\n", + " new\n", + " {\n", + " keyword1 = new TemplateDataItem(\"3075309865\"),\n", + " keyword2 = new TemplateDataItem(\"2023-09-10 17:02:22\"),\n", + " keyword3 = new TemplateDataItem(\"49路\"),\n", + " keyword4 = new TemplateDataItem(\"010678\"),\n", + " keyword5 = new TemplateDataItem(\"2.00\")\n", + " });\n", + "result.Display();" + ] } ], "metadata": { From e2cdd3f14d5867ef368964e8c5f43e6eb6475208 Mon Sep 17 00:00:00 2001 From: Jeffrey Su Date: Tue, 12 Dec 2023 00:36:11 +0800 Subject: [PATCH 3/4] Update readme.md --- Samples/All/readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Samples/All/readme.md b/Samples/All/readme.md index 11d42144e9..3b47ea6757 100644 --- a/Samples/All/readme.md +++ b/Samples/All/readme.md @@ -13,7 +13,7 @@ | net45-mvc | 【停止更新】ASP.NET 4.5 MVC 示例,可用于直接部署,此项目中包含了 CommonServices 项目,供其他各 Sample 公用 | Senparc.Weixin.Sample.CommonService | 所有 Sample 中共享的公共代码库(仅为 Sample 服务,和 SDK 源码无关) | Senparc.Weixin.Sample.Shared | 所有 Sample 中共享的 wwwroot 等静态文件资源(仅为 Sample 服务,和 SDK 源码无关) - +| notebook | 用于运行 Polyglot Notebooks 的 VS code 示例 > 注意: net45-mvc Sample 自 2022 年 5 月 4 日起,升级为 .NET Framework 4.6.2,并将一直支持到微软官方停止对该版本的支持,其后升级到 .NET Framework 4.8。为了方便交流,暂时保留 `net45` 这个名字,用以代表 .NET Framework Sample。 From e8bbd311917e8ca2b856e559e5c7921faad314cc Mon Sep 17 00:00:00 2001 From: Jeffrey Su Date: Tue, 12 Dec 2023 00:39:13 +0800 Subject: [PATCH 4/4] Update readme.md --- Samples/All/readme.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Samples/All/readme.md b/Samples/All/readme.md index 3b47ea6757..87bdc53283 100644 --- a/Samples/All/readme.md +++ b/Samples/All/readme.md @@ -6,14 +6,14 @@ | 文件夹 | 说明 | |--------|--------| -| net8-mvc | 【推荐】.NET 8.0 示例,可用于直接部署 -| net7-mvc | .NET 7.0 示例,可用于直接部署 -| net6-mvc | .NET 6.0 示例,可用于直接部署 -| console | 命令行注册过程演示 Demo(接口调可参考 Web 项目) +| net8-mvc | 【快速更新,推荐】.NET 8.0 示例,可用于直接部署 +| net7-mvc | 【停止更新】.NET 7.0 示例,可用于直接部署 +| net6-mvc | 【停止更新】.NET 6.0 示例,可用于直接部署 +| console | 【正常更新】命令行注册过程演示 Demo(接口调可参考 Web 项目) | net45-mvc | 【停止更新】ASP.NET 4.5 MVC 示例,可用于直接部署,此项目中包含了 CommonServices 项目,供其他各 Sample 公用 | Senparc.Weixin.Sample.CommonService | 所有 Sample 中共享的公共代码库(仅为 Sample 服务,和 SDK 源码无关) | Senparc.Weixin.Sample.Shared | 所有 Sample 中共享的 wwwroot 等静态文件资源(仅为 Sample 服务,和 SDK 源码无关) -| notebook | 用于运行 Polyglot Notebooks 的 VS code 示例 +| notebook | 用于运行 Polyglot Notebooks 的 VS code 示例 > 注意: net45-mvc Sample 自 2022 年 5 月 4 日起,升级为 .NET Framework 4.6.2,并将一直支持到微软官方停止对该版本的支持,其后升级到 .NET Framework 4.8。为了方便交流,暂时保留 `net45` 这个名字,用以代表 .NET Framework Sample。