using System;
using System.Data;
using System.Web;
using Newtonsoft.Json;
///
/// 会员相关操作
///
public class Member {
//private static HttpContext context = HttpContext.Current;
///
/// 判断是否登陆过,如果没有登录将转向登录页,默认登录页网址
///
public static void CheckLogined(MemberEntity m) {
if (m.Id == 0) {
string url = HttpUtility.UrlEncode(HttpContext.Current.Request.Url.ToString());
HttpContext.Current.Response.Redirect("/vue/#/member/login?redirect=" + url);
}
}
///
/// 判断是否登陆过,如果没有登录将转向登录页,传入登录页地址
///
public static void CheckLogined(MemberEntity m, string url) {
if (m.Id == 0) {
//context.Response.Write(url);
//context.Response.End();
string redirect = HttpContext.Current.Request.Url.ToString();
url += "?redirect=" + redirect;
HttpContext.Current.Response.Redirect(url);
/*try { context.Response.Redirect(url); }
catch {
HttpContext.Current.Response.Write("URL:" + url);
HttpContext.Current.Response.End();
//context.Response.End();
}*/
}
}
///
/// 检查是否有指定的权限,num可以取多个权限(用逗号隔开的字符串),满足其中一个即可
///
public static void CheckRight(MemberEntity m, string num) {
CheckLogined(m);
if (!GetRight(m, num)) {
HttpContext.Current.Response.Cookies["Message"].Value = HttpUtility.UrlEncode("权限被禁止,请与系统管理员联系!");
HttpContext.Current.Response.Cookies["Message"].HttpOnly = true;
HttpContext.Current.Response.Redirect("/inc/showmessage.aspx");
}
}
///
/// 获取指定的权限,num可以取多个权限(用逗号隔开的字符串),其中一个为真全为真
///
public static bool GetRight(MemberEntity m, string num) {
string[] tmparr = num.Split(',');
for (int i = 0; i < tmparr.Length; i++) {
if (m.Right.Substring(int.Parse(tmparr[i]) - 1, 1) == "1") { return true; }
}
return false;
}
///
/// 清空会员信息,只在该类中使用
///
private static void ClearMemberCookie() {
HttpContext.Current.Response.Cookies["PSYMEMBER"].Value = string.Empty;
}
///
/// 通过ID获取会员信息,没有获取到返回null
///
///
public static MemberEntity GetMember(int id) {
DataRow row = SqlHelper.ExecuteDataRow("select top 1 * from v_member where id=" + id);
if (row == null) { return null; }
else {
MemberEntity m = new MemberEntity();
m.Id = (int)row["id"];
m.Number = row["number"].ToString();
m.Username = row["username"].ToString();
m.Password = (string)row["password"];
m.Nickname = row["nickname"].ToString();
m.Realname = row["realname"].ToString();
m.Sex = (bool)row["sex"];
m.Sexname = (string)row["sexname"];
m.Birthday = (DateTime)row["birthday"];
m.Age = (int)row["age"];
m.Balance = Convert.IsDBNull(row["balance"]) ? (decimal)0 : (decimal)row["balance"];
m.Type = (int)row["type"];
m.Right = row["right"].ToString();
m.Province = row["province"].ToString();
m.City = row["city"].ToString();
m.District = row["district"].ToString();
m.Grade = (int)(Convert.IsDBNull(row["grade"]) ? 0 : row["grade"]);
m.IsParent = row["isparent"].ToString() == "True";
m.IsStudent = row["isstudent"].ToString() == "True";
m.IsSchoolAdmin = row["isschooladmin"].ToString() == "True";
m.Mobile = row["mobile"].ToString();
m.Face = row["face"].ToString();
m.Marriage = Convert.IsDBNull(row["marriage"]) ? (byte)0 : (byte)row["marriage"];
m.Education = Convert.IsDBNull(row["education"]) ? (byte)0 : (byte)row["education"];
m.Md5 = row["md5"].ToString();
return m;
}
}
///
/// 通过Cookie获取会员信息,Cookie不存在或者验证信息不正确,将清空Cookie并返回member.id=0
///
///
public static MemberEntity GetMember() {
MemberEntity m = new MemberEntity();
if (HttpContext.Current.Request.Cookies["PSYMEMBER"] == null) {
ClearMemberCookie();
m.Id = 0;
}
else {
string cookie = HttpContext.Current.Request.Cookies["PSYMEMBER"].Value.ToString();
if (string.IsNullOrEmpty(cookie)) {
ClearMemberCookie();
m.Id = 0;
}
else {
cookie = cookie.Replace("%2E",".");
string id = cookie.Split('.')[0];
string password = cookie.Split('.')[1];
if (!Common.IsInteger(id) || !Common.IsMD5(password)) {
ClearMemberCookie();
m.Id = 0;
}
else {
string sql = "select top 1 * from v_member where id=" + id + " and password='" + password + "'";
DataRow row = SqlHelper.ExecuteDataRow(sql);
if (row == null) {
ClearMemberCookie();
m.Id = 0;
}
else {
m.Id = (int)row["id"];
m.Number = row["number"].ToString();
m.Username = row["username"].ToString();
m.Password = (string)row["password"];
m.Nickname = row["nickname"].ToString();
m.Realname = row["realname"].ToString();
m.Sex = (bool)row["sex"];
m.Sexname = (string)row["sexname"];
m.Birthday = (DateTime)row["birthday"];
m.Age = (int)row["age"];
m.Balance = Convert.IsDBNull(row["balance"]) ? (decimal)0 : (decimal)row["balance"];
m.Type = (int)row["type"];
m.Right = row["right"].ToString();
m.Province = row["province"].ToString();
m.City = row["city"].ToString();
m.District = row["district"].ToString();
m.Grade = (int)(Convert.IsDBNull(row["grade"]) ? 0 : row["grade"]);
m.IsParent = row["isparent"].ToString() == "True";
m.IsStudent = row["isstudent"].ToString() == "True";
m.IsSchoolAdmin = row["isschooladmin"].ToString() == "True";
m.Mobile = row["mobile"].ToString();
m.Face = row["face"].ToString();
m.Marriage = Convert.IsDBNull(row["marriage"]) ? (byte)0 : (byte)row["marriage"];
m.Education = Convert.IsDBNull(row["education"]) ? (byte)0 : (byte)row["education"];
m.Md5 = row["md5"].ToString();
}
}
}
}
return m;
}
public Member() { }
}
#region 会员实体
public class MemberEntity {
///
/// 会员ID
///
[JsonProperty("id")]
public int Id { get; set; }
///
/// 学号
///
[JsonProperty("number")]
public string Number { get; set; }
///
/// 登录用户名
///
[JsonProperty("username")]
public string Username { get; set; }
///
/// MD5密码
///
[JsonProperty("password")]
public string Password { get; set; }
///
/// 会员昵称
///
[JsonProperty("nickname")]
public string Nickname { get; set; }
///
/// 真实姓名
///
[JsonProperty("realname")]
public string Realname { get; set; }
///
/// 性别,男true,女false
///
[JsonProperty("sex")]
public bool Sex { get; set; }
///
/// 性别名称:汉字男或女
///
[JsonProperty("sexname")]
public string Sexname { get; set; }
///
/// 出生日期
///
[JsonProperty("birthday")]
public DateTime Birthday { get; set; }
///
/// 年龄数字
///
[JsonProperty("age")]
public int Age { get; set; }
///
/// 账户余额
///
[JsonProperty("balance")]
public decimal Balance { get; set; }
///
/// 会员类型
///
[JsonProperty("type")]
public int Type { get; set; }
///
/// 会员权限
///
[JsonProperty("right")]
public string Right { get; set; }
///
/// 省份名称
///
[JsonProperty("province")]
public string Province { get; set; }
///
/// 城市名称
///
[JsonProperty("city")]
public string City { get; set; }
///
/// 区县名称
///
[JsonProperty("district")]
public string District { get; set; }
///
/// 年级(smallint-short)
///
[JsonProperty("grade")]
public int Grade { get; set; }
///
/// 是否家长
///
[JsonProperty("isparent")]
public bool IsParent { get; set; }
///
/// 是否学生
///
[JsonProperty("isstudent")]
public bool IsStudent { get; set; }
///
/// 是否学校管理员
///
[JsonProperty("isschooladmin")]
public bool IsSchoolAdmin { get; set; }
///
/// 手机号码
///
[JsonProperty("mobile")]
public string Mobile { get; set; }
///
/// 头像网址
///
[JsonProperty("face")]
public string Face { get; set; }
///
/// 婚姻状况:0保密,1单身,2恋爱,3订婚,4已婚,5离异,6丧偶
///
[JsonProperty("marriage")]
public byte Marriage { get; set; }
///
/// 受教育程度:0初中及以下,1高中,2大专,3本科,4硕士,5博士
///
[JsonProperty("education")]
public byte Education { get; set; }
///
/// 会员MD5信息
///
[JsonProperty("md5")]
public string Md5 { get; set; }
}
#endregion