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

提交代码微信token缓存

parent 54deb488
using Mall.WeChat.Common;
using Mall.Common.Pay.WeChatPat.Model;
using System;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
namespace Mall.Common.Pay.WeChatPat
{
......@@ -9,44 +11,38 @@ namespace Mall.Common.Pay.WeChatPat
/// </summary>
public class TokenHelper
{
/// <summary>
/// 缓存实例对象
/// </summary>
// private static Cache.ICache cache = Cache.MemcacheFactory.GetMenchageClient(Config.GetAppSetting("MemcachedKeyPoolName"));
/// <summary>
/// 获取access_token。会缓存,过期后自动重新获取新的token。
/// 获取token
/// </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();
//var secret = WeChatConfig.GetSecret();
var result = new TokenResult();
//if (cache.Exists("AccessToken" + appId))
//{
// result = (TokenResult)cache.Get("AccessToken" + appId);
//}
//if (result == null || string.IsNullOrWhiteSpace(result.access_token))
//{
// result = HttpHelper.Get<TokenResult>(ApiList.GetTokenUrl, new
// {
// grant_type = "client_credential",
// appid = appId,
// secret = secret
// });
// if (result.IsSuccess)
// {
// cache.Set("AccessToken" + appId, result, result.expires_in - 60);
// }
// else
// {
// Plugin.LogHelper.Write(null,"GetAccessToken失败!" + result.ErrMsg);
// }
//}
return result;
//向微信服务端 获取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)
{
Plugin.LogHelper.Write(ex, string.Format("GetWx:wenXinResult:{0}", wenXinResult));
}
return token;
}
}
}
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
/// <returns></returns>
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)
{
foreach (var item in list)
......@@ -264,7 +264,7 @@ namespace Mall.Module.User
return programRepository.GetPageListRepository(pageIndex, pageSize, out rowCount, query);
}
/// <summary>
/// 判断用户是否存在
......@@ -415,7 +415,7 @@ namespace Mall.Module.User
/// <param name="isGetUserCenterMenu">是否获取用户中心菜单</param>
/// <param name="isGetHomeData">是否获取首页配置数据</param>
/// <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);
if (extModel == null)
......@@ -540,7 +540,7 @@ namespace Mall.Module.User
{
var flag = false;
flag = bottomnavRepository.DeleteBottomnavRepository(TagId);
return flag;
return flag;
}
/// <summary>
......@@ -909,7 +909,7 @@ namespace Mall.Module.User
string token = WeiXinReidsCache.Get(appletWeChatModel.MiniAppId);
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));
}
if (string.IsNullOrEmpty(token))
......@@ -999,7 +999,7 @@ namespace Mall.Module.User
string token = WeiXinReidsCache.Get(appletWeChatModel.MiniAppId);
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));
}
if (string.IsNullOrEmpty(token))
......@@ -1090,7 +1090,7 @@ namespace Mall.Module.User
string token = WeiXinReidsCache.Get(appletWeChatModel.MiniAppId);
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));
}
if (string.IsNullOrEmpty(token))
......@@ -1179,7 +1179,7 @@ namespace Mall.Module.User
string token = WeiXinReidsCache.Get(appletWeChatModel.MiniAppId);
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));
}
if (string.IsNullOrEmpty(token))
......@@ -1267,7 +1267,7 @@ namespace Mall.Module.User
string token = WeiXinReidsCache.Get(appletWeChatModel.MiniAppId);
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));
}
if (string.IsNullOrEmpty(token))
......@@ -1351,7 +1351,7 @@ namespace Mall.Module.User
string token = WeiXinReidsCache.Get(appletWeChatModel.MiniAppId);
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));
}
if (string.IsNullOrEmpty(token))
......@@ -1439,7 +1439,7 @@ namespace Mall.Module.User
string token = WeiXinReidsCache.Get(appletWeChatModel.MiniAppId);
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));
}
if (string.IsNullOrEmpty(token))
......@@ -1525,7 +1525,7 @@ namespace Mall.Module.User
string token = WeiXinReidsCache.Get(appletWeChatModel.MiniAppId);
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));
}
if (string.IsNullOrEmpty(token))
......@@ -1603,7 +1603,7 @@ namespace Mall.Module.User
string token = WeiXinReidsCache.Get(appletWeChatModel.MiniAppId);
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));
}
if (string.IsNullOrEmpty(token))
......@@ -1663,34 +1663,6 @@ namespace Mall.Module.User
}
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
string token = WeiXinReidsCache.Get(model.MiniAppId);
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));
}
if (string.IsNullOrEmpty(token))
......@@ -1730,7 +1702,7 @@ namespace Mall.Module.User
string access_TokenStr = WeiXinReidsCache.Get(model.MiniAppId);
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));
}
string picStr = string.Empty;
......@@ -1775,7 +1747,7 @@ namespace Mall.Module.User
string token = WeiXinReidsCache.Get(model.MiniAppId);
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));
}
if (string.IsNullOrEmpty(token))
......
......@@ -57,7 +57,7 @@ namespace Mall.WebApi.Controllers.AppletWeChat
var userModel = userModule.GetMemberUserInfo(21233);
if (userInfo == null)
if (userInfo != null)
{
userInfo = new Common.AppletUserInfo();
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