forked from liaozb/APIJSON.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonController.cs
More file actions
334 lines (317 loc) · 13.4 KB
/
Copy pathJsonController.cs
File metadata and controls
334 lines (317 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
namespace APIJSON.NET.Controllers
{
using System;
using System.Collections.Generic;
using System.Web;
using APIJSON.NET.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Newtonsoft.Json.Linq;
using SqlSugar;
using System.Linq;
using APIJSON.NET.Services;
using System.Reflection;
using Microsoft.AspNetCore.Cors;
[Route("api/[controller]")]
[ApiController]
[EnableCors("localhost")]
public class JsonController : ControllerBase
{
private SelectTable selectTable;
private DbContext db;
private readonly IIdentityService _identitySvc;
public JsonController(SelectTable _selectTable, DbContext _db,IIdentityService identityService)
{
selectTable = _selectTable;
db = _db;
_identitySvc = identityService;
}
/// <summary>
/// 查询
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
[HttpPost("/get")]
public ActionResult Query([FromBody]string json)
{
json = HttpUtility.UrlDecode(json);
JObject ht = new JObject();
ht.Add("code", "200");
ht.Add("msg", "success");
try
{
JObject jobject = JObject.Parse(json);
int page = 0, count = 0, query = 0, total = 0;
foreach (var item in jobject)
{
string key = item.Key.Trim();
JObject jb;
if (key.Equals("[]"))
{
jb = JObject.Parse(item.Value.ToString());
page = jb["page"] == null ? 0 : int.Parse(jb["page"].ToString());
count = jb["count"] == null ? 0 : int.Parse(jb["count"].ToString());
query = jb["query"] == null ? 0 : int.Parse(jb["query"].ToString());
jb.Remove("page"); jb.Remove("count"); jb.Remove("query");
var htt = new JArray();
List<string> tables = new List<string>(), where = new List<string>();
foreach (var t in jb)
{
tables.Add(t.Key); where.Add(t.Value.ToString());
}
if (tables.Count > 0)
{
string table = tables[0];
var temp = selectTable.GetTableData(table, page, count, where[0], null);
if (query > 0)
{
total = temp.Item2;
}
foreach (var dd in temp.Item1)
{
var zht = new JObject();
zht.Add(table, JToken.FromObject(dd));
for (int i = 1; i < tables.Count; i++)
{
string subtable = tables[i];
if (subtable.EndsWith("[]"))
{
subtable = subtable.TrimEnd("[]".ToCharArray());
var jbb = JObject.Parse(where[i]);
page = jbb["page"] == null ? 0 : int.Parse(jbb["page"].ToString());
count = jbb["count"] == null ? 0 : int.Parse(jbb["count"].ToString());
var lt = new JArray();
foreach (var d in selectTable.GetTableData(subtable, page, count, jbb[subtable].ToString(), zht).Item1)
{
lt.Add(JToken.FromObject(d));
}
zht.Add(tables[i], lt);
}
else
{
var ddf = selectTable.GetFirstData(subtable, where[i].ToString(), zht);
if (ddf != null)
{
zht.Add(subtable, JToken.FromObject(ddf));
}
}
}
htt.Add(zht);
}
}
if (query != 1)
{
ht.Add("[]", htt);
}
}
else if (key.EndsWith("[]"))
{
jb = JObject.Parse(item.Value.ToString());
page = jb["page"] == null ? 0 : int.Parse(jb["page"].ToString());
count = jb["count"] == null ? 0 : int.Parse(jb["count"].ToString());
query = jb["query"] == null ? 0 : int.Parse(jb["query"].ToString());
jb.Remove("page"); jb.Remove("count"); jb.Remove("query");
var htt = new JArray();
foreach (var t in jb)
{
foreach (var d in selectTable.GetTableData(t.Key, page, count, t.Value.ToString(), null).Item1)
{
htt.Add(JToken.FromObject(d));
}
}
ht.Add(key, htt);
}
else if (key.Equals("func"))
{
jb = JObject.Parse(item.Value.ToString());
Type type = typeof(FuncList);
Object obj = Activator.CreateInstance(type);
var bb = new JObject();
foreach (var f in jb)
{
var types = new List<Type>();
var param = new List<object>();
foreach (var va in JArray.Parse(f.Value.ToString()))
{
types.Add(typeof(object));
param.Add(va);
}
bb.Add(f.Key, JToken.FromObject(selectTable.ExecFunc(f.Key,param.ToArray(), types.ToArray())));
}
ht.Add("func", bb);
}
else if (key.Equals("total@"))
{
ht.Add("total", total);
}
else
{
var template = selectTable.GetFirstData(key, item.Value.ToString(), ht);
if (template != null)
{
ht.Add(key, JToken.FromObject(template));
}
}
}
}
catch (Exception ex)
{
ht["code"] = "500";
ht["msg"] = ex.Message;
}
return Ok(ht);
}
/// <summary>
/// 新增
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
[HttpPost("/add")]
public ActionResult Add([FromBody]string json)
{
json = HttpUtility.UrlDecode(json);
JObject ht = new JObject();
ht.Add("code", "200");
ht.Add("msg", "success");
try
{
JObject jobject = JObject.Parse(json);
var sb = new System.Text.StringBuilder(100);
foreach (var item in jobject)
{
string key = item.Key.Trim();
var role = _identitySvc.GetRole();
if (!role.Insert.Table.Contains(key, StringComparer.CurrentCultureIgnoreCase))
{
ht["code"] = "500";
ht["msg"] = $"没权限添加{key}";
break;
}
var dt = new Dictionary<string, object>();
foreach (var f in JObject.Parse(item.Value.ToString()))
{
if (f.Key.ToLower() != "id" && selectTable.IsCol(key, f.Key) && (role.Insert.Column.Contains("*") || role.Insert.Column.Contains(f.Key, StringComparer.CurrentCultureIgnoreCase)))
dt.Add(f.Key, f.Value);
}
int id = db.Db.Insertable(dt).AS(key).ExecuteReturnIdentity();
ht.Add(key, JToken.FromObject(new { code = 200, msg = "success", id }));
}
}
catch (Exception ex)
{
ht["code"] = "500";
ht["msg"] = ex.Message;
}
return Ok(ht);
}
/// <summary>
/// 修改
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
[HttpPost("/edit")]
public ActionResult Edit([FromBody]string json)
{
json = HttpUtility.UrlDecode(json);
JObject ht = new JObject();
ht.Add("code", "200");
ht.Add("msg", "success");
try
{
JObject jobject = JObject.Parse(json);
foreach (var item in jobject)
{
string key = item.Key.Trim();
var role = _identitySvc.GetRole();
if (!role.Update.Table.Contains(key, StringComparer.CurrentCultureIgnoreCase))
{
ht["code"] = "500";
ht["msg"] = $"没权限修改{key}";
break;
}
var value = JObject.Parse(item.Value.ToString());
if (!value.ContainsKey("id"))
{
ht["code"] = "500";
ht["msg"] = "未传主键id";
break;
}
var dt = new Dictionary<string, object>();
foreach (var f in value)
{
if (f.Key.ToLower() != "id"&& selectTable.IsCol(key,f.Key) && (role.Update.Column.Contains ("*")||role.Update.Column.Contains(f.Key, StringComparer.CurrentCultureIgnoreCase)))
{
dt.Add(f.Key, f.Value.ToString());
}
}
db.Db.Updateable(dt).AS(key).Where("id=@id" ,new { id= value["id"].ToString() }).ExecuteCommand();
ht.Add(key, JToken.FromObject(new { code = 200, msg = "success", id = value["id"].ToString() }));
}
}
catch (Exception ex)
{
ht["code"] = "500";
ht["msg"] = ex.Message;
}
return Ok(ht);
}
/// <summary>
/// 删除
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
[HttpPost("/remove")]
public ActionResult Remove([FromBody]string json)
{
json = HttpUtility.UrlDecode(json);
JObject ht = new JObject();
ht.Add("code", "200");
ht.Add("msg", "success");
try
{
var role = _identitySvc.GetRole();
JObject jobject = JObject.Parse(json);
foreach (var item in jobject)
{
string key = item.Key.Trim();
var value = JObject.Parse(item.Value.ToString());
var sb = new System.Text.StringBuilder(100);
sb.Append($"delete FROM {key} where ");
if (role.Delete==null||role.Delete.Table==null)
{
ht["code"] = "500";
ht["msg"] = "delete权限未配置";
break;
}
if (!role.Delete.Table.Contains(key,StringComparer.CurrentCultureIgnoreCase))
{
ht["code"] = "500";
ht["msg"] = $"没权限删除{key}";
break;
}
if (!value.ContainsKey("id"))
{
ht["code"] = "500";
ht["msg"] = "未传主键id";
break;
}
var p = new List<SugarParameter>();
foreach (var f in value)
{
sb.Append($"{f.Key}=@{f.Key},");
p.Add(new SugarParameter($"@{f.Key}", f.Value.ToString()));
}
string sql = sb.ToString().TrimEnd(',');
db.Db.Ado.ExecuteCommand(sql, p);
ht.Add(key, JToken.FromObject(new { code = 200, msg = "success", id = value["id"].ToString() }));
}
}
catch (Exception ex)
{
ht["code"] = "500";
ht["msg"] = ex.Message;
}
return Ok(ht);
}
}
}