User.go 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460
  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. CompanyId int
  789. CompanyName string
  790. CompanyExpirationTime string
  791. }
  792. var info Info
  793. info.T_name = admin_r.T_name
  794. info.T_pid = admin_r.T_pid
  795. var Company_r Account.Company
  796. if c.T_pid > 0 {
  797. Company_r, err = Account.Read_Company_ById(c.T_pid)
  798. if err != nil {
  799. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  800. c.ServeJSON()
  801. return
  802. }
  803. info.CompanyKey = Company_r.T_key
  804. info.CompanyName = Company_r.T_name
  805. info.CompanyId = Company_r.Id
  806. info.CompanyExpirationTime = Company_r.T_expirationTime
  807. }
  808. if admin_r.T_pid > 0 {
  809. info.WarningSign = T_warning_sign
  810. bindSN, err := Account.Read_UserDevice_List(c.Admin_r)
  811. if err != nil {
  812. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  813. c.ServeJSON()
  814. return
  815. }
  816. company, _ := Account.Read_Company_ById(admin_r.T_pid)
  817. info.T_Days = lib.GetDiffDays(time.Now(), company.CreateTime)
  818. AdminList := Account.Read_Admin_List_T_pids(admin_r.T_pid)
  819. var Admin_ []Account.Admin_
  820. for _, v := range AdminList {
  821. Admin_ = append(Admin_, Account.Admin_RToAdmin_(v))
  822. }
  823. info.Admin = Admin_
  824. T_pids := []int{}
  825. // 拥有公司管理-设备报警权限 统计公司及其所有子公司的今日报警数量、未处理报警数量、我的消息数量
  826. if T_warning_sign == Warning.CompanyCallthepolice {
  827. T_pids = Account.ReadCompanyIds_T_path(Company_r.T_path)
  828. info.WarningNum = Warning.Read_Warning_ALL_T_State_Count(0, T_pids, 1, bindSN, power.T_warning, true)
  829. info.UntreatedWarningNum = Warning.Read_Warning_ALL_T_State_Count(0, T_pids, 3, bindSN, power.T_warning, false)
  830. info.NewsNum = Warning.Read_Warning_ALL_T_State_Count(0, T_pids, 1, bindSN, "T1003|T1002|", false)
  831. }
  832. // 拥有设备管理-设备报警权限 统计本公司今日报警数量、未处理报警数量、我的消息数量
  833. if T_warning_sign == Warning.Callthepolice {
  834. info.WarningNum = Warning.Read_Warning_ALL_T_State_Count(admin_r.T_pid, T_pids, 1, bindSN, power.T_warning, true)
  835. info.UntreatedWarningNum = Warning.Read_Warning_ALL_T_State_Count(admin_r.T_pid, T_pids, 3, bindSN, power.T_warning, false)
  836. info.NewsNum = Warning.Read_Warning_ALL_T_State_Count(admin_r.T_pid, T_pids, 1, bindSN, "T1003|T1002|", false)
  837. }
  838. info.Balance = company.T_money
  839. info.NoticeRecord.Sms = Warning.Get_WarningSandNum_CurrentMonth(admin_r.T_pid, 1)
  840. info.NoticeRecord.Phone = Warning.Get_WarningSandNum_CurrentMonth(admin_r.T_pid, 2)
  841. }
  842. if admin_r.T_pid == 0 {
  843. if len(admin_r.T_pids) > 0 && admin_r.T_pids != "*" {
  844. info.T_pids = admin_r.T_pids
  845. info.WarningRate = Warning.Read_WarningRate_Yesterday(admin_r.T_uuid)
  846. }
  847. info.WorkOrderNum, info.UntreatedWorkOrderNum = NatsServer.Read_WorkOrderT_State_Count(admin_r.T_pids)
  848. }
  849. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: info}
  850. c.ServeJSON()
  851. return
  852. }
  853. // 内部用户管理
  854. func (c *UserController) Admin_List() {
  855. var r_jsons lib.R_JSONS
  856. page, _ := c.GetInt("page")
  857. if page < 1 {
  858. page = 1
  859. }
  860. page_z, _ := c.GetInt("page_z")
  861. if page_z < 1 {
  862. page_z = conf.Page_size
  863. }
  864. T_name := c.GetString("T_name")
  865. if c.Admin_r.T_pid > 0 {
  866. c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权查看!"}
  867. c.ServeJSON()
  868. return
  869. }
  870. T_bind_company, _ := c.GetInt("T_bind_company") // 管理员绑定公司 0-全部 1-绑定公司
  871. Account.Read_Power_All_Map()
  872. userList, num := Account.Read_Admin_List(0, T_name, page, page_z)
  873. r_jsons.Data, r_jsons.Num = userList, num
  874. // 过滤已绑定公司列表
  875. if T_bind_company == 1 {
  876. var adminList []Account.Admin_R
  877. for _, v := range userList {
  878. if len(v.T_pids) == 0 || v.T_pids == "*" {
  879. continue
  880. }
  881. adminList = append(adminList, v)
  882. }
  883. r_jsons.Data = adminList
  884. }
  885. r_jsons.Page = page
  886. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  887. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  888. c.ServeJSON()
  889. return
  890. }
  891. func (c *UserController) Admin_Get() {
  892. T_uuid := c.GetString("T_uuid")
  893. if len(T_uuid) < 8 {
  894. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  895. c.ServeJSON()
  896. return
  897. }
  898. admin_r, err := Account.Read_Admin_ByUuid(T_uuid)
  899. if err != nil {
  900. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  901. c.ServeJSON()
  902. return
  903. }
  904. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Account.AdminToAdmin_Details(admin_r)}
  905. c.ServeJSON()
  906. return
  907. }
  908. func (c *UserController) Admin_Add() {
  909. T_power, _ := c.GetInt("T_power")
  910. T_name := c.GetString("T_name")
  911. T_user := c.GetString("T_user")
  912. T_pass := c.GetString("T_pass")
  913. T_pass_str := c.GetString("T_pass_str")
  914. T_phone := c.GetString("T_phone")
  915. T_mail := c.GetString("T_mail")
  916. T_wx := c.GetString("T_wx")
  917. var_ := Account.Admin{
  918. T_uuid: uuid.NewV4().String(),
  919. T_pid: 0,
  920. T_name: T_name,
  921. T_user: T_user,
  922. T_phone: T_phone,
  923. T_mail: T_mail,
  924. T_wx: T_wx,
  925. T_pass: T_pass,
  926. T_pass_str: T_pass_str,
  927. T_power: T_power,
  928. T_State: 1,
  929. }
  930. if len(T_pass) < 8 {
  931. c.Data["json"] = lib.JSONS{Code: 202, Msg: "密码异常!"}
  932. c.ServeJSON()
  933. return
  934. }
  935. _, err := Account.Read_Admin_T_user(T_user)
  936. if err == nil {
  937. c.Data["json"] = lib.JSONS{Code: 302, Msg: "账户重复!"}
  938. c.ServeJSON()
  939. return
  940. }
  941. _, err = Account.Add_Admin(var_)
  942. if err != nil {
  943. c.Data["json"] = lib.JSONS{Code: 301, Msg: "添加失败!"}
  944. c.ServeJSON()
  945. return
  946. }
  947. var_.T_pass = "******"
  948. System.Add_UserLogs_T(c.Admin_r.T_uuid, "内部用户管理", "添加", var_)
  949. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  950. c.ServeJSON()
  951. return
  952. }
  953. func (c *UserController) Admin_Edit() {
  954. T_uuid := c.GetString("T_uuid")
  955. if len(T_uuid) < 8 {
  956. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  957. c.ServeJSON()
  958. return
  959. }
  960. r, err := Account.Read_Admin_ByUuid(T_uuid)
  961. if err != nil {
  962. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  963. c.ServeJSON()
  964. return
  965. }
  966. T_power, T_power_err := c.GetInt("T_power")
  967. T_user := c.GetString("T_user")
  968. T_name := c.GetString("T_name")
  969. T_pass := c.GetString("T_pass")
  970. T_pass_str := c.GetString("T_pass_str")
  971. T_phone := c.GetString("T_phone")
  972. T_mail := c.GetString("T_mail")
  973. T_wx := c.GetString("T_wx")
  974. if T_power_err == nil {
  975. r.T_power = T_power
  976. }
  977. if len(T_pass) < 8 && len(T_pass) > 0 {
  978. c.Data["json"] = lib.JSONS{Code: 202, Msg: "密码异常!"}
  979. c.ServeJSON()
  980. return
  981. }
  982. if len(T_user) > 0 {
  983. var admin Account.Admin
  984. admin, err = Account.Read_Admin_T_user(T_user)
  985. if err == nil && admin.T_uuid != T_uuid {
  986. c.Data["json"] = lib.JSONS{Code: 302, Msg: "用户名重复!"}
  987. c.ServeJSON()
  988. return
  989. }
  990. r.T_user = T_user
  991. }
  992. if len(T_name) > 0 {
  993. r.T_name = T_name
  994. }
  995. if len(T_pass) >= 8 {
  996. r.T_pass = T_pass
  997. }
  998. if len(T_pass_str) > 0 {
  999. r.T_pass_str = T_pass_str
  1000. }
  1001. if len(T_phone) > 0 {
  1002. r.T_phone = T_phone
  1003. }
  1004. if len(T_mail) > 0 {
  1005. r.T_mail = T_mail
  1006. }
  1007. if len(T_wx) > 0 {
  1008. r.T_wx = T_wx
  1009. }
  1010. is := Account.Update_Admin(r, "T_user", "T_power", "T_name", "T_pass", "T_pass_str", "T_phone", "T_mail", "T_wx")
  1011. if !is {
  1012. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  1013. c.ServeJSON()
  1014. return
  1015. }
  1016. if len(T_name) > 0 {
  1017. if is = Company.Update_CompanyNotice_Bind_By_T_uuid(T_uuid, "内管-"+T_name); !is {
  1018. logs.Info("修改内部用户 => 修改内部用户关联报警通知失败", T_uuid, T_name)
  1019. }
  1020. }
  1021. r.T_pass = "********"
  1022. System.Add_UserLogs_T(c.Admin_r.T_uuid, "内部用户管理", "修改", r)
  1023. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1024. c.ServeJSON()
  1025. return
  1026. }
  1027. func (c *UserController) Admin_Del() {
  1028. T_uuid := c.GetString("T_uuid")
  1029. if len(T_uuid) <= 0 {
  1030. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  1031. c.ServeJSON()
  1032. return
  1033. }
  1034. admin_r_lod, err := Account.Read_Admin_ByUuid(T_uuid)
  1035. if err != nil {
  1036. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  1037. c.ServeJSON()
  1038. return
  1039. }
  1040. if admin_r_lod.Id == 1 {
  1041. c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权删除!"}
  1042. c.ServeJSON()
  1043. return
  1044. }
  1045. if is := Account.Delete_Admin(admin_r_lod); !is {
  1046. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  1047. c.ServeJSON()
  1048. return
  1049. }
  1050. if is := Company.Delete_CompanyNotice_Bind_By_T_uuid(T_uuid); !is {
  1051. logs.Info("删除内部用户 => 删除内部用户关联报警通知失败", T_uuid)
  1052. }
  1053. System.Add_UserLogs_T(c.Admin_r.T_uuid, "内部用户管理", "删除", T_uuid)
  1054. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1055. c.ServeJSON()
  1056. return
  1057. }
  1058. // 报警通知的用户列表
  1059. func (c *UserController) User_Notice_List() {
  1060. T_name := c.GetString("T_name")
  1061. type R_JSONS struct {
  1062. //必须的大写开头
  1063. AdminList []Account.Admin_R
  1064. UserList []Account.Admin_R
  1065. }
  1066. var AdminList, UserList []Account.Admin_R
  1067. var r_jsons R_JSONS
  1068. // 操作pid
  1069. company_r, err := Account.Read_Company_ById(c.T_pid)
  1070. if err != nil {
  1071. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1072. c.ServeJSON()
  1073. return
  1074. }
  1075. T_pids := []int{}
  1076. for _, v := range strings.Split(strings.Trim(company_r.T_path, "/"), "/") {
  1077. pid, _ := strconv.Atoi(v)
  1078. T_pids = append(T_pids, pid)
  1079. }
  1080. // 绑定公司id的内部用户列表
  1081. AdminList = Account.Read_Admin_List_T_pids(c.T_pid)
  1082. // 绑定公司id的公司用户列表
  1083. //r_jsons.UserList = Account.Read_Admin_List_T_pid(c.T_pid)
  1084. // 绑定上级公司用户列表
  1085. UserList = Account.Read_Parent_Admin_List(T_pids)
  1086. if len(T_name) > 0 {
  1087. for _, v := range AdminList {
  1088. if strings.Contains(v.T_name, T_name) {
  1089. r_jsons.AdminList = append(r_jsons.AdminList, v)
  1090. }
  1091. }
  1092. for _, v := range UserList {
  1093. if strings.Contains(v.T_name, T_name) {
  1094. r_jsons.UserList = append(r_jsons.UserList, v)
  1095. }
  1096. }
  1097. } else {
  1098. r_jsons.AdminList = AdminList
  1099. r_jsons.UserList = UserList
  1100. }
  1101. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1102. c.ServeJSON()
  1103. return
  1104. }
  1105. // 管理员绑定公司
  1106. func (c *UserController) AdminCompanyBind_List() {
  1107. var r_jsons lib.R_JSONS
  1108. T_uuid := c.GetString("T_uuid")
  1109. if len(T_uuid) < 8 {
  1110. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  1111. c.ServeJSON()
  1112. return
  1113. }
  1114. r, err := Account.Read_Admin_ByUuid(T_uuid)
  1115. if err != nil {
  1116. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  1117. c.ServeJSON()
  1118. return
  1119. }
  1120. r_jsons.Data = Account.Read_Company_List_ByT_pids(r.T_pids)
  1121. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1122. c.ServeJSON()
  1123. return
  1124. }
  1125. func (c *UserController) AdminCompanyUnBind_List() {
  1126. var r_jsons lib.R_JSONS
  1127. T_name := c.GetString("T_name")
  1128. T_uuid := c.GetString("T_uuid")
  1129. if len(T_uuid) < 8 {
  1130. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  1131. c.ServeJSON()
  1132. return
  1133. }
  1134. r, err := Account.Read_Admin_ByUuid(T_uuid)
  1135. if err != nil {
  1136. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  1137. c.ServeJSON()
  1138. return
  1139. }
  1140. r_jsons.Data = Account.Read_UnBind_Company_List_ByT_pids(r.T_pids, T_name)
  1141. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1142. c.ServeJSON()
  1143. return
  1144. }
  1145. func (c *UserController) AdminCompanyBind_Add_All() {
  1146. T_uuid := c.GetString("T_uuid")
  1147. if len(T_uuid) < 8 {
  1148. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  1149. c.ServeJSON()
  1150. return
  1151. }
  1152. r, err := Account.Read_Admin_ByUuid(T_uuid)
  1153. if err != nil {
  1154. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  1155. c.ServeJSON()
  1156. return
  1157. }
  1158. if err = Account.Add_Company_bind_All(r); err != nil {
  1159. c.Data["json"] = lib.JSONS{Code: 202, Msg: "公司绑定失败"}
  1160. c.ServeJSON()
  1161. return
  1162. }
  1163. System.Add_UserLogs(c.Admin_r.T_uuid, "内部用户管理", "公司绑定全部", T_uuid)
  1164. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1165. c.ServeJSON()
  1166. return
  1167. }
  1168. func (c *UserController) AdminCompanyBind_Add() {
  1169. T_uuid := c.GetString("T_uuid")
  1170. if len(T_uuid) < 8 {
  1171. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  1172. c.ServeJSON()
  1173. return
  1174. }
  1175. T_pid, _ := c.GetInt("T_pid")
  1176. if T_pid == 0 {
  1177. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  1178. c.ServeJSON()
  1179. return
  1180. }
  1181. r, err := Account.Read_Admin_ByUuid(T_uuid)
  1182. if err != nil {
  1183. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  1184. c.ServeJSON()
  1185. return
  1186. }
  1187. if r.T_pids == "*" {
  1188. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1189. c.ServeJSON()
  1190. return
  1191. }
  1192. Company_r, err := Account.Read_Company_ById(T_pid)
  1193. if err != nil {
  1194. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  1195. c.ServeJSON()
  1196. return
  1197. }
  1198. T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path)
  1199. if err = Account.Add_Company_bind(r, T_pids); err != nil {
  1200. c.Data["json"] = lib.JSONS{Code: 202, Msg: "公司绑定失败"}
  1201. c.ServeJSON()
  1202. return
  1203. }
  1204. System.Add_UserLogs(c.Admin_r.T_uuid, "内部用户管理", "公司绑定添加", T_uuid+"-"+strconv.Itoa(T_pid))
  1205. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1206. c.ServeJSON()
  1207. return
  1208. }
  1209. func (c *UserController) AdminCompanyBind_Del() {
  1210. T_uuid := c.GetString("T_uuid")
  1211. if len(T_uuid) < 8 {
  1212. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  1213. c.ServeJSON()
  1214. return
  1215. }
  1216. T_pid, _ := c.GetInt("T_pid")
  1217. if T_pid == 0 {
  1218. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  1219. c.ServeJSON()
  1220. return
  1221. }
  1222. r, err := Account.Read_Admin_ByUuid(T_uuid)
  1223. if err != nil {
  1224. c.Data["json"] = lib.JSONS{Code: 202, Msg: "获取用户信息失败!"}
  1225. c.ServeJSON()
  1226. return
  1227. }
  1228. if r.T_pids == "*" {
  1229. c.Data["json"] = lib.JSONS{Code: 202, Msg: "绑定全部公司的用户不能单个解绑!"}
  1230. c.ServeJSON()
  1231. return
  1232. }
  1233. Company_r, err := Account.Read_Company_ById(T_pid)
  1234. if err != nil {
  1235. c.Data["json"] = lib.JSONS{Code: 202, Msg: "获取公司信息失败!"}
  1236. c.ServeJSON()
  1237. return
  1238. }
  1239. T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path)
  1240. if len(T_pids) == 0 {
  1241. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1242. c.ServeJSON()
  1243. return
  1244. }
  1245. if err = Account.Delete_Company_bind(r, T_pids); err != nil {
  1246. c.Data["json"] = lib.JSONS{Code: 202, Msg: "解绑公司失败"}
  1247. c.ServeJSON()
  1248. return
  1249. }
  1250. if is := Company.Delete_CompanyNotice_Bind_By_T_uuid_T_pid(T_uuid, T_pids); !is {
  1251. logs.Info("删除内部用户公司绑定 => 删除内部用户绑定的公司关联报警通知失败", T_uuid)
  1252. }
  1253. System.Add_UserLogs(c.Admin_r.T_uuid, "内部用户管理", "公司绑定删除", T_uuid+"-"+strconv.Itoa(T_pid))
  1254. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1255. c.ServeJSON()
  1256. return
  1257. }
  1258. func (c *UserController) AdminCompanyBind_Del_All() {
  1259. T_uuid := c.GetString("T_uuid")
  1260. if len(T_uuid) < 8 {
  1261. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  1262. c.ServeJSON()
  1263. return
  1264. }
  1265. r, err := Account.Read_Admin_ByUuid(T_uuid)
  1266. if err != nil {
  1267. c.Data["json"] = lib.JSONS{Code: 202, Msg: "获取用户信息失败!"}
  1268. c.ServeJSON()
  1269. return
  1270. }
  1271. var T_pids []int
  1272. if len(r.T_pids) > 0 && r.T_pids != "*" {
  1273. T_pids = lib.SplitStringToIntIds(r.T_pids, "P")
  1274. }
  1275. if err = Account.Delete_Company_bind(r, T_pids); err != nil {
  1276. c.Data["json"] = lib.JSONS{Code: 202, Msg: "解绑公司失败"}
  1277. c.ServeJSON()
  1278. return
  1279. }
  1280. if is := Company.Delete_CompanyNotice_Bind_By_T_uuid_T_pid(T_uuid, T_pids); !is {
  1281. logs.Info("删除内部用户公司绑定 => 删除内部用户绑定的公司关联报警通知失败", T_uuid)
  1282. }
  1283. System.Add_UserLogs(c.Admin_r.T_uuid, "内部用户管理", "公司绑定删除全部", T_uuid)
  1284. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1285. c.ServeJSON()
  1286. return
  1287. }
  1288. // 权限管理
  1289. func (c *UserController) Power_List() {
  1290. var r_jsons lib.R_JSONS
  1291. page, _ := c.GetInt("page")
  1292. if page < 1 {
  1293. page = 1
  1294. }
  1295. page_z, _ := c.GetInt("page_z")
  1296. if page_z < 1 {
  1297. page_z = conf.Page_size
  1298. }
  1299. Name := c.GetString("T_name")
  1300. r_jsons.Data, r_jsons.Num = Account.Read_Power_List(Name, page, page_z)
  1301. r_jsons.Page = page
  1302. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  1303. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1304. c.ServeJSON()
  1305. return
  1306. }
  1307. // 添加用户 权限选择列表
  1308. func (c *UserController) Power_List_All() {
  1309. r, err := Account.Read_Power_ById(c.Admin_r.T_power)
  1310. if err != nil {
  1311. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  1312. c.ServeJSON()
  1313. return
  1314. }
  1315. var r_jsons lib.R_JSONS
  1316. r_jsons.Data = Account.Read_Power_List_ALL_User(r.T_select)
  1317. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1318. c.ServeJSON()
  1319. return
  1320. }
  1321. // removeIncompleteParentMenus 移除不完整的父菜单ID
  1322. // 递归检查多级菜单的完整性,确保父菜单只有在所有子孙菜单都被勾选时才保留
  1323. func removeIncompleteParentMenus(checkedIds []int, allMenus []Account.Menu) []int {
  1324. // 创建已勾选菜单ID的映射,便于快速查找
  1325. checkedMap := make(map[int]bool)
  1326. for _, id := range checkedIds {
  1327. checkedMap[id] = true
  1328. }
  1329. // 递归获取所有菜单及其父子关系
  1330. parentChildrenMap := make(map[int][]int)
  1331. buildMenuParentChildrenMap(allMenus, parentChildrenMap)
  1332. // 递归检查每个菜单的完整性,从叶子节点开始向上检查
  1333. result := make([]int, 0)
  1334. for _, menuId := range checkedIds {
  1335. if shouldKeepMenu(menuId, checkedMap, parentChildrenMap) {
  1336. result = append(result, menuId)
  1337. }
  1338. }
  1339. return result
  1340. }
  1341. // shouldKeepMenu 递归检查菜单是否应该保留
  1342. // 对于父菜单,只有当其所有子菜单(及子孙菜单)都满足条件时才保留
  1343. func shouldKeepMenu(menuId int, checkedMap map[int]bool, parentChildrenMap map[int][]int) bool {
  1344. // 如果菜单没有被勾选,直接返回false
  1345. if !checkedMap[menuId] {
  1346. return false
  1347. }
  1348. // 如果是叶子菜单(没有子菜单),直接保留
  1349. children, hasChildren := parentChildrenMap[menuId]
  1350. if !hasChildren {
  1351. return true
  1352. }
  1353. // 如果是父菜单,检查所有子菜单是否都满足条件
  1354. for _, childId := range children {
  1355. // 递归检查每个子菜单
  1356. if !shouldKeepMenu(childId, checkedMap, parentChildrenMap) {
  1357. return false // 只要有一个子菜单不满足条件,父菜单就不保留
  1358. }
  1359. }
  1360. // 所有子菜单都满足条件,保留父菜单
  1361. return true
  1362. }
  1363. // buildMenuParentChildrenMap 构建父子关系映射
  1364. func buildMenuParentChildrenMap(menus []Account.Menu, parentChildrenMap map[int][]int) {
  1365. for _, menu := range menus {
  1366. // 如果有子菜单,建立父子关系
  1367. if len(menu.Children) > 0 {
  1368. for _, child := range menu.Children {
  1369. parentChildrenMap[menu.Id] = append(parentChildrenMap[menu.Id], child.Id)
  1370. }
  1371. // 递归处理子菜单
  1372. buildMenuParentChildrenMap(menu.Children, parentChildrenMap)
  1373. }
  1374. }
  1375. }
  1376. func (c *UserController) Power_Get() {
  1377. type Data struct {
  1378. Power Account.Power_
  1379. Menu []Account.Menu
  1380. Menu_checked []int
  1381. Select []Account.Power_R
  1382. Warning []Warning.WarningType_R
  1383. }
  1384. var data Data
  1385. id, id_err := c.GetInt("T_id")
  1386. if id_err != nil || id == 0 {
  1387. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  1388. c.ServeJSON()
  1389. return
  1390. }
  1391. power, err := Account.Read_Power_ById(id)
  1392. if err != nil {
  1393. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  1394. c.ServeJSON()
  1395. return
  1396. }
  1397. data.Power = Account.PowerToPower_(power)
  1398. data.Menu, data.Menu_checked = Account.Read_Menu_List_All()
  1399. if err != nil {
  1400. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_menu Err!"}
  1401. c.ServeJSON()
  1402. return
  1403. }
  1404. if power.T_menu != "*" {
  1405. data.Menu_checked = lib.SplitStringToIntIds(power.T_menu, "M")
  1406. // 检查父菜单完整性,移除不完整的父菜单ID
  1407. data.Menu_checked = removeIncompleteParentMenus(data.Menu_checked, data.Menu)
  1408. }
  1409. if err != nil {
  1410. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_menu Err!"}
  1411. c.ServeJSON()
  1412. return
  1413. }
  1414. data.Select = Account.Read_Power_List_ALL_T_select(power.T_select)
  1415. if err != nil {
  1416. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_select Err!"}
  1417. c.ServeJSON()
  1418. return
  1419. }
  1420. data.Warning = Warning.Read_WarningType_All_Power_T_Warning(power.T_warning)
  1421. if err != nil {
  1422. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_warning Err!"}
  1423. c.ServeJSON()
  1424. return
  1425. }
  1426. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: data}
  1427. c.ServeJSON()
  1428. return
  1429. }
  1430. func (c *UserController) Power_Add() {
  1431. T_name := c.GetString("T_name")
  1432. T_select := c.GetString("T_select")
  1433. T_warning := c.GetString("T_warning")
  1434. T_menu := c.GetString("T_menu")
  1435. var_ := Account.Power{
  1436. T_name: T_name,
  1437. T_select: T_select,
  1438. T_warning: T_warning,
  1439. T_menu: T_menu,
  1440. T_State: 1,
  1441. }
  1442. _, err := Account.Add_Power(var_)
  1443. if err != nil {
  1444. c.Data["json"] = lib.JSONS{Code: 301, Msg: "添加失败!"}
  1445. c.ServeJSON()
  1446. return
  1447. }
  1448. System.Add_UserLogs_T(c.Admin_r.T_uuid, "权限管理", "添加", var_)
  1449. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1450. c.ServeJSON()
  1451. return
  1452. }
  1453. func (c *UserController) Power_Edit() {
  1454. Id, Id_err := c.GetInt("T_id")
  1455. if Id_err != nil || Id == 0 {
  1456. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  1457. c.ServeJSON()
  1458. return
  1459. }
  1460. if Id == 1 {
  1461. c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权修改!"}
  1462. c.ServeJSON()
  1463. return
  1464. }
  1465. r, err := Account.Read_Power_ById(Id)
  1466. if err != nil {
  1467. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  1468. c.ServeJSON()
  1469. return
  1470. }
  1471. T_name := c.GetString("T_name")
  1472. T_select := c.GetString("T_select")
  1473. T_warning := c.GetString("T_warning")
  1474. T_menu := c.GetString("T_menu")
  1475. if len(T_name) > 0 {
  1476. r.T_name = T_name
  1477. }
  1478. if len(T_select) > 0 {
  1479. r.T_select = T_select
  1480. }
  1481. if len(T_warning) > 0 {
  1482. r.T_warning = T_warning
  1483. }
  1484. if len(T_menu) > 0 {
  1485. r.T_menu = T_menu
  1486. }
  1487. is := Account.Update_Power(r, "T_name", "T_select", "T_warning", "T_menu")
  1488. if !is {
  1489. c.Data["json"] = lib.JSONS{Code: 302, Msg: "修改失败!"}
  1490. c.ServeJSON()
  1491. return
  1492. }
  1493. System.Add_UserLogs_T(c.Admin_r.T_uuid, "权限管理", "修改", r)
  1494. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1495. c.ServeJSON()
  1496. return
  1497. }
  1498. func (c *UserController) Power_Del() {
  1499. id, id_err := c.GetInt("T_id")
  1500. if id_err != nil || id == 0 {
  1501. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  1502. c.ServeJSON()
  1503. return
  1504. }
  1505. r, err := Account.Read_Power_ById(id)
  1506. if err != nil {
  1507. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  1508. c.ServeJSON()
  1509. return
  1510. }
  1511. if cnt := Account.Read_Admin_Num_ByT_power(id); cnt > 0 {
  1512. c.Data["json"] = lib.JSONS{Code: 202, Msg: "有关联用户,禁止删除!"}
  1513. c.ServeJSON()
  1514. return
  1515. }
  1516. is := Account.Delete_Power(r)
  1517. if !is {
  1518. c.Data["json"] = lib.JSONS{Code: 302, Msg: "删除失败!"}
  1519. c.ServeJSON()
  1520. return
  1521. }
  1522. System.Add_UserLogs(c.Admin_r.T_uuid, "权限管理", "删除", strconv.Itoa(id))
  1523. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1524. c.ServeJSON()
  1525. return
  1526. }
  1527. // 菜单列表
  1528. func (c *UserController) User_Menu_List() {
  1529. type R_JSONS struct {
  1530. //必须的大写开头
  1531. Data interface{}
  1532. Permission interface{}
  1533. }
  1534. var r_jsons R_JSONS
  1535. power, err := Account.Read_Power_ById(c.Admin_r.T_power)
  1536. if err != nil {
  1537. c.Data["json"] = lib.JSONS{Code: 202, Msg: "获取菜单失败"}
  1538. c.ServeJSON()
  1539. return
  1540. }
  1541. //logs.Info("登录用户 菜单 pid 为:", c.T_pid)
  1542. ret := Account.Read_Menu_Bind_List(power.Id, power.T_menu, c.T_pid)
  1543. r_jsons.Data = ret.Menu
  1544. r_jsons.Permission = ret.Permission
  1545. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1546. c.ServeJSON()
  1547. return
  1548. }
  1549. func (c *UserController) GetUserMenuList() {
  1550. type R_JSONS struct {
  1551. //必须的大写开头
  1552. Pid int
  1553. Data interface{}
  1554. Permission interface{}
  1555. }
  1556. var r_jsons R_JSONS
  1557. power, err := Account.Read_Power_ById(c.Admin_r.T_power)
  1558. if err != nil {
  1559. c.Data["json"] = lib.JSONS{Code: 202, Msg: "获取菜单失败"}
  1560. c.ServeJSON()
  1561. return
  1562. }
  1563. //logs.Info("登录用户 菜单 pid 为:", c.T_pid)
  1564. ret := Account.Read_Menu_Bind_List(power.Id, power.T_menu, c.T_pid)
  1565. r_jsons.Data = ret.Menu
  1566. //r_jsons.Permission = ret.Permission
  1567. r_jsons.Pid = c.T_pid
  1568. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1569. c.ServeJSON()
  1570. return
  1571. }
  1572. // 权限菜单列表
  1573. func (c *UserController) Menu_List_All() {
  1574. var r_jsons lib.R_JSONS
  1575. r_jsons.Data, _ = Account.Read_Menu_List_All()
  1576. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1577. c.ServeJSON()
  1578. return
  1579. }
  1580. func (c *UserController) Menu_Excel() {
  1581. filename := fmt.Sprintf("冷链3.0菜单")
  1582. f := excelize.NewFile()
  1583. Style1, _ := f.NewStyle(
  1584. &excelize.Style{
  1585. Font: &excelize.Font{Bold: true, Size: 16, Family: "宋体"},
  1586. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  1587. })
  1588. Style2, _ := f.NewStyle(
  1589. &excelize.Style{
  1590. Font: &excelize.Font{Bold: true, Size: 14, Family: "宋体"},
  1591. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1592. Border: []excelize.Border{
  1593. {Type: "left", Color: "000000", Style: 1},
  1594. {Type: "top", Color: "000000", Style: 1},
  1595. {Type: "bottom", Color: "000000", Style: 1},
  1596. {Type: "right", Color: "000000", Style: 1},
  1597. },
  1598. })
  1599. f.MergeCell("Sheet1", "A1", "G1")
  1600. f.SetRowStyle("Sheet1", 1, 1, Style1)
  1601. f.SetCellValue("Sheet1", "A1", "Menu")
  1602. f.SetRowHeight("Sheet1", 1, 30)
  1603. f.MergeCell("Sheet1", "H1", "M1")
  1604. f.SetRowStyle("Sheet1", 1, 1, Style1)
  1605. f.SetCellValue("Sheet1", "H1", "API")
  1606. f.SetRowHeight("Sheet1", 1, 30)
  1607. f.SetCellStyle("Sheet1", "A2", "G2", Style2)
  1608. f.SetRowHeight("Sheet1", 2, 25)
  1609. // 这里设置表头
  1610. f.SetCellValue("Sheet1", "A2", "ID")
  1611. f.SetCellValue("Sheet1", "B2", "t_mid")
  1612. f.SetCellValue("Sheet1", "C2", "t_name")
  1613. f.SetCellValue("Sheet1", "D2", "t_permission")
  1614. f.SetCellValue("Sheet1", "E2", "t_sort")
  1615. f.SetCellValue("Sheet1", "F2", "t_pid")
  1616. f.SetCellValue("Sheet1", "G2", "t_icon")
  1617. f.SetCellValue("Sheet1", "H2", "ID")
  1618. f.SetCellValue("Sheet1", "I2", "t__menu__id")
  1619. f.SetCellValue("Sheet1", "J2", "t_name")
  1620. f.SetCellValue("Sheet1", "K2", "t_uri")
  1621. f.SetCellValue("Sheet1", "L2", "t_method")
  1622. f.SetCellValue("Sheet1", "M2", "t_enable")
  1623. // 设置列宽
  1624. f.SetColWidth("Sheet1", "A", "M", 15)
  1625. line := 2
  1626. menu := Account.Read_Menu_All()
  1627. for _, m := range menu {
  1628. Menu_Call(f, m, line)
  1629. }
  1630. Style11, _ := f.NewStyle(
  1631. &excelize.Style{
  1632. Font: &excelize.Font{Size: 10, Family: "宋体"},
  1633. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1634. Border: []excelize.Border{
  1635. {Type: "top", Color: "000000", Style: 1},
  1636. },
  1637. })
  1638. Style22, _ := f.NewStyle(
  1639. &excelize.Style{
  1640. Font: &excelize.Font{Size: 10, Family: "宋体"},
  1641. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1642. Border: []excelize.Border{
  1643. {Type: "left", Color: "000000", Style: 1},
  1644. },
  1645. })
  1646. Style33, _ := f.NewStyle(
  1647. &excelize.Style{
  1648. Font: &excelize.Font{Size: 10, Family: "宋体"},
  1649. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1650. Border: []excelize.Border{
  1651. {Type: "right", Color: "000000", Style: 1},
  1652. },
  1653. })
  1654. Style44, _ := f.NewStyle(
  1655. &excelize.Style{
  1656. Font: &excelize.Font{Size: 10, Family: "宋体"},
  1657. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1658. Border: []excelize.Border{
  1659. {Type: "bottom", Color: "000000", Style: 1},
  1660. },
  1661. })
  1662. //// 循环写入数据
  1663. for _, v := range menu {
  1664. line1 := line
  1665. line = Menu_Call(f, v, line)
  1666. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line1), fmt.Sprintf("M%d", line1), Style11)
  1667. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("A%d", line), Style22)
  1668. f.SetCellStyle("Sheet1", fmt.Sprintf("M%d", line), fmt.Sprintf("M%d", line), Style33)
  1669. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("M%d", line), Style44)
  1670. }
  1671. timeStr := filename + fmt.Sprintf("(%s)", lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 0))
  1672. // 保存文件
  1673. if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  1674. logs.Error(err)
  1675. }
  1676. var url string
  1677. ////// 上传 OSS
  1678. //url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
  1679. //if !is {
  1680. // c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  1681. // c.ServeJSON()
  1682. // return
  1683. //}
  1684. ////删除目录
  1685. //err = os.Remove("ofile/" + timeStr + ".xlsx")
  1686. //if err != nil {
  1687. // logs.Error(err)
  1688. //}
  1689. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  1690. c.ServeJSON()
  1691. return
  1692. }
  1693. func Menu_Call(f *excelize.File, v Account.Menu, line int) int {
  1694. line++
  1695. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), v.Id)
  1696. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_mid)
  1697. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_name)
  1698. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_permission)
  1699. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_sort)
  1700. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.T_pid)
  1701. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.T_icon)
  1702. apiList := Account.Read_API_List_ByMenuId(v.Id)
  1703. for _, api := range apiList {
  1704. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), api.Id)
  1705. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), api.T_Menu_Id)
  1706. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), api.T_name)
  1707. f.SetCellValue("Sheet1", fmt.Sprintf("K%d", line), api.T_uri)
  1708. f.SetCellValue("Sheet1", fmt.Sprintf("L%d", line), api.T_method)
  1709. f.SetCellValue("Sheet1", fmt.Sprintf("M%d", line), api.T_enable)
  1710. line++
  1711. }
  1712. return line
  1713. }
  1714. // 流量池
  1715. func (c *UserController) Flow_Pool() {
  1716. var r_jsons lib.R_JSONS
  1717. r_jsons.Data, _ = lib.Yidong_group_data_margin()
  1718. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1719. c.ServeJSON()
  1720. return
  1721. }
  1722. // 账单下载
  1723. func (c *UserController) CompanyBill_Excel() {
  1724. T_month := c.GetString("T_month")
  1725. _, err := time.Parse("2006-01", T_month)
  1726. if err != nil {
  1727. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  1728. c.ServeJSON()
  1729. return
  1730. }
  1731. year, month := strings.Split(T_month, "-")[0], strings.Split(T_month, "-")[1]
  1732. company, _ := Account.Read_Company_ById(c.Admin_r.T_pid)
  1733. if err != nil {
  1734. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1735. c.ServeJSON()
  1736. return
  1737. }
  1738. filename := fmt.Sprintf("%s%s年%s月账单", company.T_name, year, month)
  1739. f := excelize.NewFile()
  1740. Style1, _ := f.NewStyle(
  1741. &excelize.Style{
  1742. Font: &excelize.Font{Bold: true, Size: 16, Family: "宋体"},
  1743. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  1744. })
  1745. Style2, _ := f.NewStyle(
  1746. &excelize.Style{
  1747. Font: &excelize.Font{Bold: true, Size: 14, Family: "宋体"},
  1748. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1749. Border: []excelize.Border{
  1750. {Type: "left", Color: "000000", Style: 1},
  1751. {Type: "top", Color: "000000", Style: 1},
  1752. {Type: "bottom", Color: "000000", Style: 1},
  1753. {Type: "right", Color: "000000", Style: 1},
  1754. },
  1755. })
  1756. f.MergeCell("Sheet1", "A1", "G1")
  1757. f.SetRowStyle("Sheet1", 1, 1, Style1)
  1758. f.SetCellValue("Sheet1", "A1", filename)
  1759. f.SetRowHeight("Sheet1", 1, 30)
  1760. f.SetCellStyle("Sheet1", "A2", "G2", Style2)
  1761. f.SetRowHeight("Sheet1", 2, 25)
  1762. // 这里设置表头
  1763. f.SetCellValue("Sheet1", "A2", "编号")
  1764. f.SetCellValue("Sheet1", "B2", "项目")
  1765. f.SetCellValue("Sheet1", "C2", "说明")
  1766. f.SetCellValue("Sheet1", "D2", "扣费/充值")
  1767. f.SetCellValue("Sheet1", "E2", "金额(元)")
  1768. f.SetCellValue("Sheet1", "F2", "余额(元)")
  1769. f.SetCellValue("Sheet1", "G2", "时间")
  1770. // 设置列宽
  1771. f.SetColWidth("Sheet1", "A", "A", 10)
  1772. f.SetColWidth("Sheet1", "B", "B", 50)
  1773. f.SetColWidth("Sheet1", "C", "C", 15)
  1774. f.SetColWidth("Sheet1", "D", "D", 12)
  1775. f.SetColWidth("Sheet1", "E", "E", 15)
  1776. f.SetColWidth("Sheet1", "F", "F", 15)
  1777. f.SetColWidth("Sheet1", "G", "G", 20)
  1778. line := 2
  1779. CompanyBill_List, err := Company.Read_CompanyBill_List(c.Admin_r.T_pid, T_month)
  1780. if err != nil {
  1781. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1782. c.ServeJSON()
  1783. return
  1784. }
  1785. // 循环写入数据
  1786. for i, v := range CompanyBill_List {
  1787. line++
  1788. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), i+1)
  1789. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_project)
  1790. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_bill)
  1791. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_type)
  1792. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_charging)
  1793. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.T_balance)
  1794. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.CreateTime.Format("2006-01-02"))
  1795. }
  1796. Style4, _ := f.NewStyle(
  1797. &excelize.Style{
  1798. Font: &excelize.Font{Size: 12, Family: "宋体"},
  1799. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1800. Border: []excelize.Border{
  1801. {Type: "left", Color: "000000", Style: 1},
  1802. {Type: "top", Color: "000000", Style: 1},
  1803. {Type: "bottom", Color: "000000", Style: 1},
  1804. {Type: "right", Color: "000000", Style: 1},
  1805. },
  1806. })
  1807. f.SetCellStyle("Sheet1", "A2", fmt.Sprintf("G%d", line), Style4)
  1808. timeStr := filename + fmt.Sprintf("(%s)", lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 0))
  1809. // 保存文件
  1810. if err = f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  1811. logs.Error(err)
  1812. }
  1813. var url string
  1814. //// 上传 OSS
  1815. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
  1816. if !is {
  1817. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  1818. c.ServeJSON()
  1819. return
  1820. }
  1821. //删除目录
  1822. err = os.Remove("ofile/" + timeStr + ".xlsx")
  1823. if err != nil {
  1824. logs.Error(err)
  1825. }
  1826. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  1827. c.ServeJSON()
  1828. return
  1829. }
  1830. // 获取微信支付二维码
  1831. func (c *UserController) WxPay_QRCode() {
  1832. if c.Admin_r.T_pid == 0 {
  1833. c.Data["json"] = lib.JSONS{Code: 202, Msg: "非公司用户不能充值"}
  1834. c.ServeJSON()
  1835. return
  1836. }
  1837. Total, _ := c.GetFloat("Total")
  1838. Title := "冷链3.0-" + c.Admin_r.T_user
  1839. res, err := lib.PayTransactionNative(Title, Total)
  1840. if err != nil {
  1841. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  1842. c.ServeJSON()
  1843. return
  1844. }
  1845. if res.Code != 200 {
  1846. c.Data["json"] = lib.JSONS{Code: 202, Msg: res.Message}
  1847. c.ServeJSON()
  1848. return
  1849. }
  1850. var_ := Company.CompanyPayOrder{
  1851. T_pid: c.Admin_r.T_pid,
  1852. T_type: "微信",
  1853. T_title: Title,
  1854. T_total: Total,
  1855. T_order_no: res.OrderNo,
  1856. T_remark: c.Admin_r.T_uuid,
  1857. }
  1858. _, err = Company.Add_CompanyPayOrder(var_)
  1859. if err != nil {
  1860. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加订单失败!"}
  1861. c.ServeJSON()
  1862. return
  1863. }
  1864. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: res}
  1865. c.ServeJSON()
  1866. return
  1867. }
  1868. // 微信支付回调
  1869. func (c *UserController) WxPay_Notify() {
  1870. type RequestBody struct {
  1871. TradeNo string
  1872. Status string
  1873. }
  1874. type JSON struct {
  1875. Code int
  1876. Message string
  1877. }
  1878. buf := make([]byte, 1024)
  1879. n, _ := c.Ctx.Request.Body.Read(buf)
  1880. logs.Info("Body================ ", string(buf[0:n]))
  1881. //解密
  1882. adc_str, _ := lib.AesDeCrypt(buf[0:n], []byte(conf.Weixin_PwdKey))
  1883. //adc_str, _ := lib.AesDeCrypt(buf[0:n], []byte(conf.Weixin_PwdKey))
  1884. var body RequestBody
  1885. logs.Info("data================ ", string(adc_str))
  1886. err := json.Unmarshal(adc_str, &body)
  1887. if err != nil {
  1888. c.Data["json"] = JSON{Code: 202, Message: "json.Unmarshal is err:" + err.Error()}
  1889. c.ServeJSON()
  1890. }
  1891. payOrder, err := Company.Get_CompanyPayOrder_ByT_order_no(body.TradeNo)
  1892. if err != nil {
  1893. c.Data["json"] = JSON{Code: 202, Message: "查询失败!"}
  1894. c.ServeJSON()
  1895. return
  1896. }
  1897. //交易状态
  1898. //SUCCESS:支付成功
  1899. //REFUND:转入退款
  1900. //NOTPAY:未支付
  1901. //CLOSED:已关闭
  1902. //REVOKED:已撤销(付款码支付)
  1903. //USERPAYING:用户支付中(付款码支付)
  1904. //PAYERROR:支付失败(其他原因,如银行返回失败)
  1905. payOrder.T_status = body.Status
  1906. err = Company.Update_CompanyPayOrder(payOrder, "T_status")
  1907. if err != nil {
  1908. c.Data["json"] = JSON{Code: 202, Message: "更新状态失败!"}
  1909. c.ServeJSON()
  1910. return
  1911. }
  1912. // 添加充值记录
  1913. // 余额加上充值金额
  1914. if body.Status == "SUCCESS" {
  1915. Company_r, err := Account.Read_Company_ById(payOrder.T_pid)
  1916. if err != nil {
  1917. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  1918. c.ServeJSON()
  1919. return
  1920. }
  1921. T_money64, _ := decimal.NewFromFloat(float64(Company_r.T_money) + payOrder.T_total).Round(2).Float64()
  1922. Company_r.T_money = float32(T_money64)
  1923. is := Account.Update_Company(Company_r, "T_money")
  1924. if !is {
  1925. c.Data["json"] = lib.JSONS{Code: 202, Msg: "充值失败!"}
  1926. c.ServeJSON()
  1927. return
  1928. }
  1929. }
  1930. c.Data["json"] = JSON{Code: 200, Message: "成功"}
  1931. c.ServeJSON()
  1932. return
  1933. }
  1934. // 获取微信支付订单状态
  1935. func (c *UserController) WxPay_GetOrderState() {
  1936. OrderNo := c.GetString("OrderNo")
  1937. payOrder, err := Company.Get_CompanyPayOrder_ByT_order_no(OrderNo)
  1938. if err != nil {
  1939. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1940. c.ServeJSON()
  1941. return
  1942. }
  1943. c.Data["json"] = lib.JSONS{Code: 200, Msg: "成功", Data: payOrder}
  1944. c.ServeJSON()
  1945. return
  1946. }
  1947. // 设备分类绑定
  1948. func (c *UserController) UserDeviceBind_List() {
  1949. var r_jsons lib.R_JSONS
  1950. page, _ := c.GetInt("page")
  1951. if page < 1 {
  1952. page = 1
  1953. }
  1954. page_z, _ := c.GetInt("page_z")
  1955. if page_z < 1 {
  1956. page_z = conf.Page_size
  1957. }
  1958. T_name := c.GetString("T_name")
  1959. T_uuid := c.GetString("T_uuid")
  1960. admin_r, err := Account.Read_Admin_ByUuid(T_uuid)
  1961. if err != nil {
  1962. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  1963. c.ServeJSON()
  1964. return
  1965. }
  1966. bindSN, err := Account.Read_UserDevice_List(admin_r)
  1967. if err != nil {
  1968. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  1969. c.ServeJSON()
  1970. return
  1971. }
  1972. if len(bindSN) == 0 {
  1973. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1974. c.ServeJSON()
  1975. return
  1976. }
  1977. r_jsons.Data, r_jsons.Num = Device.Read_Device_List(&c.Admin_r, bindSN, c.T_pid, T_name, "", "", -1, page, page_z)
  1978. r_jsons.Page = page
  1979. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  1980. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1981. c.ServeJSON()
  1982. return
  1983. }
  1984. func (c *UserController) UserDeviceUnbind_List() {
  1985. var r_jsons lib.R_JSONS
  1986. page, _ := c.GetInt("page")
  1987. if page < 1 {
  1988. page = 1
  1989. }
  1990. page_z, _ := c.GetInt("page_z")
  1991. if page_z < 1 {
  1992. page_z = conf.Page_size
  1993. }
  1994. T_name := c.GetString("T_name")
  1995. T_uuid := c.GetString("T_uuid")
  1996. admin_r, err := Account.Read_Admin_ByUuid(T_uuid)
  1997. if err != nil {
  1998. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  1999. c.ServeJSON()
  2000. return
  2001. }
  2002. bindSN, err := Account.Read_UserDevice_List(admin_r)
  2003. if err != nil {
  2004. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  2005. c.ServeJSON()
  2006. return
  2007. }
  2008. r_jsons.Data, r_jsons.Num = Device.Read_User_Unbind_Device_List(&c.Admin_r, bindSN, c.T_pid, T_name, page, page_z)
  2009. r_jsons.Page = page
  2010. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  2011. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  2012. c.ServeJSON()
  2013. return
  2014. }
  2015. func (c *UserController) UserDeviceBind_Add() {
  2016. T_sn := c.GetString("T_sn")
  2017. T_uuid := c.GetString("T_uuid")
  2018. T_oneHey, _ := c.GetInt("T_oneKey") // 一键绑定
  2019. if T_oneHey == 1 {
  2020. admin_r, err := Account.Read_Admin_ByUuid(T_uuid)
  2021. if err != nil {
  2022. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  2023. c.ServeJSON()
  2024. return
  2025. }
  2026. bindSN, err := Account.Read_UserDevice_List(admin_r)
  2027. if err != nil {
  2028. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  2029. c.ServeJSON()
  2030. return
  2031. }
  2032. Unbind_Device_List, _ := Device.Read_User_Unbind_Device_List(&c.Admin_r, bindSN, c.T_pid, "", 0, 9999)
  2033. var UserDeviceList []Account.UserDevice
  2034. for _, r := range Unbind_Device_List {
  2035. UserDeviceList = append(UserDeviceList, Account.UserDevice{T_sn: r.T_sn, T_uuid: T_uuid})
  2036. }
  2037. if _, err = Account.Add_UserDevice_Multi(T_uuid, UserDeviceList); err != nil {
  2038. c.Data["json"] = lib.JSONS{Code: 200, Msg: "绑定失败"}
  2039. c.ServeJSON()
  2040. return
  2041. }
  2042. System.Add_UserLogs(c.Admin_r.T_uuid, "用户管理", "一键绑定主机操作", T_uuid+"-"+T_sn)
  2043. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  2044. c.ServeJSON()
  2045. return
  2046. }
  2047. if len(T_uuid) == 0 || len(T_sn) == 0 {
  2048. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn T_id T_class_id Err!"}
  2049. c.ServeJSON()
  2050. return
  2051. }
  2052. if len(T_uuid) < 8 {
  2053. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  2054. c.ServeJSON()
  2055. return
  2056. }
  2057. device, err := Device.Read_Device_ByT_sn(T_sn)
  2058. if err != nil {
  2059. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id Err!"}
  2060. c.ServeJSON()
  2061. return
  2062. }
  2063. admin_r, err := Account.Read_Admin_ByUuid(T_uuid)
  2064. if err != nil {
  2065. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  2066. c.ServeJSON()
  2067. return
  2068. }
  2069. if admin_r.T_pid != device.T_pid {
  2070. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  2071. c.ServeJSON()
  2072. return
  2073. }
  2074. userdievice, err := Account.Read_UserDevice_ByT_uuid_T_sn(T_uuid, T_sn)
  2075. if userdievice.Id > 0 {
  2076. c.Data["json"] = lib.JSONS{Code: 202, Msg: "重复绑定!"}
  2077. c.ServeJSON()
  2078. return
  2079. }
  2080. if _, err = Account.Add_UserDevice(Account.UserDevice{T_sn: T_sn, T_uuid: T_uuid}); err != nil {
  2081. c.Data["json"] = lib.JSONS{Code: 200, Msg: "绑定失败"}
  2082. c.ServeJSON()
  2083. return
  2084. }
  2085. System.Add_UserLogs(c.Admin_r.T_uuid, "用户管理", "绑定主机操作", T_uuid+"-"+T_sn)
  2086. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  2087. c.ServeJSON()
  2088. return
  2089. }
  2090. func (c *UserController) UserDeviceBind_Del() {
  2091. T_sn := c.GetString("T_sn")
  2092. T_uuid := c.GetString("T_uuid")
  2093. if len(T_uuid) == 0 || len(T_sn) == 0 {
  2094. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn T_id T_class_id Err!"}
  2095. c.ServeJSON()
  2096. return
  2097. }
  2098. if len(T_uuid) < 8 {
  2099. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  2100. c.ServeJSON()
  2101. return
  2102. }
  2103. device, err := Device.Read_Device_ByT_sn(T_sn)
  2104. if err != nil {
  2105. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn Err!"}
  2106. c.ServeJSON()
  2107. return
  2108. }
  2109. admin_r, err := Account.Read_Admin_ByUuid(T_uuid)
  2110. if err != nil {
  2111. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  2112. c.ServeJSON()
  2113. return
  2114. }
  2115. if admin_r.T_pid != device.T_pid {
  2116. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  2117. c.ServeJSON()
  2118. return
  2119. }
  2120. userdievice, err := Account.Read_UserDevice_ByT_uuid_T_sn(T_uuid, T_sn)
  2121. if err != nil {
  2122. if err.Error() == orm.ErrNoRows.Error() {
  2123. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  2124. c.ServeJSON()
  2125. return
  2126. }
  2127. c.Data["json"] = lib.JSONS{Code: 202, Msg: "解绑失败!"}
  2128. c.ServeJSON()
  2129. return
  2130. }
  2131. if err = Account.Delete_UserDevice(Account.UserDevice{Id: userdievice.Id, T_sn: T_sn, T_uuid: T_uuid}); err != nil {
  2132. c.Data["json"] = lib.JSONS{Code: 200, Msg: "删除失败"}
  2133. c.ServeJSON()
  2134. return
  2135. }
  2136. System.Add_UserLogs(c.Admin_r.T_uuid, "用户管理", "解绑主机操作", T_uuid+"-"+T_sn)
  2137. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  2138. c.ServeJSON()
  2139. return
  2140. }