Commit 4606f810 authored by 吴春's avatar 吴春

提交代码微信token缓存

parent 54deb488
using Mall.WeChat.Common; using Mall.WeChat.Common;
using Mall.Common.Pay.WeChatPat.Model; using Mall.Common.Pay.WeChatPat.Model;
using System; using System;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
namespace Mall.Common.Pay.WeChatPat namespace Mall.Common.Pay.WeChatPat
{ {
...@@ -9,44 +11,38 @@ namespace Mall.Common.Pay.WeChatPat ...@@ -9,44 +11,38 @@ namespace Mall.Common.Pay.WeChatPat
/// </summary> /// </summary>
public class TokenHelper public class TokenHelper
{ {
/// <summary> /// <summary>
/// 缓存实例对象 /// 获取token
/// </summary>
// private static Cache.ICache cache = Cache.MemcacheFactory.GetMenchageClient(Config.GetAppSetting("MemcachedKeyPoolName"));
/// <summary>
/// 获取access_token。会缓存,过期后自动重新获取新的token。
/// </summary> /// </summary>
public static TokenResult GetAccessToken() /// <param name="token"></param>
/// <returns></returns>
public static string GetLXYToken(string token, string Appid, string Secret)
{ {
//var appId = WeChatConfig.GetAppId(); //向微信服务端 获取token
//var secret = WeChatConfig.GetSecret(); string url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + Appid + "&secret=" + Secret;
var result = new TokenResult(); string type = "utf-8";
//if (cache.Exists("AccessToken" + appId)) Mall.Common.Plugin.GetUsersHelper GetUsersHelper = new Mall.Common.Plugin.GetUsersHelper();
//{ string wenXinResult = string.Empty;
// result = (TokenResult)cache.Get("AccessToken" + appId); JObject jo = null;
//} Mall.Common.Plugin.result res = new Mall.Common.Plugin.result();
//if (result == null || string.IsNullOrWhiteSpace(result.access_token)) try
//{ {
// result = HttpHelper.Get<TokenResult>(ApiList.GetTokenUrl, new wenXinResult = GetUsersHelper.GetUrltoHtml(url, type);
// { jo = (JObject)JsonConvert.DeserializeObject(wenXinResult);
// grant_type = "client_credential", token = jo["access_token"].ToString();
// appid = appId, }
// secret = secret catch (Exception ex)
// }); {
// if (result.IsSuccess) Plugin.LogHelper.Write(ex, string.Format("GetWx:wenXinResult:{0}", wenXinResult));
// { }
// cache.Set("AccessToken" + appId, result, result.expires_in - 60); return token;
// }
// else
// {
// Plugin.LogHelper.Write(null,"GetAccessToken失败!" + result.ErrMsg);
// }
//}
return result;
} }
} }
} }
using Mall.Common.Pay.WeChatPat.Model;
using Mall.WeChat.Common;
using System.Collections.Generic;
namespace Mall.Common.Pay.WeChatPat
{
/// <summary>
/// 微信帮助类
/// </summary>
public class WeChatHelper
{
/// <summary>
/// 根据Code获取openid失败
/// </summary>
/// <param name="sCode"></param>
/// <returns></returns>
public static UserInfoResult GetUserInfoByCode(string sCode)
{
var dic = GetOauth2AccessToken(sCode);
if (dic.ContainsKey("openid"))
{
var result = HttpHelper.Get<UserInfoResult>(ApiList.GetUserInfo, new
{
access_token = TokenHelper.GetAccessToken().access_token,
openid = dic["openid"],
lang = "zh_CN"
});
return result;
}
Plugin.LogHelper.Write(null, "GetUserInfoByCode");
return null;
}
/// <summary>
/// 根据Code获取用户授权信息
/// </summary>
/// <param name="sCode"></param>
/// <returns></returns>
public static Dictionary<string, string> GetOauth2AccessToken(string sCode)
{
var param2 = new
{
appid = "", //WeChatConfig.GetAppId(),
secret = "",// WeChatConfig.GetSecret(),
code = sCode,
grant_type = "authorization_code"
};
var dic = HttpHelper.Get<Dictionary<string, string>>(ApiList.GetOauth2AccessTokenUrl, param2);
return dic;
}
}
}
...@@ -162,7 +162,7 @@ namespace Mall.Module.User ...@@ -162,7 +162,7 @@ namespace Mall.Module.User
/// <returns></returns> /// <returns></returns>
public List<RB_Mall_Magic_Extend> GetMallMagicListModule(RB_Mall_Magic_Extend query) public List<RB_Mall_Magic_Extend> GetMallMagicListModule(RB_Mall_Magic_Extend query)
{ {
var list= magicRepository.GetMallMagicListRepository(query); var list = magicRepository.GetMallMagicListRepository(query);
if (list != null && list.Count > 0) if (list != null && list.Count > 0)
{ {
foreach (var item in list) foreach (var item in list)
...@@ -264,7 +264,7 @@ namespace Mall.Module.User ...@@ -264,7 +264,7 @@ namespace Mall.Module.User
return programRepository.GetPageListRepository(pageIndex, pageSize, out rowCount, query); return programRepository.GetPageListRepository(pageIndex, pageSize, out rowCount, query);
} }
/// <summary> /// <summary>
/// 判断用户是否存在 /// 判断用户是否存在
...@@ -415,7 +415,7 @@ namespace Mall.Module.User ...@@ -415,7 +415,7 @@ namespace Mall.Module.User
/// <param name="isGetUserCenterMenu">是否获取用户中心菜单</param> /// <param name="isGetUserCenterMenu">是否获取用户中心菜单</param>
/// <param name="isGetHomeData">是否获取首页配置数据</param> /// <param name="isGetHomeData">是否获取首页配置数据</param>
/// <returns></returns> /// <returns></returns>
public RB_MiniProgram_Extend GetMiniProgramModule(RB_MiniProgram_Extend query, int isGetNav = 0, bool isGetUserCenterMenu = false,bool isGetHomeData=false) public RB_MiniProgram_Extend GetMiniProgramModule(RB_MiniProgram_Extend query, int isGetNav = 0, bool isGetUserCenterMenu = false, bool isGetHomeData = false)
{ {
RB_MiniProgram_Extend extModel = programRepository.GetMiniProgramRepository(query); RB_MiniProgram_Extend extModel = programRepository.GetMiniProgramRepository(query);
if (extModel == null) if (extModel == null)
...@@ -540,7 +540,7 @@ namespace Mall.Module.User ...@@ -540,7 +540,7 @@ namespace Mall.Module.User
{ {
var flag = false; var flag = false;
flag = bottomnavRepository.DeleteBottomnavRepository(TagId); flag = bottomnavRepository.DeleteBottomnavRepository(TagId);
return flag; return flag;
} }
/// <summary> /// <summary>
...@@ -909,7 +909,7 @@ namespace Mall.Module.User ...@@ -909,7 +909,7 @@ namespace Mall.Module.User
string token = WeiXinReidsCache.Get(appletWeChatModel.MiniAppId); string token = WeiXinReidsCache.Get(appletWeChatModel.MiniAppId);
if (string.IsNullOrEmpty(token)) if (string.IsNullOrEmpty(token))
{ {
token = GetLXYToken(token, appletWeChatModel); token = Mall.Common.Pay.WeChatPat.TokenHelper.GetLXYToken(token, appletWeChatModel.MiniAppId, appletWeChatModel.MiniAppSecret);
System.Threading.Tasks.Task.Run(() => WeiXinReidsCache.Set(appletWeChatModel.MiniAppId, token)); System.Threading.Tasks.Task.Run(() => WeiXinReidsCache.Set(appletWeChatModel.MiniAppId, token));
} }
if (string.IsNullOrEmpty(token)) if (string.IsNullOrEmpty(token))
...@@ -999,7 +999,7 @@ namespace Mall.Module.User ...@@ -999,7 +999,7 @@ namespace Mall.Module.User
string token = WeiXinReidsCache.Get(appletWeChatModel.MiniAppId); string token = WeiXinReidsCache.Get(appletWeChatModel.MiniAppId);
if (string.IsNullOrEmpty(token)) if (string.IsNullOrEmpty(token))
{ {
token = GetLXYToken(token, appletWeChatModel); token = Mall.Common.Pay.WeChatPat.TokenHelper.GetLXYToken(token, appletWeChatModel.MiniAppId, appletWeChatModel.MiniAppSecret);
System.Threading.Tasks.Task.Run(() => WeiXinReidsCache.Set(appletWeChatModel.MiniAppId, token)); System.Threading.Tasks.Task.Run(() => WeiXinReidsCache.Set(appletWeChatModel.MiniAppId, token));
} }
if (string.IsNullOrEmpty(token)) if (string.IsNullOrEmpty(token))
...@@ -1090,7 +1090,7 @@ namespace Mall.Module.User ...@@ -1090,7 +1090,7 @@ namespace Mall.Module.User
string token = WeiXinReidsCache.Get(appletWeChatModel.MiniAppId); string token = WeiXinReidsCache.Get(appletWeChatModel.MiniAppId);
if (string.IsNullOrEmpty(token)) if (string.IsNullOrEmpty(token))
{ {
token = GetLXYToken(token, appletWeChatModel); token = Mall.Common.Pay.WeChatPat.TokenHelper.GetLXYToken(token, appletWeChatModel.MiniAppId, appletWeChatModel.MiniAppSecret);
System.Threading.Tasks.Task.Run(() => WeiXinReidsCache.Set(appletWeChatModel.MiniAppId, token)); System.Threading.Tasks.Task.Run(() => WeiXinReidsCache.Set(appletWeChatModel.MiniAppId, token));
} }
if (string.IsNullOrEmpty(token)) if (string.IsNullOrEmpty(token))
...@@ -1179,7 +1179,7 @@ namespace Mall.Module.User ...@@ -1179,7 +1179,7 @@ namespace Mall.Module.User
string token = WeiXinReidsCache.Get(appletWeChatModel.MiniAppId); string token = WeiXinReidsCache.Get(appletWeChatModel.MiniAppId);
if (string.IsNullOrEmpty(token)) if (string.IsNullOrEmpty(token))
{ {
token = GetLXYToken(token, appletWeChatModel); token = Mall.Common.Pay.WeChatPat.TokenHelper.GetLXYToken(token, appletWeChatModel.MiniAppId, appletWeChatModel.MiniAppSecret);
System.Threading.Tasks.Task.Run(() => WeiXinReidsCache.Set(appletWeChatModel.MiniAppId, token)); System.Threading.Tasks.Task.Run(() => WeiXinReidsCache.Set(appletWeChatModel.MiniAppId, token));
} }
if (string.IsNullOrEmpty(token)) if (string.IsNullOrEmpty(token))
...@@ -1267,7 +1267,7 @@ namespace Mall.Module.User ...@@ -1267,7 +1267,7 @@ namespace Mall.Module.User
string token = WeiXinReidsCache.Get(appletWeChatModel.MiniAppId); string token = WeiXinReidsCache.Get(appletWeChatModel.MiniAppId);
if (string.IsNullOrEmpty(token)) if (string.IsNullOrEmpty(token))
{ {
token = GetLXYToken(token, appletWeChatModel); token = Mall.Common.Pay.WeChatPat.TokenHelper.GetLXYToken(token, appletWeChatModel.MiniAppId, appletWeChatModel.MiniAppSecret);
System.Threading.Tasks.Task.Run(() => WeiXinReidsCache.Set(appletWeChatModel.MiniAppId, token)); System.Threading.Tasks.Task.Run(() => WeiXinReidsCache.Set(appletWeChatModel.MiniAppId, token));
} }
if (string.IsNullOrEmpty(token)) if (string.IsNullOrEmpty(token))
...@@ -1351,7 +1351,7 @@ namespace Mall.Module.User ...@@ -1351,7 +1351,7 @@ namespace Mall.Module.User
string token = WeiXinReidsCache.Get(appletWeChatModel.MiniAppId); string token = WeiXinReidsCache.Get(appletWeChatModel.MiniAppId);
if (string.IsNullOrEmpty(token)) if (string.IsNullOrEmpty(token))
{ {
token = GetLXYToken(token, appletWeChatModel); token = Mall.Common.Pay.WeChatPat.TokenHelper.GetLXYToken(token, appletWeChatModel.MiniAppId, appletWeChatModel.MiniAppSecret);
System.Threading.Tasks.Task.Run(() => WeiXinReidsCache.Set(appletWeChatModel.MiniAppId, token)); System.Threading.Tasks.Task.Run(() => WeiXinReidsCache.Set(appletWeChatModel.MiniAppId, token));
} }
if (string.IsNullOrEmpty(token)) if (string.IsNullOrEmpty(token))
...@@ -1439,7 +1439,7 @@ namespace Mall.Module.User ...@@ -1439,7 +1439,7 @@ namespace Mall.Module.User
string token = WeiXinReidsCache.Get(appletWeChatModel.MiniAppId); string token = WeiXinReidsCache.Get(appletWeChatModel.MiniAppId);
if (string.IsNullOrEmpty(token)) if (string.IsNullOrEmpty(token))
{ {
token = GetLXYToken(token, appletWeChatModel); token = Mall.Common.Pay.WeChatPat.TokenHelper.GetLXYToken(token, appletWeChatModel.MiniAppId, appletWeChatModel.MiniAppSecret);
System.Threading.Tasks.Task.Run(() => WeiXinReidsCache.Set(appletWeChatModel.MiniAppId, token)); System.Threading.Tasks.Task.Run(() => WeiXinReidsCache.Set(appletWeChatModel.MiniAppId, token));
} }
if (string.IsNullOrEmpty(token)) if (string.IsNullOrEmpty(token))
...@@ -1525,7 +1525,7 @@ namespace Mall.Module.User ...@@ -1525,7 +1525,7 @@ namespace Mall.Module.User
string token = WeiXinReidsCache.Get(appletWeChatModel.MiniAppId); string token = WeiXinReidsCache.Get(appletWeChatModel.MiniAppId);
if (string.IsNullOrEmpty(token)) if (string.IsNullOrEmpty(token))
{ {
token = GetLXYToken(token, appletWeChatModel); token = Mall.Common.Pay.WeChatPat.TokenHelper.GetLXYToken(token, appletWeChatModel.MiniAppId, appletWeChatModel.MiniAppSecret);
System.Threading.Tasks.Task.Run(() => WeiXinReidsCache.Set(appletWeChatModel.MiniAppId, token)); System.Threading.Tasks.Task.Run(() => WeiXinReidsCache.Set(appletWeChatModel.MiniAppId, token));
} }
if (string.IsNullOrEmpty(token)) if (string.IsNullOrEmpty(token))
...@@ -1603,7 +1603,7 @@ namespace Mall.Module.User ...@@ -1603,7 +1603,7 @@ namespace Mall.Module.User
string token = WeiXinReidsCache.Get(appletWeChatModel.MiniAppId); string token = WeiXinReidsCache.Get(appletWeChatModel.MiniAppId);
if (string.IsNullOrEmpty(token)) if (string.IsNullOrEmpty(token))
{ {
token = GetLXYToken(token, appletWeChatModel); token = Mall.Common.Pay.WeChatPat.TokenHelper.GetLXYToken(token, appletWeChatModel.MiniAppId, appletWeChatModel.MiniAppSecret);
System.Threading.Tasks.Task.Run(() => WeiXinReidsCache.Set(appletWeChatModel.MiniAppId, token)); System.Threading.Tasks.Task.Run(() => WeiXinReidsCache.Set(appletWeChatModel.MiniAppId, token));
} }
if (string.IsNullOrEmpty(token)) if (string.IsNullOrEmpty(token))
...@@ -1663,34 +1663,6 @@ namespace Mall.Module.User ...@@ -1663,34 +1663,6 @@ namespace Mall.Module.User
} }
return false; return false;
} }
/// <summary>
/// 获取token
/// </summary>
/// <param name="token"></param>
/// <returns></returns>
private static string GetLXYToken(string token, RB_MiniProgram_Extend model)
{
string Appid = model.MiniAppId;
string Secret = model.MiniAppSecret;
//向微信服务端 获取token
string url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + Appid + "&secret=" + Secret;
string type = "utf-8";
Mall.Common.Plugin.GetUsersHelper GetUsersHelper = new Mall.Common.Plugin.GetUsersHelper();
string wenXinResult = string.Empty;
JObject jo = null;
Mall.Common.Plugin.result res = new Mall.Common.Plugin.result();
try
{
wenXinResult = GetUsersHelper.GetUrltoHtml(url, type);
jo = (JObject)JsonConvert.DeserializeObject(wenXinResult);
token = jo["access_token"].ToString();
}
catch (Exception ex)
{
LogHelper.Write(ex, string.Format("GetWx:wenXinResult:{0}", wenXinResult));
}
return token;
}
...@@ -1709,7 +1681,7 @@ namespace Mall.Module.User ...@@ -1709,7 +1681,7 @@ namespace Mall.Module.User
string token = WeiXinReidsCache.Get(model.MiniAppId); string token = WeiXinReidsCache.Get(model.MiniAppId);
if (string.IsNullOrEmpty(token)) if (string.IsNullOrEmpty(token))
{ {
token = GetLXYToken(token, model); token = Mall.Common.Pay.WeChatPat.TokenHelper.GetLXYToken(token, model.MiniAppId, model.MiniAppSecret);
System.Threading.Tasks.Task.Run(() => WeiXinReidsCache.Set(model.MiniAppId, token)); System.Threading.Tasks.Task.Run(() => WeiXinReidsCache.Set(model.MiniAppId, token));
} }
if (string.IsNullOrEmpty(token)) if (string.IsNullOrEmpty(token))
...@@ -1730,7 +1702,7 @@ namespace Mall.Module.User ...@@ -1730,7 +1702,7 @@ namespace Mall.Module.User
string access_TokenStr = WeiXinReidsCache.Get(model.MiniAppId); string access_TokenStr = WeiXinReidsCache.Get(model.MiniAppId);
if (string.IsNullOrEmpty(access_TokenStr)) if (string.IsNullOrEmpty(access_TokenStr))
{ {
access_TokenStr = GetLXYToken(access_TokenStr, model); access_TokenStr = Mall.Common.Pay.WeChatPat.TokenHelper.GetLXYToken(access_TokenStr, model.MiniAppId, model.MiniAppSecret);
System.Threading.Tasks.Task.Run(() => WeiXinReidsCache.Set(model.MiniAppId, access_TokenStr)); System.Threading.Tasks.Task.Run(() => WeiXinReidsCache.Set(model.MiniAppId, access_TokenStr));
} }
string picStr = string.Empty; string picStr = string.Empty;
...@@ -1775,7 +1747,7 @@ namespace Mall.Module.User ...@@ -1775,7 +1747,7 @@ namespace Mall.Module.User
string token = WeiXinReidsCache.Get(model.MiniAppId); string token = WeiXinReidsCache.Get(model.MiniAppId);
if (string.IsNullOrEmpty(token)) if (string.IsNullOrEmpty(token))
{ {
token = GetLXYToken(token, model); token = Mall.Common.Pay.WeChatPat.TokenHelper.GetLXYToken(token, model.MiniAppId, model.MiniAppSecret);
System.Threading.Tasks.Task.Run(() => WeiXinReidsCache.Set(model.MiniAppId, token)); System.Threading.Tasks.Task.Run(() => WeiXinReidsCache.Set(model.MiniAppId, token));
} }
if (string.IsNullOrEmpty(token)) if (string.IsNullOrEmpty(token))
......
...@@ -57,7 +57,7 @@ namespace Mall.WebApi.Controllers.AppletWeChat ...@@ -57,7 +57,7 @@ namespace Mall.WebApi.Controllers.AppletWeChat
var userModel = userModule.GetMemberUserInfo(21233); var userModel = userModule.GetMemberUserInfo(21233);
if (userInfo == null) if (userInfo != null)
{ {
userInfo = new Common.AppletUserInfo(); userInfo = new Common.AppletUserInfo();
userInfo.TenantId = userModel.TenantId; userInfo.TenantId = userModel.TenantId;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment