123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417 |
- #include "stdafx.h"
- #include "SMSHandle.h"
- #include <WinNls.h>
- #include <Windows.h>
- #include "DataManager.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- #define SENDCOUNT _T("SendCount")
- #define BALANCE _T("Balance")
- #define ERPHONE _T("ErrorPhone")
- #define SENDSID _T("Sid")
- #define QUERYCOUNT _T("SMSCharacterCount")
- #define QUERYSIGLE _T("Signatures")
- #define QUERYOWNER _T("OwnedOperators")
- #define ACCOUNT _T("account")
- #define SIGNATURES _T("Signatures")
- #define OPERATORS _T("OwnedOperators")
- #define RETH _T("<ReturnInfo><![CDATA[")
- #define RETE _T("]]></ReturnInfo>")
- //#define USE_DOMAIN 0
- #define SENDSMS _T("http://%s:%d/webService/SendSmsMessage?account=%s&password=%s&phone=%s&content=%s&time=%s&t=%s")
- #define QUERYACCOUNT _T("http://%s:%d/webService/getuserinfo?account=%s&password=%s&t=%s")
- #define QUERYSEND _T("http://%s:%d/webService/QuerySendRecordInfo?account=%s&password=%s&sid=%s&t=%s")
- #define INSERTSENDREG _T("insert into sendreg([msgtype],[phones],[content],[timestamp],[msgcount],[status],[issended],[isautosend],[ren]) values('%s','%s','%s','%s','%d','%s','%s','%s','%s')")
- //#define INSERTSENDREG_old _T("insert into sendreg([phones],[content],[timestamp],[msgcount],[status],[issended],[isautosend],[ren]) values('%s','%s','%s','%d','%s','%s','%s','%s')")
- #define UPDATESENDREG _T("update sendreg set msgcount='%s',log='%s',status='%s',issended='%s' where autoid='%s'")
- //#define UPDATE_VERSION _T(" update [version] set [msgbalance] = convert(int,msgbalance,10) - %s, [msgused] = convert(int,msgused,10) + %s ");
- #define UPDATE_VERSION _T(" update [version] set [msgbalance] = '%s', [msgused] = convert(int,msgused) + %s ")
- CSMSHandle::CSMSHandle()
- {
- m_url.Initialize();
- }
- CSMSHandle::~CSMSHandle()
- {
-
- }
- void CSMSHandle::UrlUTF8Decode(LPBYTE lpRespone, CString &strRespone)
- {
- // 从uft-8转到gb2312,再转到unicode;
- std::string str = strCoding::UrlUTF8Decode((LPSTR)lpRespone);
- INT wLen = 0;
- wLen = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
- WCHAR *ptRespone = new WCHAR[wLen];
- memset(ptRespone, 0, wLen*sizeof(WCHAR));
- MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, ptRespone, wLen);
- strRespone.Format(_T("%s"), ptRespone);
- if (ptRespone)
- {
- delete ptRespone;
- ptRespone = NULL;
- }
- }
- //************************************//
- // [函数]:GetSMSBlance
- // [描述]:获取帐户信息
- // [参数]:
- // pSMSServer 短信服务端主机名称;
- // dwPort 短信服务端主机通信端口
- // strAccount 登录帐号
- // strPwd 密码
- // tAccountInfo 返回帐户的信息
- // [返回]:0成功, -1失败
- //************************************//
- int CSMSHandle::GetSMSBlance(const TCHAR* pSMSServer, const DWORD& dwPort, CString& strAccount, CString& strPwd, QuerAccountInfo& tAccountInfo)
- {
- AutoThreadSection aSection(&s_critSection);
- CString str = _T("");
- CString strRespone;
- str.Format(QUERYACCOUNT,pSMSServer, dwPort,
- utilitySdk::CharEncoding::EnCode_UTF8URL(strAccount).c_str(),
- utilitySdk::CharEncoding::EnCode_UTF8URL(strPwd).c_str(), utilitySdk::CharEncoding::EnCode_UTF8URL(CTime::GetCurrentTime().Format(_T("%Y%m%d%H%M%S"))).c_str());
- if (0 == m_url.Get(str, strRespone))
- {
- INT nPos = strRespone.Find(RETH);
- strRespone.Delete(0, nPos + _tcslen(RETH));
- nPos = strRespone.Find(RETE);
- strRespone = strRespone.Mid(0, nPos);
- // 解析参数值;
- nPos = strRespone.Find(ACCOUNT);
- tAccountInfo.strSendStatus = strRespone.Left(nPos - 1);
- strRespone.Delete(0, nPos + _tcslen(ACCOUNT) + 1);
- nPos = strRespone.Find(BALANCE);
- tAccountInfo.strAccount = strRespone.Left(nPos - 1);
- strRespone.Delete(0, nPos + _tcslen(BALANCE) + 1);
- nPos = strRespone.Find(QUERYCOUNT);
- tAccountInfo.strBalance = strRespone.Left(nPos - 1);
- strRespone.Delete(0, nPos + _tcslen(QUERYCOUNT) + 1);
- nPos = strRespone.Find(SIGNATURES);
- tAccountInfo.strSMSCharacterCount = strRespone.Left(nPos - 1);
- strRespone.Delete(0, nPos + _tcslen(SIGNATURES) + 1);
- nPos = strRespone.Find(OPERATORS);
- tAccountInfo.strSignatures = strRespone.Left(nPos - 1);
- strRespone.Delete(0, nPos + _tcslen(OPERATORS) + 1);
- tAccountInfo.strOwnedOperators = strRespone;
- return TRUE;
- }
- return FALSE;
- }
- void CSMSHandle::UTF8ToWideChar(LPBYTE lpRespone, CString &strRespone)
- {
- // 从uft-8转到gb2312,再转到unicode;
- std::string str;
- strCoding::UTF_8ToGB2312(str, (char*)lpRespone, strlen((char*)lpRespone));
- INT wLen = 0;
- wLen = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
- WCHAR *ptRespone = new WCHAR[wLen];
- memset(ptRespone, 0, wLen*sizeof(WCHAR));
- MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, ptRespone, wLen);
- strRespone.Format(_T("%s"), ptRespone);
- if (ptRespone)
- {
- delete ptRespone;
- ptRespone = NULL;
- }
- }
- INT CSMSHandle::AnalyzeSendStatus(LPBYTE lpRespone, ResponebySend &lRespone)
- {
- CString strRespone = (TCHAR*)lpRespone;
- WriteTextLog("服务端返回内容:%s",strRespone);
- INT nIndex = strRespone.Find(RETH);
- if ( nIndex == -1 )
- {
- WriteTextLog("没有找到:\"<ReturnInfo><![CDATA[\" 元素");
- return -13;
- }
- strRespone.Delete(0, nIndex + _tcslen(RETH));
- nIndex = strRespone.Find(RETE);
- if ( nIndex == -1 )
- {
- WriteTextLog("没有找到:\"]]></ReturnInfo>\" 元素");
- return -13;
- }
- strRespone = strRespone.Mid(0, nIndex);
- // 解析参数值;
- nIndex = strRespone.Find(SENDCOUNT);
- if ( nIndex == -1 )
- {
- WriteTextLog("没有找到:\"SendCount\" 元素");
- return -13;
- }
- lRespone.strSendStatus = strRespone.Left(nIndex - 1);
- strRespone.Delete(0, nIndex + _tcslen(SENDCOUNT) + 1);
- nIndex = strRespone.Find(BALANCE);
- if ( nIndex == -1 )
- {
- WriteTextLog("没有找到:\"Balance\" 元素");
- return -13;
- }
- lRespone.strSendCount = strRespone.Left(nIndex - 1);
- strRespone.Delete(0, nIndex + _tcslen(BALANCE) + 1);
- nIndex = strRespone.Find(ERPHONE);
- if ( nIndex == -1 )
- {
- WriteTextLog("没有找到:\"ErrorPhone\" 元素");
- return -13;
- }
- lRespone.strBalance = strRespone.Left(nIndex - 1);
- strRespone.Delete(0, nIndex + _tcslen(ERPHONE) + 1);
- nIndex = strRespone.Find(SENDSID);
- if ( nIndex == -1 )
- {
- WriteTextLog("没有找到:\"Sid\" 元素");
- return -13;
- }
- lRespone.strErPhone = strRespone.Left(nIndex - 1);
- strRespone.Delete(0, nIndex + _tcslen(SENDSID) + 1);
- lRespone.strSid = strRespone;
- return _ttoi(lRespone.strSendStatus);
- }
- //************************************//
- // [函数]:SendSMToSever
- // [描述]:获取帐户信息
- // [参数]:
- // pSMSServer 短信服务端主机名称;
- // dwPort 短信服务端主机通信端口
- // strAccount 登录帐号
- // strPwd 密码
- // strPhones 所有要发送的号码
- // strContent 发送的内容
- // strStampTime 时间
- // strAutoID 短信记录ID
- // [返回]:0成功, -1失败
- //************************************//
- int CSMSHandle::SendSMToSever(const TCHAR* pSMSServer, const DWORD& dwPort, const CString& strAccount, const CString& strPwd, const CString& strPhones, const CString& strContent, const CString& strStampTime, const CString& strAutoID)
- {
- if(strAutoID == _T(""))
- return -1;
-
- CString str = _T("");
- str.Format(SENDSMS, pSMSServer, dwPort,
- utilitySdk::CharEncoding::EnCode_UTF8URL(strAccount).c_str(),
- utilitySdk::CharEncoding::EnCode_UTF8URL(strPwd).c_str(),
- utilitySdk::CharEncoding::EnCode_UTF8URL(strPhones).c_str(),
- //strPhones,
- utilitySdk::CharEncoding::EnCode_UTF8URL(strContent).c_str(),
- utilitySdk::CharEncoding::EnCode_UTF8URL(strStampTime).c_str(),
- utilitySdk::CharEncoding::EnCode_UTF8URL(CTime::GetCurrentTime().Format(_T("%Y%m%d%H%M%S"))).c_str());
- CString strResponse;
- if (0 != m_url.Gets(str, strResponse))
- {
- return -1;
- }
- //update sendreg set msgcount='%s',log='%s',status='%s',issended='%s' where autoid='%s'"
- //解析
- ResponebySend lRespone;
- INT nRet = AnalyzeSendStatus((LPBYTE)strResponse.GetString(), lRespone);
- //更新记录
- if(UpdateSMLog(nRet, &lRespone, strAutoID) == -1)
- return -1;
- return 0;
- }
- //************************************//
- // [函数]:GetSMState
- // [描述]:获取短信状态
- // [参数]:
- // pSMSServer 短信服务端主机名称;
- // dwPort 短信服务端主机通信端口
- // strAccount 登录帐号
- // strPwd 密码
- // sr 短信记录信息
- // [返回]:0成功, -1失败
- //************************************//
- int CSMSHandle::GetSMState(const TCHAR* pSMSServer, const DWORD& dwPort, const CString& strAccount, const CString& strPwd, const SSendReg* pSR)
- {
- AutoThreadSection aSection(&s_critSection);
- static CString strQuerySMS;
- strQuerySMS = _T("");
- strQuerySMS.Format(QUERYSEND, pSMSServer, dwPort,
- utilitySdk::CharEncoding::EnCode_UTF8URL(strAccount).c_str(),
- utilitySdk::CharEncoding::EnCode_UTF8URL(strPwd).c_str(),
- utilitySdk::CharEncoding::EnCode_UTF8URL(pSR->Sid).c_str(),
- utilitySdk::CharEncoding::EnCode_UTF8URL(CTime::GetCurrentTime().Format(_T("%Y%m%d%H%M%S"))).c_str());
- static CString strRespone = _T("");
- if (TRUE == m_url.Get(strQuerySMS, strRespone))
- {
- WriteTextLog("查询时返回:%s",strRespone);
- static ResponebySend lRespone;
- lRespone.strSendStatus = _T("");
- lRespone.strSendCount = _T("");
- lRespone.strErPhone = _T("");
- lRespone.strSid = _T("");
- static INT nPos = 0;
- nPos = strRespone.Find(RETH);
- strRespone.Delete(0, nPos + _tcslen(RETH));
- nPos = strRespone.Find(RETE);
- strRespone = strRespone.Mid(0, nPos);
- // 解析参数值;
- nPos = strRespone.Find(SENDCOUNT);
- lRespone.strSendStatus = strRespone.Left(nPos - 1);
- strRespone.Delete(0, nPos + _tcslen(SENDCOUNT) + 1);
- nPos = strRespone.Find(SENDSID);
- lRespone.strSendCount = strRespone.Left(nPos - 1);
- strRespone.Delete(0, nPos + _tcslen(SENDSID) + 1);
- lRespone.strSid = strRespone;
- nPos = 0;
- strRespone = _T("");
- //更新记录
- if(UpdateSMLog(_ttoi(lRespone.strSendStatus), &lRespone, pSR->autoid) == -1)
- return -1;
- }
- return 0;
- }
- //************************************//
- // [函数]:UpdateSMLog
- // [描述]:更新短信记录
- // [参数]:
- // lRespone 服务器响应信息
- // strAutoID 记录ID
- // [返回]:0成功, -1失败
- //************************************//
- int CSMSHandle::UpdateSMLog(const int nRet, const ResponebySend* pRespone, const CString& strAutoID)
- {
- if(pRespone == NULL || strAutoID == _T(""))
- return -1;
- CString strValues = _T("");
- CString strfilter = _T("");
- switch(nRet)
- {
- case 0: // 成功;
- strValues = _T("msgcount='") + pRespone->strSendCount + _T("',log='1/1',status='OK',") + _T("Sid='0'");
- break;
- case -1: // 当前账号余额不足;
- strValues = _T("msgcount='") + pRespone->strSendCount + _T("',log='当前账号余额不足',status='") + pRespone->strSendStatus + _T("',Sid='0'");
- break;
- case -2: // 当前账号错误;
- strValues = _T("msgcount='") + pRespone->strSendCount + _T("',log='当前账号错误',status='") + pRespone->strSendStatus + _T("',Sid='0'");
- break;
- case -3: // 当前密码错误;
- strValues = _T("msgcount='") + pRespone->strSendCount + _T("',log='当前密码错误',status='") + pRespone->strSendStatus + _T("',Sid='0'");
- break;
- case -4: // 参数不够或参数内容的类型错误;
- strValues = _T("msgcount='") + pRespone->strSendCount + _T("',log='参数不够或参数内容的类型错误',status='") + pRespone->strSendStatus + _T("',Sid='0'");
- break;
- case -5: // 手机号码格式不对;
- strValues = _T("msgcount='") + pRespone->strSendCount + _T("',log='手机号码格式不对',status='") + pRespone->strSendStatus + _T("',Sid='0'");
- break;
- case -6: // 短信内容编码不对;
- strValues = _T("msgcount='") + pRespone->strSendCount + _T("',log='短信内容编码不对',status='") + pRespone->strSendStatus + _T("',Sid='0'");
- break;
- case -7: // 短信内容含有敏感字符;
- strValues = _T("msgcount='") + pRespone->strSendCount + _T("',log='短信内容含有敏感字符',status='") + pRespone->strSendStatus + _T("',Sid='0'");
- break;
- case -8: // 无接收数据;
- strValues = _T("msgcount='") + pRespone->strSendCount + _T("',log='无接收数据',status='") + pRespone->strSendStatus + _T("',Sid='0'");
- break;
- case -9: // 系统维护中;
- strValues = _T("msgcount='") + pRespone->strSendCount + _T("',log='无接收数据',status='") + pRespone->strSendStatus + _T("',Sid='0'");
- break;
- case -10: // 手机号码数量超长_每次最多100个;
- strValues = _T("msgcount='") + pRespone->strSendCount + _T("',log='手机号码数量超长_每次最多100个',status='") + pRespone->strSendStatus + _T("',Sid='0'");
- break;
- case -11: // 短信内容超长_每条390个字符;
- strValues = _T("msgcount='") + pRespone->strSendCount + _T("',log='短信内容超长_每条390个字符',status='") + pRespone->strSendStatus + _T("',Sid='0'");
- break;
- case -12: // 其它错误;
- strValues = _T("msgcount='") + pRespone->strSendCount + _T("',log='其它错误',status='") + pRespone->strSendStatus + _T("',Sid='0'");
- break;
- case -13: // 服务器错误;
- strValues = _T("msgcount='") + pRespone->strSendCount + _T("',log='服务器错误',status='") + pRespone->strSendStatus + _T("',Sid='0'");
- break;
- case -14: // 域名不正确;//短信接口不会有该错误返回;
- strValues = _T("msgcount='") + pRespone->strSendCount + _T("',log='域名不正确',status='") + pRespone->strSendStatus + _T("',Sid='0'");
- break;
- case -15: // 域名所在服务器未提交过IP;//短信接口不会有该错误返回;
- strValues = _T("msgcount='") + pRespone->strSendCount + _T("',log='域名所在服务器未提交过IP',status='") + pRespone->strSendStatus + _T("',Sid='0'");
- break;
- case -16: // 硬件码不能小于5个字符;//短信接口不会有该错误返回;
- strValues = _T("msgcount='") + pRespone->strSendCount + _T("',log='硬件码不能小于5个字符',status='") + pRespone->strSendStatus + _T("',Sid='0'");
- break;
- case -17: // 服务器连接失败;
- strValues = _T("msgcount='") + pRespone->strSendCount + _T("',log='服务器连接失败',status='") + pRespone->strSendStatus + _T("',Sid='0'");
- break;
- case -18: // 硬件码不能为空;//短信接口不会有该错误返回;
- strValues = _T("msgcount='") + pRespone->strSendCount + _T("',log='硬件码不能为空',status='") + pRespone->strSendStatus + _T("',Sid='0'");
- break;
- case -19: // 查询的短信记录不存在或已被删除;
- strValues = _T("msgcount='") + pRespone->strSendCount + _T("',log='查询的短信记录不存在或已被删除',status='") + pRespone->strSendStatus + _T("',Sid='0'");
- break;
- case -20: // 短信超时过期忽略发送;
- strValues = _T("msgcount='") + pRespone->strSendCount + _T("',log='短信超时过期忽略发送',status='") + pRespone->strSendStatus + _T("',Sid='0'");
- break;
- case 100: // 服务器授理并等待发送中;并将返回的Sid更新到status中保存;
- strValues = _T("msgcount='") + pRespone->strSendCount + _T("',log='服务器授理并等待发送中',status='") + pRespone->strSendStatus + _T("',") + _T("Sid='") + pRespone->strSid + _T("'");
- break;
- default:
- strValues = _T("");
- break;
- }
- // update数据库;
- CString strTableName = _T("sendreg");
- strfilter = _T("autoid=") + strAutoID;
- if (CDataManager::GetInstance()->Update(strTableName, strValues, strfilter) == -1)
- {
- // 记录失败,需要相应的补救处理,将更新内容写入SQLite数据库中或其他文本中记录;
- // 但同时会加大处理难度,暂时不处理这种情况。
- // 可能会多发一至多条短信;
- WriteTextLog(_T("更新记录数据表%s失败, autoid = %s"), strTableName, strAutoID);
- return -1;
- }
- return 0;
- }
|