数据库设计:
根据laymi接收的json数据格式设计的
html:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>title>
<script type="text/javascript" src="/Scripts/jquery/jquery-1.4.4.min.js">script>
<script type="text/javascript" src="/Scripts/layim-v3.7.5/dist/layui.js">script>
<script type="text/javascript" src="/Scripts/js/index.js">script>
<link rel="stylesheet" href="/Scripts/layim-v3.7.5/dist/css/layui.css" />
head>
<body>
body>
html>
index.js:
layui.use('layim', function (layim) {
//基础配置
layim.config({
//初始化接口
init: {
url: '/main/index'
, data: {}
}
});
});
然后是controller:
using filemaneger.Models;
using layim.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
namespace layim.Controllers
{
public class mainController : ApiController
{
[HttpGet]
[Route("main/index")]
public IHttpActionResult getmain()
{
try
{
//var request = HttpContext.Current.Request;
//var menuid = request.QueryString["menuid"];
//List
//var total = lt.Count; //总数
//var rows = lt.ToList(); //分页存储到List中
string sqlmine = "select msg,code,username,status,sign,avatar,id from Mine inner join(select msg,code,mineid from data a inner join(select id,dataid from userta where username='user')b on a.id=b.id)c on id=c.mineid";
DataBase db = new DataBase();
DataTable dt = db.GetDataTable(sqlmine);
string msg = dt.Rows[0]["msg"].ToString();
string code = dt.Rows[0]["code"].ToString();
BaseListResult data = new BaseListResult();
data.mine = new Mine();
//mime赋值
data.mine.id = dt.Rows[0]["id"].ToString();
data.mine.username = dt.Rows[0]["username"].ToString();
data.mine.status = dt.Rows[0]["status"].ToString();
data.mine.sign = dt.Rows[0]["sign"].ToString();
data.mine.avatar = dt.Rows[0]["avatar"].ToString();
//friend
data.friend = new List<Friend>();
// string sqlfriend = "select d.id,groupname,online,friendoneid,e.id,e.avatar,e.sign,e.username from friendone e inner join(select c.id,groupname,online,friendoneid from friendtofriendone left join(select id,groupname,online from Friend inner join(select friendid from userfriend inner join(select id,dataid from userta where username='user')a on a.id=userid)b on b.friendid=id)c on c.id=friendoneid)d on d.friendoneid=e.id ";//order by d.id asc
string sqlfriend = "select efriendid,groupname,online,friendoneid,id,avatar,sign,username from friendone inner join(select efriendid,groupname,online,friendoneid from friendtofriendone e inner join(select id as efriendid,groupname,online from Friend inner join(select b.friendid from userfriend b inner join(select id as userid from userta where username='user')a on a.userid=b.userid)c on c.friendid=id)d on d.efriendid=e.Friendid)e on id=e.friendoneid ";
DataTable dt2 = db.GetDataTable(sqlfriend);
int count = dt2.Rows.Count;
List<int> listid = new List<int>();
int isornot = 0;
for (int i = 0; i < count; i++)
{
foreach (var item in listid)
{
if (i == item) {
isornot = 1;
break;
}
}
if (isornot == 1)
{
isornot = 0;
}
else
{
var ggid = "";
Friend f = new Friend();
f.id = dt2.Rows[i][0].ToString();
listid.Add(i);
ggid = f.id;
f.groupname = dt2.Rows[i][1].ToString();
friendone gg = new friendone();
f.list = new List<friendone>();
f.online = dt2.Rows[i][2].ToString();
gg.id = dt2.Rows[i][4].ToString();
gg.avatar = dt2.Rows[i][5].ToString();
gg.sign = dt2.Rows[i][6].ToString();
gg.username = dt2.Rows[i][7].ToString();
f.list.Add(gg);
for (int j = 0; j < count; j++) //判断是否还有其他同一朋友组的
{
if (i != j && ggid == dt2.Rows[j][0].ToString())
{
friendone lili = new friendone();
lili.id = dt2.Rows[j][4].ToString();
lili.avatar = dt2.Rows[j][5].ToString();
lili.sign = dt2.Rows[j][6].ToString();
lili.username = dt2.Rows[j][7].ToString();
listid.Add(j);
f.list.Add(lili);
}
}
data.friend.Add(f);
}
}
//group
data.group = new List<Group>();
string groupsql = "select id,groupname,avatar from Groupta inner join(select groupid from usergroup a inner join(select id from userta where username='user')b on b.id=a.id)c on c.groupid=id";
DataTable dt3=db.GetDataTable(groupsql);
int num = dt3.Rows.Count;
for (int k = 0; k < num; k++)
{
Group gp = new Group();
gp.id=dt3.Rows[k][0].ToString();
gp.groupname = dt3.Rows[k][1].ToString();
gp.avatar = dt3.Rows[k][2].ToString();
data.group.Add(gp);
}
return Json(new { msg = msg,code=code,data=data }); //然后才转换成Json传递
}
catch (Exception ex)
{
return Ok(ex.Message);
}
}
}
}
然后实体类: 记得分开在不同的cs文件 database.cs在其他博客有贴出来
public class User
{
public string id { get; set; }
public string username { get; set; }
public string password { get; set; }
}
public class Mine
{
//自己的信息
public string username { get; set; }
public string id { get; set; }
public string status { get; set; }
public string sign { get; set; }
public string avatar { get; set; }
}
public class Group
{
public string groupname { get; set; }
public string id { get; set; }
public string avatar { get; set; }
}
public class friendone
{
//一个朋友的信息
public string username { get; set; }
public string id { get; set; }
public string avatar { get; set; }
public string sign { get; set; }
}
public class Friend
{
//每个列表组下面的好友列表
public string groupname { get; set; }
public string id { get; set; }
public string online { get; set; }
public List<friendone> list { get; set; }
}
sql生成
USE [layim]
GO
/****** Object: Table [dbo].[friendone] Script Date: 04/16/2019 15:38:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[friendone](
[id] [varchar](100) NOT NULL,
[username] [varchar](100) NULL,
[avatar] [varchar](100) NULL,
[sign] [varchar](100) NULL,
CONSTRAINT [PK_friendone_id] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[Friend] Script Date: 04/16/2019 15:38:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Friend](
[id] [varchar](100) NOT NULL,
[groupname] [varchar](100) NULL,
[online] [varchar](100) NULL,
CONSTRAINT [PK_Friend_id] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[Mine] Script Date: 04/16/2019 15:38:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Mine](
[id] [varchar](100) NOT NULL,
[username] [varchar](100) NULL,
[status] [varchar](100) NULL,
[sign] [varchar](100) NULL,
[avatar] [varchar](100) NULL,
CONSTRAINT [PK_Mine_id] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[Groupta] Script Date: 04/16/2019 15:38:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Groupta](
[id] [varchar](100) NOT NULL,
[groupname] [varchar](100) NULL,
[avatar] [varchar](100) NULL,
CONSTRAINT [PK_Groupta_id] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[friendtofriendone] Script Date: 04/16/2019 15:38:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[friendtofriendone](
[id] [int] IDENTITY(1,1) NOT NULL,
[Friendid] [varchar](100) NULL,
[friendoneid] [varchar](100) NULL,
CONSTRAINT [PK_friendtofriendone_id] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[data] Script Date: 04/16/2019 15:38:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[data](
[id] [varchar](100) NOT NULL,
[msg] [varchar](100) NULL,
[code] [varchar](100) NULL,
[mineid] [varchar](100) NULL,
CONSTRAINT [PK_data_id] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[userta] Script Date: 04/16/2019 15:38:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[userta](
[id] [varchar](100) NOT NULL,
[username] [varchar](100) NULL,
[password] [varchar](100) NULL,
[dataid] [varchar](100) NULL,
CONSTRAINT [PK_userta_id] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[usergroup] Script Date: 04/16/2019 15:38:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[usergroup](
[id] [int] IDENTITY(1,1) NOT NULL,
[userid] [varchar](100) NULL,
[groupid] [varchar](100) NULL,
CONSTRAINT [PK_usergroup_id] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[userfriend] Script Date: 04/16/2019 15:38:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[userfriend](
[id] [int] IDENTITY(1,1) NOT NULL,
[userid] [varchar](100) NULL,
[friendid] [varchar](100) NULL,
CONSTRAINT [PK_userfriend_id] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object: ForeignKey [FK_data_mineid] Script Date: 04/16/2019 15:38:11 ******/
ALTER TABLE [dbo].[data] WITH CHECK ADD CONSTRAINT [FK_data_mineid] FOREIGN KEY([mineid])
REFERENCES [dbo].[Mine] ([id])
GO
ALTER TABLE [dbo].[data] CHECK CONSTRAINT [FK_data_mineid]
GO
/****** Object: ForeignKey [FK_friendtone_FriendidDEA8] Script Date: 04/16/2019 15:38:11 ******/
ALTER TABLE [dbo].[friendtofriendone] WITH CHECK ADD CONSTRAINT [FK_friendtone_FriendidDEA8] FOREIGN KEY([Friendid])
REFERENCES [dbo].[Friend] ([id])
GO
ALTER TABLE [dbo].[friendtofriendone] CHECK CONSTRAINT [FK_friendtone_FriendidDEA8]
GO
/****** Object: ForeignKey [FK_friendtone_friendoeid0046] Script Date: 04/16/2019 15:38:11 ******/
ALTER TABLE [dbo].[friendtofriendone] WITH CHECK ADD CONSTRAINT [FK_friendtone_friendoeid0046] FOREIGN KEY([friendoneid])
REFERENCES [dbo].[friendone] ([id])
GO
ALTER TABLE [dbo].[friendtofriendone] CHECK CONSTRAINT [FK_friendtone_friendoeid0046]
GO
/****** Object: ForeignKey [FK_userfriend_friendid] Script Date: 04/16/2019 15:38:11 ******/
ALTER TABLE [dbo].[userfriend] WITH CHECK ADD CONSTRAINT [FK_userfriend_friendid] FOREIGN KEY([friendid])
REFERENCES [dbo].[Friend] ([id])
GO
ALTER TABLE [dbo].[userfriend] CHECK CONSTRAINT [FK_userfriend_friendid]
GO
/****** Object: ForeignKey [FK_userfriend_userid] Script Date: 04/16/2019 15:38:11 ******/
ALTER TABLE [dbo].[userfriend] WITH CHECK ADD CONSTRAINT [FK_userfriend_userid] FOREIGN KEY([userid])
REFERENCES [dbo].[userta] ([id])
GO
ALTER TABLE [dbo].[userfriend] CHECK CONSTRAINT [FK_userfriend_userid]
GO
/****** Object: ForeignKey [FK_usergroup_groupid] Script Date: 04/16/2019 15:38:11 ******/
ALTER TABLE [dbo].[usergroup] WITH CHECK ADD CONSTRAINT [FK_usergroup_groupid] FOREIGN KEY([groupid])
REFERENCES [dbo].[Groupta] ([id])
GO
ALTER TABLE [dbo].[usergroup] CHECK CONSTRAINT [FK_usergroup_groupid]
GO
/****** Object: ForeignKey [FK_usergroup_userid] Script Date: 04/16/2019 15:38:11 ******/
ALTER TABLE [dbo].[usergroup] WITH CHECK ADD CONSTRAINT [FK_usergroup_userid] FOREIGN KEY([userid])
REFERENCES [dbo].[userta] ([id])
GO
ALTER TABLE [dbo].[usergroup] CHECK CONSTRAINT [FK_usergroup_userid]
GO
/****** Object: ForeignKey [FK_userta_dataid] Script Date: 04/16/2019 15:38:11 ******/
ALTER TABLE [dbo].[userta] WITH CHECK ADD CONSTRAINT [FK_userta_dataid] FOREIGN KEY([dataid])
REFERENCES [dbo].[data] ([id])
GO
ALTER TABLE [dbo].[userta] CHECK CONSTRAINT [FK_userta_dataid]
GO