using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Caching; using System.Web; using System.Web.Mvc; using DSWeb.Areas.Mobile.DAL; using DSWeb.Areas.Mobile.Helper; using DSWeb.Areas.Mobile.Models.MpService; using DSWeb.Areas.SoftMng.DB; using DSWeb.TruckMng.Helper; namespace DSWeb.Areas.Mobile.Controllers { public class MpServiceController : Controller { [HttpGet] public ActionResult Regist(string openid) { ViewData["openid"] = openid; DsDataContext dsDataContext = new DsDataContext("MpBillDB"); WS_USER user = dsDataContext.WebsiteUsers.FirstOrDefault(u => u.OPENID2 == openid); if(user!=null) { ViewData["name"] = user.NAME; ViewData["mobile"] = user.MOBILE; ViewData["company_name"] = user.COMPANY_NAME; } return View(); } [HttpPost] public ActionResult RegistSave(MpRegistModel model) { if (string.IsNullOrEmpty(model.OpenId)) { return Json(new { Success = false, Message = "openid无效" }); } else if (string.IsNullOrEmpty(model.Tel)) { return Json(new { Success = false, Message = "手机号码无效" }); } string key = $"VC_{model.Tel}"; if (!MemoryCache.Default.Contains(key)) { return Json(new { Success = false, Message = "验证码错误" }); } else if (MemoryCache.Default[key].ToString() != model.Code) { return Json(new { Success = false, Message = "验证码错误" }); } MemoryCache.Default.Remove(key); DsDataContext dsDataContext = new DsDataContext("MpBillDB"); WS_USER user = dsDataContext.WebsiteUsers.FirstOrDefault(u => u.MOBILE == model.Tel); if (user == null) { user = new WS_USER(); user.GID = Guid.NewGuid().ToString(); user.MOBILE = model.Tel; user.NAME = model.Name; user.OPENID2 = model.OpenId; user.COMPANY_NAME = model.CompanyName; user.CREATE_TIME = DateTime.Now; dsDataContext.WebsiteUsers.Add(user); dsDataContext.SaveChanges(); return Json(new { Success = true, Message = "注册成功" }); } else { user.NAME = model.Name; user.OPENID2 = model.OpenId; user.COMPANY_NAME = model.CompanyName; dsDataContext.SaveChanges(); return Json(new { Success = true, Message = "更新成功" }); } } [HttpPost] public ActionResult MobileCode(string mobile) { if (MemoryCache.Default.Contains($"VC_{mobile}")) { return Json(new { Success = false, Message = "频繁请求" }); } else { Random rnd = new Random(); string code = rnd.Next(1000, 10000).ToString(); MemoryCache.Default.Add(new CacheItem($"VC_{mobile}", code), new CacheItemPolicy() { AbsoluteExpiration = DateTime.Now.AddSeconds(120) }); AliMessageTools.SendSignCodeMsg(mobile, code); return Json(new { Success = true, Message = "" }); } } } }