using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using Itlead.Architecture.Contract;
using Itlead.Architecture.Contract.DataContract;
using Newtonsoft.Json;
using System.Threading;
using System.IO;
namespace Itlead.Architecture.WebAppUI.tools
{
///
/// getPhoneCode 的摘要说明
///
public class getPhoneCode : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/json";
ReturnData re = new ReturnData();
try
{
string username = context.Request.Params["username"];
string phone = context.Request.Params["phone"];
int type = int.Parse(context.Request.Params["type"]);
string code = context.Request.Params["code"];
int sms_num = 0;
if (type != 0 && type != 1 && type != 2 && type != 3 && type != 4 && type != 5 && type != 6)
{
return;
}
string type_msg = "";
re.flag = true;
if (type != 5 && type != 6)
{
if (context.Session["SecurityComputeCode" + type] == null || code == null || code.Trim().Length == 0)
{
re.flag = false;
re.msg = "请输入验证码";
re.data = 1;
}
else if (!context.Session["SecurityComputeCode" + type].Equals(code.Trim()))
{
re.flag = false;
re.msg = "验证码错误";
re.data = 2;
}
}
if (re.flag) {
if (type == 0)
{
var s_user = MyBasicLib.Utilities.ServiceInvoker.Invoke("userService", u => u.GetUser(username));
if (s_user.ConfirmPhone == true)
{
phone = s_user.telphone;
}
else {
var s = MyBasicLib.Utilities.ServiceInvoker.Invoke("userService", u => u.CheckPhone(phone, type, null));
re.flag = s;
if (!s)
{
re.msg = "手机号码已存在";
re.data = 0;
}
}
if (re.flag) {
type_msg = "登录";
sms_num = MyBasicLib.Utilities.ServiceInvoker.Invoke("PhoneSMSService", u => u.getNum(phone, type));
}
}
else if (type == 1)
{
sms_num = MyBasicLib.Utilities.ServiceInvoker.Invoke("PhoneSMSService", u => u.getNum(phone, type));
var s = MyBasicLib.Utilities.ServiceInvoker.Invoke("userService", u => u.CheckPhone(phone, type, null));
re.flag = s;
if (!s) {
re.msg = "手机号码已存在";
re.data = 0;
}
type_msg = "注册";
}
else if (type == 2)
{
if (!(context.Session["ConfirmPhone"] == null || (bool)context.Session["ConfirmPhone"] == false))
{
var s_user = MyBasicLib.Utilities.ServiceInvoker.Invoke("userService", u => u.GetUserId(new Guid(context.Request.Cookies["user"]["UserId"])));
phone = s_user.telphone;
}
else {
var s = MyBasicLib.Utilities.ServiceInvoker.Invoke("userService", u => u.CheckPhone(phone, type, new Guid(context.Request.Cookies["user"]["UserId"])));
re.flag = s;
if (!s)
{
re.msg = "手机号码已存在";
re.data = 0;
}
}
sms_num = MyBasicLib.Utilities.ServiceInvoker.Invoke("PhoneSMSService", u => u.getNum(phone, type));
type_msg = "验证";
}
else if (type == 3)
{
var s_user = MyBasicLib.Utilities.ServiceInvoker.Invoke("userService", u => u.GetUserId(new Guid(context.Request.Cookies["user"]["UserId"])));
if (s_user.EditPhoneTime == null)
{
re.flag = false;
re.msg = "非法请求,请先验证原手机号码";
re.data = 4;
}
else if ((System.DateTime.Now - (DateTime)s_user.EditPhoneTime).TotalMinutes > int.Parse(System.Configuration.ConfigurationManager.AppSettings["CodeTime"]))
{
re.flag = false;
re.msg = "请求已超时,请重新认证";
re.data = 5;
}
else if (!MyBasicLib.Utilities.ServiceInvoker.Invoke("userService", u => u.CheckPhone(phone, type, new Guid(context.Request.Cookies["user"]["UserId"])))) {
re.flag = false;
re.msg = "手机号码已存在";
re.data = 0;
}
else
{
sms_num = MyBasicLib.Utilities.ServiceInvoker.Invoke("PhoneSMSService", u => u.getNum(phone, 2));
type_msg = "绑定";
}
}
else if (type == 4)
{
var s_user = MyBasicLib.Utilities.ServiceInvoker.Invoke("userService", u => u.GetUser(username));
if (s_user.ConfirmPhone == false) {
re.flag = false;
re.msg = "非法请求,用户名和手机号码没有绑定关系";
re.data = 7;
}
else if (!s_user.telphone.Equals(phone))
{
re.flag = false;
re.msg = "非法请求,用户名和手机号码没有绑定关系";
re.data = 7;
}
else
{
sms_num = MyBasicLib.Utilities.ServiceInvoker.Invoke("PhoneSMSService", u => u.getNum(phone, type));
type_msg = "重置密码";
}
}
else if (type == 5)
{
sms_num = MyBasicLib.Utilities.ServiceInvoker.Invoke("PhoneSMSService", u => u.getNum(phone, type));
var s = MyBasicLib.Utilities.ServiceInvoker.Invoke("userService", u => u.CheckPhone(phone, type, null));
if (s)
{
re.msg = "手机号码不存在";
re.data = 0;
re.flag = false;
}
}
else if (type == 6)
{
sms_num = MyBasicLib.Utilities.ServiceInvoker.Invoke("PhoneSMSService", u => u.getNum(phone, type));
var s = MyBasicLib.Utilities.ServiceInvoker.Invoke("userService", u => u.CheckPhone(phone, type, null));
if (!s)
{
re.msg = "手机号码已存在";
re.data = 0;
re.flag = false;
}
}
}
if (re.flag)
{
if (sms_num >= int.Parse(System.Configuration.ConfigurationManager.AppSettings["MaxPhoneCode"]))
{
context.Session.Remove("phone_code" + type);
context.Session.Remove("phone_code_time" + type);
context.Session.Remove("SecurityComputeCode" + type);
re.flag = false;
if (System.Configuration.ConfigurationManager.AppSettings["getCodePhone"].Length == 0)
{
re.msg = "该手机已经超过每日接收短信上限
暂时无法进行操作,请联系客服";
}
else {
re.msg = "该手机已经超过每日接收短信上限
请用该手机发送\"" + type_msg + "\"到" + System.Configuration.ConfigurationManager.AppSettings["getCodePhone"] + "
(运营商收取0.1元/条)";
}
re.data = 6;
}
else {
MyBasicLib.Utilities.ServiceInvoker.Invoke("PhoneSMSService", u => u.setNum(phone, type == 3 ? 2 : type));
int code_len = 4;
string phone_code = new Random((int)DateTime.Now.Ticks).Next((int)Math.Pow(10, (code_len - 1)), (int)Math.Pow(10, (code_len))).ToString();
SendSMSData sms_data = new SendSMSData();
sms_data.telphone = phone;
sms_data.code = phone_code;
sms_data.type = type;
context.Session.Add("phone_code" + type, phone + phone_code);
context.Session.Add("phone_code_time" + type, System.DateTime.Now.ToString());
context.Session.Remove("SecurityComputeCode" + type);
WxPayAPI.Log.Info(this.GetType().ToString(), "用户名:"+username + ",开始发送" + type_msg + "短信到:" + phone);
Thread t1 = new Thread(new ParameterizedThreadStart(sendSMS));
t1.Start(sms_data);
re.flag = true;
}
}
}
catch (Exception e)
{
re.flag = false;
re.msg = e.Message;
}
context.Response.Write(JsonConvert.SerializeObject(re));
}
public ReturnData checkSMS(HttpContext context,string phone,string code,int type) {
ReturnData re = new ReturnData();
string msg = "";
if (type == 0) {
msg = "登录";
} else if (type == 1) {
msg = "注册";
}
else if (type == 2)
{
msg = "验证";
}
else if (type == 3)
{
msg = "绑定";
}
else if (type == 4)
{
msg = "重置密码";
}
else if (type == 5)
{
msg = "手机登录";
}
else if (type == 6)
{
msg = "手机注册";
}
re.flag = true;
try {
if (context.Session["phone_code" + type] == null)
{
NewSMSData ns = MyBasicLib.Utilities.ServiceInvoker.Invoke("NewSMSService", u => u.getSms(phone, msg));
DateTime? sms_time;
if (ns == null) {
sms_time = null;
} else {
sms_time = ns.sms_time;
}
if (sms_time == null)
{
re.flag = false;
re.msg = "请点击获取验证码或者发送验证短信";
re.data = "notsmscode";
}
else if ((System.DateTime.Now - (DateTime)sms_time).TotalMinutes > int.Parse(System.Configuration.ConfigurationManager.AppSettings["CodeTime"]))
{
re.flag = false;
re.msg = "验证短信已过期,请重新发送";
re.data = "getsmstimeout";
}
}
else if ((System.DateTime.Now - Convert.ToDateTime(context.Session["phone_code_time" + type])).TotalMinutes > int.Parse(System.Configuration.ConfigurationManager.AppSettings["CodeTime"]))
{
re.flag = false;
re.msg = "短信验证码已过期,请重新获取";
re.data = "phonecodetimeout";
}
else if (!context.Session["phone_code" + type].Equals(phone + code))
{
re.flag = false;
re.msg = "短信验证码错误";
re.data = "phonecodeerror";
}
if (re.flag == true)
{
NewSMSData ns = new NewSMSData();
ns.telphone = phone;
ns.msg = msg;
context.Session.Remove("phone_code_time" + type);
context.Session.Remove("phone_code" + type);
MyBasicLib.Utilities.ServiceInvoker.Invoke("NewSMSService", u => u.delSMS(ns));
}
} catch (Exception e) {
re.flag = false;
re.msg = e.Message;
re.data = "error";
}
return re;
}
private void sendSMS(object oj) {
//向用户手机发送短信
SendSMSData sms_data = (SendSMSData)oj;
try {
var type_msg = "";
if (sms_data.type == 0)
{
type_msg = "登录";
}
else if (sms_data.type == 1)
{
type_msg = "注册";
}
else if (sms_data.type == 2)
{
type_msg = "验证手机";
}
else if (sms_data.type == 3)
{
type_msg = "绑定新手机";
}
else if (sms_data.type == 4)
{
type_msg = "重置密码";
}
string send_text = "验证码:"+sms_data.code+",陪学网用户,您正在进行"+ type_msg + "操作,验证码有效期为"+ System.Configuration.ConfigurationManager.AppSettings["CodeTime"] + "分钟[请勿将验证码告知他人]";
send_text = System.Web.HttpUtility.UrlEncode(send_text, System.Text.Encoding.GetEncoding("gb2312"));
// 备用IP地址为203.81.21.13
string url = "http://203.81.21.34/send/gsend.asp?name="+ System.Configuration.ConfigurationManager.AppSettings["PhoneSMSAccount"] + "&pwd="+ System.Configuration.ConfigurationManager.AppSettings["PhoneSMSPassword"] + "&dst=" + sms_data.telphone + "&msg=" + send_text ;
System.Net.HttpWebResponse rs = (System.Net.HttpWebResponse)System.Net.HttpWebRequest.Create(url).GetResponse();
System.IO.StreamReader sr = new System.IO.StreamReader(rs.GetResponseStream(), System.Text.Encoding.Default);
string url_get_text = sr.ReadToEnd();
//string url_get_text = "num=0";
if (url_get_text.Length == 0 || url_get_text.Substring(0, 5) != "num=0")
{
WxPayAPI.Log.Error(this.GetType().ToString(), "手机号码:" + sms_data.telphone + ",短信验证码:" + sms_data.code + ",短信类型:" + type_msg + ",返回内容:" + url_get_text + ",状态:error");
//发送失败
}
else {
WxPayAPI.Log.Info(this.GetType().ToString(), "手机号码:" + sms_data.telphone + ",短信验证码:" + sms_data.code + ",短信类型:" + type_msg + ",状态:success");
//发送成功
}
}
catch (Exception e) {
WxPayAPI.Log.Error(this.GetType().ToString(), e.ToString());
//出现错误
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}