UsersController.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. using CB.Cache;
  2. using Common;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Net.Http;
  8. using System.Text.RegularExpressions;
  9. using System.Web.Http;
  10. using System.Web.Http.Description;
  11. using UC.Api.Models.Entity;
  12. using UC.Api.Models.Query;
  13. using UC.BLL.IBLL;
  14. using UC.Models;
  15. using UC.Models.Entity;
  16. namespace UC.Api.Controllers
  17. {
  18. /// <summary>
  19. /// 用户管理
  20. /// </summary>
  21. public class UsersController : BaseApiController
  22. {
  23. #region 构造
  24. private IUsers_LoginBLL bll_user { get; set; }
  25. private ICache cache = CacheFactory.GetCache();
  26. public UsersController(IUsers_LoginBLL users_LoginBLL, ICommonBLL commonBLL) : base(commonBLL)
  27. {
  28. bll_user = users_LoginBLL;
  29. }
  30. #endregion
  31. /// <summary>
  32. /// 注册
  33. /// </summary>
  34. /// <param name="entity"></param>
  35. [NoYz]
  36. [HttpPost]
  37. public Users_Login Register(RegisterEntity entity)
  38. {
  39. Users_Login userModel = new Users_Login();
  40. switch (entity.RegisterType)
  41. {
  42. case RegisterTypeEnum.手机注册:
  43. #region 手机注册
  44. if (!Regex.IsMatch(entity.UserName, @"^[1]+[3,5,6,7,8,9]+\d{9}"))
  45. {
  46. Accepted("手机号码错误!");
  47. }
  48. if (IsExistUserName(entity.UserName))
  49. Accepted("该手机号已注册!");
  50. var key = $"{CacheConstant.RegisterPhoneCode}_{entity.UserName}";
  51. var code = cache.GetCache<string>(key);//"123456"
  52. if (code != entity.PhoneCode)
  53. {
  54. Accepted("验证码已过期,请重新获取!");
  55. }
  56. var pwdsalt = Md5Helper.CheckRandomCode();
  57. var password = "";
  58. //h5注册来源,不需要密码
  59. if (entity.RegisteredSource != 7)
  60. {
  61. password = Md5Helper.Md5(entity.Password, pwdsalt);
  62. }
  63. userModel = new Users_Login
  64. {
  65. PwdSalt = pwdsalt,
  66. Password = password,
  67. CreateTime = DateTime.Now,
  68. UserName = entity.UserName,
  69. NickName = $"新用户" + entity.UserName.Substring(6, 5),//NewGetNickName()
  70. Phone = entity.UserName,
  71. Logo = entity.Logo,
  72. LoginCode = bll_user.LoginCode(entity.UserName),
  73. Remark = NewGetNickName()
  74. };
  75. userModel.Id = bll_com.Insert(userModel);
  76. //清理缓存
  77. cache.RemoveCache(key);
  78. userModel.Password = "";
  79. userModel.PwdSalt = "";
  80. #endregion;
  81. break;
  82. case RegisterTypeEnum.QQ注册:
  83. #region qq注册
  84. //IsAddUp为1直接新增,否则修改
  85. if (entity.IsAddUp == 1)
  86. {
  87. var model = bll_com.FindEntity<Users_Login>(p => p.Qq == entity.OpenCode);
  88. if (model != null)
  89. {
  90. Accepted("该QQ已注册!");
  91. }
  92. var qqkey = $"{CacheConstant.RegisterCode}_{entity.UserName}";
  93. //从缓存中获取验证码
  94. var qqcode = cache.GetCache<string>(qqkey); //"123456";
  95. if (qqcode != entity.PhoneCode)
  96. {
  97. Accepted("验证码已过期,请重新获取!");
  98. }
  99. var qqpwdsalt = Md5Helper.CheckRandomCode();
  100. var qqpassword = Md5Helper.Md5(entity.Password, qqpwdsalt);
  101. userModel = new Users_Login
  102. {
  103. PwdSalt = qqpwdsalt,
  104. Password = qqpassword,
  105. CreateTime = DateTime.Now,
  106. UserName = entity.UserName,
  107. Qq = entity.OpenCode,
  108. NickName = $"新用户" + entity.UserName.Substring(6, 4),
  109. Logo = entity.Logo,
  110. LoginCode = bll_user.LoginCode(entity.UserName),
  111. Remark = NewGetNickName()
  112. };
  113. userModel.Id = bll_com.Insert(userModel);
  114. //清理缓存
  115. cache.RemoveCache(qqkey);
  116. userModel.Password = "";
  117. userModel.PwdSalt = "";
  118. }
  119. else
  120. {
  121. var qqkey = $"{CacheConstant.RegisterCode}_{entity.UserName}";
  122. //从缓存中获取验证码
  123. var qqcode = cache.GetCache<string>(qqkey);//"123456";
  124. if (qqcode != entity.PhoneCode)
  125. {
  126. Accepted("验证码已过期,请重新获取!");
  127. }
  128. userModel = bll_com.FindEntity<Users_Login>(p => p.UserName == entity.UserName);
  129. userModel.Qq = entity.OpenCode;
  130. bll_com.Update<Users_Login>(userModel);
  131. }
  132. #endregion
  133. break;
  134. case RegisterTypeEnum.微信注册:
  135. #region 微信注册
  136. //IsAddUp为1直接新增,否则修改
  137. if (entity.IsAddUp == 1)
  138. {
  139. //从缓存中获取验证码
  140. var wxmodel = bll_com.FindEntity<Users_Login>(p => p.WeChat == entity.OpenCode);
  141. if (wxmodel != null)
  142. {
  143. Accepted("该微信号已注册!");
  144. }
  145. var wxkey = $"{CacheConstant.RegisterCode}_{entity.UserName}";
  146. var wxcode = cache.GetCache<string>(wxkey);//"123456";
  147. if (wxcode != entity.PhoneCode)
  148. {
  149. Accepted("验证码已过期,请重新获取!");
  150. }
  151. var wxpwdsalt = Md5Helper.CheckRandomCode();
  152. var wxpassword = Md5Helper.Md5(entity.Password, wxpwdsalt);
  153. userModel = new Users_Login
  154. {
  155. PwdSalt = wxpwdsalt,
  156. Password = wxpassword,
  157. CreateTime = DateTime.Now,
  158. UserName = entity.UserName,
  159. WeChat = entity.OpenCode,
  160. NickName = $"新用户" + entity.UserName.Substring(6, 4),//NewGetNickName(),
  161. Logo = entity.Logo,
  162. LoginCode = bll_user.LoginCode(entity.UserName),
  163. Remark = NewGetNickName()
  164. };
  165. userModel.Id = bll_com.Insert(userModel);
  166. //清理缓存
  167. cache.RemoveCache(wxkey);
  168. userModel.Password = "";
  169. userModel.PwdSalt = "";
  170. }
  171. else
  172. {
  173. //从缓存中获取验证码
  174. var wxkey = $"{CacheConstant.RegisterCode}_{entity.UserName}";
  175. var wxcode = cache.GetCache<string>(wxkey);//"123456";
  176. if (wxcode != entity.PhoneCode)
  177. {
  178. Accepted("验证码已过期,请重新获取!");
  179. }
  180. userModel = bll_com.FindEntity<Users_Login>(p => p.UserName == entity.UserName);
  181. userModel.WeChat = entity.OpenCode;
  182. bll_com.Update<Users_Login>(userModel);
  183. }
  184. #endregion
  185. break;
  186. }
  187. return userModel;
  188. }
  189. /// <summary>
  190. /// 账户是否存在
  191. /// </summary>
  192. /// <param name="userName"></param>
  193. /// <returns></returns>
  194. [NoYz]
  195. [HttpGet]
  196. public bool IsExistUserName(string userName)
  197. {
  198. if (!Regex.IsMatch(userName, @"^[1]+[3,5,6,7,8,9]+\d{9}"))
  199. {
  200. Accepted("手机号码错误!");
  201. }
  202. var model = bll_com.FindEntity<Users_Login>(p => p.UserName == userName);
  203. if (model == null)
  204. return false;
  205. return true;
  206. }
  207. /// <summary>
  208. /// 登录
  209. /// </summary>
  210. /// <param name="query"></param>
  211. /// <returns></returns>
  212. [NoYz]
  213. [HttpPost]
  214. public Users_Login Login(LoginQuery query)
  215. {
  216. switch (query.LoginType)
  217. {
  218. case UC.Models.Enum.LoginTypeEnum.账号密码:
  219. userModel = bll_com.FindEntity<Users_Login>(p => p.UserName == query.UserName);
  220. if (userModel == null || Md5Helper.Md5(query.Password, userModel.PwdSalt) != userModel.Password)
  221. {
  222. Accepted("账号或密码错误!");
  223. }
  224. break;
  225. case UC.Models.Enum.LoginTypeEnum.手机验证:
  226. userModel = bll_com.FindEntity<Users_Login>(p => p.UserName == query.UserName);
  227. if (userModel == null)
  228. Accepted("您的账号不存在,请前往注册页面,注册您的账户!");
  229. var key = $"{CacheConstant.LoginPhoneCode}_{query.UserName}";
  230. var code = cache.GetCache<string>(key);//"123456";
  231. if (code != query.Password)
  232. {
  233. Accepted("验证码已过期,请重新获取!");
  234. }
  235. cache.RemoveCache(key);
  236. break;
  237. case UC.Models.Enum.LoginTypeEnum.QQ快捷登陆:
  238. userModel = bll_com.FindEntity<Users_Login>(p => p.Qq == query.UserName);
  239. if (userModel == null)
  240. {
  241. return new Users_Login();
  242. }
  243. break;
  244. case UC.Models.Enum.LoginTypeEnum.微信快捷登陆:
  245. userModel = bll_com.FindEntity<Users_Login>(p => p.WeChat == query.UserName);
  246. if (userModel == null)
  247. {
  248. return new Users_Login();
  249. }
  250. break;
  251. default:
  252. break;
  253. }
  254. //增加登录码
  255. userModel.LoginCode = bll_user.LoginCode(userModel.UserName);
  256. bll_com.Update(userModel);
  257. userModel.Password = "";
  258. userModel.PwdSalt = "";
  259. return userModel;
  260. }
  261. /// <summary>
  262. /// 根据用户名查询用户信息
  263. /// </summary>
  264. /// <param name="query"></param>
  265. /// <returns></returns>
  266. [Jm]
  267. [HttpPost]
  268. public List<Users_Login> GetAllUserList(AllUserQuery query)
  269. {
  270. var data = bll_user.GetUserList(query.UserNameList).ToList();
  271. data.ForEach(p =>
  272. {
  273. p.Password = ""; p.PwdSalt = "";
  274. });
  275. return data;
  276. }
  277. /// <summary>
  278. /// 退出登陆
  279. /// </summary>
  280. /// <param name="userId"></param>
  281. /// <returns></returns>
  282. [NoYz]
  283. [HttpGet]
  284. public bool LoginOut()
  285. {
  286. try
  287. {
  288. userModel.LoginCode = "";
  289. bll_com.Update<Users_Login>(userModel);
  290. bll_user.RemoveLoginCode(userModel.UserName);
  291. return true;
  292. }
  293. catch (Exception)
  294. {
  295. return false;
  296. }
  297. }
  298. /// <summary>
  299. /// 用户编辑
  300. /// </summary>
  301. /// <param name="model"></param>
  302. [HttpPost]
  303. public bool EditUser(EditUserEntity model)
  304. {
  305. //Users_Login userModel = new Users_Login();
  306. switch (model.Type)
  307. {
  308. case EditUserTypeEnum.绑定手机:
  309. if (!Regex.IsMatch(model.Content1, @"^[1]+[3,5,6,7,8,9]+\d{9}"))
  310. {
  311. Accepted("手机号码错误!");
  312. }
  313. var bdkey = $"{CacheConstant.EditPhoneCode}_{model.Content}";
  314. if (model.Content2 != cache.GetCache<string>(bdkey)) //cache.GetCache<string>(bdkey)
  315. {
  316. FilesHelper.LogBD(model.TryToJson(), "code");
  317. Accepted("验证码错误!");
  318. }
  319. cache.RemoveCache(bdkey);
  320. userModel.UserName = model.Content1;
  321. userModel.Phone = model.Content1;
  322. break;
  323. case EditUserTypeEnum.修改手机号:
  324. 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}$"))
  325. {
  326. Accepted("手机号码错误!");
  327. }
  328. if (bll_com.FindEntity<Users_Login>(p => p.UserName == model.Content1) != null)
  329. {
  330. Accepted("已存在该手机号账户!");
  331. }
  332. var key = $"{CacheConstant.EditPhoneCode}_{model.Content1}";
  333. if (model.Content2 != cache.GetCache<string>(key))//cache.GetCache<string>(key)
  334. {
  335. FilesHelper.LogBD(model.TryToJson(), "code");
  336. Accepted("验证码错误!");
  337. }
  338. cache.RemoveCache(key);
  339. userModel.UserName = model.Content1;
  340. userModel.Phone = model.Content1;
  341. break;
  342. case EditUserTypeEnum.修改密码:
  343. if (!userModel.Password.IsEmpty() && (model.Content.IsEmpty() || model.Content1.IsEmpty()))
  344. {
  345. Accepted("密码不能为空!");
  346. }
  347. if (!userModel.Password.IsEmpty() && !model.Content.IsEmpty())
  348. {
  349. if (Md5Helper.Md5(model.Content, userModel.PwdSalt) != userModel.Password)
  350. {
  351. Accepted("旧密码错误!");
  352. }
  353. }
  354. userModel.PwdSalt = Md5Helper.CheckRandomCode();
  355. userModel.Password = Md5Helper.Md5(model.Content1, userModel.PwdSalt);
  356. break;
  357. case EditUserTypeEnum.修改昵称:
  358. if (bll_com.FindList<Users_Login>(p => p.Id != userModel.Id && p.NickName == model.Content).Count() > 0)
  359. {
  360. Accepted("昵称已存在,请输入新昵称!");
  361. }
  362. break;
  363. case EditUserTypeEnum.修改头像:
  364. userModel.Logo = model.Content;
  365. break;
  366. case EditUserTypeEnum.忘记密码:
  367. userModel = bll_com.FindEntity<Users_Login>(x => x.UserName == model.Content);
  368. if (userModel == null)
  369. {
  370. Accepted("当前手机号未注册");
  371. }
  372. if (string.IsNullOrEmpty(model.Content1))
  373. {
  374. Accepted("密码不能为空!");
  375. }
  376. var key1 = $"{CacheConstant.ForgotPwdPhoneCode}_{model.Content}";
  377. if (model.Content2 != cache.GetCache<string>(key1))//cache.GetCache<string>(key1)
  378. {
  379. FilesHelper.LogBD(model.TryToJson(), "code");
  380. Accepted("验证码错误!");
  381. }
  382. userModel.PwdSalt = Md5Helper.CheckRandomCode();
  383. userModel.Password = Md5Helper.Md5(model.Content1, userModel.PwdSalt);
  384. break;
  385. default:
  386. break;
  387. }
  388. bll_com.Update(userModel);
  389. return true;
  390. }
  391. /// <summary>
  392. /// 根据id查询用户信息
  393. /// </summary>
  394. /// <param name="userId"></param>
  395. /// <returns></returns>
  396. [HttpGet]
  397. public Users_Login GetUser(int userId)
  398. {
  399. var model = bll_com.FindEntity<Users_Login>(p => p.Id == userId);
  400. model.Password = "";
  401. model.PwdSalt = "";
  402. return model;
  403. }
  404. /// <summary>
  405. /// 获取当前用户想信息
  406. /// </summary>
  407. /// <returns></returns>
  408. [HttpGet]
  409. public Users_Login GetUser()
  410. {
  411. var data = UserInfo;
  412. data.Password = "";
  413. data.PwdSalt = "";
  414. return data;
  415. }
  416. /// <summary>
  417. /// 是否登录
  418. /// </summary>
  419. [NoYz]
  420. [HttpGet]
  421. public string IsLogin(string userName, string loginCode)
  422. {
  423. var _loginCode = bll_user.GetLoginCode(userName);
  424. if (!_loginCode.IsEmpty() && loginCode == _loginCode)
  425. return loginCode;
  426. return "";
  427. }
  428. /// <summary>
  429. /// 规则:A-Z中一位+9位流水号
  430. /// </summary>
  431. /// <returns></returns>
  432. [ApiExplorerSettings(IgnoreApi = true)]
  433. public string NewGetNickName()
  434. {
  435. var Newnum = "";
  436. string NickName = "";
  437. /// <summary>
  438. /// A-Z字母
  439. /// </summary>
  440. 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";
  441. try
  442. {
  443. string NickResult = bll_user.GetNewUserNick();//取最大用户
  444. //如 A00002
  445. if (!string.IsNullOrEmpty(NickResult))
  446. {
  447. if (NickResult == "Z999999999")
  448. {
  449. Accepted("用户已满");
  450. }
  451. else
  452. {
  453. string first = NickResult.Substring(0, 1);
  454. string num = NickResult.Substring(1);
  455. if (num == "999999999")//满999999999时应该换编码首字母
  456. {
  457. string[] arry = UpperCaseSerial.Split(',');
  458. for (int i = 0; i < arry.Length; i++)
  459. {
  460. if (arry[i] == first)//相同 取下一个字母
  461. {
  462. first = arry.Length > i ? arry[i + 1].ToString() : "";
  463. break;
  464. }
  465. }
  466. NickName = first + "1";
  467. }
  468. else
  469. {
  470. Newnum = (Convert.ToInt32(num) + 1).ToString();
  471. NickName = first + Newnum;
  472. }
  473. }
  474. }
  475. else
  476. {
  477. NickName = "A1";
  478. }
  479. }
  480. catch (Exception e)
  481. {
  482. Accepted(e.Message);
  483. }
  484. return NickName;
  485. }
  486. }
  487. }