User.go 53 KB

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