You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
292 lines
12 KiB
292 lines
12 KiB
|
|
namespace CounsellorBL.GROUP
|
|
{
|
|
using CounsellorBL.BLStructure;
|
|
using CounsellorBL.Common;
|
|
using CounsellorBL.Helper;
|
|
using MonumentDefine;
|
|
using Newtonsoft.Json;
|
|
using OT.COM.ArsenalDB;
|
|
using OT.COM.SignalerMessage;
|
|
using SoldierData.EnterprizeV4;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using static CounsellorBL.GROUP.Helper.FbHelper;
|
|
|
|
public class GroupUserConnectService : SingleDataTableTemplate<tb_grp_group>
|
|
{
|
|
#region 私有類
|
|
private class Group2UserViewModel
|
|
{
|
|
public string app_id { get; set; }
|
|
public string client_secret { get; set; }
|
|
public string user_token { get; set; }
|
|
public string fb_account { get; set; }
|
|
}
|
|
|
|
private class FbTokenVerification
|
|
{
|
|
public VerificationContent data { get; set; }
|
|
}
|
|
|
|
private class VerificationContent
|
|
{
|
|
public string app_id { get; set; }
|
|
public string type { get; set; }
|
|
public int data_access_expires_at { get; set; }
|
|
public error error { get; set; }
|
|
public int expires_at { get; set; }
|
|
public bool is_valid { get; set; }
|
|
public string user_id { get; set; }
|
|
}
|
|
|
|
private class error
|
|
{
|
|
public int code { get; set; }
|
|
public string message { get; set; }
|
|
public int subcode { get; set; }
|
|
}
|
|
|
|
private class ErrorMsg
|
|
{
|
|
public string message { get; set; }
|
|
public string fb_account { get; set; }
|
|
}
|
|
#endregion
|
|
|
|
#region 私有方法
|
|
|
|
/// <summary>
|
|
/// 驗證token
|
|
/// </summary>
|
|
/// <param name="authorizationToken">user_token</param>
|
|
/// <param name="client_id">app_id</param>
|
|
/// <param name="client_secret">client_secret</param>
|
|
/// <param name="status"> 預設0: 正常 1: 即將到期 2: 驗證失敗</param>
|
|
/// <returns></returns>
|
|
private string CallFbCheckTokenGetAPI(string authorizationToken, string client_id, string client_secret, out int status)
|
|
{
|
|
// 送出資料
|
|
status = 0; // 預設0: 正常 1: 即將到期 2: 驗證失敗
|
|
string uri = "https://graph.facebook.com/debug_token?";
|
|
var dicData = new Dictionary<string, string>()
|
|
{
|
|
{ "input_token", authorizationToken },
|
|
{ "access_token", string.Format("{0}|{1}",client_id,client_secret) },
|
|
};
|
|
APIHelper.BaseGet(uri, null, dicData, out HttpResponseMessage responseMessage);
|
|
if (responseMessage.IsSuccessStatusCode)
|
|
{
|
|
var responseData = JsonConvert.DeserializeObject<FbTokenVerification>(responseMessage.Content.ReadAsStringAsync().Result);
|
|
if (responseData.data.is_valid)
|
|
{
|
|
return null;
|
|
}
|
|
else
|
|
{
|
|
status = 2;
|
|
return responseData.data.error.message;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
status = 2;
|
|
var error = $"{nameof(CallFbCheckTokenGetAPI)} Error, ReasonPhrase:{responseMessage.ReasonPhrase} hrmResult.Headers.WwwAuthenticate:{responseMessage.Headers.WwwAuthenticate} ";
|
|
return $"Token Check 失敗, {error}";
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 取得Group2User列表
|
|
/// </summary>
|
|
/// <param name="appId"></param>
|
|
/// <param name="qdGroup"></param>
|
|
/// <returns></returns>
|
|
private string GetListGroup2User(string appId, out List<Group2UserViewModel> qdGroup)
|
|
{
|
|
// 送出資料
|
|
qdGroup = new List<Group2UserViewModel>();
|
|
|
|
string sMsg;
|
|
QueryJsonElementCollection lBlocks = new QueryJsonElementCollection();
|
|
|
|
QueryJsonElement qjeGroup = lBlocks.GetInst();
|
|
qjeGroup.table = tb_grp_group.TABLENAME;
|
|
qjeGroup.displaycols = new List<string>()
|
|
{
|
|
tb_grp_group.CN_APP_ID,
|
|
tb_grp_group.CN_CLIENT_SECRET
|
|
};
|
|
qjeGroup.wherecols = new WhereNode(tb_grp_group.CN_APP_ID, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_grp_group), appId);
|
|
lBlocks.Add(qjeGroup);
|
|
|
|
QueryJsonElement qjeGroupUser = lBlocks.GetInst();
|
|
qjeGroupUser.table = tb_grp_group2user.TABLENAME;
|
|
qjeGroupUser.jointable = qjeGroup;
|
|
qjeGroupUser.jointype = QueryJsonElement.LEFT_JOIN;
|
|
qjeGroupUser.joincols = new Dictionary<string, string> { { tb_grp_group2user.CN_GROUP_UID, tb_grp_group.CN_UID } };
|
|
qjeGroupUser.displaycols = new List<string>()
|
|
{
|
|
tb_grp_group2user.CN_USER_TOKEN,
|
|
tb_grp_group2user.CN_FB_ACCOUNT
|
|
};
|
|
qjeGroupUser.wherecols = new WhereNode(tb_grp_group2user.CN_STATUS_FLAG, WhereNode.EColumnOperation.EOT_EQ, typeof(tb_grp_group2user), BLWording.STATUS_FLAG_ON);
|
|
lBlocks.Add(qjeGroupUser);
|
|
|
|
sMsg = MakeSelectJoinByBlocks(lBlocks, out Command cRes);
|
|
|
|
if (sMsg != null)
|
|
{
|
|
qdGroup = null;
|
|
return sMsg;
|
|
}
|
|
ArsenalInterface ai = ArsenalDBMgr.GetInst(cRes);
|
|
qdGroup = ai.RunQueryList<Group2UserViewModel>(cRes);
|
|
sMsg = GetLastErrorCode(cRes);
|
|
if (sMsg != null)
|
|
{
|
|
return sMsg;
|
|
}
|
|
return sMsg;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 取得設定中的值
|
|
/// </summary>
|
|
/// <param name="i_crmInput"></param>
|
|
/// <returns></returns>
|
|
private string GetAppId(CRequestMessage i_crmInput)
|
|
{
|
|
object oAppId = i_crmInput != null && i_crmInput.customparam != null && i_crmInput.customparam.ContainsKey(BLWording.APP_ID) ? i_crmInput.customparam[BLWording.APP_ID] : null;
|
|
return oAppId?.ToString();
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
[Auth(false)]
|
|
public CResponseMessage FbCheckToken(CRequestMessage i_crmInput)
|
|
{
|
|
Logger.Info("FbCheckToken debug start");
|
|
string sMsg = null;
|
|
CResponseMessage crmRes = null;
|
|
try
|
|
{
|
|
do
|
|
{
|
|
string strAppId = GetAppId(i_crmInput);
|
|
if (string.IsNullOrEmpty(strAppId))
|
|
{
|
|
Logger.Info(string.Format("請在appsettings.join中添加{0}的配置", BLWording.APP_ID));
|
|
sMsg = string.Format("請在appsettings.join中添加{0}的配置", BLWording.APP_ID) ;
|
|
break;
|
|
}
|
|
|
|
List<MailAccountInfo> lsMailList = new List<MailAccountInfo>();
|
|
// 取得當發文錯誤要發送的名單
|
|
string sMsgMailList = SystemSettingHelper.GetSetting(BLWording.ERROR_MAILLIST, out var mailList);
|
|
if (sMsg != null)
|
|
{
|
|
Logger.Error(sMsgMailList);
|
|
}
|
|
if (mailList != null && !string.IsNullOrEmpty(mailList.key_value))
|
|
{
|
|
var lsMail = mailList.key_value.Split(new String[] { ",", ";" }, StringSplitOptions.RemoveEmptyEntries).ToList();
|
|
foreach (var mail in lsMail)
|
|
{
|
|
lsMailList.Add(new MailAccountInfo() { EMail = mail });
|
|
Logger.Info(string.Format("errorMail:{0}", mail));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Logger.Info(string.Format("請先設置{0}的值", BLWording.ERROR_MAILLIST));
|
|
sMsg = string.Format("請先設置{0}的值", BLWording.ERROR_MAILLIST);
|
|
break;
|
|
}
|
|
|
|
sMsg = GetListGroup2User(strAppId, out var qdGroup);
|
|
if (sMsg != null)
|
|
{
|
|
break;
|
|
}
|
|
if (qdGroup.Count() <0)
|
|
{
|
|
Logger.Info("未查詢到user資料");
|
|
sMsg = "未查詢到user資料";
|
|
break;
|
|
}
|
|
List<ErrorMsg> lsErrorMsg = new List<ErrorMsg>();
|
|
foreach (Group2UserViewModel Group2UserData in qdGroup)
|
|
{
|
|
string isValid = CallFbCheckTokenGetAPI(Group2UserData.user_token, Group2UserData.app_id, Group2UserData.client_secret, out int status);
|
|
if (isValid != null)
|
|
{
|
|
sMsg = isValid;
|
|
if (status == 2)
|
|
{
|
|
Logger.Info(string.Format("Token驗證失敗:fb_account:{0}&&&app_id:{1}&&&client_secret:{2}&&&user_token:{3}", Group2UserData.fb_account, Group2UserData.app_id, Group2UserData.client_secret, Group2UserData.user_token));
|
|
lsErrorMsg.Add(new ErrorMsg() { fb_account = Group2UserData.fb_account,message= sMsg });
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Logger.Info(string.Format("Token驗證成功:fb_account:{0}&&&app_id:{1}&&&client_secret:{2}&&&user_token:{3}", Group2UserData.fb_account, Group2UserData.app_id, Group2UserData.client_secret, Group2UserData.user_token));
|
|
}
|
|
|
|
}
|
|
//存在錯誤訊息時
|
|
if (lsErrorMsg.Count() > 0)
|
|
{
|
|
|
|
var mailHelper = new MailHelper();
|
|
var strBody = "";
|
|
|
|
foreach (ErrorMsg ErrorMsgData in lsErrorMsg)
|
|
{
|
|
strBody += string.Format("fb賬號:{0}錯誤信息:{1}<br/>", ErrorMsgData.fb_account, ErrorMsgData.message);
|
|
}
|
|
var mailMsg = mailHelper.Send(string.Format("appid:{0}驗證token錯誤", strAppId), strBody, lsMailList);
|
|
if (string.IsNullOrEmpty(mailMsg))
|
|
{
|
|
Logger.Info("信件發送成功");
|
|
}
|
|
else
|
|
{
|
|
Logger.Error(string.Format("信件發送失敗:{0} => {1}", mailMsg, lsMailList));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Logger.Info(string.Format("app_id:{0}下未有驗證token錯誤的user", strAppId));
|
|
}
|
|
|
|
crmRes = new CSuccessResponseMessage(null, i_crmInput);
|
|
// 填寫回傳
|
|
|
|
}
|
|
while (false);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
sMsg = $"{nameof(PushPost)} exception. i_crmInput={JsonConvert.SerializeObject(i_crmInput)}. ex={ex.Message}";
|
|
Logger.Error(sMsg);
|
|
#if DEBUG
|
|
System.Diagnostics.Debug.WriteLine(sMsg);
|
|
#endif
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(sMsg))
|
|
{
|
|
crmRes = new CErrorResponseMessage(sMsg, i_crmInput);
|
|
Logger.Error(JsonConvert.SerializeObject(i_crmInput));
|
|
Logger.Error(JsonConvert.SerializeObject(crmRes));
|
|
}
|
|
return crmRes;
|
|
}
|
|
|
|
}
|
|
}
|