Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
mall.oytour.com
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
黄奎
mall.oytour.com
Commits
4606f810
Commit
4606f810
authored
May 29, 2020
by
吴春
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
提交代码微信token缓存
parent
54deb488
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
47 additions
and
133 deletions
+47
-133
TokenHelper.cs
Mall.Common/Pay/WeChatPat/TokenHelper.cs
+30
-34
WeChatHelper.cs
Mall.Common/Pay/WeChatPat/WeChatHelper.cs
+0
-54
MiniProgramModule.cs
Mall.Module.User/MiniProgramModule.cs
+16
-44
WeChatPayController.cs
Mall.WebApi/Controllers/AppletWeChat/WeChatPayController.cs
+1
-1
No files found.
Mall.Common/Pay/WeChatPat/TokenHelper.cs
View file @
4606f810
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
;
}
}
}
Mall.Common/Pay/WeChatPat/WeChatHelper.cs
deleted
100644 → 0
View file @
54deb488
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
;
}
}
}
Mall.Module.User/MiniProgramModule.cs
View file @
4606f810
...
...
@@ -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
))
...
...
Mall.WebApi/Controllers/AppletWeChat/WeChatPayController.cs
View file @
4606f810
...
...
@@ -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
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment