123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509 |
- using CB.Cache;
- using Common;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Text.RegularExpressions;
- using System.Web.Http;
- using System.Web.Http.Description;
- using UC.Api.Models.Entity;
- using UC.Api.Models.Query;
- using UC.BLL.IBLL;
- using UC.Models;
- using UC.Models.Entity;
- namespace UC.Api.Controllers
- {
- /// <summary>
- /// 用户管理
- /// </summary>
- public class UsersController : BaseApiController
- {
- #region 构造
- private IUsers_LoginBLL bll_user { get; set; }
- private ICache cache = CacheFactory.GetCache();
- public UsersController(IUsers_LoginBLL users_LoginBLL, ICommonBLL commonBLL) : base(commonBLL)
- {
- bll_user = users_LoginBLL;
- }
- #endregion
- /// <summary>
- /// 注册
- /// </summary>
- /// <param name="entity"></param>
- [NoYz]
- [HttpPost]
- public Users_Login Register(RegisterEntity entity)
- {
- Users_Login userModel = new Users_Login();
- switch (entity.RegisterType)
- {
- case RegisterTypeEnum.手机注册:
- #region 手机注册
- if (!Regex.IsMatch(entity.UserName, @"^[1]+[3,5,6,7,8,9]+\d{9}"))
- {
- Accepted("手机号码错误!");
- }
- if (IsExistUserName(entity.UserName))
- Accepted("该手机号已注册!");
- var key = $"{CacheConstant.RegisterPhoneCode}_{entity.UserName}";
- var code = cache.GetCache<string>(key);//"123456"
- if (code != entity.PhoneCode)
- {
- Accepted("验证码已过期,请重新获取!");
- }
- var pwdsalt = Md5Helper.CheckRandomCode();
- var password = "";
- //h5注册来源,不需要密码
- if (entity.RegisteredSource != 7)
- {
- password = Md5Helper.Md5(entity.Password, pwdsalt);
- }
- userModel = new Users_Login
- {
- PwdSalt = pwdsalt,
- Password = password,
- CreateTime = DateTime.Now,
- UserName = entity.UserName,
- NickName = $"新用户" + entity.UserName.Substring(6, 5),//NewGetNickName()
- Phone = entity.UserName,
- Logo = entity.Logo,
- LoginCode = bll_user.LoginCode(entity.UserName),
- Remark = NewGetNickName()
- };
- userModel.Id = bll_com.Insert(userModel);
- //清理缓存
- cache.RemoveCache(key);
- userModel.Password = "";
- userModel.PwdSalt = "";
- #endregion;
- break;
- case RegisterTypeEnum.QQ注册:
- #region qq注册
- //IsAddUp为1直接新增,否则修改
- if (entity.IsAddUp == 1)
- {
- var model = bll_com.FindEntity<Users_Login>(p => p.Qq == entity.OpenCode);
- if (model != null)
- {
- Accepted("该QQ已注册!");
- }
- var qqkey = $"{CacheConstant.RegisterCode}_{entity.UserName}";
- //从缓存中获取验证码
- var qqcode = cache.GetCache<string>(qqkey); //"123456";
- if (qqcode != entity.PhoneCode)
- {
- Accepted("验证码已过期,请重新获取!");
- }
- var qqpwdsalt = Md5Helper.CheckRandomCode();
- var qqpassword = Md5Helper.Md5(entity.Password, qqpwdsalt);
- userModel = new Users_Login
- {
- PwdSalt = qqpwdsalt,
- Password = qqpassword,
- CreateTime = DateTime.Now,
- UserName = entity.UserName,
- Qq = entity.OpenCode,
- NickName = $"新用户" + entity.UserName.Substring(6, 4),
- Logo = entity.Logo,
- LoginCode = bll_user.LoginCode(entity.UserName),
- Remark = NewGetNickName()
- };
- userModel.Id = bll_com.Insert(userModel);
- //清理缓存
- cache.RemoveCache(qqkey);
- userModel.Password = "";
- userModel.PwdSalt = "";
- }
- else
- {
- var qqkey = $"{CacheConstant.RegisterCode}_{entity.UserName}";
- //从缓存中获取验证码
- var qqcode = cache.GetCache<string>(qqkey);//"123456";
- if (qqcode != entity.PhoneCode)
- {
- Accepted("验证码已过期,请重新获取!");
- }
- userModel = bll_com.FindEntity<Users_Login>(p => p.UserName == entity.UserName);
- userModel.Qq = entity.OpenCode;
- bll_com.Update<Users_Login>(userModel);
- }
- #endregion
- break;
- case RegisterTypeEnum.微信注册:
- #region 微信注册
- //IsAddUp为1直接新增,否则修改
- if (entity.IsAddUp == 1)
- {
- //从缓存中获取验证码
- var wxmodel = bll_com.FindEntity<Users_Login>(p => p.WeChat == entity.OpenCode);
- if (wxmodel != null)
- {
- Accepted("该微信号已注册!");
- }
- var wxkey = $"{CacheConstant.RegisterCode}_{entity.UserName}";
- var wxcode = cache.GetCache<string>(wxkey);//"123456";
- if (wxcode != entity.PhoneCode)
- {
- Accepted("验证码已过期,请重新获取!");
- }
- var wxpwdsalt = Md5Helper.CheckRandomCode();
- var wxpassword = Md5Helper.Md5(entity.Password, wxpwdsalt);
- userModel = new Users_Login
- {
- PwdSalt = wxpwdsalt,
- Password = wxpassword,
- CreateTime = DateTime.Now,
- UserName = entity.UserName,
- WeChat = entity.OpenCode,
- NickName = $"新用户" + entity.UserName.Substring(6, 4),//NewGetNickName(),
- Logo = entity.Logo,
- LoginCode = bll_user.LoginCode(entity.UserName),
- Remark = NewGetNickName()
- };
- userModel.Id = bll_com.Insert(userModel);
- //清理缓存
- cache.RemoveCache(wxkey);
- userModel.Password = "";
- userModel.PwdSalt = "";
- }
- else
- {
- //从缓存中获取验证码
- var wxkey = $"{CacheConstant.RegisterCode}_{entity.UserName}";
- var wxcode = cache.GetCache<string>(wxkey);//"123456";
- if (wxcode != entity.PhoneCode)
- {
- Accepted("验证码已过期,请重新获取!");
- }
- userModel = bll_com.FindEntity<Users_Login>(p => p.UserName == entity.UserName);
- userModel.WeChat = entity.OpenCode;
- bll_com.Update<Users_Login>(userModel);
- }
- #endregion
- break;
- }
- return userModel;
- }
- /// <summary>
- /// 账户是否存在
- /// </summary>
- /// <param name="userName"></param>
- /// <returns></returns>
- [NoYz]
- [HttpGet]
- public bool IsExistUserName(string userName)
- {
- if (!Regex.IsMatch(userName, @"^[1]+[3,5,6,7,8,9]+\d{9}"))
- {
- Accepted("手机号码错误!");
- }
- var model = bll_com.FindEntity<Users_Login>(p => p.UserName == userName);
- if (model == null)
- return false;
- return true;
- }
- /// <summary>
- /// 登录
- /// </summary>
- /// <param name="query"></param>
- /// <returns></returns>
- [NoYz]
- [HttpPost]
- public Users_Login Login(LoginQuery query)
- {
- switch (query.LoginType)
- {
- case UC.Models.Enum.LoginTypeEnum.账号密码:
- userModel = bll_com.FindEntity<Users_Login>(p => p.UserName == query.UserName);
- if (userModel == null || Md5Helper.Md5(query.Password, userModel.PwdSalt) != userModel.Password)
- {
- Accepted("账号或密码错误!");
- }
- break;
- case UC.Models.Enum.LoginTypeEnum.手机验证:
- userModel = bll_com.FindEntity<Users_Login>(p => p.UserName == query.UserName);
- if (userModel == null)
- Accepted("您的账号不存在,请前往注册页面,注册您的账户!");
- var key = $"{CacheConstant.LoginPhoneCode}_{query.UserName}";
- var code = cache.GetCache<string>(key);//"123456";
- if (code != query.Password)
- {
- Accepted("验证码已过期,请重新获取!");
- }
- cache.RemoveCache(key);
- break;
- case UC.Models.Enum.LoginTypeEnum.QQ快捷登陆:
- userModel = bll_com.FindEntity<Users_Login>(p => p.Qq == query.UserName);
- if (userModel == null)
- {
- return new Users_Login();
- }
- break;
- case UC.Models.Enum.LoginTypeEnum.微信快捷登陆:
- userModel = bll_com.FindEntity<Users_Login>(p => p.WeChat == query.UserName);
- if (userModel == null)
- {
- return new Users_Login();
- }
- break;
- default:
- break;
- }
- //增加登录码
- userModel.LoginCode = bll_user.LoginCode(userModel.UserName);
- bll_com.Update(userModel);
- userModel.Password = "";
- userModel.PwdSalt = "";
- return userModel;
- }
- /// <summary>
- /// 根据用户名查询用户信息
- /// </summary>
- /// <param name="query"></param>
- /// <returns></returns>
- [Jm]
- [HttpPost]
- public List<Users_Login> GetAllUserList(AllUserQuery query)
- {
- var data = bll_user.GetUserList(query.UserNameList).ToList();
- data.ForEach(p =>
- {
- p.Password = ""; p.PwdSalt = "";
- });
- return data;
- }
- /// <summary>
- /// 退出登陆
- /// </summary>
- /// <param name="userId"></param>
- /// <returns></returns>
- [NoYz]
- [HttpGet]
- public bool LoginOut()
- {
- try
- {
- userModel.LoginCode = "";
- bll_com.Update<Users_Login>(userModel);
- bll_user.RemoveLoginCode(userModel.UserName);
- return true;
- }
- catch (Exception)
- {
- return false;
- }
- }
- /// <summary>
- /// 用户编辑
- /// </summary>
- /// <param name="model"></param>
- [HttpPost]
- public bool EditUser(EditUserEntity model)
- {
- //Users_Login userModel = new Users_Login();
- switch (model.Type)
- {
- case EditUserTypeEnum.绑定手机:
- if (!Regex.IsMatch(model.Content1, @"^[1]+[3,5,6,7,8,9]+\d{9}"))
- {
- Accepted("手机号码错误!");
- }
- var bdkey = $"{CacheConstant.EditPhoneCode}_{model.Content}";
- if (model.Content2 != cache.GetCache<string>(bdkey)) //cache.GetCache<string>(bdkey)
- {
- FilesHelper.LogBD(model.TryToJson(), "code");
- Accepted("验证码错误!");
- }
- cache.RemoveCache(bdkey);
- userModel.UserName = model.Content1;
- userModel.Phone = model.Content1;
- break;
- case EditUserTypeEnum.修改手机号:
- if (!Regex.IsMatch(model.Content, @"^[1]+[3,5,6,7,8,9]+\d{9}$") || !Regex.IsMatch(model.Content1, @"^[1]+[3,5,6,7,8]+\d{9}$"))
- {
- Accepted("手机号码错误!");
- }
- if (bll_com.FindEntity<Users_Login>(p => p.UserName == model.Content1) != null)
- {
- Accepted("已存在该手机号账户!");
- }
- var key = $"{CacheConstant.EditPhoneCode}_{model.Content1}";
- if (model.Content2 != cache.GetCache<string>(key))//cache.GetCache<string>(key)
- {
- FilesHelper.LogBD(model.TryToJson(), "code");
- Accepted("验证码错误!");
- }
- cache.RemoveCache(key);
- userModel.UserName = model.Content1;
- userModel.Phone = model.Content1;
- break;
- case EditUserTypeEnum.修改密码:
- if (!userModel.Password.IsEmpty() && (model.Content.IsEmpty() || model.Content1.IsEmpty()))
- {
- Accepted("密码不能为空!");
- }
- if (!userModel.Password.IsEmpty() && !model.Content.IsEmpty())
- {
- if (Md5Helper.Md5(model.Content, userModel.PwdSalt) != userModel.Password)
- {
- Accepted("旧密码错误!");
- }
- }
- userModel.PwdSalt = Md5Helper.CheckRandomCode();
- userModel.Password = Md5Helper.Md5(model.Content1, userModel.PwdSalt);
- break;
- case EditUserTypeEnum.修改昵称:
- if (bll_com.FindList<Users_Login>(p => p.Id != userModel.Id && p.NickName == model.Content).Count() > 0)
- {
- Accepted("昵称已存在,请输入新昵称!");
- }
- break;
- case EditUserTypeEnum.修改头像:
- userModel.Logo = model.Content;
- break;
- case EditUserTypeEnum.忘记密码:
- userModel = bll_com.FindEntity<Users_Login>(x => x.UserName == model.Content);
- if (userModel == null)
- {
- Accepted("当前手机号未注册");
- }
- if (string.IsNullOrEmpty(model.Content1))
- {
- Accepted("密码不能为空!");
- }
- var key1 = $"{CacheConstant.ForgotPwdPhoneCode}_{model.Content}";
- if (model.Content2 != cache.GetCache<string>(key1))//cache.GetCache<string>(key1)
- {
- FilesHelper.LogBD(model.TryToJson(), "code");
- Accepted("验证码错误!");
- }
- userModel.PwdSalt = Md5Helper.CheckRandomCode();
- userModel.Password = Md5Helper.Md5(model.Content1, userModel.PwdSalt);
- break;
- default:
- break;
- }
- bll_com.Update(userModel);
- return true;
- }
- /// <summary>
- /// 根据id查询用户信息
- /// </summary>
- /// <param name="userId"></param>
- /// <returns></returns>
- [HttpGet]
- public Users_Login GetUser(int userId)
- {
- var model = bll_com.FindEntity<Users_Login>(p => p.Id == userId);
- model.Password = "";
- model.PwdSalt = "";
- return model;
- }
- /// <summary>
- /// 获取当前用户想信息
- /// </summary>
- /// <returns></returns>
- [HttpGet]
- public Users_Login GetUser()
- {
- var data = UserInfo;
- data.Password = "";
- data.PwdSalt = "";
- return data;
- }
- /// <summary>
- /// 是否登录
- /// </summary>
- [NoYz]
- [HttpGet]
- public string IsLogin(string userName, string loginCode)
- {
- var _loginCode = bll_user.GetLoginCode(userName);
- if (!_loginCode.IsEmpty() && loginCode == _loginCode)
- return loginCode;
- return "";
- }
- /// <summary>
- /// 规则:A-Z中一位+9位流水号
- /// </summary>
- /// <returns></returns>
- [ApiExplorerSettings(IgnoreApi = true)]
- public string NewGetNickName()
- {
- var Newnum = "";
- string NickName = "";
- /// <summary>
- /// A-Z字母
- /// </summary>
- string UpperCaseSerial = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
- try
- {
- string NickResult = bll_user.GetNewUserNick();//取最大用户
- //如 A00002
- if (!string.IsNullOrEmpty(NickResult))
- {
- if (NickResult == "Z999999999")
- {
- Accepted("用户已满");
- }
- else
- {
- string first = NickResult.Substring(0, 1);
- string num = NickResult.Substring(1);
- if (num == "999999999")//满999999999时应该换编码首字母
- {
- string[] arry = UpperCaseSerial.Split(',');
- for (int i = 0; i < arry.Length; i++)
- {
- if (arry[i] == first)//相同 取下一个字母
- {
- first = arry.Length > i ? arry[i + 1].ToString() : "";
- break;
- }
- }
- NickName = first + "1";
- }
- else
- {
- Newnum = (Convert.ToInt32(num) + 1).ToString();
- NickName = first + Newnum;
- }
- }
- }
- else
- {
- NickName = "A1";
- }
- }
- catch (Exception e)
- {
- Accepted(e.Message);
- }
- return NickName;
- }
- }
- }
|