TG.WXCRM.V4/RiaService/SmsService.svc.cs

510 lines
24 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Ninject;
using RiaServiceLibrary;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.ServiceModel.Web;
using System.Web;
using WX.CRM.BLL.Util;
using WX.CRM.Common;
using WX.CRM.DAL;
using WX.CRM.IBLL.Isvr;
using WX.CRM.IBLL.Res;
using WX.CRM.IBLL.Sms;
using WX.CRM.IBLL.Soft;
using WX.CRM.IBLL.Util;
using WX.CRM.Model.Entity;
using WX.CRM.Model.Enum;
using WX.CRM.Model.QueryMap;
using WX.CRM.WebHelper;
using WX.CRM.WebHelper.Infrastructure;
namespace RiaService
{
public class SmsService : ISmsService
{
private ValidationErrors errors = new ValidationErrors();
private ISecurityHelper sHelper = NinjectControllerFactory.ninjectKernel.Get<ISecurityHelper>();
private IISVR_INTERFACECALLLOG logHelper = NinjectControllerFactory.ninjectKernel.Get<IISVR_INTERFACECALLLOG>();
private ICACHE_Q cacheQ = NinjectControllerFactory.ninjectKernel.Get<ICACHE_Q>();
private ISMS_USERVERIFYCODE sms_VerifyCode = NinjectControllerFactory.ninjectKernel.Get<ISMS_USERVERIFYCODE>();
private ISMS_USERVERIFYCODE_Q sms_VerifyCode_Q = NinjectControllerFactory.ninjectKernel.Get<ISMS_USERVERIFYCODE_Q>();
private ISOFT_USER_Q soft_user = NinjectControllerFactory.ninjectKernel.Get<ISOFT_USER_Q>();
private IRES_CUSTOMER res_customer = NinjectControllerFactory.ninjectKernel.Get<IRES_CUSTOMER>();
private IRES_RESOURCEMOBILE_Q RESOURCEMOBILE_Q = NinjectControllerFactory.ninjectKernel.Get<IRES_RESOURCEMOBILE_Q>();
private string clientKey = WX.CRM.Common.Utility.GetSettingByKey("CRMClientKey");
#region
#region
// 在此处添加更多操作并使用 [OperationContract] 标记它们
public Stream GetVerifyCode(string content, string clientid, string sign)
{
WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
string url = Utility.GetClassAndMethodName(1);
string mobile = "";
string retMsg = string.Empty;
logHelper.AddCallLog(content, clientid, sign, url);
//验证是否非法请求
if (!sHelper.CheckClientValid(clientid, content, sign))
{
return Utility.GetStream(JsonHelper.ObjDivertToJson(new { result = false, retcode = (int)EnumInterfaceErrcode. }));
}
bool isTrue = true;
SmsServiceModel getInData = null;
try
{
content = sHelper.decyptData(clientid, content); //解密操作
getInData = JsonHelper.JsonDivertToObj<SmsServiceModel>(content);
mobile = getInData.mobile;
if (string.IsNullOrEmpty(mobile))
{
retMsg = Utility.ObjectToJson(new { result = false, retcode = (int)EnumInterfaceErrcode., retMessage = "手机号码错误" });
isTrue = false;
}
if (string.IsNullOrEmpty(getInData.typeCode) && string.IsNullOrWhiteSpace(getInData.SubTypeCode))
{
retMsg = Utility.ObjectToJson(new { result = false, retcode = (int)EnumInterfaceErrcode., retMessage = "短信类别为空" });
isTrue = false;
}
}
catch (Exception e)
{
retMsg = Utility.ObjectToJson(new { result = false, retcode = (int)EnumInterfaceErrcode., retMessage = "参数错误" });
isTrue = false;
LogHelper.Error(string.Concat("当前请求Url", url, e.Message, e.StackTrace));
}
if (isTrue)
{
try
{
if (!Utility.ChekMobile(mobile))
{
retMsg = Utility.ObjectToJson(new { result = false, retcode = (int)EnumInterfaceErrcode., retMessage = "手机号码错误" });
}
else
{
var theSms = cacheQ.GetList_Parameter(ParameterGroup.SMS_CONFIG);
int sendcodetime = Convert.ToInt32(theSms.Where(p => p.PARAKEY.Trim() == Parameter.SMS_sendcodetime.ToString().Trim()).FirstOrDefault().PARAVALUE);
int sendcodecount = Convert.ToInt32(theSms.Where(p => p.PARAKEY.Trim() == Parameter.SMS_sendcodecount.ToString().Trim()).FirstOrDefault().PARAVALUE);
retMsg = sms_VerifyCode.GetVerifyCodeHandler(mobile, sendcodetime, sendcodecount, getInData.SubTypeCode, getInData.typeCode, getInData.createteuser);
}
}
catch (Exception e)
{
retMsg = Utility.ObjectToJson(new { result = false, retcode = (int)EnumInterfaceErrcode., retMessage = "系统错误" });
LogHelper.Error(string.Concat("用户重置获取激活码出错,用户名:", "用户手机号码:", mobile, e.Message, e.StackTrace));
}
}
//retMsg = sHelper.encyptData(clientid, retMsg);
//return retMsg;
return Utility.GetStream(sHelper.encyptData(clientid, retMsg));
}
#endregion
#region
public Stream ConfirmVerifyCode(string content, string clientid, string sign)
{
WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
string url = Utility.GetClassAndMethodName(1);
string mobile = "";
string retMsg = string.Empty;
logHelper.AddCallLog(content, clientid, sign, url);
//验证是否非法请求
if (!sHelper.CheckClientValid(clientid, content, sign))
{
//retMsg = JsonHelper.ObjDivertToJson(new { result = false, retcode = (int)EnumInterfaceErrcode.非法请求 });
//retMsg = sHelper.encyptData(clientid, retMsg);
//return retMsg;
return Utility.GetStream(JsonHelper.ObjDivertToJson(new { result = false, retcode = (int)EnumInterfaceErrcode. }));
}
content = sHelper.decyptData(clientid, content); //解密操作
bool isTrue = true;
SmsServiceModel getInData = null;
try
{
getInData = JsonHelper.JsonDivertToObj<SmsServiceModel>(content);
mobile = getInData.mobile;
if (string.IsNullOrEmpty(mobile))
{
retMsg = Utility.ObjectToJson(new { result = false, retcode = (int)EnumInterfaceErrcode., retMessage = "手机号码错误" });
isTrue = false;
}
if (string.IsNullOrEmpty(getInData.typeCode) && string.IsNullOrWhiteSpace(getInData.SubTypeCode))
{
retMsg = Utility.ObjectToJson(new { result = false, retcode = (int)EnumInterfaceErrcode., retMessage = "短信类别为空" });
isTrue = false;
}
}
catch (Exception e)
{
retMsg = Utility.ObjectToJson(new { result = false, retcode = (int)EnumInterfaceErrcode., retMessage = "参数错误" });
isTrue = false;
LogHelper.Error(string.Concat("当前请求Url", url, e.Message, e.StackTrace));
}
if (isTrue)
{
try
{
if (string.IsNullOrWhiteSpace(getInData.resid) && !string.IsNullOrWhiteSpace(getInData.mobile))
{
getInData.resid = ResUtil.CreateResId(getInData.mobile);
}
retMsg = sms_VerifyCode.CheckOpenAccount(getInData.resid, getInData.code, getInData.SubTypeCode);
}
catch (Exception e)
{
retMsg = Utility.ObjectToJson(new { result = false, retcode = (int)EnumInterfaceErrcode. });
LogHelper.Error(e.Message + "::" + e.StackTrace);
}
}
//retMsg = Utility.GetStream(sHelper.encyptData(clientid, retMsg));
//return retMsg;
return Utility.GetStream(sHelper.encyptData(clientid, retMsg));
}
#endregion
#endregion
#region
public Stream SendMsg(string content, string clientid, string sign)
{
WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
string url = Utility.GetClassAndMethodName(1);
string retMsg = string.Empty;
logHelper.AddCallLog(content, clientid, sign, url);
//验证是否非法请求
if (!sHelper.CheckClientValid(clientid, content, sign))
{
//retMsg = JsonHelper.ObjDivertToJson(new { result = false, retcode = (int)EnumInterfaceErrcode.非法请求 });
//retMsg = sHelper.encyptData(clientid, retMsg);
//return retMsg;
return Utility.GetStream(JsonHelper.ObjDivertToJson(new { result = false, retcode = (int)EnumInterfaceErrcode. }));
}
content = sHelper.decyptData(clientid, content); //解密操作
bool isTrue = true;
SmsServiceModel getInData = null;
try
{
getInData = JsonHelper.JsonDivertToObj<SmsServiceModel>(content);
if (string.IsNullOrEmpty(getInData.mobile) && string.IsNullOrWhiteSpace(getInData.resid))
{
retMsg = Utility.ObjectToJson(new { result = false, retcode = (int)EnumInterfaceErrcode., retMessage = "手机号码或resid为空" });
isTrue = false;
}
if (string.IsNullOrEmpty(getInData.typeCode) && string.IsNullOrWhiteSpace(getInData.SubTypeCode))
{
retMsg = Utility.ObjectToJson(new { result = false, retcode = (int)EnumInterfaceErrcode., retMessage = "短信类别为空" });
isTrue = false;
}
if (string.IsNullOrWhiteSpace(getInData.message))
{
retMsg = Utility.ObjectToJson(new { result = false, retcode = (int)EnumInterfaceErrcode., retMessage = "不能发送空信息" });
isTrue = false;
}
}
catch (Exception e)
{
retMsg = Utility.ObjectToJson(new { result = false, retcode = (int)EnumInterfaceErrcode., retMessage = "参数错误" });
isTrue = false;
LogHelper.Error(string.Concat("当前请求Url", url, e.Message, e.StackTrace));
}
if (isTrue)
{
try
{
if (string.IsNullOrWhiteSpace(getInData.resid) && !string.IsNullOrWhiteSpace(getInData.mobile))
{
getInData.resid = ResUtil.CreateResId(getInData.mobile);
res_customer.ResgisterCustomer(getInData.mobile, getInData.resid, getInData.typeCode);
}
string cilentCode = sms_VerifyCode_Q.GetCilentCode(getInData.SubTypeCode);
if (!string.IsNullOrWhiteSpace(cilentCode))
{
sms_VerifyCode.SendMessageToUser(getInData.resid, getInData.typeCode, getInData.SubTypeCode, getInData.message, cilentCode, getInData.createteuser);
retMsg = Utility.ObjectToJson(new { result = true, retcode = (int)EnumInterfaceErrcode., retMessage = "调用成功" });
}
else
{
retMsg = Utility.ObjectToJson(new { result = false, retcode = (int)EnumInterfaceErrcode., retMessage = "参数错误" });
}
}
catch (Exception e)
{
LogHelper.Error(e.ToString());
retMsg = Utility.ObjectToJson(new { result = false, retcode = (int)EnumInterfaceErrcode., retMessage = "系统错误" });
}
}
return Utility.GetStream(sHelper.encyptData(clientid, retMsg));
}
#endregion
#region
/// <summary>
/// 重置密码
/// </summary>
/// <param name="content"></param>
/// <param name="clientid"></param>
/// <param name="sign"></param>
/// <returns></returns>
public string ResetPwd(string content, string clientid, string sign)
{
string url = Utility.GetClassAndMethodName(1);
string retMsg = string.Empty;
string resid = string.Empty;
logHelper.AddCallLog(content, clientid, sign, url);
//验证是否非法请求
if (!sHelper.CheckClientValid(clientid, content, sign))
{
retMsg = JsonHelper.ObjDivertToJson(new { result = false, retcode = (int)EnumInterfaceErrcode. });
retMsg = sHelper.encyptData(clientid, retMsg);
return retMsg;
}
content = sHelper.decyptData(clientid, content); //解密操作
bool isTrue = true;
SmsServiceModel getInData = null;
try
{
getInData = JsonHelper.JsonDivertToObj<SmsServiceModel>(content);
if (string.IsNullOrWhiteSpace(getInData.resid))
{
retMsg = Utility.ObjectToJson(new { result = false, retcode = (int)EnumInterfaceErrcode., retMessage = "客户ID不能为空" });
isTrue = false;
}
if (string.IsNullOrEmpty(getInData.typeCode) && string.IsNullOrWhiteSpace(getInData.SubTypeCode))
{
retMsg = Utility.ObjectToJson(new { result = false, retcode = (int)EnumInterfaceErrcode., retMessage = "短信类别为空" });
isTrue = false;
}
}
catch (Exception e)
{
retMsg = Utility.ObjectToJson(new { result = false, retcode = (int)EnumInterfaceErrcode., retMessage = "参数错误" });
isTrue = false;
LogHelper.Error(string.Concat("当前请求Url", url, e.Message, e.StackTrace));
}
if (isTrue)
{
try
{
//SOFT_USER_EXTEND user = soft_user.GetUser_ByUserName(getInData.resid);//getInData.resid 存的是username
SOFT_USER_EXTEND user = new SOFT_USER_EXTEND();
if (!string.IsNullOrEmpty(getInData.resid) && !string.IsNullOrEmpty(getInData.username))
{
user = soft_user.GetUser_ByResId(getInData.username, getInData.resid);
}
else
{
user = soft_user.GetUser_ByUserName(getInData.resid);//getInData.resid 存的是username
}
if (null != user && user.USERNO.HasValue)
{
if (clientKey == "nj_crm")
{
clientid = SecurityHelper.OrderClientIdKey;
}
PhoneLogModel phoneLogModel = new PhoneLogModel
{
Method = System.Reflection.MethodBase.GetCurrentMethod().Name,
userid = 0
};
string mobile = RESOURCEMOBILE_Q.GetNumberByResId(user.RESID, phoneLogModel);
string json = JsonHelper.ObjDivertToJson(new { username = user.USERNAME, mobile = mobile });
string key = sHelper.createSignEncodingStr2(json, clientid);
string host = cacheQ.GetValue_Parameter(Parameter.SMS_BCandJMDRetPwd);//获取密码接口
string result = Utility.PostData(host + "?" + key, System.Text.Encoding.UTF8);
// Utility.HttpPostData(host, key, Encoding.UTF8);
// result = JsonHelper.JsonDivertToObj<string>(result);
result = sHelper.decyptData(clientid, result); //解密操作
Retinfo rt = JsonHelper.JsonDivertToObj<Retinfo>(result);
if (rt != null)
{
if (rt.result && rt.retcode == "10000" && rt.retpwd != "")
{
string RetrievePwdconfig = cacheQ.GetValue_Parameter(WX.CRM.Model.Enum.Parameter.Sys_RetrievePwd_Name);//从Sys_RetrievePwd_Name配置中读取找回密码短信配置
RetrievePwdconfig = string.IsNullOrEmpty(RetrievePwdconfig) ? "您好,您的账号{0}的新密码是{1}" : RetrievePwdconfig;
string cilentCode = sms_VerifyCode_Q.GetCilentCode(getInData.SubTypeCode);
string mssage = string.Format(RetrievePwdconfig, user.USERNAME, rt.retpwd);// "您好,您的" + companyPName + "账号" + user.USERNAME + "的新密码是" + rt.pwd + "";
if (!string.IsNullOrWhiteSpace(cilentCode))
{
sms_VerifyCode.SendMessageToUser(user.RESID, getInData.typeCode, getInData.SubTypeCode, mssage, cilentCode, getInData.createteuser);
retMsg = Utility.ObjectToJson(new { result = true, retcode = (int)EnumInterfaceErrcode., retMessage = "调用成功" });
}
else
{
retMsg = Utility.ObjectToJson(new { result = false, retcode = (int)EnumInterfaceErrcode., retMessage = "参数错误" });
}
}
else
{
string msg = ((EnumInterfaceErrcode)Enum.Parse(typeof(EnumInterfaceErrcode), rt.retcode)).ToString();
retMsg = Utility.ObjectToJson(new { result = false, retcode = rt.retcode, retMessage = msg });
}
}
else
{
retMsg = Utility.ObjectToJson(new { result = false, retcode = (int)EnumInterfaceErrcode., retMessage = "参数错误" });
}
}
else
{
retMsg = Utility.ObjectToJson(new { result = false, retcode = (int)EnumInterfaceErrcode., retMessage = "此用户无卡号" });
}
}
catch (Exception e)
{
LogHelper.Error(e.Message + e.StackTrace);
retMsg = Utility.ObjectToJson(new { result = false, retcode = (int)EnumInterfaceErrcode., retMessage = "系统错误" });
}
}
retMsg = sHelper.encyptData(clientid, retMsg);
return retMsg;
}
#endregion
#region
public JsonResult PutSms(Input input)
{
var content = HttpUtility.UrlDecode(input.content);
var clientid = input.clientid;
var sign = HttpUtility.UrlDecode(input.sign);
//string url = Utility.GetClassAndMethodName(1);
LogHelper.Info(content);
string retMsg = string.Empty;
string resid = string.Empty;
//logHelper.AddCallLog(content, clientid, sign, url);
//验证是否非法请求
if (!sHelper.CheckClientValid(clientid, content, sign))
{
return new JsonResult() { result = false, retcode = (int)EnumInterfaceErrcode. };
}
content = sHelper.decyptData(clientid, content);
try
{
var guid = Guid.NewGuid();
var smsList = Utility.JSONToObject<List<SmsDto>>(content);
DataTable dt = new DataTable();
dt.Columns.Add("to", typeof(string));
dt.Columns.Add("sms", typeof(string));
dt.Columns.Add("pici", typeof(Guid));
dt.Columns.Add("mobile", typeof(string));
dt.Columns.Add("isfirst", typeof(int));
foreach (var item in smsList)
{
DataRow dr = dt.NewRow();
dr["to"] = item.mobile;
dr["sms"] = item.message;
dr["pici"] = guid;
dr["mobile"] = sHelper.encyptData(clientid, item.mobile);
dr["isfirst"] = 0;
dt.Rows.Add(dr);
}
SqlHelper.BulkInsert(SqlHelper.DatabaseType.AYCRM, "SmsTask", dt);
return new JsonResult() { result = true, retcode = (int)EnumInterfaceErrcode. };
}
catch (Exception ex)
{
LogHelper.Error(ex.ToString());
return new JsonResult() { result = false, retcode = (int)EnumInterfaceErrcode. };
}
}
public JsonResult PutSms2(Input input)
{
var content = HttpUtility.UrlDecode(input.content);
var clientid = input.clientid;
var sign = HttpUtility.UrlDecode(input.sign);
//string url = Utility.GetClassAndMethodName(1);
LogHelper.Info(content);
string retMsg = string.Empty;
string resid = string.Empty;
//logHelper.AddCallLog(content, clientid, sign, url);
//验证是否非法请求
if (!sHelper.CheckClientValid(clientid, content, sign))
{
return new JsonResult() { result = false, retcode = (int)EnumInterfaceErrcode. };
}
content = sHelper.decyptData(clientid, content);
try
{
var guid = Guid.NewGuid();
var smsList = Utility.JSONToObject<List<SmsDto>>(content);
DataTable dt = new DataTable();
dt.Columns.Add("to", typeof(string));
dt.Columns.Add("sms", typeof(string));
dt.Columns.Add("pici", typeof(Guid));
dt.Columns.Add("mobile", typeof(string));
dt.Columns.Add("isfirst", typeof(int));
foreach (var item in smsList)
{
DataRow dr = dt.NewRow();
dr["to"] = item.mobile;
dr["sms"] = item.message;
dr["pici"] = guid;
dr["mobile"] = sHelper.encyptData(clientid, item.mobile);
dr["isfirst"] = 1;
dt.Rows.Add(dr);
}
SqlHelper.BulkInsert(SqlHelper.DatabaseType.AYCRM, "SmsTask", dt);
return new JsonResult() { result = true, retcode = (int)EnumInterfaceErrcode. };
}
catch (Exception ex)
{
LogHelper.Error(ex.ToString());
return new JsonResult() { result = false, retcode = (int)EnumInterfaceErrcode. };
}
}
#endregion
}
public class SmsDto
{
public string message { get; set; }
public string mobile { get; set; }
}
}