SUser.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. using Castle.ActiveRecord;
  2. using NHibernate.Criterion;
  3. using System;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Text;
  7. namespace BizCom
  8. {
  9. [ActiveRecord("S_User")]
  10. public class SUser : ComBase<SUser>
  11. {
  12. /// <summary>
  13. ///
  14. /// </summary>
  15. [PrimaryKey(PrimaryKeyType.Native)]
  16. public int ID { get; set; }
  17. private string _lgName = "";
  18. /// <summary>
  19. ///
  20. /// </summary>
  21. [Property]
  22. public string lgName
  23. {
  24. get { return _lgName; }
  25. set { _lgName = value; }
  26. }
  27. private string _nickName = "";
  28. /// <summary>
  29. ///会员名
  30. /// </summary>
  31. [Property]
  32. public string NickName
  33. {
  34. get { return _nickName; }
  35. set { _nickName = value; }
  36. }
  37. private string _realName = "";
  38. /// <summary>
  39. ///
  40. /// </summary>
  41. [Property]
  42. public string RealName
  43. {
  44. get { return _realName; }
  45. set { _realName = value; }
  46. }
  47. private string _mobile = "";
  48. /// <summary>
  49. ///手机
  50. /// </summary>
  51. [Property]
  52. public string Mobile
  53. {
  54. get { return _mobile; }
  55. set { _mobile = value; }
  56. }
  57. private string _lgPwd = "";
  58. /// <summary>
  59. ///密码
  60. /// </summary>
  61. [Property]
  62. public string lgPwd
  63. {
  64. get { return _lgPwd; }
  65. set { _lgPwd = value; }
  66. }
  67. private string _userPic = "";
  68. /// <summary>
  69. ///
  70. /// </summary>
  71. [Property]
  72. public string UserPic
  73. {
  74. get { return _userPic; }
  75. set { _userPic = value; }
  76. }
  77. private string _miniopenid = "";
  78. /// <summary>
  79. ///
  80. /// </summary>
  81. [Property]
  82. public string miniopenid
  83. {
  84. get { return _miniopenid; }
  85. set { _miniopenid = value; }
  86. }
  87. private string _openid = "";
  88. /// <summary>
  89. ///
  90. /// </summary>
  91. [Property]
  92. public string openid
  93. {
  94. get { return _openid; }
  95. set { _openid = value; }
  96. }
  97. private string _unionid = "";
  98. /// <summary>
  99. ///
  100. /// </summary>
  101. [Property]
  102. public string unionid
  103. {
  104. get { return _unionid; }
  105. set { _unionid = value; }
  106. }
  107. private int _sex = 0;
  108. /// <summary>
  109. ///性别
  110. /// </summary>
  111. [Property]
  112. public int Sex
  113. {
  114. get { return _sex; }
  115. set { _sex = value; }
  116. }
  117. private string _loginIP = "";
  118. /// <summary>
  119. ///登录IP
  120. /// </summary>
  121. [Property]
  122. public string LoginIP
  123. {
  124. get { return _loginIP; }
  125. set { _loginIP = value; }
  126. }
  127. private int _state = 0;
  128. /// <summary>
  129. ///
  130. /// </summary>
  131. [Property]
  132. public int State
  133. {
  134. get { return _state; }
  135. set { _state = value; }
  136. }
  137. private int _expertID = 0;
  138. /// <summary>
  139. ///
  140. /// </summary>
  141. [Property]
  142. public int ExpertID
  143. {
  144. get { return _expertID; }
  145. set { _expertID = value; }
  146. }
  147. /// <summary>
  148. ///
  149. /// </summary>
  150. [Property]
  151. public DateTime? AddTime { get; set; }
  152. public static void Del(object id)
  153. {
  154. StringBuilder sql = new StringBuilder();
  155. sql.AppendFormat("delete from S_User where id=" + id);
  156. ExecuteNonQuery(sql.ToString());
  157. }
  158. public static SUser GetByWeixin(string unionid)
  159. {
  160. SUser SUser = FindFirst(Expression.Sql(string.Format("unionid='{0}'", unionid)));
  161. return SUser;
  162. }
  163. public static SUser Get(string uname, string pwd)
  164. {
  165. return FindFirst(Expression.Sql(string.Format("lgName='{0}' and lgPwd='{1}'", uname, pwd)));
  166. }
  167. public static SUser GetByWeixinMiniOpenId(string uid, string opid)
  168. {
  169. SUser SUser = FindFirst(Expression.Sql(string.Format("unionid='{0}' or miniopenid='{1}'", uid, opid)));
  170. return SUser;
  171. }
  172. public static SUser GetByWeixinMiniOpenId(string opid)
  173. {
  174. SUser SUser = FindFirst(Expression.Sql(string.Format("miniopenid='{0}'", opid)));
  175. return SUser;
  176. }
  177. public static void UserSave(SUser user, string ip, string ticket)
  178. {
  179. UserSave(user, true, ip, ticket);
  180. }
  181. public static void UserSave(SUser user, bool isFeed, string ip, string ticket)
  182. {
  183. user.LoginIP = ip;
  184. user.Create();
  185. }
  186. public static double GetUserMoney(int userId)
  187. {
  188. object obj = DbConn.ExecuteScalar("select usermoney from s_user where ID=" + userId);
  189. return Convert.ToDouble(obj);
  190. }
  191. public static string GetOpenId(int userId)
  192. {
  193. object obj = DbConn.ExecuteScalar("select miniopenid from s_user where ID=" + userId);
  194. if (obj != null) return obj.ToString();
  195. return "";
  196. }
  197. /// <summary>
  198. /// 查询是否已经存在当前用户
  199. /// </summary>
  200. /// <param name="userName"></param>
  201. /// <returns></returns>
  202. public static bool CheckExitsUser(string userName)
  203. {
  204. return CheckExits("UserName", userName);
  205. }
  206. public static bool CheckExitsNickName(string name)
  207. {
  208. string sql = "select count(0) from lc_user where username='{0}' or nickname='{0}'";
  209. object result = DbConn.ExecuteScalar(string.Format(sql, name, name));
  210. if (result == null) return false;
  211. if (Convert.ToInt32(result) > 0) return true;
  212. return false;
  213. }
  214. public static bool CheckExitsByWhere(string sWhere, string parms)
  215. {
  216. return Exists(sWhere, parms.Split(','));
  217. }
  218. /// <summary>
  219. /// 查询某个字段值是否存在
  220. /// </summary>
  221. /// <param name="fieldName"></param>
  222. /// <param name="fieldValue"></param>
  223. /// <returns></returns>
  224. public static bool CheckExits(string fieldName, string fieldValue)
  225. {
  226. return Exists(fieldName + "=?", fieldValue);
  227. }
  228. public static bool CheckUser(string key)
  229. {
  230. string sql = "select count(0) from lc_user where userName='{0}' or mobile='{0}' or email='{0}'";
  231. object result = DbConn.ExecuteScalar(string.Format(sql, key));
  232. if (result != null && Convert.ToInt32(result) > 0)
  233. return true;
  234. return false;
  235. }
  236. public static SUser GetUserBySomeCondition(string user, string mail, string mobile)
  237. {
  238. user = user == "" ? "$$$" : user;
  239. mobile = mobile == "" ? "$$$" : mobile;
  240. mail = mail == "" ? "$$$" : mail;
  241. return FindFirst(Expression.Sql(string.Format("UserName='{0}' or Mobile='{1}' or Email='{2}'", user, mobile, mail)));
  242. }
  243. /// <summary>
  244. /// 找回密码
  245. /// </summary>
  246. /// <param name="user"></param>
  247. /// <param name="email"></param>
  248. /// <returns></returns>
  249. public static SUser GetByUserMail(string user, string email)
  250. {
  251. //return Exists("Email=? and SecretSignal=?", email, secretSignal);
  252. SUser SUser = FindFirst(Expression.And(Expression.Eq("Email", email), Expression.Eq("UserName", user)));
  253. return SUser;
  254. }
  255. public static SUser GetByIdMail(int id, string email)
  256. {
  257. //return Exists("Email=? and SecretSignal=?", email, secretSignal);
  258. SUser SUser = FindFirst(Expression.And(Expression.Eq("Email", email), Expression.Eq("ID", id)));
  259. return SUser;
  260. }
  261. public static SUser GetByMail(string user)
  262. {
  263. //return Exists("Email=? and SecretSignal=?", email, secretSignal);
  264. SUser SUser = FindFirst(Expression.Or(Expression.Eq("Email", user), Expression.Eq("Mobile", user)));
  265. return SUser;
  266. }
  267. public static SUser GetUser(string user)
  268. {
  269. //return Exists("Email=? and SecretSignal=?", email, secretSignal);
  270. SUser SUser = FindFirst(Expression.Or(Expression.Eq("Email", user), Expression.Eq("Mobile", user)));
  271. return SUser;
  272. }
  273. /// <summary>
  274. /// 查询cookie的票据是否符合
  275. /// </summary>
  276. /// <param name="userName"></param>
  277. /// <param name="ticket"></param>
  278. /// <returns></returns>
  279. public static SUser GetUserByCookie(string userName, string ticket)
  280. {
  281. //FindFirst(Expression.And(Expression.Eq("UserName", userName), Expression.Eq("Ticket", ticket)));
  282. SUser SUser = FindFirst(Expression.Sql(string.Format("(Mobile='{0}' or Email='{0}') and Ticket='{1}'", userName, ticket)));
  283. return SUser;
  284. }
  285. /// <summary>
  286. /// 查询用户名密码
  287. /// </summary>
  288. /// <param name="key"></param>
  289. /// <param name="passWrod"></param>
  290. /// <returns></returns>
  291. public static SUser GetUserByPassWord(string key, string passWrod)
  292. {
  293. SUser SUser = FindFirst(Expression.Sql(string.Format("(Mobile='{0}' or Email='{0}') and PassWord='{1}'", key, passWrod)));
  294. return SUser;
  295. }
  296. public static SUser GetUserByUserId(string UserId, string passWrod)
  297. {
  298. SUser SUser = FindFirst(Expression.Sql(string.Format("ID='{0}'and PassWord='{1}'", UserId, passWrod)));
  299. return SUser;
  300. }
  301. public static SUser GetUserByUserId(string UserId)
  302. {
  303. SUser SUser = FindFirst(Expression.Sql(string.Format("ID='{0}'", UserId)));
  304. return SUser;
  305. }
  306. public static SUser GetUserByMobile(string mobile)
  307. {
  308. SUser SUser = FindFirst(Expression.Sql(string.Format("Mobile='{0}'", mobile)));
  309. return SUser;
  310. }
  311. public static void ResetPwd(string userIds)
  312. {
  313. string sql = "Update Lc_User set PassWord='4kTUiQQOdDM=' where ID in(" + userIds + ")";
  314. DbConn.ExecuteNonQuery(sql);
  315. }
  316. /// <summary>
  317. /// 验证手机
  318. /// </summary>
  319. /// <param name="userId"></param>
  320. /// <param name="mobile"></param>
  321. /// <param name="verifyCode"></param>
  322. /// <returns></returns>
  323. public static string VerifyMobile(int userId, string mobile, string verifyCode)
  324. {
  325. string errMsg = "";
  326. SqlParameter[] sqlParameter ={
  327. new SqlParameter("@userId", SqlDbType.Int, 4),
  328. new SqlParameter("@mobile", SqlDbType.VarChar, 20),
  329. new SqlParameter("@verifyCode", SqlDbType.VarChar, 10),
  330. new SqlParameter("@errMsg",SqlDbType.VarChar,100)};
  331. sqlParameter[0].Value = userId;
  332. sqlParameter[1].Value = mobile;
  333. sqlParameter[2].Value = verifyCode;
  334. sqlParameter[3].Direction = ParameterDirection.Output;
  335. DbConn.ExecuteScalar(CommandType.StoredProcedure, "sp_VerifyMobile", sqlParameter);
  336. errMsg = sqlParameter[3].Value.ToString();
  337. return errMsg;
  338. }
  339. /// <summary>
  340. /// 发送短信验证
  341. /// </summary>
  342. /// <param name="userId"></param>
  343. /// <param name="mobile"></param>
  344. /// <param name="verifyCode"></param>
  345. /// <returns></returns>
  346. public static string SendSmsVerify(int userId, string mobile, string verifyCode)
  347. {
  348. string errMsg = "";
  349. SqlParameter[] sqlParameter ={
  350. new SqlParameter("@userId", SqlDbType.Int, 4),
  351. new SqlParameter("@mobile", SqlDbType.VarChar, 20),
  352. new SqlParameter("@verifyCode", SqlDbType.VarChar, 10),
  353. new SqlParameter("@errMsg",SqlDbType.VarChar,100)};
  354. sqlParameter[0].Value = userId;
  355. sqlParameter[1].Value = mobile;
  356. sqlParameter[2].Value = verifyCode;
  357. sqlParameter[3].Direction = ParameterDirection.Output;
  358. DbConn.ExecuteScalar(CommandType.StoredProcedure, "sp_SendSms", sqlParameter);
  359. errMsg = sqlParameter[3].Value.ToString();
  360. return errMsg;
  361. }
  362. /// <summary>
  363. /// 用户登录
  364. /// </summary>
  365. /// <param name="key"></param>
  366. /// <param name="passWrod"></param>
  367. /// <returns></returns>
  368. public static SUser UserLogin(string key, string passWrod, string ip, string ticket)
  369. {
  370. SUser SUser = GetUserByPassWord(key, passWrod);
  371. //找不到
  372. if (SUser == null) return null;
  373. //更改用户登录状态
  374. SUser.LoginIP = ip; //CommonHelper.ClientIP;
  375. SUser.Update();
  376. return SUser;
  377. }
  378. }
  379. }