VerifyAjax.cs 19.7 KB
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using Quiz.BLL;
using Quiz.Models;
using Quiz.Models.Domain;
using Quiz.Utility;

namespace Quiz.SiteBase
{
    public partial class VerifyAjax : WebSiteBase
    {
        public void Verify(string action)
        {
            OperationResult m = new OperationResult(OperationResultType.Error);
            RequestGeter rg = new RequestGeter(Request.Form);
            switch (action)
            {
               //发送验证码
                case "sendmsgcode":
                {
                    string mobile = Request["mobile"];
                    //先判断是否答过题,如果答过题不发送短信
                    if (BLL.UserBll.IsAnswer(mobile)>0)
                    {
                        m.Status=OperationResultType.ValidError;
                        m.Message = "你已经答过题了";
                        break;
                    }
                    //判断该号码是否在库里面,如果不在则不发短信
                    List<Models.User> userList = (List<Models.User>)SiteBase.Cache.CacheManage.User.CacheData;
                    var curUser = userList.Find(o => o.PhoneName == mobile&&o.UserType==(int)UserTypeEnum.学生家长);
                    if (curUser == null)
                    {
                           
                        m.Message = "仅收到【绍兴市教育局】邀请短信的抽样用户可以参与本次调查。";
                        break;
                    }
                       
                        //生成4位随机码
                        Random rad = new Random();//实例化随机数产生器rad;
                    int code = rad.Next(1000, 10000);//用rad生成大于等于1000,小于等于9999的随机数;
                        #region 老版短信发送


                        //    var ss = new XSTWeb.Modules.SmsServer.Models.SmsSendRequest()
                        //{
                        //    MsgContent = string.Format("您正在参与的教育调查4位验证码为:{0}",code),
                        //    SchoolId = 2,
                        //    SendId = 0,
                        //    Sno = "",
                        //    SendTime = DateTime.Now,
                        //    Type = 1,//*
                        //    SmsType = 2,//*
                        //    SendUserId = mobile,
                        //    ReceiveInfos = new List<XSTWeb.Modules.SmsServer.Models.SmsSendRequest.ReceiveInfo>()
                        //};
                        //ss.ReceiveInfos.Add(new XSTWeb.Modules.SmsServer.Models.SmsSendRequest.ReceiveInfo()
                        //{
                        //    ClassId = 0,
                        //    ReceiveId = 1,
                        //    ReceivePhone = mobile,
                        //    ReceiveUserId = mobile,
                        //    Type = 3
                        //});
                        #region



                        //var ss = new XSTWeb.Modules.SmsServer.Models.SmsSendRequest()
                        //{
                        //    MsgContent = string.Format("您正在参与的教育调查4位验证码为:{0}", code),
                        //    SchoolId = 2,
                        //    SendId = 0,
                        //    Sno = "",
                        //    SendTime = DateTime.Now,
                        //    Type = 1,//*
                        //    SmsType = 2,//*
                        //    SendUserId = mobile,
                        //    ReceiveInfos = new List<XSTWeb.Modules.SmsServer.Models.SmsSendRequest.ReceiveInfo>()
                        //};
                        //ss.ReceiveInfos.Add(new XSTWeb.Modules.SmsServer.Models.SmsSendRequest.ReceiveInfo()
                        //{
                        //    ClassId = 0,
                        //    ReceiveId = 1,
                        //    ReceivePhone = mobile,
                        //    ReceiveUserId = mobile,
                        //    Type = 3
                        //});
                        //var result = XSTWeb.Modules.SmsServer.DataSource.SendSms(ss);
                        //Utility.Helper.SaveLog(string.Format("status={0}&code={1}&mobile={2}", result.Status, code, mobile), "mobilecode");
                        //if (result.Status == XSTWeb.Modules.SmsServer.Utility.OperationResultType.Success)
                        //{
                        //    m.Status = OperationResultType.Success;
                        //    m.Data = code;
                        //}
                        //else
                        //{
                        //    m.Message = result.Message;
                        //}
                        #endregion
                        #endregion
                        List<string> Mymobile = new List<string>();
                        Mymobile.Add(mobile);
                        string cons = string.Format("您正在参与的教育调查4位验证码为:{0}", code);
                        var ss = new sendDC()
                        {
                            Content = cons,
                            Mobile = Mymobile
                        };
                        string postData = Newtonsoft.Json.JsonConvert.SerializeObject(ss);
                        var reset = BLL.UserBll.ExamineSendMsg(cons, mobile);
                       // string result = PostUrl("http://campus.myjxt.com/api/Common/ExamineSendMsg", postData);

                        //resetModel reset = Newtonsoft.Json.JsonConvert.DeserializeObject<resetModel>(result);
                        //Utility.Helper.SaveLog(string.Format("status={0}&code={1}&mobile={2}", reset.status, code, mobile), "mobilecode");
                        if (reset)
                        {
                            m.Status = OperationResultType.Success;
                            m.Data = code;
                        }
                        else
                        {
                            m.Message ="失败";
                        }
                    }
                    break;
                case "checkcode":
                {
                    var mobile = Request["mobile"];
                    int usertype = RequestInt("usertype");
                    int flag = UserBll.IsAnswer(mobile);
                    if (flag > 0)
                    {
                        m.Status=OperationResultType.ValidError;
                        m.Message = "你已经答过题了";
                        break;
                    }

                    List<Models.User> userList = (List<Models.User>)SiteBase.Cache.CacheManage.User.CacheData;
                    var curUser = userList.Find(o => o.PhoneName == mobile&&o.UserType==usertype);
                    if (curUser != null) //如果在缓存中能找到该手机 号码
                    {
                        m.Status = OperationResultType.Success;
                        m.Data = curUser.StudentName;
                        m.Message = curUser.SchoolName;
                    }
                    else
                    {
                        m.Message = "仅收到【绍兴市教育局】邀请短信的抽样用户可以参与本次调查,请确认您的身份是“家长”或“市民”";
                    }
                }
                    break;
                    case "getquestion":
                {
                    int current = RequestInt("current");
                    int qtype = RequestInt("qtype");
                    int num = RequestInt("num");
                    List<Models.Topic> list = null;
                    if (SiteBase.Cache.CacheManage.Topic.CacheData == null) list = new List<Models.Topic>();
                    else
                    {
                        list = (List<Models.Topic>) SiteBase.Cache.CacheManage.Topic.CacheData;
                    }
                    var TopicList = list.Where(o => o.TopicType == qtype).ToList();

                    int total = TopicList.Count;
                    StringBuilder html = new StringBuilder();
                    for (int i = current; i < current + num; i++)
                    {
                        if (i + 1 > total)
                        {
                            break;
                        }
                        if (!string.IsNullOrEmpty(TopicList[i].A) && !string.IsNullOrEmpty(TopicList[i].D))
                        {
                            html.AppendFormat("<article class=\"question required\">" +
                                              "<div class=\"t\"><span class=\"num\" >{0}.</span>{1}<span style=\" font-weight: bolder;\">{8}</span>{9}</div>" +
                                              "<div class=\"b\">" +
                                              "<input class=\"questionId\" name=\"topid{2}\" value=\"{3}\" type=\"hidden\" />" +
                                              "<label class=\"opt\"><input type=\"radio\" name=\"question{2}\" value=\"A\" />A.{4}</label>" +
                                              "<label class=\"opt\"><input type=\"radio\" name=\"question{2}\" value=\"B\" />B.{5}</label>" +
                                              "<label class=\"opt\"><input type=\"radio\" name=\"question{2}\" value=\"C\" />C.{6}</label>" +
                                              "<label class=\"opt\" {8}><input type=\"radio\" name=\"question{2}\" value=\"D\" />D.{7}</label>" +
                                              "</div></article>",
                                i + 1,
                                TopicList[i].TitleOne,
                                i,
                                TopicList[i].ID,
                                TopicList[i].A,
                                TopicList[i].B,
                                TopicList[i].C,
                                TopicList[i].D,
                                !string.IsNullOrEmpty(TopicList[i].D) ? "" : "style=\"display:none;\"",TopicList[i].TitleTwo, TopicList[i].TitleTree);
                        }
                        else if (!string.IsNullOrEmpty(TopicList[i].A) && string.IsNullOrEmpty(TopicList[i].D))
                        {
                            html.AppendFormat("<article class=\"question required\">" +
                                              "<div class=\"t\"><span class=\"num\" >{0}.</span>{1}<span style=\" font-weight: bolder;\">{7}</span>{8}</div>" +
                                              "<div class=\"b\">" +
                                              "<input class=\"questionId\" name=\"topid{2}\" value=\"{3}\" type=\"hidden\" />" +
                                              "<label class=\"opt\"><input type=\"radio\" name=\"question{2}\" value=\"A\" />A.{4}</label>" +
                                              "<label class=\"opt\"><input type=\"radio\" name=\"question{2}\" value=\"B\" />B.{5}</label>" +
                                              "<label class=\"opt\"><input type=\"radio\" name=\"question{2}\" value=\"C\" />C.{6}</label>" +
                                              "</div></article>",
                                i + 1,
                                TopicList[i].TitleOne,
                                i,
                                TopicList[i].ID,
                                TopicList[i].A,
                                TopicList[i].B,
                                TopicList[i].C, TopicList[i].TitleTwo, TopicList[i].TitleTree);
                        }
                        else
                        {
                            html.AppendFormat("<article class=\"question question-textarea\">" +
                                              "<div class=\"t\">评价/建议:</div>" +
                                              "<div class=\"b\" >" +
                                              "<input class=\"questionId\" name=\"topid{0}\" value=\"{1}\" type=\"hidden\" />" +
                                              "<textarea name=\"question{0}\" placeholder=\"{2}:\" style=\" height: 300px;\"></textarea>" +
                                              "</div></article>",
                                i,
                                TopicList[i].ID,
                                 TopicList[i].Question
                                );
                               // html.AppendFormat("<article class=\"question question-textarea\">" +
                               //              "<div class=\"t\">意见与建议</div>" +
                               //              "<div class=\"b\">" +
                               //              "<input class=\"questionId\" name=\"topid{0}\" value=\"{1}\" type=\"hidden\" />" +
                               //              "<textarea name=\"question{0}\" placeholder=\"您的意见与建议:\"></textarea>" +
                               //              "</div></article>",
                               //i,
                               //TopicList[i].ID
                               //);

                            }
                    }
                    if (!string.IsNullOrEmpty(html.ToString()))
                    {
                        m.Status = OperationResultType.Success;
                        m.Data = html.ToString();
                    }
                }
                    break;

                    case "questions":
                {
                    string mobile = Request["mobile"];
                    int qtype = RequestInt("qtype");
                    string name = Request["name"];
                    string schoolname = Request["schoolname"];

                        //判断是否答过题
                        if (BLL.UserBll.IsAnswer(mobile) > 0)
                        {
                            m.Message = "请勿重复提交";
                            break;
                        }

                        List<Models.User> userList = (List<Models.User>)SiteBase.Cache.CacheManage.User.CacheData;
                        var curUser = userList.Find(o => o.PhoneName == mobile&&o.UserType==qtype);
                    if (curUser == null)
                    {
                        m.Message = "仅收到【绍兴市教育局】邀请短信的抽样用户可以参与本次调查,请确认您的身份是“家长”或“市民”";
                        break;
                    }
                    Models.UserAnswer people = new Models.UserAnswer
                    {
                        StudentName = curUser.StudentName,
                        SchoolName = curUser.SchoolName,
                        ClassName = curUser.ClassName,
                        ParentName = curUser.ParentName,
                        PhoneName = curUser.PhoneName,
                        schoolType=curUser.schoolType,
                        State = 1,
                        Intime = DateTime.Now,
                        UserType = qtype
                    };
                    if (qtype == (int) UserTypeEnum.社会人士)
                    {
                        people.StudentName = name;
                        people.SchoolName = schoolname;
                    }

                    List<Models.Topic> list = null;
                    if (SiteBase.Cache.CacheManage.Topic.CacheData == null) list = new List<Models.Topic>();
                    else
                    {
                        list = (List<Models.Topic>)SiteBase.Cache.CacheManage.Topic.CacheData;
                    }
                    var TopicList = list.Where(o => o.TopicType == qtype).ToList();
                        try
                        {
                            List<Models.Domain.TopicKeys> tkList = new List<TopicKeys>();
                            string str = "";
                            int correctnumbers = 0;
                            for (int i = 0; i < TopicList.Count; i++)
                            {
                                var ans = Request["question" + i];
                                var tid = RequestInt("topid" + i);
                                tkList.Add(new TopicKeys { Keys = ans, TopicId = tid });
                            }
                            TopicKeyTypes keyTypes=new TopicKeyTypes();
                            keyTypes.KeyList=new List<TopicKeys>();
                            keyTypes.KeyList.AddRange(tkList);
                            str += Quiz.Utility.XmlSerializer.Serializer<TopicKeyTypes>(keyTypes);
                            people.Keys = str;
                            people.Intime = DateTime.Now;
                            if (BLL.UserBll.AddUserAnswer(people))
                            {
                                m.Status = OperationResultType.Success;
                                m.Data = correctnumbers;
                            }
                        }
                        catch (Exception e)
                        {
                            m.Message = e.Message;
                        }
                    } break;
                    case "getinfo":
                {
                    string mobile = Request["mobile"];
                    //List<Models.User> userList = (List<Models.User>)SiteBase.Cache.CacheManage.User.CacheData;
                    //var curUser = userList.Find(o => o.PhoneName == mobile);
                    //if (curUser != null)
                    //{
                    //    m.Status=OperationResultType.Success;
                    //    m.Message = curUser.StudentName;
                    //    m.Data = curUser.SchoolName;
                    //}
                    var userAnswer = BLL.UserBll.GetUserAnswerOne(mobile);
                    if (userAnswer != null)
                    {
                        m.Status=OperationResultType.Success;
                        m.Message = userAnswer.StudentName;
                        m.Data = userAnswer.SchoolName;
                    }
                }
                    break;
                    case "getschool":
                {
                    int schooltype = RequestInt("schooltype");
                    List<Models.School> schoolList = (List<Models.School>) SiteBase.Cache.CacheManage.School.CacheData;
                    if (schoolList == null || schoolList.Count <= 0)
                    {
                        m.Message = "数据库中没有数据";
                        break;
                    }
                    var list = schoolList.Where(o => o.SchoolType == schooltype).ToList();
                    if (list == null || list.Count <= 0)
                    {
                        m.Message = "不存在该种类型的学校";
                        break;
                    }
                    m.Status=OperationResultType.Success;
                    m.Data = list;
                }
                    break;
            }
            Response.Write(JsonHelper.SerializeObject(m)); 
            Response.End();
        }


        public static string PostUrl(string url, string postData)
        {
            string result = "";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            //req.TimeOut = "800";//设置请求超时时间,单位为毫秒
            req.ContentType = "application/json";
            byte[] data = Encoding.UTF8.GetBytes(postData);
            req.ContentLength = data.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);

                reqStream.Close();
            }
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            Stream stream = resp.GetResponseStream();
            //获取响应内容
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                result = reader.ReadToEnd();
            }
            return result;
        }
    }
}