User.go 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458
  1. package controllers
  2. import (
  3. "Cold_Api/Nats/NatsServer"
  4. "Cold_Api/conf"
  5. "Cold_Api/controllers/lib"
  6. "Cold_Api/models/Account"
  7. "Cold_Api/models/Company"
  8. "Cold_Api/models/Device"
  9. "Cold_Api/models/System"
  10. "Cold_Api/models/Warning"
  11. "encoding/json"
  12. "fmt"
  13. "math"
  14. "os"
  15. "strconv"
  16. "strings"
  17. "time"
  18. "github.com/beego/beego/v2/adapter/orm"
  19. "github.com/beego/beego/v2/core/logs"
  20. beego "github.com/beego/beego/v2/server/web"
  21. uuid "github.com/satori/go.uuid"
  22. "github.com/shopspring/decimal"
  23. "github.com/xuri/excelize/v2"
  24. )
  25. type UserController struct {
  26. beego.Controller
  27. Admin_r Account.Admin // 登陆的用户
  28. T_pid int // 公司id
  29. }
  30. func (c *UserController) Prepare() {
  31. GetCookie := c.Ctx.GetCookie("User_tokey")
  32. GetString := c.GetString("User_tokey")
  33. User_tokey := GetString
  34. if len(User_tokey) == 0 {
  35. User_tokey = GetCookie
  36. }
  37. if Account.Admin_r == nil {
  38. return
  39. }
  40. c.Admin_r = *Account.Admin_r
  41. T_pid := c.Admin_r.T_pid
  42. EntryPid, _ := Account.Redis_Tokey_T_pid_Get(User_tokey)
  43. if EntryPid > 0 {
  44. T_pid = EntryPid
  45. }
  46. c.T_pid = T_pid
  47. }
  48. // 获取微信二维码 ( 扫描后 ,Nats 自动绑定在 Admin->T_wx )
  49. func (c *UserController) User_WxQRCode() {
  50. T_name := c.GetString("T_name")
  51. T_uuid := c.GetString("T_uuid")
  52. if len(T_uuid) == 0 {
  53. T_uuid = c.Admin_r.T_uuid
  54. }
  55. WxQRCode_str, _ := NatsServer.Wx_GenerateQRCode("-|" + lib.AesEncryptCBC(T_uuid+"/"+T_name, "0123456789012345") + "|- @宝智达冷链 微信公众号通知")
  56. WxQRCode_str = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + WxQRCode_str
  57. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: WxQRCode_str}
  58. c.ServeJSON()
  59. return
  60. }
  61. // 用户绑定的公司列表
  62. func (c *UserController) Company_Tree() {
  63. var r_jsons lib.R_JSONS
  64. T_name := c.GetString("T_name")
  65. r_jsons.Data = Account.Read_Company_Tree(c.Admin_r, T_name)
  66. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  67. c.ServeJSON()
  68. return
  69. }
  70. func (c *UserController) Company_Transport_List() {
  71. var r_jsons lib.R_JSONS
  72. T_name := c.GetString("T_name")
  73. r_jsons.Data = Account.Read_Transport_Company_List(T_name)
  74. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  75. c.ServeJSON()
  76. return
  77. }
  78. // 所有公司树
  79. func (c *UserController) Company_List() {
  80. var r_jsons lib.R_JSONS
  81. T_name := c.GetString("T_name")
  82. r_jsons.Data = Account.Read_Company_List(T_name)
  83. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  84. c.ServeJSON()
  85. return
  86. }
  87. func (c *UserController) Company_Get() {
  88. var r_jsons lib.R_JSONS
  89. Company_r, err := Account.Read_Company_ById(c.T_pid)
  90. if err != nil {
  91. logs.Warning(lib.FuncName(), "获取公司信息失败", err)
  92. }
  93. r_jsons.Data = Account.CompanyToCompany_R(Company_r)
  94. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  95. c.ServeJSON()
  96. return
  97. }
  98. func (c *UserController) Company_Details() {
  99. Id, Id_err := c.GetInt("Id")
  100. if Id_err != nil {
  101. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  102. c.ServeJSON()
  103. return
  104. }
  105. var r_jsons lib.R_JSONS
  106. Company_r, err := Account.Read_Company_ById(Id)
  107. if err != nil {
  108. logs.Warning(lib.FuncName(), "获取公司信息失败", err)
  109. }
  110. r_jsons.Data = Account.CompanyToCompany_R(Company_r)
  111. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  112. c.ServeJSON()
  113. return
  114. }
  115. func (c *UserController) Company_Add() {
  116. T_mid, _ := c.GetInt("T_mid")
  117. T_name := c.GetString("T_name")
  118. T_plan := c.GetString("T_plan")
  119. T_data := c.GetString("T_data")
  120. T_v3d := c.GetString("T_v3d")
  121. T_type, _ := c.GetInt("T_type")
  122. T_Address := c.GetString("T_Address")
  123. T_coordinate := c.GetString("T_coordinate")
  124. var_ := Account.Company{
  125. T_mid: T_mid,
  126. T_name: T_name,
  127. T_State: 1,
  128. T_plan: T_plan,
  129. T_data: T_data,
  130. T_v3d: T_v3d,
  131. T_warning: 1,
  132. T_type: T_type,
  133. T_Address: T_Address,
  134. T_coordinate: T_coordinate,
  135. }
  136. id, err := Account.Add_Company(var_)
  137. if err != nil {
  138. c.Data["json"] = lib.JSONS{Code: 301, Msg: "添加失败!"}
  139. c.ServeJSON()
  140. return
  141. }
  142. if c.Admin_r.T_pids != "*" && c.Admin_r.T_pid == 0 {
  143. T_pids := []int{int(id)}
  144. if err = Account.Add_Company_bind(c.Admin_r, T_pids); err != nil {
  145. c.Data["json"] = lib.JSONS{Code: 202, Msg: "公司绑定失败"}
  146. c.ServeJSON()
  147. return
  148. }
  149. }
  150. System.Add_UserLogs_T(c.Admin_r.T_uuid, "公司管理", "添加", var_)
  151. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: id}
  152. c.ServeJSON()
  153. return
  154. }
  155. func (c *UserController) Company_Edit() {
  156. Id, Id_err := c.GetInt("Id")
  157. if Id_err != nil {
  158. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  159. c.ServeJSON()
  160. return
  161. }
  162. Company_r, err := Account.Read_Company_ById(Id)
  163. if err != nil {
  164. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  165. c.ServeJSON()
  166. return
  167. }
  168. T_name := c.GetString("T_name")
  169. T_type, _ := c.GetInt("T_type")
  170. T_Address := c.GetString("T_Address")
  171. T_coordinate := c.GetString("T_coordinate")
  172. T_file_size_limit, _ := c.GetInt64("T_file_size_limit")
  173. if len(T_name) > 0 {
  174. Company_r.T_name = T_name
  175. }
  176. if T_type > 0 {
  177. Company_r.T_type = T_type
  178. }
  179. if len(T_Address) > 0 {
  180. Company_r.T_Address = T_Address
  181. }
  182. if len(T_coordinate) > 0 {
  183. Company_r.T_coordinate = T_coordinate
  184. }
  185. if T_file_size_limit >= 0 {
  186. Company_r.T_file_size_limit = T_file_size_limit
  187. }
  188. is := Account.Update_Company(Company_r, "T_name", "T_type", "T_Address", "T_coordinate", "T_file_size_limit")
  189. if !is {
  190. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  191. c.ServeJSON()
  192. return
  193. }
  194. System.Add_UserLogs_T(c.Admin_r.T_uuid, "公司管理", "修改", Company_r)
  195. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  196. c.ServeJSON()
  197. return
  198. }
  199. // 数字孪生
  200. func (c *UserController) Company_DigitalTwin() {
  201. Id, Id_err := c.GetInt("Id")
  202. if Id_err != nil {
  203. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  204. c.ServeJSON()
  205. return
  206. }
  207. Company_r, err := Account.Read_Company_ById(Id)
  208. if err != nil {
  209. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  210. c.ServeJSON()
  211. return
  212. }
  213. T_plan := c.GetString("T_plan")
  214. T_data := c.GetString("T_data")
  215. T_v3d := c.GetString("T_v3d")
  216. T_warning, _ := c.GetInt("T_warning")
  217. Company_r.T_plan = T_plan
  218. Company_r.T_data = T_data
  219. Company_r.T_v3d = T_v3d
  220. Company_r.T_warning = T_warning
  221. is := Account.Update_Company(Company_r, "T_plan", "T_data", "T_v3d", "T_warning")
  222. if !is {
  223. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  224. c.ServeJSON()
  225. return
  226. }
  227. System.Add_UserLogs_T(c.Admin_r.T_uuid, "公司管理", "修改数字孪生", Company_r)
  228. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  229. c.ServeJSON()
  230. return
  231. }
  232. // 充值
  233. func (c *UserController) Company_Pay() {
  234. Id, Id_err := c.GetInt("Id")
  235. if Id_err != nil {
  236. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  237. c.ServeJSON()
  238. return
  239. }
  240. Company_r, err := Account.Read_Company_ById(Id)
  241. if err != nil {
  242. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  243. c.ServeJSON()
  244. return
  245. }
  246. T_balance, _ := c.GetFloat("T_balance")
  247. T_money64, _ := decimal.NewFromFloat(float64(Company_r.T_money) + T_balance).Round(2).Float64()
  248. Company_r.T_money = float32(T_money64)
  249. is := Account.Update_Company(Company_r, "T_money")
  250. if !is {
  251. c.Data["json"] = lib.JSONS{Code: 202, Msg: "充值失败!"}
  252. c.ServeJSON()
  253. return
  254. }
  255. // 添加充值记录
  256. bill := Company.CompanyBill{
  257. T_pid: Company_r.Id,
  258. T_type: "充值",
  259. T_project: "充值",
  260. T_bill: "充值",
  261. T_charging: float32(T_balance),
  262. T_balance: float32(T_money64),
  263. }
  264. _, err = Company.Add_CompanyBill(bill)
  265. if err != nil {
  266. c.Data["json"] = lib.JSONS{Code: 202, Msg: "充值失败!"}
  267. c.ServeJSON()
  268. return
  269. }
  270. System.Add_UserLogs_T(c.Admin_r.T_uuid, "公司管理", "充值", Company_r)
  271. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  272. c.ServeJSON()
  273. return
  274. }
  275. // 报警统计
  276. func (c *UserController) Company_Warning() {
  277. Id, Id_err := c.GetInt("Id")
  278. if Id_err != nil {
  279. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  280. c.ServeJSON()
  281. return
  282. }
  283. Company_r, err := Account.Read_Company_ById(Id)
  284. if err != nil {
  285. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  286. c.ServeJSON()
  287. return
  288. }
  289. T_warning, _ := c.GetInt("T_warning")
  290. if T_warning > 0 {
  291. Company_r.T_warning = T_warning
  292. }
  293. is := Account.Update_Company(Company_r, "T_warning")
  294. if !is {
  295. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  296. c.ServeJSON()
  297. return
  298. }
  299. System.Add_UserLogs_T(c.Admin_r.T_uuid, "公司管理", "修改报警统计", Company_r)
  300. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  301. c.ServeJSON()
  302. return
  303. }
  304. // 第三方跳转
  305. func (c *UserController) Company_ThirdPartiesSkip() {
  306. Id, Id_err := c.GetInt("Id")
  307. if Id_err != nil {
  308. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  309. c.ServeJSON()
  310. return
  311. }
  312. Company_r, err := Account.Read_Company_ById(Id)
  313. if err != nil {
  314. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  315. c.ServeJSON()
  316. return
  317. }
  318. T_skip := c.GetString("T_skip")
  319. if len(T_skip) > 0 {
  320. var list []Account.ThirdPartiesSkip
  321. if err = json.Unmarshal([]byte(T_skip), &list); err != nil {
  322. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_skip JSON 解析失败!"}
  323. c.ServeJSON()
  324. return
  325. }
  326. Company_r.T_ThirdPartiesSkip = T_skip
  327. }
  328. is := Account.Update_Company(Company_r, "T_ThirdPartiesSkip")
  329. if !is {
  330. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  331. c.ServeJSON()
  332. return
  333. }
  334. System.Add_UserLogs_T(c.Admin_r.T_uuid, "公司管理", "第三方跳转", Company_r)
  335. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  336. c.ServeJSON()
  337. return
  338. }
  339. // 到期时间
  340. func (c *UserController) Company_ExpirationTime() {
  341. Id, Id_err := c.GetInt("Id")
  342. if Id_err != nil {
  343. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  344. c.ServeJSON()
  345. return
  346. }
  347. Company_r, err := Account.Read_Company_ById(Id)
  348. if err != nil {
  349. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  350. c.ServeJSON()
  351. return
  352. }
  353. T_expirationTime := c.GetString("T_expirationTime")
  354. Company_r.T_expirationTime = T_expirationTime
  355. is := Account.Update_Company(Company_r, "T_expirationTime")
  356. if !is {
  357. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  358. c.ServeJSON()
  359. return
  360. }
  361. System.Add_UserLogs_T(c.Admin_r.T_uuid, "公司管理", "到期时间", Company_r)
  362. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  363. c.ServeJSON()
  364. return
  365. }
  366. func (c *UserController) Company_Del() {
  367. Id, Id_err := c.GetInt("Id")
  368. if Id_err != nil {
  369. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  370. c.ServeJSON()
  371. return
  372. }
  373. Company_r, err := Account.Read_Company_ById(Id)
  374. if err != nil {
  375. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  376. c.ServeJSON()
  377. return
  378. }
  379. ids := Account.ReadCompanyIds_T_path(Company_r.T_path)
  380. if cnt := Account.Read_Admin_Num_ByT_pid(ids); cnt > 0 {
  381. c.Data["json"] = lib.JSONS{Code: 202, Msg: "有关联用户,禁止删除!"}
  382. c.ServeJSON()
  383. return
  384. }
  385. o := orm.NewOrm()
  386. o.Begin()
  387. for _, id := range ids {
  388. if is := Account.Delete_Company(o, id); !is {
  389. o.Rollback()
  390. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  391. c.ServeJSON()
  392. return
  393. }
  394. if err = Account.Delete_Company_bind_T_pids(o, id); err != nil {
  395. o.Rollback()
  396. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  397. c.ServeJSON()
  398. return
  399. }
  400. }
  401. o.Commit()
  402. System.Add_UserLogs_T(c.Admin_r.T_uuid, "公司管理", "删除", Company_r)
  403. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  404. c.ServeJSON()
  405. return
  406. }
  407. // 进入公司
  408. func (c *UserController) Company_Entry() {
  409. GetCookie := c.Ctx.GetCookie("User_tokey")
  410. GetString := c.GetString("User_tokey")
  411. User_tokey := GetString
  412. if len(User_tokey) == 0 {
  413. User_tokey = GetCookie
  414. }
  415. T_pid, _ := c.GetInt("T_pid")
  416. // 操作pid
  417. company_r, err := Account.Read_Company_ById(T_pid)
  418. if err != nil {
  419. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  420. c.ServeJSON()
  421. return
  422. }
  423. visit := false
  424. if c.Admin_r.T_pid > 0 && strings.Contains(company_r.T_path, fmt.Sprintf("/%d/", c.Admin_r.T_pid)) {
  425. visit = true
  426. }
  427. if c.Admin_r.T_pid == 0 {
  428. if c.Admin_r.T_pids == "*" {
  429. visit = true
  430. } else {
  431. pids := lib.SplitStringToIntIds(c.Admin_r.T_pids, "P")
  432. if len(pids) > 0 {
  433. list := Account.ReadCompanyIds_T_pids(pids)
  434. for _, v := range list {
  435. if T_pid == v {
  436. visit = true
  437. break
  438. }
  439. }
  440. }
  441. }
  442. }
  443. if !visit {
  444. c.Data["json"] = lib.JSONS{Code: 202, Msg: "没有权限!"}
  445. c.ServeJSON()
  446. return
  447. }
  448. Account.Redis_Tokey_T_pid_Set(User_tokey, T_pid)
  449. System.Add_UserLogs_T(c.Admin_r.T_uuid, "公司管理", "进入公司", T_pid)
  450. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  451. c.ServeJSON()
  452. return
  453. }
  454. // 记账扣费
  455. func (c *UserController) Company_Charging() {
  456. Id, Id_err := c.GetInt("Id")
  457. if Id_err != nil {
  458. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  459. c.ServeJSON()
  460. return
  461. }
  462. Company_r, err := Account.Read_Company_ById(Id)
  463. if err != nil {
  464. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  465. c.ServeJSON()
  466. return
  467. }
  468. T_Charging, _ := c.GetInt("T_Charging")
  469. Company_r.T_Charging = T_Charging
  470. is := Account.Update_Company(Company_r, "T_Charging")
  471. if !is {
  472. c.Data["json"] = lib.JSONS{Code: 202, Msg: "充值失败!"}
  473. c.ServeJSON()
  474. return
  475. }
  476. System.Add_UserLogs_T(c.Admin_r.T_uuid, "公司管理", "记账扣费", Company_r)
  477. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  478. c.ServeJSON()
  479. return
  480. }
  481. // 检查 Tokey 是否过期
  482. // 用户管理
  483. func (c *UserController) User_List() {
  484. var r_jsons lib.R_JSONS
  485. page, _ := c.GetInt("page")
  486. if page < 1 {
  487. page = 1
  488. }
  489. page_z, _ := c.GetInt("page_z")
  490. if page_z < 1 {
  491. page_z = conf.Page_size
  492. }
  493. T_name := c.GetString("T_name")
  494. Account.Read_Power_All_Map()
  495. r_jsons.Data, r_jsons.Num = Account.Read_Admin_List(c.T_pid, T_name, page, page_z)
  496. r_jsons.Page = page
  497. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  498. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  499. c.ServeJSON()
  500. return
  501. }
  502. func (c *UserController) User_Get() {
  503. T_uuid := c.GetString("T_uuid")
  504. if len(T_uuid) < 8 {
  505. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  506. c.ServeJSON()
  507. return
  508. }
  509. admin_r, err := Account.Read_Admin_ByUuid(T_uuid)
  510. if err != nil {
  511. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  512. c.ServeJSON()
  513. return
  514. }
  515. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Account.AdminToAdmin_Details(admin_r)}
  516. c.ServeJSON()
  517. return
  518. }
  519. func (c *UserController) User_Info() {
  520. type User_ struct {
  521. User Account.Admin_R
  522. //Power Account.Power // 权限
  523. //Menu map[string]Account.Menu_R // 菜单
  524. //Menu []Account.Menu // 菜单
  525. }
  526. Admin_r := User_{}
  527. Admin_r.User = Account.AdminToAdmin_R(c.Admin_r)
  528. //Admin_r.Power, _ = Account.Read_Power_ById(admin_r.T_power)
  529. //Admin_r.Menu = Account.Read_Menu_Tree(Power.T_menu, T_pid)
  530. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Admin_r}
  531. c.ServeJSON()
  532. return
  533. }
  534. func (c *UserController) User_Post() {
  535. admin_r := c.Admin_r
  536. T_name := c.GetString("T_name")
  537. T_pass := c.GetString("T_pass")
  538. T_pass_str := c.GetString("T_pass_str")
  539. T_phone := c.GetString("T_phone")
  540. T_mail := c.GetString("T_mail")
  541. T_wx := c.GetString("T_wx")
  542. if len(T_pass) < 8 && len(T_pass) > 0 {
  543. c.Data["json"] = lib.JSONS{Code: 202, Msg: "密码异常!"}
  544. c.ServeJSON()
  545. return
  546. }
  547. if len(T_name) > 0 {
  548. admin_r.T_name = T_name
  549. }
  550. if len(T_pass) >= 8 {
  551. admin_r.T_pass = T_pass
  552. }
  553. if len(T_pass_str) > 0 {
  554. admin_r.T_pass_str = T_pass_str
  555. }
  556. if len(T_phone) > 0 {
  557. admin_r.T_phone = T_phone
  558. }
  559. if len(T_mail) > 0 {
  560. admin_r.T_mail = T_mail
  561. }
  562. if len(T_wx) > 0 {
  563. if T_wx == "-" {
  564. T_wx = ""
  565. }
  566. admin_r.T_wx = T_wx
  567. }
  568. is := Account.Update_Admin(admin_r, "T_name", "T_pass", "T_pass_str", "T_phone", "T_mail", "T_wx")
  569. if !is {
  570. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  571. c.ServeJSON()
  572. return
  573. }
  574. admin_r.T_pass = "******"
  575. System.Add_UserLogs_T(c.Admin_r.T_uuid, "用户管理", "修改个人信息", admin_r)
  576. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  577. c.ServeJSON()
  578. return
  579. }
  580. func (c *UserController) User_Add() {
  581. T_power, _ := c.GetInt("T_power")
  582. T_name := c.GetString("T_name")
  583. T_user := c.GetString("T_user")
  584. T_pass := c.GetString("T_pass")
  585. T_pass_str := c.GetString("T_pass_str")
  586. T_phone := c.GetString("T_phone")
  587. T_mail := c.GetString("T_mail")
  588. T_wx := c.GetString("T_wx")
  589. T_uuid := uuid.NewV4().String()
  590. var_ := Account.Admin{
  591. T_uuid: T_uuid,
  592. T_pid: c.T_pid,
  593. T_name: T_name,
  594. T_user: T_user,
  595. T_phone: T_phone,
  596. T_mail: T_mail,
  597. T_wx: T_wx,
  598. T_pass: T_pass,
  599. T_pass_str: T_pass_str,
  600. T_power: T_power,
  601. T_State: 1,
  602. }
  603. if len(T_pass) < 8 {
  604. c.Data["json"] = lib.JSONS{Code: 202, Msg: "密码异常!"}
  605. c.ServeJSON()
  606. return
  607. }
  608. _, err := Account.Read_Admin_T_user(T_user)
  609. if err == nil {
  610. c.Data["json"] = lib.JSONS{Code: 302, Msg: "账户重复!"}
  611. c.ServeJSON()
  612. return
  613. }
  614. _, err = Account.Add_Admin(var_)
  615. if err != nil {
  616. c.Data["json"] = lib.JSONS{Code: 301, Msg: "添加失败!"}
  617. c.ServeJSON()
  618. return
  619. }
  620. var_.T_pass = "******"
  621. System.Add_UserLogs_T(c.Admin_r.T_uuid, "用户管理", "添加", var_)
  622. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  623. c.ServeJSON()
  624. return
  625. }
  626. func (c *UserController) User_Edit() {
  627. T_uuid := c.GetString("T_uuid")
  628. if len(T_uuid) < 8 {
  629. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  630. c.ServeJSON()
  631. return
  632. }
  633. r, err := Account.Read_Admin_ByUuid(T_uuid)
  634. if err != nil {
  635. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  636. c.ServeJSON()
  637. return
  638. }
  639. T_power, T_power_err := c.GetInt("T_power")
  640. T_name := c.GetString("T_name")
  641. T_pass := c.GetString("T_pass")
  642. T_pass_str := c.GetString("T_pass_str")
  643. T_phone := c.GetString("T_phone")
  644. T_mail := c.GetString("T_mail")
  645. if T_power_err == nil {
  646. r.T_power = T_power
  647. }
  648. if len(T_pass) < 8 && len(T_pass) > 0 {
  649. c.Data["json"] = lib.JSONS{Code: 202, Msg: "密码异常!"}
  650. c.ServeJSON()
  651. return
  652. }
  653. if len(T_name) > 0 {
  654. r.T_name = T_name
  655. }
  656. if len(T_pass) >= 8 {
  657. r.T_pass = T_pass
  658. }
  659. if len(T_pass_str) > 0 {
  660. r.T_pass_str = T_pass_str
  661. }
  662. if len(T_phone) > 0 {
  663. r.T_phone = T_phone
  664. }
  665. if len(T_mail) > 0 {
  666. r.T_mail = T_mail
  667. }
  668. is := Account.Update_Admin(r, "T_power", "T_pid", "T_name", "T_pass", "T_pass_str", "T_phone", "T_mail")
  669. if !is {
  670. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  671. c.ServeJSON()
  672. return
  673. }
  674. if len(T_name) > 0 {
  675. if is = Company.Update_CompanyNotice_Bind_By_T_uuid(T_uuid, T_name); !is {
  676. logs.Info("修改用户 => 修改用户关联报警通知失败", T_uuid, T_name)
  677. }
  678. }
  679. r.T_pass = "******"
  680. System.Add_UserLogs_T(c.Admin_r.T_uuid, "用户管理", "编辑", r)
  681. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  682. c.ServeJSON()
  683. return
  684. }
  685. func (c *UserController) User_Del() {
  686. T_uuid := c.GetString("T_uuid")
  687. if len(T_uuid) <= 0 {
  688. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  689. c.ServeJSON()
  690. return
  691. }
  692. admin_r_lod, err := Account.Read_Admin_ByUuid(T_uuid)
  693. if err != nil {
  694. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  695. c.ServeJSON()
  696. return
  697. }
  698. if is := Account.Delete_Admin(admin_r_lod); !is {
  699. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  700. c.ServeJSON()
  701. return
  702. }
  703. if is := Company.Delete_CompanyNotice_Bind_By_T_uuid(T_uuid); !is {
  704. logs.Info("删除用户 => 删除用户关联报警通知失败", T_uuid)
  705. }
  706. System.Add_UserLogs_T(c.Admin_r.T_uuid, "用户管理", "删除", T_uuid)
  707. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  708. c.ServeJSON()
  709. return
  710. }
  711. func (c *UserController) User_GetCompanyInfo() {
  712. // 报警标识
  713. //设备管理-设备报警 callthepolice
  714. //公司管理-设备报警 companyCallthepolice
  715. //T_warning_sign := c.GetString("T_warning_sign")
  716. // 验证登录
  717. admin_r := c.Admin_r
  718. type Info struct {
  719. T_name string // 姓名
  720. T_pid int // 公司id
  721. Admin []Account.Admin_ // 专属管理员
  722. CompanyName string
  723. CompanyExpirationTime string
  724. }
  725. var info Info
  726. info.T_name = admin_r.T_name
  727. info.T_pid = admin_r.T_pid
  728. if admin_r.T_pid > 0 {
  729. company, _ := Account.Read_Company_ById(admin_r.T_pid)
  730. AdminList := Account.Read_Admin_List_T_pids(admin_r.T_pid)
  731. var Admin_ []Account.Admin_
  732. for _, v := range AdminList {
  733. Admin_ = append(Admin_, Account.Admin_RToAdmin_(v))
  734. }
  735. info.Admin = Admin_
  736. info.CompanyName = company.T_name
  737. info.CompanyExpirationTime = company.T_expirationTime
  738. }
  739. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: info}
  740. c.ServeJSON()
  741. return
  742. }
  743. // 管理员
  744. func (c *UserController) User_Home() {
  745. // 报警标识
  746. //设备管理-设备报警 callthepolice
  747. //公司管理-设备报警 companyCallthepolice
  748. //T_warning_sign := c.GetString("T_warning_sign")
  749. power, err := Account.Read_Power_ById(c.Admin_r.T_power)
  750. if err != nil {
  751. c.Data["json"] = lib.JSONS{Code: 202, Msg: "获取菜单失败"}
  752. c.ServeJSON()
  753. return
  754. }
  755. ret := Account.Read_Menu_Bind_List(power.Id, power.T_menu, c.T_pid)
  756. var T_warning_sign string
  757. for _, v := range ret.MenuPermission {
  758. if v == Warning.CompanyCallthepolice {
  759. T_warning_sign = Warning.CompanyCallthepolice
  760. break
  761. }
  762. if v == Warning.Callthepolice {
  763. T_warning_sign = Warning.Callthepolice
  764. }
  765. }
  766. // 验证登录
  767. admin_r := c.Admin_r
  768. type NoticeRecord struct {
  769. Sms int // 短信
  770. Phone int // 电话
  771. }
  772. type Info struct {
  773. T_name string // 姓名
  774. T_pids string // 内部用户绑定的公司id
  775. T_pid int // 公司id
  776. T_Days int // 服务天数
  777. Admin []Account.Admin_ // 专属管理员
  778. WarningSign string // 报警标识
  779. WarningNum int64 // 报警数量
  780. UntreatedWarningNum int64 // 未处理报警数量
  781. NewsNum int64 // 消息数量
  782. WorkOrderNum int64 // 工单数量
  783. WarningRate float32 // 设备报警率
  784. UntreatedWorkOrderNum int64 // 未处理工单数量
  785. Balance float32 // 余额
  786. NoticeRecord NoticeRecord // 通知记录
  787. CompanyKey string
  788. CompanyName string
  789. CompanyExpirationTime string
  790. }
  791. var info Info
  792. info.T_name = admin_r.T_name
  793. info.T_pid = admin_r.T_pid
  794. var Company_r Account.Company
  795. if c.T_pid > 0 {
  796. Company_r, err = Account.Read_Company_ById(c.T_pid)
  797. if err != nil {
  798. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  799. c.ServeJSON()
  800. return
  801. }
  802. info.CompanyKey = Company_r.T_key
  803. info.CompanyName = Company_r.T_name
  804. info.CompanyExpirationTime = Company_r.T_expirationTime
  805. }
  806. if admin_r.T_pid > 0 {
  807. info.WarningSign = T_warning_sign
  808. bindSN, err := Account.Read_UserDevice_List(c.Admin_r)
  809. if err != nil {
  810. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  811. c.ServeJSON()
  812. return
  813. }
  814. company, _ := Account.Read_Company_ById(admin_r.T_pid)
  815. info.T_Days = lib.GetDiffDays(time.Now(), company.CreateTime)
  816. AdminList := Account.Read_Admin_List_T_pids(admin_r.T_pid)
  817. var Admin_ []Account.Admin_
  818. for _, v := range AdminList {
  819. Admin_ = append(Admin_, Account.Admin_RToAdmin_(v))
  820. }
  821. info.Admin = Admin_
  822. T_pids := []int{}
  823. // 拥有公司管理-设备报警权限 统计公司及其所有子公司的今日报警数量、未处理报警数量、我的消息数量
  824. if T_warning_sign == Warning.CompanyCallthepolice {
  825. T_pids = Account.ReadCompanyIds_T_path(Company_r.T_path)
  826. info.WarningNum = Warning.Read_Warning_ALL_T_State_Count(0, T_pids, 1, bindSN, power.T_warning, true)
  827. info.UntreatedWarningNum = Warning.Read_Warning_ALL_T_State_Count(0, T_pids, 3, bindSN, power.T_warning, false)
  828. info.NewsNum = Warning.Read_Warning_ALL_T_State_Count(0, T_pids, 1, bindSN, "T1003|T1002|", false)
  829. }
  830. // 拥有设备管理-设备报警权限 统计本公司今日报警数量、未处理报警数量、我的消息数量
  831. if T_warning_sign == Warning.Callthepolice {
  832. info.WarningNum = Warning.Read_Warning_ALL_T_State_Count(admin_r.T_pid, T_pids, 1, bindSN, power.T_warning, true)
  833. info.UntreatedWarningNum = Warning.Read_Warning_ALL_T_State_Count(admin_r.T_pid, T_pids, 3, bindSN, power.T_warning, false)
  834. info.NewsNum = Warning.Read_Warning_ALL_T_State_Count(admin_r.T_pid, T_pids, 1, bindSN, "T1003|T1002|", false)
  835. }
  836. info.Balance = company.T_money
  837. info.NoticeRecord.Sms = Warning.Get_WarningSandNum_CurrentMonth(admin_r.T_pid, 1)
  838. info.NoticeRecord.Phone = Warning.Get_WarningSandNum_CurrentMonth(admin_r.T_pid, 2)
  839. }
  840. if admin_r.T_pid == 0 {
  841. if len(admin_r.T_pids) > 0 && admin_r.T_pids != "*" {
  842. info.T_pids = admin_r.T_pids
  843. info.WarningRate = Warning.Read_WarningRate_Yesterday(admin_r.T_uuid)
  844. }
  845. info.WorkOrderNum, info.UntreatedWorkOrderNum = NatsServer.Read_WorkOrderT_State_Count(admin_r.T_pids)
  846. }
  847. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: info}
  848. c.ServeJSON()
  849. return
  850. }
  851. // 内部用户管理
  852. func (c *UserController) Admin_List() {
  853. var r_jsons lib.R_JSONS
  854. page, _ := c.GetInt("page")
  855. if page < 1 {
  856. page = 1
  857. }
  858. page_z, _ := c.GetInt("page_z")
  859. if page_z < 1 {
  860. page_z = conf.Page_size
  861. }
  862. T_name := c.GetString("T_name")
  863. if c.Admin_r.T_pid > 0 {
  864. c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权查看!"}
  865. c.ServeJSON()
  866. return
  867. }
  868. T_bind_company, _ := c.GetInt("T_bind_company") // 管理员绑定公司 0-全部 1-绑定公司
  869. Account.Read_Power_All_Map()
  870. userList, num := Account.Read_Admin_List(0, T_name, page, page_z)
  871. r_jsons.Data, r_jsons.Num = userList, num
  872. // 过滤已绑定公司列表
  873. if T_bind_company == 1 {
  874. var adminList []Account.Admin_R
  875. for _, v := range userList {
  876. if len(v.T_pids) == 0 || v.T_pids == "*" {
  877. continue
  878. }
  879. adminList = append(adminList, v)
  880. }
  881. r_jsons.Data = adminList
  882. }
  883. r_jsons.Page = page
  884. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  885. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  886. c.ServeJSON()
  887. return
  888. }
  889. func (c *UserController) Admin_Get() {
  890. T_uuid := c.GetString("T_uuid")
  891. if len(T_uuid) < 8 {
  892. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  893. c.ServeJSON()
  894. return
  895. }
  896. admin_r, err := Account.Read_Admin_ByUuid(T_uuid)
  897. if err != nil {
  898. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  899. c.ServeJSON()
  900. return
  901. }
  902. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Account.AdminToAdmin_Details(admin_r)}
  903. c.ServeJSON()
  904. return
  905. }
  906. func (c *UserController) Admin_Add() {
  907. T_power, _ := c.GetInt("T_power")
  908. T_name := c.GetString("T_name")
  909. T_user := c.GetString("T_user")
  910. T_pass := c.GetString("T_pass")
  911. T_pass_str := c.GetString("T_pass_str")
  912. T_phone := c.GetString("T_phone")
  913. T_mail := c.GetString("T_mail")
  914. T_wx := c.GetString("T_wx")
  915. var_ := Account.Admin{
  916. T_uuid: uuid.NewV4().String(),
  917. T_pid: 0,
  918. T_name: T_name,
  919. T_user: T_user,
  920. T_phone: T_phone,
  921. T_mail: T_mail,
  922. T_wx: T_wx,
  923. T_pass: T_pass,
  924. T_pass_str: T_pass_str,
  925. T_power: T_power,
  926. T_State: 1,
  927. }
  928. if len(T_pass) < 8 {
  929. c.Data["json"] = lib.JSONS{Code: 202, Msg: "密码异常!"}
  930. c.ServeJSON()
  931. return
  932. }
  933. _, err := Account.Read_Admin_T_user(T_user)
  934. if err == nil {
  935. c.Data["json"] = lib.JSONS{Code: 302, Msg: "账户重复!"}
  936. c.ServeJSON()
  937. return
  938. }
  939. _, err = Account.Add_Admin(var_)
  940. if err != nil {
  941. c.Data["json"] = lib.JSONS{Code: 301, Msg: "添加失败!"}
  942. c.ServeJSON()
  943. return
  944. }
  945. var_.T_pass = "******"
  946. System.Add_UserLogs_T(c.Admin_r.T_uuid, "内部用户管理", "添加", var_)
  947. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  948. c.ServeJSON()
  949. return
  950. }
  951. func (c *UserController) Admin_Edit() {
  952. T_uuid := c.GetString("T_uuid")
  953. if len(T_uuid) < 8 {
  954. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  955. c.ServeJSON()
  956. return
  957. }
  958. r, err := Account.Read_Admin_ByUuid(T_uuid)
  959. if err != nil {
  960. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  961. c.ServeJSON()
  962. return
  963. }
  964. T_power, T_power_err := c.GetInt("T_power")
  965. T_user := c.GetString("T_user")
  966. T_name := c.GetString("T_name")
  967. T_pass := c.GetString("T_pass")
  968. T_pass_str := c.GetString("T_pass_str")
  969. T_phone := c.GetString("T_phone")
  970. T_mail := c.GetString("T_mail")
  971. T_wx := c.GetString("T_wx")
  972. if T_power_err == nil {
  973. r.T_power = T_power
  974. }
  975. if len(T_pass) < 8 && len(T_pass) > 0 {
  976. c.Data["json"] = lib.JSONS{Code: 202, Msg: "密码异常!"}
  977. c.ServeJSON()
  978. return
  979. }
  980. if len(T_user) > 0 {
  981. var admin Account.Admin
  982. admin, err = Account.Read_Admin_T_user(T_user)
  983. if err == nil && admin.T_uuid != T_uuid {
  984. c.Data["json"] = lib.JSONS{Code: 302, Msg: "用户名重复!"}
  985. c.ServeJSON()
  986. return
  987. }
  988. r.T_user = T_user
  989. }
  990. if len(T_name) > 0 {
  991. r.T_name = T_name
  992. }
  993. if len(T_pass) >= 8 {
  994. r.T_pass = T_pass
  995. }
  996. if len(T_pass_str) > 0 {
  997. r.T_pass_str = T_pass_str
  998. }
  999. if len(T_phone) > 0 {
  1000. r.T_phone = T_phone
  1001. }
  1002. if len(T_mail) > 0 {
  1003. r.T_mail = T_mail
  1004. }
  1005. if len(T_wx) > 0 {
  1006. r.T_wx = T_wx
  1007. }
  1008. is := Account.Update_Admin(r, "T_user", "T_power", "T_name", "T_pass", "T_pass_str", "T_phone", "T_mail", "T_wx")
  1009. if !is {
  1010. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  1011. c.ServeJSON()
  1012. return
  1013. }
  1014. if len(T_name) > 0 {
  1015. if is = Company.Update_CompanyNotice_Bind_By_T_uuid(T_uuid, "内管-"+T_name); !is {
  1016. logs.Info("修改内部用户 => 修改内部用户关联报警通知失败", T_uuid, T_name)
  1017. }
  1018. }
  1019. r.T_pass = "********"
  1020. System.Add_UserLogs_T(c.Admin_r.T_uuid, "内部用户管理", "修改", r)
  1021. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1022. c.ServeJSON()
  1023. return
  1024. }
  1025. func (c *UserController) Admin_Del() {
  1026. T_uuid := c.GetString("T_uuid")
  1027. if len(T_uuid) <= 0 {
  1028. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  1029. c.ServeJSON()
  1030. return
  1031. }
  1032. admin_r_lod, err := Account.Read_Admin_ByUuid(T_uuid)
  1033. if err != nil {
  1034. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  1035. c.ServeJSON()
  1036. return
  1037. }
  1038. if admin_r_lod.Id == 1 {
  1039. c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权删除!"}
  1040. c.ServeJSON()
  1041. return
  1042. }
  1043. if is := Account.Delete_Admin(admin_r_lod); !is {
  1044. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  1045. c.ServeJSON()
  1046. return
  1047. }
  1048. if is := Company.Delete_CompanyNotice_Bind_By_T_uuid(T_uuid); !is {
  1049. logs.Info("删除内部用户 => 删除内部用户关联报警通知失败", T_uuid)
  1050. }
  1051. System.Add_UserLogs_T(c.Admin_r.T_uuid, "内部用户管理", "删除", T_uuid)
  1052. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1053. c.ServeJSON()
  1054. return
  1055. }
  1056. // 报警通知的用户列表
  1057. func (c *UserController) User_Notice_List() {
  1058. T_name := c.GetString("T_name")
  1059. type R_JSONS struct {
  1060. //必须的大写开头
  1061. AdminList []Account.Admin_R
  1062. UserList []Account.Admin_R
  1063. }
  1064. var AdminList, UserList []Account.Admin_R
  1065. var r_jsons R_JSONS
  1066. // 操作pid
  1067. company_r, err := Account.Read_Company_ById(c.T_pid)
  1068. if err != nil {
  1069. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1070. c.ServeJSON()
  1071. return
  1072. }
  1073. T_pids := []int{}
  1074. for _, v := range strings.Split(strings.Trim(company_r.T_path, "/"), "/") {
  1075. pid, _ := strconv.Atoi(v)
  1076. T_pids = append(T_pids, pid)
  1077. }
  1078. // 绑定公司id的内部用户列表
  1079. AdminList = Account.Read_Admin_List_T_pids(c.T_pid)
  1080. // 绑定公司id的公司用户列表
  1081. //r_jsons.UserList = Account.Read_Admin_List_T_pid(c.T_pid)
  1082. // 绑定上级公司用户列表
  1083. UserList = Account.Read_Parent_Admin_List(T_pids)
  1084. if len(T_name) > 0 {
  1085. for _, v := range AdminList {
  1086. if strings.Contains(v.T_name, T_name) {
  1087. r_jsons.AdminList = append(r_jsons.AdminList, v)
  1088. }
  1089. }
  1090. for _, v := range UserList {
  1091. if strings.Contains(v.T_name, T_name) {
  1092. r_jsons.UserList = append(r_jsons.UserList, v)
  1093. }
  1094. }
  1095. } else {
  1096. r_jsons.AdminList = AdminList
  1097. r_jsons.UserList = UserList
  1098. }
  1099. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1100. c.ServeJSON()
  1101. return
  1102. }
  1103. // 管理员绑定公司
  1104. func (c *UserController) AdminCompanyBind_List() {
  1105. var r_jsons lib.R_JSONS
  1106. T_uuid := c.GetString("T_uuid")
  1107. if len(T_uuid) < 8 {
  1108. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  1109. c.ServeJSON()
  1110. return
  1111. }
  1112. r, err := Account.Read_Admin_ByUuid(T_uuid)
  1113. if err != nil {
  1114. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  1115. c.ServeJSON()
  1116. return
  1117. }
  1118. r_jsons.Data = Account.Read_Company_List_ByT_pids(r.T_pids)
  1119. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1120. c.ServeJSON()
  1121. return
  1122. }
  1123. func (c *UserController) AdminCompanyUnBind_List() {
  1124. var r_jsons lib.R_JSONS
  1125. T_name := c.GetString("T_name")
  1126. T_uuid := c.GetString("T_uuid")
  1127. if len(T_uuid) < 8 {
  1128. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  1129. c.ServeJSON()
  1130. return
  1131. }
  1132. r, err := Account.Read_Admin_ByUuid(T_uuid)
  1133. if err != nil {
  1134. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  1135. c.ServeJSON()
  1136. return
  1137. }
  1138. r_jsons.Data = Account.Read_UnBind_Company_List_ByT_pids(r.T_pids, T_name)
  1139. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1140. c.ServeJSON()
  1141. return
  1142. }
  1143. func (c *UserController) AdminCompanyBind_Add_All() {
  1144. T_uuid := c.GetString("T_uuid")
  1145. if len(T_uuid) < 8 {
  1146. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  1147. c.ServeJSON()
  1148. return
  1149. }
  1150. r, err := Account.Read_Admin_ByUuid(T_uuid)
  1151. if err != nil {
  1152. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  1153. c.ServeJSON()
  1154. return
  1155. }
  1156. if err = Account.Add_Company_bind_All(r); err != nil {
  1157. c.Data["json"] = lib.JSONS{Code: 202, Msg: "公司绑定失败"}
  1158. c.ServeJSON()
  1159. return
  1160. }
  1161. System.Add_UserLogs(c.Admin_r.T_uuid, "内部用户管理", "公司绑定全部", T_uuid)
  1162. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1163. c.ServeJSON()
  1164. return
  1165. }
  1166. func (c *UserController) AdminCompanyBind_Add() {
  1167. T_uuid := c.GetString("T_uuid")
  1168. if len(T_uuid) < 8 {
  1169. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  1170. c.ServeJSON()
  1171. return
  1172. }
  1173. T_pid, _ := c.GetInt("T_pid")
  1174. if T_pid == 0 {
  1175. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  1176. c.ServeJSON()
  1177. return
  1178. }
  1179. r, err := Account.Read_Admin_ByUuid(T_uuid)
  1180. if err != nil {
  1181. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  1182. c.ServeJSON()
  1183. return
  1184. }
  1185. if r.T_pids == "*" {
  1186. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1187. c.ServeJSON()
  1188. return
  1189. }
  1190. Company_r, err := Account.Read_Company_ById(T_pid)
  1191. if err != nil {
  1192. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  1193. c.ServeJSON()
  1194. return
  1195. }
  1196. T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path)
  1197. if err = Account.Add_Company_bind(r, T_pids); err != nil {
  1198. c.Data["json"] = lib.JSONS{Code: 202, Msg: "公司绑定失败"}
  1199. c.ServeJSON()
  1200. return
  1201. }
  1202. System.Add_UserLogs(c.Admin_r.T_uuid, "内部用户管理", "公司绑定添加", T_uuid+"-"+strconv.Itoa(T_pid))
  1203. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1204. c.ServeJSON()
  1205. return
  1206. }
  1207. func (c *UserController) AdminCompanyBind_Del() {
  1208. T_uuid := c.GetString("T_uuid")
  1209. if len(T_uuid) < 8 {
  1210. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  1211. c.ServeJSON()
  1212. return
  1213. }
  1214. T_pid, _ := c.GetInt("T_pid")
  1215. if T_pid == 0 {
  1216. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  1217. c.ServeJSON()
  1218. return
  1219. }
  1220. r, err := Account.Read_Admin_ByUuid(T_uuid)
  1221. if err != nil {
  1222. c.Data["json"] = lib.JSONS{Code: 202, Msg: "获取用户信息失败!"}
  1223. c.ServeJSON()
  1224. return
  1225. }
  1226. if r.T_pids == "*" {
  1227. c.Data["json"] = lib.JSONS{Code: 202, Msg: "绑定全部公司的用户不能单个解绑!"}
  1228. c.ServeJSON()
  1229. return
  1230. }
  1231. Company_r, err := Account.Read_Company_ById(T_pid)
  1232. if err != nil {
  1233. c.Data["json"] = lib.JSONS{Code: 202, Msg: "获取公司信息失败!"}
  1234. c.ServeJSON()
  1235. return
  1236. }
  1237. T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path)
  1238. if len(T_pids) == 0 {
  1239. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1240. c.ServeJSON()
  1241. return
  1242. }
  1243. if err = Account.Delete_Company_bind(r, T_pids); err != nil {
  1244. c.Data["json"] = lib.JSONS{Code: 202, Msg: "解绑公司失败"}
  1245. c.ServeJSON()
  1246. return
  1247. }
  1248. if is := Company.Delete_CompanyNotice_Bind_By_T_uuid_T_pid(T_uuid, T_pids); !is {
  1249. logs.Info("删除内部用户公司绑定 => 删除内部用户绑定的公司关联报警通知失败", T_uuid)
  1250. }
  1251. System.Add_UserLogs(c.Admin_r.T_uuid, "内部用户管理", "公司绑定删除", T_uuid+"-"+strconv.Itoa(T_pid))
  1252. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1253. c.ServeJSON()
  1254. return
  1255. }
  1256. func (c *UserController) AdminCompanyBind_Del_All() {
  1257. T_uuid := c.GetString("T_uuid")
  1258. if len(T_uuid) < 8 {
  1259. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  1260. c.ServeJSON()
  1261. return
  1262. }
  1263. r, err := Account.Read_Admin_ByUuid(T_uuid)
  1264. if err != nil {
  1265. c.Data["json"] = lib.JSONS{Code: 202, Msg: "获取用户信息失败!"}
  1266. c.ServeJSON()
  1267. return
  1268. }
  1269. var T_pids []int
  1270. if len(r.T_pids) > 0 && r.T_pids != "*" {
  1271. T_pids = lib.SplitStringToIntIds(r.T_pids, "P")
  1272. }
  1273. if err = Account.Delete_Company_bind(r, T_pids); err != nil {
  1274. c.Data["json"] = lib.JSONS{Code: 202, Msg: "解绑公司失败"}
  1275. c.ServeJSON()
  1276. return
  1277. }
  1278. if is := Company.Delete_CompanyNotice_Bind_By_T_uuid_T_pid(T_uuid, T_pids); !is {
  1279. logs.Info("删除内部用户公司绑定 => 删除内部用户绑定的公司关联报警通知失败", T_uuid)
  1280. }
  1281. System.Add_UserLogs(c.Admin_r.T_uuid, "内部用户管理", "公司绑定删除全部", T_uuid)
  1282. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1283. c.ServeJSON()
  1284. return
  1285. }
  1286. // 权限管理
  1287. func (c *UserController) Power_List() {
  1288. var r_jsons lib.R_JSONS
  1289. page, _ := c.GetInt("page")
  1290. if page < 1 {
  1291. page = 1
  1292. }
  1293. page_z, _ := c.GetInt("page_z")
  1294. if page_z < 1 {
  1295. page_z = conf.Page_size
  1296. }
  1297. Name := c.GetString("T_name")
  1298. r_jsons.Data, r_jsons.Num = Account.Read_Power_List(Name, page, page_z)
  1299. r_jsons.Page = page
  1300. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  1301. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1302. c.ServeJSON()
  1303. return
  1304. }
  1305. // 添加用户 权限选择列表
  1306. func (c *UserController) Power_List_All() {
  1307. r, err := Account.Read_Power_ById(c.Admin_r.T_power)
  1308. if err != nil {
  1309. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  1310. c.ServeJSON()
  1311. return
  1312. }
  1313. var r_jsons lib.R_JSONS
  1314. r_jsons.Data = Account.Read_Power_List_ALL_User(r.T_select)
  1315. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1316. c.ServeJSON()
  1317. return
  1318. }
  1319. // removeIncompleteParentMenus 移除不完整的父菜单ID
  1320. // 递归检查多级菜单的完整性,确保父菜单只有在所有子孙菜单都被勾选时才保留
  1321. func removeIncompleteParentMenus(checkedIds []int, allMenus []Account.Menu) []int {
  1322. // 创建已勾选菜单ID的映射,便于快速查找
  1323. checkedMap := make(map[int]bool)
  1324. for _, id := range checkedIds {
  1325. checkedMap[id] = true
  1326. }
  1327. // 递归获取所有菜单及其父子关系
  1328. parentChildrenMap := make(map[int][]int)
  1329. buildMenuParentChildrenMap(allMenus, parentChildrenMap)
  1330. // 递归检查每个菜单的完整性,从叶子节点开始向上检查
  1331. result := make([]int, 0)
  1332. for _, menuId := range checkedIds {
  1333. if shouldKeepMenu(menuId, checkedMap, parentChildrenMap) {
  1334. result = append(result, menuId)
  1335. }
  1336. }
  1337. return result
  1338. }
  1339. // shouldKeepMenu 递归检查菜单是否应该保留
  1340. // 对于父菜单,只有当其所有子菜单(及子孙菜单)都满足条件时才保留
  1341. func shouldKeepMenu(menuId int, checkedMap map[int]bool, parentChildrenMap map[int][]int) bool {
  1342. // 如果菜单没有被勾选,直接返回false
  1343. if !checkedMap[menuId] {
  1344. return false
  1345. }
  1346. // 如果是叶子菜单(没有子菜单),直接保留
  1347. children, hasChildren := parentChildrenMap[menuId]
  1348. if !hasChildren {
  1349. return true
  1350. }
  1351. // 如果是父菜单,检查所有子菜单是否都满足条件
  1352. for _, childId := range children {
  1353. // 递归检查每个子菜单
  1354. if !shouldKeepMenu(childId, checkedMap, parentChildrenMap) {
  1355. return false // 只要有一个子菜单不满足条件,父菜单就不保留
  1356. }
  1357. }
  1358. // 所有子菜单都满足条件,保留父菜单
  1359. return true
  1360. }
  1361. // buildMenuParentChildrenMap 构建父子关系映射
  1362. func buildMenuParentChildrenMap(menus []Account.Menu, parentChildrenMap map[int][]int) {
  1363. for _, menu := range menus {
  1364. // 如果有子菜单,建立父子关系
  1365. if len(menu.Children) > 0 {
  1366. for _, child := range menu.Children {
  1367. parentChildrenMap[menu.Id] = append(parentChildrenMap[menu.Id], child.Id)
  1368. }
  1369. // 递归处理子菜单
  1370. buildMenuParentChildrenMap(menu.Children, parentChildrenMap)
  1371. }
  1372. }
  1373. }
  1374. func (c *UserController) Power_Get() {
  1375. type Data struct {
  1376. Power Account.Power_
  1377. Menu []Account.Menu
  1378. Menu_checked []int
  1379. Select []Account.Power_R
  1380. Warning []Warning.WarningType_R
  1381. }
  1382. var data Data
  1383. id, id_err := c.GetInt("T_id")
  1384. if id_err != nil || id == 0 {
  1385. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  1386. c.ServeJSON()
  1387. return
  1388. }
  1389. power, err := Account.Read_Power_ById(id)
  1390. if err != nil {
  1391. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  1392. c.ServeJSON()
  1393. return
  1394. }
  1395. data.Power = Account.PowerToPower_(power)
  1396. data.Menu, data.Menu_checked = Account.Read_Menu_List_All()
  1397. if err != nil {
  1398. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_menu Err!"}
  1399. c.ServeJSON()
  1400. return
  1401. }
  1402. if power.T_menu != "*" {
  1403. data.Menu_checked = lib.SplitStringToIntIds(power.T_menu, "M")
  1404. // 检查父菜单完整性,移除不完整的父菜单ID
  1405. data.Menu_checked = removeIncompleteParentMenus(data.Menu_checked, data.Menu)
  1406. }
  1407. if err != nil {
  1408. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_menu Err!"}
  1409. c.ServeJSON()
  1410. return
  1411. }
  1412. data.Select = Account.Read_Power_List_ALL_T_select(power.T_select)
  1413. if err != nil {
  1414. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_select Err!"}
  1415. c.ServeJSON()
  1416. return
  1417. }
  1418. data.Warning = Warning.Read_WarningType_All_Power_T_Warning(power.T_warning)
  1419. if err != nil {
  1420. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_warning Err!"}
  1421. c.ServeJSON()
  1422. return
  1423. }
  1424. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: data}
  1425. c.ServeJSON()
  1426. return
  1427. }
  1428. func (c *UserController) Power_Add() {
  1429. T_name := c.GetString("T_name")
  1430. T_select := c.GetString("T_select")
  1431. T_warning := c.GetString("T_warning")
  1432. T_menu := c.GetString("T_menu")
  1433. var_ := Account.Power{
  1434. T_name: T_name,
  1435. T_select: T_select,
  1436. T_warning: T_warning,
  1437. T_menu: T_menu,
  1438. T_State: 1,
  1439. }
  1440. _, err := Account.Add_Power(var_)
  1441. if err != nil {
  1442. c.Data["json"] = lib.JSONS{Code: 301, Msg: "添加失败!"}
  1443. c.ServeJSON()
  1444. return
  1445. }
  1446. System.Add_UserLogs_T(c.Admin_r.T_uuid, "权限管理", "添加", var_)
  1447. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1448. c.ServeJSON()
  1449. return
  1450. }
  1451. func (c *UserController) Power_Edit() {
  1452. Id, Id_err := c.GetInt("T_id")
  1453. if Id_err != nil || Id == 0 {
  1454. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  1455. c.ServeJSON()
  1456. return
  1457. }
  1458. if Id == 1 {
  1459. c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权修改!"}
  1460. c.ServeJSON()
  1461. return
  1462. }
  1463. r, err := Account.Read_Power_ById(Id)
  1464. if err != nil {
  1465. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  1466. c.ServeJSON()
  1467. return
  1468. }
  1469. T_name := c.GetString("T_name")
  1470. T_select := c.GetString("T_select")
  1471. T_warning := c.GetString("T_warning")
  1472. T_menu := c.GetString("T_menu")
  1473. if len(T_name) > 0 {
  1474. r.T_name = T_name
  1475. }
  1476. if len(T_select) > 0 {
  1477. r.T_select = T_select
  1478. }
  1479. if len(T_warning) > 0 {
  1480. r.T_warning = T_warning
  1481. }
  1482. if len(T_menu) > 0 {
  1483. r.T_menu = T_menu
  1484. }
  1485. is := Account.Update_Power(r, "T_name", "T_select", "T_warning", "T_menu")
  1486. if !is {
  1487. c.Data["json"] = lib.JSONS{Code: 302, Msg: "修改失败!"}
  1488. c.ServeJSON()
  1489. return
  1490. }
  1491. System.Add_UserLogs_T(c.Admin_r.T_uuid, "权限管理", "修改", r)
  1492. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1493. c.ServeJSON()
  1494. return
  1495. }
  1496. func (c *UserController) Power_Del() {
  1497. id, id_err := c.GetInt("T_id")
  1498. if id_err != nil || id == 0 {
  1499. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  1500. c.ServeJSON()
  1501. return
  1502. }
  1503. r, err := Account.Read_Power_ById(id)
  1504. if err != nil {
  1505. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  1506. c.ServeJSON()
  1507. return
  1508. }
  1509. if cnt := Account.Read_Admin_Num_ByT_power(id); cnt > 0 {
  1510. c.Data["json"] = lib.JSONS{Code: 202, Msg: "有关联用户,禁止删除!"}
  1511. c.ServeJSON()
  1512. return
  1513. }
  1514. is := Account.Delete_Power(r)
  1515. if !is {
  1516. c.Data["json"] = lib.JSONS{Code: 302, Msg: "删除失败!"}
  1517. c.ServeJSON()
  1518. return
  1519. }
  1520. System.Add_UserLogs(c.Admin_r.T_uuid, "权限管理", "删除", strconv.Itoa(id))
  1521. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1522. c.ServeJSON()
  1523. return
  1524. }
  1525. // 菜单列表
  1526. func (c *UserController) User_Menu_List() {
  1527. type R_JSONS struct {
  1528. //必须的大写开头
  1529. Data interface{}
  1530. Permission interface{}
  1531. }
  1532. var r_jsons R_JSONS
  1533. power, err := Account.Read_Power_ById(c.Admin_r.T_power)
  1534. if err != nil {
  1535. c.Data["json"] = lib.JSONS{Code: 202, Msg: "获取菜单失败"}
  1536. c.ServeJSON()
  1537. return
  1538. }
  1539. //logs.Info("登录用户 菜单 pid 为:", c.T_pid)
  1540. ret := Account.Read_Menu_Bind_List(power.Id, power.T_menu, c.T_pid)
  1541. r_jsons.Data = ret.Menu
  1542. r_jsons.Permission = ret.Permission
  1543. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1544. c.ServeJSON()
  1545. return
  1546. }
  1547. func (c *UserController) GetUserMenuList() {
  1548. type R_JSONS struct {
  1549. //必须的大写开头
  1550. Pid int
  1551. Data interface{}
  1552. Permission interface{}
  1553. }
  1554. var r_jsons R_JSONS
  1555. power, err := Account.Read_Power_ById(c.Admin_r.T_power)
  1556. if err != nil {
  1557. c.Data["json"] = lib.JSONS{Code: 202, Msg: "获取菜单失败"}
  1558. c.ServeJSON()
  1559. return
  1560. }
  1561. //logs.Info("登录用户 菜单 pid 为:", c.T_pid)
  1562. ret := Account.Read_Menu_Bind_List(power.Id, power.T_menu, c.T_pid)
  1563. r_jsons.Data = ret.Menu
  1564. //r_jsons.Permission = ret.Permission
  1565. r_jsons.Pid = c.T_pid
  1566. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1567. c.ServeJSON()
  1568. return
  1569. }
  1570. // 权限菜单列表
  1571. func (c *UserController) Menu_List_All() {
  1572. var r_jsons lib.R_JSONS
  1573. r_jsons.Data, _ = Account.Read_Menu_List_All()
  1574. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1575. c.ServeJSON()
  1576. return
  1577. }
  1578. func (c *UserController) Menu_Excel() {
  1579. filename := fmt.Sprintf("冷链3.0菜单")
  1580. f := excelize.NewFile()
  1581. Style1, _ := f.NewStyle(
  1582. &excelize.Style{
  1583. Font: &excelize.Font{Bold: true, Size: 16, Family: "宋体"},
  1584. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  1585. })
  1586. Style2, _ := f.NewStyle(
  1587. &excelize.Style{
  1588. Font: &excelize.Font{Bold: true, Size: 14, Family: "宋体"},
  1589. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1590. Border: []excelize.Border{
  1591. {Type: "left", Color: "000000", Style: 1},
  1592. {Type: "top", Color: "000000", Style: 1},
  1593. {Type: "bottom", Color: "000000", Style: 1},
  1594. {Type: "right", Color: "000000", Style: 1},
  1595. },
  1596. })
  1597. f.MergeCell("Sheet1", "A1", "G1")
  1598. f.SetRowStyle("Sheet1", 1, 1, Style1)
  1599. f.SetCellValue("Sheet1", "A1", "Menu")
  1600. f.SetRowHeight("Sheet1", 1, 30)
  1601. f.MergeCell("Sheet1", "H1", "M1")
  1602. f.SetRowStyle("Sheet1", 1, 1, Style1)
  1603. f.SetCellValue("Sheet1", "H1", "API")
  1604. f.SetRowHeight("Sheet1", 1, 30)
  1605. f.SetCellStyle("Sheet1", "A2", "G2", Style2)
  1606. f.SetRowHeight("Sheet1", 2, 25)
  1607. // 这里设置表头
  1608. f.SetCellValue("Sheet1", "A2", "ID")
  1609. f.SetCellValue("Sheet1", "B2", "t_mid")
  1610. f.SetCellValue("Sheet1", "C2", "t_name")
  1611. f.SetCellValue("Sheet1", "D2", "t_permission")
  1612. f.SetCellValue("Sheet1", "E2", "t_sort")
  1613. f.SetCellValue("Sheet1", "F2", "t_pid")
  1614. f.SetCellValue("Sheet1", "G2", "t_icon")
  1615. f.SetCellValue("Sheet1", "H2", "ID")
  1616. f.SetCellValue("Sheet1", "I2", "t__menu__id")
  1617. f.SetCellValue("Sheet1", "J2", "t_name")
  1618. f.SetCellValue("Sheet1", "K2", "t_uri")
  1619. f.SetCellValue("Sheet1", "L2", "t_method")
  1620. f.SetCellValue("Sheet1", "M2", "t_enable")
  1621. // 设置列宽
  1622. f.SetColWidth("Sheet1", "A", "M", 15)
  1623. line := 2
  1624. menu := Account.Read_Menu_All()
  1625. for _, m := range menu {
  1626. Menu_Call(f, m, line)
  1627. }
  1628. Style11, _ := f.NewStyle(
  1629. &excelize.Style{
  1630. Font: &excelize.Font{Size: 10, Family: "宋体"},
  1631. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1632. Border: []excelize.Border{
  1633. {Type: "top", Color: "000000", Style: 1},
  1634. },
  1635. })
  1636. Style22, _ := f.NewStyle(
  1637. &excelize.Style{
  1638. Font: &excelize.Font{Size: 10, Family: "宋体"},
  1639. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1640. Border: []excelize.Border{
  1641. {Type: "left", Color: "000000", Style: 1},
  1642. },
  1643. })
  1644. Style33, _ := f.NewStyle(
  1645. &excelize.Style{
  1646. Font: &excelize.Font{Size: 10, Family: "宋体"},
  1647. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1648. Border: []excelize.Border{
  1649. {Type: "right", Color: "000000", Style: 1},
  1650. },
  1651. })
  1652. Style44, _ := f.NewStyle(
  1653. &excelize.Style{
  1654. Font: &excelize.Font{Size: 10, Family: "宋体"},
  1655. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1656. Border: []excelize.Border{
  1657. {Type: "bottom", Color: "000000", Style: 1},
  1658. },
  1659. })
  1660. //// 循环写入数据
  1661. for _, v := range menu {
  1662. line1 := line
  1663. line = Menu_Call(f, v, line)
  1664. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line1), fmt.Sprintf("M%d", line1), Style11)
  1665. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("A%d", line), Style22)
  1666. f.SetCellStyle("Sheet1", fmt.Sprintf("M%d", line), fmt.Sprintf("M%d", line), Style33)
  1667. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("M%d", line), Style44)
  1668. }
  1669. timeStr := filename + fmt.Sprintf("(%s)", lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 0))
  1670. // 保存文件
  1671. if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  1672. logs.Error(err)
  1673. }
  1674. var url string
  1675. ////// 上传 OSS
  1676. //url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
  1677. //if !is {
  1678. // c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  1679. // c.ServeJSON()
  1680. // return
  1681. //}
  1682. ////删除目录
  1683. //err = os.Remove("ofile/" + timeStr + ".xlsx")
  1684. //if err != nil {
  1685. // logs.Error(err)
  1686. //}
  1687. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  1688. c.ServeJSON()
  1689. return
  1690. }
  1691. func Menu_Call(f *excelize.File, v Account.Menu, line int) int {
  1692. line++
  1693. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), v.Id)
  1694. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_mid)
  1695. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_name)
  1696. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_permission)
  1697. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_sort)
  1698. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.T_pid)
  1699. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.T_icon)
  1700. apiList := Account.Read_API_List_ByMenuId(v.Id)
  1701. for _, api := range apiList {
  1702. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), api.Id)
  1703. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), api.T_Menu_Id)
  1704. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), api.T_name)
  1705. f.SetCellValue("Sheet1", fmt.Sprintf("K%d", line), api.T_uri)
  1706. f.SetCellValue("Sheet1", fmt.Sprintf("L%d", line), api.T_method)
  1707. f.SetCellValue("Sheet1", fmt.Sprintf("M%d", line), api.T_enable)
  1708. line++
  1709. }
  1710. return line
  1711. }
  1712. // 流量池
  1713. func (c *UserController) Flow_Pool() {
  1714. var r_jsons lib.R_JSONS
  1715. r_jsons.Data, _ = lib.Yidong_group_data_margin()
  1716. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1717. c.ServeJSON()
  1718. return
  1719. }
  1720. // 账单下载
  1721. func (c *UserController) CompanyBill_Excel() {
  1722. T_month := c.GetString("T_month")
  1723. _, err := time.Parse("2006-01", T_month)
  1724. if err != nil {
  1725. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  1726. c.ServeJSON()
  1727. return
  1728. }
  1729. year, month := strings.Split(T_month, "-")[0], strings.Split(T_month, "-")[1]
  1730. company, _ := Account.Read_Company_ById(c.Admin_r.T_pid)
  1731. if err != nil {
  1732. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1733. c.ServeJSON()
  1734. return
  1735. }
  1736. filename := fmt.Sprintf("%s%s年%s月账单", company.T_name, year, month)
  1737. f := excelize.NewFile()
  1738. Style1, _ := f.NewStyle(
  1739. &excelize.Style{
  1740. Font: &excelize.Font{Bold: true, Size: 16, Family: "宋体"},
  1741. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  1742. })
  1743. Style2, _ := f.NewStyle(
  1744. &excelize.Style{
  1745. Font: &excelize.Font{Bold: true, Size: 14, Family: "宋体"},
  1746. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1747. Border: []excelize.Border{
  1748. {Type: "left", Color: "000000", Style: 1},
  1749. {Type: "top", Color: "000000", Style: 1},
  1750. {Type: "bottom", Color: "000000", Style: 1},
  1751. {Type: "right", Color: "000000", Style: 1},
  1752. },
  1753. })
  1754. f.MergeCell("Sheet1", "A1", "G1")
  1755. f.SetRowStyle("Sheet1", 1, 1, Style1)
  1756. f.SetCellValue("Sheet1", "A1", filename)
  1757. f.SetRowHeight("Sheet1", 1, 30)
  1758. f.SetCellStyle("Sheet1", "A2", "G2", Style2)
  1759. f.SetRowHeight("Sheet1", 2, 25)
  1760. // 这里设置表头
  1761. f.SetCellValue("Sheet1", "A2", "编号")
  1762. f.SetCellValue("Sheet1", "B2", "项目")
  1763. f.SetCellValue("Sheet1", "C2", "说明")
  1764. f.SetCellValue("Sheet1", "D2", "扣费/充值")
  1765. f.SetCellValue("Sheet1", "E2", "金额(元)")
  1766. f.SetCellValue("Sheet1", "F2", "余额(元)")
  1767. f.SetCellValue("Sheet1", "G2", "时间")
  1768. // 设置列宽
  1769. f.SetColWidth("Sheet1", "A", "A", 10)
  1770. f.SetColWidth("Sheet1", "B", "B", 50)
  1771. f.SetColWidth("Sheet1", "C", "C", 15)
  1772. f.SetColWidth("Sheet1", "D", "D", 12)
  1773. f.SetColWidth("Sheet1", "E", "E", 15)
  1774. f.SetColWidth("Sheet1", "F", "F", 15)
  1775. f.SetColWidth("Sheet1", "G", "G", 20)
  1776. line := 2
  1777. CompanyBill_List, err := Company.Read_CompanyBill_List(c.Admin_r.T_pid, T_month)
  1778. if err != nil {
  1779. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1780. c.ServeJSON()
  1781. return
  1782. }
  1783. // 循环写入数据
  1784. for i, v := range CompanyBill_List {
  1785. line++
  1786. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), i+1)
  1787. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_project)
  1788. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_bill)
  1789. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_type)
  1790. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_charging)
  1791. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.T_balance)
  1792. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.CreateTime.Format("2006-01-02"))
  1793. }
  1794. Style4, _ := f.NewStyle(
  1795. &excelize.Style{
  1796. Font: &excelize.Font{Size: 12, Family: "宋体"},
  1797. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1798. Border: []excelize.Border{
  1799. {Type: "left", Color: "000000", Style: 1},
  1800. {Type: "top", Color: "000000", Style: 1},
  1801. {Type: "bottom", Color: "000000", Style: 1},
  1802. {Type: "right", Color: "000000", Style: 1},
  1803. },
  1804. })
  1805. f.SetCellStyle("Sheet1", "A2", fmt.Sprintf("G%d", line), Style4)
  1806. timeStr := filename + fmt.Sprintf("(%s)", lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 0))
  1807. // 保存文件
  1808. if err = f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  1809. logs.Error(err)
  1810. }
  1811. var url string
  1812. //// 上传 OSS
  1813. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
  1814. if !is {
  1815. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  1816. c.ServeJSON()
  1817. return
  1818. }
  1819. //删除目录
  1820. err = os.Remove("ofile/" + timeStr + ".xlsx")
  1821. if err != nil {
  1822. logs.Error(err)
  1823. }
  1824. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  1825. c.ServeJSON()
  1826. return
  1827. }
  1828. // 获取微信支付二维码
  1829. func (c *UserController) WxPay_QRCode() {
  1830. if c.Admin_r.T_pid == 0 {
  1831. c.Data["json"] = lib.JSONS{Code: 202, Msg: "非公司用户不能充值"}
  1832. c.ServeJSON()
  1833. return
  1834. }
  1835. Total, _ := c.GetFloat("Total")
  1836. Title := "冷链3.0-" + c.Admin_r.T_user
  1837. res, err := lib.PayTransactionNative(Title, Total)
  1838. if err != nil {
  1839. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  1840. c.ServeJSON()
  1841. return
  1842. }
  1843. if res.Code != 200 {
  1844. c.Data["json"] = lib.JSONS{Code: 202, Msg: res.Message}
  1845. c.ServeJSON()
  1846. return
  1847. }
  1848. var_ := Company.CompanyPayOrder{
  1849. T_pid: c.Admin_r.T_pid,
  1850. T_type: "微信",
  1851. T_title: Title,
  1852. T_total: Total,
  1853. T_order_no: res.OrderNo,
  1854. T_remark: c.Admin_r.T_uuid,
  1855. }
  1856. _, err = Company.Add_CompanyPayOrder(var_)
  1857. if err != nil {
  1858. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加订单失败!"}
  1859. c.ServeJSON()
  1860. return
  1861. }
  1862. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: res}
  1863. c.ServeJSON()
  1864. return
  1865. }
  1866. // 微信支付回调
  1867. func (c *UserController) WxPay_Notify() {
  1868. type RequestBody struct {
  1869. TradeNo string
  1870. Status string
  1871. }
  1872. type JSON struct {
  1873. Code int
  1874. Message string
  1875. }
  1876. buf := make([]byte, 1024)
  1877. n, _ := c.Ctx.Request.Body.Read(buf)
  1878. logs.Info("Body================ ", string(buf[0:n]))
  1879. //解密
  1880. adc_str, _ := lib.AesDeCrypt(buf[0:n], []byte(conf.Weixin_PwdKey))
  1881. //adc_str, _ := lib.AesDeCrypt(buf[0:n], []byte(conf.Weixin_PwdKey))
  1882. var body RequestBody
  1883. logs.Info("data================ ", string(adc_str))
  1884. err := json.Unmarshal(adc_str, &body)
  1885. if err != nil {
  1886. c.Data["json"] = JSON{Code: 202, Message: "json.Unmarshal is err:" + err.Error()}
  1887. c.ServeJSON()
  1888. }
  1889. payOrder, err := Company.Get_CompanyPayOrder_ByT_order_no(body.TradeNo)
  1890. if err != nil {
  1891. c.Data["json"] = JSON{Code: 202, Message: "查询失败!"}
  1892. c.ServeJSON()
  1893. return
  1894. }
  1895. //交易状态
  1896. //SUCCESS:支付成功
  1897. //REFUND:转入退款
  1898. //NOTPAY:未支付
  1899. //CLOSED:已关闭
  1900. //REVOKED:已撤销(付款码支付)
  1901. //USERPAYING:用户支付中(付款码支付)
  1902. //PAYERROR:支付失败(其他原因,如银行返回失败)
  1903. payOrder.T_status = body.Status
  1904. err = Company.Update_CompanyPayOrder(payOrder, "T_status")
  1905. if err != nil {
  1906. c.Data["json"] = JSON{Code: 202, Message: "更新状态失败!"}
  1907. c.ServeJSON()
  1908. return
  1909. }
  1910. // 添加充值记录
  1911. // 余额加上充值金额
  1912. if body.Status == "SUCCESS" {
  1913. Company_r, err := Account.Read_Company_ById(payOrder.T_pid)
  1914. if err != nil {
  1915. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  1916. c.ServeJSON()
  1917. return
  1918. }
  1919. T_money64, _ := decimal.NewFromFloat(float64(Company_r.T_money) + payOrder.T_total).Round(2).Float64()
  1920. Company_r.T_money = float32(T_money64)
  1921. is := Account.Update_Company(Company_r, "T_money")
  1922. if !is {
  1923. c.Data["json"] = lib.JSONS{Code: 202, Msg: "充值失败!"}
  1924. c.ServeJSON()
  1925. return
  1926. }
  1927. }
  1928. c.Data["json"] = JSON{Code: 200, Message: "成功"}
  1929. c.ServeJSON()
  1930. return
  1931. }
  1932. // 获取微信支付订单状态
  1933. func (c *UserController) WxPay_GetOrderState() {
  1934. OrderNo := c.GetString("OrderNo")
  1935. payOrder, err := Company.Get_CompanyPayOrder_ByT_order_no(OrderNo)
  1936. if err != nil {
  1937. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1938. c.ServeJSON()
  1939. return
  1940. }
  1941. c.Data["json"] = lib.JSONS{Code: 200, Msg: "成功", Data: payOrder}
  1942. c.ServeJSON()
  1943. return
  1944. }
  1945. // 设备分类绑定
  1946. func (c *UserController) UserDeviceBind_List() {
  1947. var r_jsons lib.R_JSONS
  1948. page, _ := c.GetInt("page")
  1949. if page < 1 {
  1950. page = 1
  1951. }
  1952. page_z, _ := c.GetInt("page_z")
  1953. if page_z < 1 {
  1954. page_z = conf.Page_size
  1955. }
  1956. T_name := c.GetString("T_name")
  1957. T_uuid := c.GetString("T_uuid")
  1958. admin_r, err := Account.Read_Admin_ByUuid(T_uuid)
  1959. if err != nil {
  1960. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  1961. c.ServeJSON()
  1962. return
  1963. }
  1964. bindSN, err := Account.Read_UserDevice_List(admin_r)
  1965. if err != nil {
  1966. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1967. c.ServeJSON()
  1968. return
  1969. }
  1970. if len(bindSN) == 0 {
  1971. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1972. c.ServeJSON()
  1973. return
  1974. }
  1975. r_jsons.Data, r_jsons.Num = Device.Read_Device_List(&c.Admin_r, bindSN, c.T_pid, T_name, "", "", -1, page, page_z)
  1976. r_jsons.Page = page
  1977. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  1978. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1979. c.ServeJSON()
  1980. return
  1981. }
  1982. func (c *UserController) UserDeviceUnbind_List() {
  1983. var r_jsons lib.R_JSONS
  1984. page, _ := c.GetInt("page")
  1985. if page < 1 {
  1986. page = 1
  1987. }
  1988. page_z, _ := c.GetInt("page_z")
  1989. if page_z < 1 {
  1990. page_z = conf.Page_size
  1991. }
  1992. T_name := c.GetString("T_name")
  1993. T_uuid := c.GetString("T_uuid")
  1994. admin_r, err := Account.Read_Admin_ByUuid(T_uuid)
  1995. if err != nil {
  1996. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  1997. c.ServeJSON()
  1998. return
  1999. }
  2000. bindSN, err := Account.Read_UserDevice_List(admin_r)
  2001. if err != nil {
  2002. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  2003. c.ServeJSON()
  2004. return
  2005. }
  2006. r_jsons.Data, r_jsons.Num = Device.Read_User_Unbind_Device_List(&c.Admin_r, bindSN, c.T_pid, T_name, page, page_z)
  2007. r_jsons.Page = page
  2008. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  2009. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  2010. c.ServeJSON()
  2011. return
  2012. }
  2013. func (c *UserController) UserDeviceBind_Add() {
  2014. T_sn := c.GetString("T_sn")
  2015. T_uuid := c.GetString("T_uuid")
  2016. T_oneHey, _ := c.GetInt("T_oneKey") // 一键绑定
  2017. if T_oneHey == 1 {
  2018. admin_r, err := Account.Read_Admin_ByUuid(T_uuid)
  2019. if err != nil {
  2020. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  2021. c.ServeJSON()
  2022. return
  2023. }
  2024. bindSN, err := Account.Read_UserDevice_List(admin_r)
  2025. if err != nil {
  2026. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  2027. c.ServeJSON()
  2028. return
  2029. }
  2030. Unbind_Device_List, _ := Device.Read_User_Unbind_Device_List(&c.Admin_r, bindSN, c.T_pid, "", 0, 9999)
  2031. var UserDeviceList []Account.UserDevice
  2032. for _, r := range Unbind_Device_List {
  2033. UserDeviceList = append(UserDeviceList, Account.UserDevice{T_sn: r.T_sn, T_uuid: T_uuid})
  2034. }
  2035. if _, err = Account.Add_UserDevice_Multi(T_uuid, UserDeviceList); err != nil {
  2036. c.Data["json"] = lib.JSONS{Code: 200, Msg: "绑定失败"}
  2037. c.ServeJSON()
  2038. return
  2039. }
  2040. System.Add_UserLogs(c.Admin_r.T_uuid, "用户管理", "一键绑定主机操作", T_uuid+"-"+T_sn)
  2041. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  2042. c.ServeJSON()
  2043. return
  2044. }
  2045. if len(T_uuid) == 0 || len(T_sn) == 0 {
  2046. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn T_id T_class_id Err!"}
  2047. c.ServeJSON()
  2048. return
  2049. }
  2050. if len(T_uuid) < 8 {
  2051. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  2052. c.ServeJSON()
  2053. return
  2054. }
  2055. device, err := Device.Read_Device_ByT_sn(T_sn)
  2056. if err != nil {
  2057. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  2058. c.ServeJSON()
  2059. return
  2060. }
  2061. admin_r, err := Account.Read_Admin_ByUuid(T_uuid)
  2062. if err != nil {
  2063. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  2064. c.ServeJSON()
  2065. return
  2066. }
  2067. if admin_r.T_pid != device.T_pid {
  2068. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  2069. c.ServeJSON()
  2070. return
  2071. }
  2072. userdievice, err := Account.Read_UserDevice_ByT_uuid_T_sn(T_uuid, T_sn)
  2073. if userdievice.Id > 0 {
  2074. c.Data["json"] = lib.JSONS{Code: 202, Msg: "重复绑定!"}
  2075. c.ServeJSON()
  2076. return
  2077. }
  2078. if _, err = Account.Add_UserDevice(Account.UserDevice{T_sn: T_sn, T_uuid: T_uuid}); err != nil {
  2079. c.Data["json"] = lib.JSONS{Code: 200, Msg: "绑定失败"}
  2080. c.ServeJSON()
  2081. return
  2082. }
  2083. System.Add_UserLogs(c.Admin_r.T_uuid, "用户管理", "绑定主机操作", T_uuid+"-"+T_sn)
  2084. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  2085. c.ServeJSON()
  2086. return
  2087. }
  2088. func (c *UserController) UserDeviceBind_Del() {
  2089. T_sn := c.GetString("T_sn")
  2090. T_uuid := c.GetString("T_uuid")
  2091. if len(T_uuid) == 0 || len(T_sn) == 0 {
  2092. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn T_id T_class_id Err!"}
  2093. c.ServeJSON()
  2094. return
  2095. }
  2096. if len(T_uuid) < 8 {
  2097. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  2098. c.ServeJSON()
  2099. return
  2100. }
  2101. device, err := Device.Read_Device_ByT_sn(T_sn)
  2102. if err != nil {
  2103. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn Err!"}
  2104. c.ServeJSON()
  2105. return
  2106. }
  2107. admin_r, err := Account.Read_Admin_ByUuid(T_uuid)
  2108. if err != nil {
  2109. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  2110. c.ServeJSON()
  2111. return
  2112. }
  2113. if admin_r.T_pid != device.T_pid {
  2114. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  2115. c.ServeJSON()
  2116. return
  2117. }
  2118. userdievice, err := Account.Read_UserDevice_ByT_uuid_T_sn(T_uuid, T_sn)
  2119. if err != nil {
  2120. if err.Error() == orm.ErrNoRows.Error() {
  2121. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  2122. c.ServeJSON()
  2123. return
  2124. }
  2125. c.Data["json"] = lib.JSONS{Code: 202, Msg: "解绑失败!"}
  2126. c.ServeJSON()
  2127. return
  2128. }
  2129. if err = Account.Delete_UserDevice(Account.UserDevice{Id: userdievice.Id, T_sn: T_sn, T_uuid: T_uuid}); err != nil {
  2130. c.Data["json"] = lib.JSONS{Code: 200, Msg: "删除失败"}
  2131. c.ServeJSON()
  2132. return
  2133. }
  2134. System.Add_UserLogs(c.Admin_r.T_uuid, "用户管理", "解绑主机操作", T_uuid+"-"+T_sn)
  2135. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  2136. c.ServeJSON()
  2137. return
  2138. }