using Abp.Application.Services.Dto;
using Abp.Runtime.Caching;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;
using Senparc.CO2NET.HttpUtility;
using Senparc.Weixin.CommonAPIs.ApiHandlerWapper;
using Senparc.Weixin.MP.Containers;
using Senparc.Weixin.MP.Entities;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks; namespace AbpProject.WeiXinMPCache
{
public class MPCacheAppService : AbpProjectAppServiceBase, IMPCacheAppService
{
/// <summary>
/// appid
/// </summary>
private readonly string AppIdSAddress = "WeiXinMP:AppID";
/// <summary>
/// appsecret
/// </summary>
private readonly string AppSecretAddress = "WeiXinMP:AppSecret";
/// <summary>
/// 获取token的路径
/// </summary>
private readonly string AccTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}"; private readonly ICacheManager _cacheManager;
private readonly IConfiguration _appConfiguration; public MPCacheAppService(IConfiguration appConfiguration, ICacheManager cacheManager)
{
_appConfiguration = appConfiguration;
_cacheManager = cacheManager;
} /// <summary>
/// 获取公众微信号的token
/// </summary>
/// <returns></returns>
public async Task<string> GetAccessToken()
{
var acctoken = await _cacheManager.GetCache("WeiXinMP").GetOrDefaultAsync("AccessToken");
if (acctoken == null || string.IsNullOrEmpty(acctoken.ToString()))
{
var appId = _appConfiguration[AppIdSAddress];
var appSecret = _appConfiguration[AppSecretAddress];
var url = string.Format(AccTokenUrl, appId, appSecret);
var result = HttpClientHelper<AccessTokenResult>.Get(url,new AccessTokenResult());
if (result != null)
{
await _cacheManager.GetCache("WeiXinMP").SetAsync("AccessToken", result.access_token, TimeSpan.FromHours());
return result.access_token;
}
}
return acctoken.ToString();
} }
}

最新文章

  1. 【PostgreSQL】PostgreSQL的安装
  2. 用jekyll制作高大上的网站(二)——实际应用
  3. The resource identified by this request is only capable of generating responses with characteristics
  4. easyUI validate函数【总结篇-部分转】
  5. 解决org.openqa.selenium.WebDriverException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms org.springframework.beans.BeanInstantiation
  6. selection伪元素小解
  7. jquery and jquery validation 常见问题解决
  8. SqlSever基础 datepart函数 返回这一秒已经过去了多少毫秒
  9. D3.js 其他选择元素方法
  10. DOM笔记(九):引用类型、基本包装类型和单体内置对象
  11. python学习笔记13(模块、包)
  12. oracle的sqlnet.ora , tnsnames.ora , Listener.ora 文件的作用(转)
  13. VS2012 快捷键 VS Resharper 设置
  14. html combobox select控件设置默认选项
  15. KVC该机制
  16. ##6.1 Neutron控制节点-- openstack pike
  17. Java Timer, TimerTask, Timer.Schedule
  18. java (图片转PDF)
  19. html学习笔记(一)
  20. jmeter接口测试2-断言

热门文章

  1. Alpha2版本-组织管理、党员管理、活动管理测试
  2. 201671030128周琳 实验十四 团队项目评审&amp;课程学习总结
  3. struts2学习1
  4. BILIBILI之滑块验证
  5. Linux/Unix 多线程通信
  6. PATB1009说反话
  7. 第02组Alpha冲刺(4/4)
  8. 记一次生产kafka消息消费的事故
  9. postcss 将px转换成rem vuecli3+vant+vue+postcss
  10. App3种开发方式的优劣分析:原生、混合和H5