コード例 #1
0
        public HttpResponseMessage GetStudent()
        {
            try
            {
                string signature = HttpUtil.GetAuthorization(Request);
                if (signature == null || !redis.IsSet(signature))
                {
                    return(new Response(2001, "未登录账户").Convert());
                }
                bool login = redis.IsSet(signature);
                if (!login)
                {
                    return(new Response(2001, "未登录账户").Convert());
                }
                string id = redis.Get <string>(signature);

                //string id = "admin";
                User user = UserDao.GetUserById(id);
                if (user.role != 4 && user.role != 3)
                {
                    return(new Response(2002).Convert());
                }

                List <User> users = UserDao.GetUserByRole(1, user.role == 3 ? user.department_id : "");
                if (users.Count() == 0)
                {
                    return(new Response(1001, "获取成功").Convert());
                }
                List <Dictionary <string, string> > ret = new List <Dictionary <string, string> >();
                Dictionary <string, string>         temp;
                var props = users.First().GetType().GetProperties();
                List <Department> departments = CourseDao.GetDepartments();
                foreach (User u in users)
                {
                    temp = new Dictionary <string, string>();
                    foreach (var pi in props)
                    {
                        var    v = u.GetType().GetProperty(pi.Name).GetValue(u, null);
                        string value;
                        if (v != null)
                        {
                            value = v.ToString();
                        }
                        else
                        {
                            value = "";
                        }
                        temp.Add(pi.Name, value);
                    }
                    Department department = departments.Find(d => d.id.Equals(u.department_id));
                    if (department == null)
                    {
                        temp.Add("department_name", "无");
                    }
                    else
                    {
                        temp.Add("department_name", department.name);
                    }
                    ret.Add(temp);
                }

                return(new Response(1001, "获取成功", ret).Convert());
            }
            catch (Exception e)
            {
                ErrorLogUtil.WriteLogToFile(e, Request);
                return(Response.Error());
            }
        }