using DS.Module.Core;
using DS.WMS.Core.WxModule.Interface;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NLog;
using Senparc.Weixin.MP;
using LogLevel = NLog.LogLevel;
namespace DS.WMS.WebApi.Controllers;
///
/// 微信公众号模块
///
[Route("api/[controller]")]
public class WebAuthController : Controller
{
static readonly Logger Logger = LogManager.GetCurrentClassLogger();
// //微信公众号的APPid
// private string AppId = "wx63325af0c60e64e4";
//
// //微信公众号的配置的Secret
// private string Secret = "e71ef689becd58b5e2d3ec345ba5a23e";
//
// //授权成功返回主页
// private string indexUrl = "http://60.209.125.238:9998/#/pages/index/index?TenantId=1595354960864874496";
private string Token = "gumang896";
private readonly IWxPublicConfigService _invokeService;
///
/// 构造函数
///
///
public WebAuthController(IWxPublicConfigService invokeService)
{
_invokeService = invokeService;
}
/// 验签
///
///
///
///
///
///
[HttpGet]
[Route("index")]
public ActionResult Get(string signature, string timestamp, string nonce, string echostr)
{
// 验证成功返回echostr,否则返回空字符串
return Content(!CheckSignature.Check(signature, timestamp, nonce, Token) ? "" : echostr);
}
///
///
///
///
///
///
///
[HttpGet]
[Route("callback")]
public ActionResult Post(string code, string state, string configId)
{
DataResult data = new DataResult();
string ReturnUrl = String.Empty;
Logger.Log(LogLevel.Info, "获取state成功:" + state);
Logger.Log(LogLevel.Info, "获取授权码成功:" + code);
var config = _invokeService.GetWxConfig(state);
string res = WxWebHelper.GetAccess_token(config.Appid, config.Secret, code);
Logger.Log(LogLevel.Info, "获取Token信息:" + res);
if (res.Contains("access_token"))
{
JObject token = JObject.Parse(res);
data.Code = ResultCode.Success;
data.Message = "授权成功!";
ReturnUrl = config.IndexUrl + "&OpenId=" + token["openid"].ToString();
}
else
{
data.Code = ResultCode.Fail;
data.Message = "授权失败!";
Logger.Log(LogLevel.Info, "获取网页Token失败:" + res);
ReturnUrl = config.IndexUrl;
}
return Redirect(ReturnUrl);
}
}