User.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. package controllers
  2. import (
  3. "ERP_user/conf"
  4. "ERP_user/models/Account"
  5. "ERP_user/models/System"
  6. "encoding/json"
  7. "fmt"
  8. "io"
  9. "math"
  10. "net/http"
  11. "net/url"
  12. "time"
  13. beego "github.com/beego/beego/v2/server/web"
  14. powerlibs "gogs.baozhida.cn/zoie/ERP_libs/Power"
  15. "gogs.baozhida.cn/zoie/ERP_libs/lib"
  16. )
  17. type UserController struct {
  18. beego.Controller
  19. User Account.User
  20. }
  21. func (c *UserController) Prepare() {
  22. if Account.User_r != nil {
  23. c.User = *Account.User_r
  24. }
  25. }
  26. // 验证登录
  27. func (c *UserController) Login_verification() {
  28. Admin_user := c.GetString("bzd_username")
  29. Admin_pass := c.GetString("bzd_password")
  30. err, user_r := Account.Read_User_verification(Admin_user, Admin_pass)
  31. if err != nil {
  32. c.Data["json"] = lib.JSONS{Code: 202, Msg: "用户名或密码错误!"}
  33. } else {
  34. User_tokey := Account.Add_Tokey(user_r.T_uuid)
  35. c.Ctx.SetCookie("User_tokey", User_tokey, time.Second*60*60)
  36. c.Data["json"] = lib.JSONS{Code: 200, Msg: "OK!", Data: User_tokey}
  37. System.Add_UserLogs_T(user_r.T_uuid, "用户", "用户登陆", lib.GetUserLoginInfo(c.Ctx))
  38. }
  39. c.ServeJSON()
  40. return
  41. }
  42. // --------------------------------------------------------------------------------------------------------------
  43. // 用户列表
  44. func (c *UserController) List() {
  45. // 分页参数 初始化
  46. page, _ := c.GetInt("page")
  47. if page < 1 {
  48. page = 1
  49. }
  50. page_z, _ := c.GetInt("page_z")
  51. if page_z < 1 {
  52. page_z = conf.Page_size
  53. }
  54. // 查询
  55. T_name := c.GetString("T_name")
  56. T_power := c.GetString("T_power")
  57. T_dept, _ := c.GetInt("T_dept")
  58. T_dept_leader, _ := c.GetInt("T_dept_leader")
  59. T_State, _ := c.GetInt("T_State")
  60. R_List, R_cnt := Account.Read_User_List(T_name, T_power, T_dept, T_dept_leader, T_State, page, page_z)
  61. var r_jsons lib.R_JSONS
  62. r_jsons.Num = R_cnt
  63. r_jsons.Data = R_List
  64. r_jsons.Page = page
  65. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  66. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  67. c.ServeJSON()
  68. return
  69. }
  70. func (c *UserController) Get() {
  71. T_uuid := c.GetString("T_uuid")
  72. user, err := Account.Read_User_ByT_uuid(T_uuid)
  73. if err != nil {
  74. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  75. c.ServeJSON()
  76. return
  77. }
  78. var r_jsons lib.R_JSONS
  79. r_jsons.Data = Account.UserToUser_R(user)
  80. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  81. c.ServeJSON()
  82. return
  83. }
  84. // 个人信息
  85. func (c *UserController) Info() {
  86. type JSONS struct {
  87. Code int16
  88. Msg string
  89. Data Account.User_R
  90. Power powerlibs.Power_R
  91. }
  92. var data JSONS
  93. data.Data = Account.UserToUser_R(c.User)
  94. // 获取权限信息
  95. power, err := Account.Read_Power_ByT_id(c.User.T_power)
  96. if err == nil {
  97. powerR := powerlibs.PowerToPower_R(power)
  98. data.Power = powerR
  99. }
  100. c.Data["json"] = data
  101. c.ServeJSON()
  102. return
  103. }
  104. // 添加用户信息
  105. func (c *UserController) Add() {
  106. T_power := c.GetString("T_power")
  107. T_name := c.GetString("T_name")
  108. T_user := c.GetString("T_user")
  109. T_pass := c.GetString("T_pass")
  110. T_dept, _ := c.GetInt("T_dept")
  111. T_post, _ := c.GetInt("T_post")
  112. T_dept_leader, _ := c.GetInt("T_dept_leader")
  113. T_sex, _ := c.GetInt("T_sex")
  114. T_id_card := c.GetString("T_id_card")
  115. T_nation := c.GetString("T_nation")
  116. T_school := c.GetString("T_school")
  117. T_major := c.GetString("T_major")
  118. T_education := c.GetString("T_education")
  119. T_phone := c.GetString("T_phone")
  120. T_marry, _ := c.GetInt("T_marry")
  121. T_spouse_name := c.GetString("T_spouse_name")
  122. T_spouse_phone := c.GetString("T_spouse_phone")
  123. T_entry_time := c.GetString("T_entry_time")
  124. T_positive_time := c.GetString("T_positive_time")
  125. T_entry_type := c.GetString("T_entry_type")
  126. T_contract_start_time := c.GetString("T_contract_start_time")
  127. T_contract_end_time := c.GetString("T_contract_end_time")
  128. T_expire, _ := c.GetInt("T_expire")
  129. T_remark := c.GetString("T_remark")
  130. T_verify_cold_uuid := c.GetString("T_verify_cold_uuid")
  131. T_verify_perf_target, _ := c.GetInt("T_verify_perf_target")
  132. var_ := Account.User{
  133. T_power: T_power,
  134. T_name: T_name,
  135. T_user: T_user,
  136. T_pass: T_pass,
  137. T_dept: T_dept,
  138. T_post: T_post,
  139. T_dept_leader: T_dept_leader,
  140. T_sex: T_sex,
  141. T_id_card: T_id_card,
  142. T_nation: T_nation,
  143. T_school: T_school,
  144. T_major: T_major,
  145. T_education: T_education,
  146. T_phone: T_phone,
  147. T_marry: T_marry,
  148. T_spouse_name: T_spouse_name,
  149. T_spouse_phone: T_spouse_phone,
  150. T_entry_time: T_entry_time,
  151. T_positive_time: T_positive_time,
  152. T_entry_type: T_entry_type,
  153. T_contract_start_time: T_contract_start_time,
  154. T_contract_end_time: T_contract_end_time,
  155. T_expire: T_expire,
  156. T_remark: T_remark,
  157. T_verify_cold_uuid: T_verify_cold_uuid,
  158. T_verify_perf_target: T_verify_perf_target,
  159. }
  160. if len(T_power) < 1 {
  161. c.Data["json"] = lib.JSONS{Code: 204, Msg: "权限异常!"}
  162. c.ServeJSON()
  163. return
  164. }
  165. _, err := Account.Read_Power_ByT_id(T_power)
  166. if err != nil {
  167. c.Data["json"] = lib.JSONS{Code: 205, Msg: "参数异常!"}
  168. c.ServeJSON()
  169. return
  170. }
  171. if len(var_.T_name) < 3 {
  172. c.Data["json"] = lib.JSONS{Code: 206, Msg: "名字 长度太短 < 3!"}
  173. c.ServeJSON()
  174. return
  175. }
  176. if len(var_.T_user) < 3 {
  177. c.Data["json"] = lib.JSONS{Code: 207, Msg: "用户名 长度太短 < 3!"}
  178. c.ServeJSON()
  179. return
  180. }
  181. if len(var_.T_pass) < 3 {
  182. c.Data["json"] = lib.JSONS{Code: 208, Msg: "密码异常!"}
  183. c.ServeJSON()
  184. return
  185. }
  186. _, err = Account.Add_User(var_)
  187. if err != nil {
  188. c.Data["json"] = lib.JSONS{Code: 209, Msg: "添加失败!"}
  189. c.ServeJSON()
  190. return
  191. }
  192. var_.T_pass = "******"
  193. System.Add_UserLogs_T(c.User.T_uuid, "用户", "新增", var_)
  194. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  195. c.ServeJSON()
  196. return
  197. }
  198. // 修改个人信息
  199. func (c *UserController) Post() {
  200. T_pass := c.GetString("T_pass")
  201. user := c.User
  202. if len(T_pass) > 0 {
  203. if len(T_pass) < 8 {
  204. c.Data["json"] = lib.JSONS{Code: 206, Msg: "密码格式不正确!"}
  205. c.ServeJSON()
  206. return
  207. }
  208. user.T_pass = T_pass
  209. }
  210. if err := Account.Update_User(user, "T_pass"); err != nil {
  211. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  212. c.ServeJSON()
  213. return
  214. }
  215. System.Add_UserLogs_T(c.User.T_uuid, "用户", "修改登录密码", "")
  216. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  217. c.ServeJSON()
  218. return
  219. }
  220. // 修改用户信息
  221. func (c *UserController) Edit() {
  222. T_uuid := c.GetString("T_uuid")
  223. T_power := c.GetString("T_power")
  224. T_name := c.GetString("T_name")
  225. T_pass := c.GetString("T_pass")
  226. T_dept, _ := c.GetInt("T_dept")
  227. T_post, _ := c.GetInt("T_post")
  228. T_dept_leader, T_dept_leader_err := c.GetInt("T_dept_leader")
  229. T_sex, T_sex_err := c.GetInt("T_sex")
  230. T_id_card := c.GetString("T_id_card")
  231. T_nation := c.GetString("T_nation")
  232. T_school := c.GetString("T_school")
  233. T_major := c.GetString("T_major")
  234. T_education := c.GetString("T_education")
  235. T_phone := c.GetString("T_phone")
  236. T_marry, T_marry_err := c.GetInt("T_marry")
  237. T_spouse_name := c.GetString("T_spouse_name")
  238. T_spouse_phone := c.GetString("T_spouse_phone")
  239. T_entry_time := c.GetString("T_entry_time")
  240. T_positive_time := c.GetString("T_positive_time")
  241. T_entry_type := c.GetString("T_entry_type")
  242. T_contract_start_time := c.GetString("T_contract_start_time")
  243. T_contract_end_time := c.GetString("T_contract_end_time")
  244. T_expire, T_expire_err := c.GetInt("T_expire")
  245. T_remark := c.GetString("T_remark")
  246. T_verify_cold_uuid := c.GetString("T_verify_cold_uuid")
  247. T_verify_perf_target, _ := c.GetInt("T_verify_perf_target")
  248. var err error
  249. var user Account.User
  250. var cols []string
  251. if len(T_uuid) > 0 {
  252. user, err = Account.Read_User_ByT_uuid(T_uuid)
  253. if err != nil {
  254. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  255. c.ServeJSON()
  256. return
  257. }
  258. }
  259. if len(T_name) > 0 {
  260. if len(T_name) < 2 {
  261. c.Data["json"] = lib.JSONS{Code: 204, Msg: "名字格式不正确!"}
  262. c.ServeJSON()
  263. return
  264. }
  265. user.T_name = T_name
  266. cols = append(cols, "T_name")
  267. }
  268. if len(T_pass) > 0 {
  269. if len(T_pass) < 8 {
  270. c.Data["json"] = lib.JSONS{Code: 206, Msg: "密码格式不正确!"}
  271. c.ServeJSON()
  272. return
  273. }
  274. user.T_pass = T_pass
  275. cols = append(cols, "T_pass")
  276. }
  277. if len(T_power) > 0 {
  278. _, err = Account.Read_Power_ByT_id(T_power)
  279. if err != nil {
  280. c.Data["json"] = lib.JSONS{Code: 208, Msg: "T_power Err!"}
  281. c.ServeJSON()
  282. return
  283. }
  284. user.T_power = T_power
  285. cols = append(cols, "T_power")
  286. }
  287. if T_dept > 0 {
  288. user.T_dept = T_dept
  289. cols = append(cols, "T_dept")
  290. }
  291. if T_post > 0 {
  292. user.T_post = T_post
  293. cols = append(cols, "T_post")
  294. }
  295. if T_dept_leader_err == nil {
  296. user.T_dept_leader = T_dept_leader
  297. cols = append(cols, "T_dept_leader")
  298. }
  299. if T_sex_err == nil {
  300. user.T_sex = T_sex
  301. cols = append(cols, "T_sex")
  302. }
  303. if len(T_id_card) > 0 {
  304. user.T_id_card = T_id_card
  305. cols = append(cols, "T_id_card")
  306. }
  307. if len(T_nation) > 0 {
  308. user.T_nation = T_nation
  309. cols = append(cols, "T_nation")
  310. }
  311. if len(T_school) > 0 {
  312. user.T_school = T_school
  313. cols = append(cols, "T_school")
  314. }
  315. if len(T_major) > 0 {
  316. user.T_major = T_major
  317. cols = append(cols, "T_major")
  318. }
  319. if len(T_education) > 0 {
  320. user.T_education = T_education
  321. cols = append(cols, "T_education")
  322. }
  323. if len(T_phone) > 0 {
  324. user.T_phone = T_phone
  325. cols = append(cols, "T_phone")
  326. }
  327. if T_marry_err == nil {
  328. user.T_marry = T_marry
  329. cols = append(cols, "T_marry")
  330. }
  331. if len(T_spouse_name) > 0 {
  332. user.T_spouse_name = T_spouse_name
  333. cols = append(cols, "T_spouse_name")
  334. }
  335. if len(T_spouse_phone) > 0 {
  336. user.T_spouse_phone = T_spouse_phone
  337. cols = append(cols, "T_spouse_phone")
  338. }
  339. if len(T_entry_time) > 0 {
  340. user.T_entry_time = T_entry_time
  341. cols = append(cols, "T_entry_time")
  342. }
  343. if len(T_positive_time) > 0 {
  344. user.T_positive_time = T_positive_time
  345. cols = append(cols, "T_positive_time")
  346. }
  347. if len(T_entry_type) > 0 {
  348. user.T_entry_type = T_entry_type
  349. cols = append(cols, "T_entry_type")
  350. }
  351. if len(T_contract_start_time) > 0 {
  352. user.T_contract_start_time = T_contract_start_time
  353. cols = append(cols, "T_contract_start_time")
  354. }
  355. if len(T_contract_end_time) > 0 {
  356. user.T_contract_end_time = T_contract_end_time
  357. cols = append(cols, "T_contract_end_time")
  358. }
  359. if T_expire_err == nil {
  360. user.T_expire = T_expire
  361. cols = append(cols, "T_expire")
  362. }
  363. if len(T_remark) > 0 {
  364. user.T_remark = T_remark
  365. cols = append(cols, "T_remark")
  366. }
  367. if len(T_verify_cold_uuid) > 0 {
  368. user.T_verify_cold_uuid = T_verify_cold_uuid
  369. cols = append(cols, "T_verify_cold_uuid")
  370. }
  371. if T_verify_perf_target > 0 {
  372. user.T_verify_perf_target = T_verify_perf_target
  373. cols = append(cols, "T_verify_perf_target")
  374. }
  375. if err = Account.Update_User(user, cols...); err != nil {
  376. c.Data["json"] = lib.JSONS{Code: 208, Msg: "修改失败!"}
  377. c.ServeJSON()
  378. return
  379. }
  380. user.T_pass = "******"
  381. System.Add_UserLogs_T(c.User.T_uuid, "用户", "修改个人信息", user)
  382. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  383. c.ServeJSON()
  384. return
  385. }
  386. // 删除用户信息
  387. func (c *UserController) Del() {
  388. T_uuid := c.GetString("T_uuid")
  389. if len(T_uuid) == 0 {
  390. c.Data["json"] = lib.JSONS{Code: 201, Msg: "T_uuid Err!"}
  391. c.ServeJSON()
  392. return
  393. }
  394. user, err := Account.Read_User_ByT_uuid(T_uuid)
  395. if err != nil {
  396. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  397. c.ServeJSON()
  398. return
  399. }
  400. if user.Id == 1 {
  401. c.Data["json"] = lib.JSONS{Code: 202, Msg: "禁止删除超级管理员!"}
  402. c.ServeJSON()
  403. return
  404. }
  405. if err = Account.Delete_User(user); err != nil {
  406. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  407. c.ServeJSON()
  408. return
  409. }
  410. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  411. c.ServeJSON()
  412. return
  413. }
  414. func (c *UserController) Dept_List() {
  415. var r_jsons lib.R_JSONS
  416. list := []string{
  417. "人事财务部",
  418. "实施",
  419. "研发部-软件组",
  420. "研发部-硬件组",
  421. }
  422. r_jsons.Data = list
  423. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  424. c.ServeJSON()
  425. return
  426. }
  427. // 修改个人信息
  428. func (c *UserController) Leave() {
  429. T_uuid := c.GetString("T_uuid")
  430. if len(T_uuid) == 0 {
  431. c.Data["json"] = lib.JSONS{Code: 201, Msg: "T_uuid Err!"}
  432. c.ServeJSON()
  433. return
  434. }
  435. user, err := Account.Read_User_ByT_uuid(T_uuid)
  436. if err != nil {
  437. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  438. c.ServeJSON()
  439. return
  440. }
  441. if user.Id == 1 {
  442. c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权操作!"}
  443. c.ServeJSON()
  444. return
  445. }
  446. user.T_State = 2
  447. if err := Account.Update_User(user, "T_State"); err != nil {
  448. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  449. c.ServeJSON()
  450. return
  451. }
  452. System.Add_UserLogs_T(c.User.T_uuid, "用户", "离职", "")
  453. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  454. c.ServeJSON()
  455. return
  456. }
  457. // 获取冷链验证用户列表
  458. func (c *UserController) ColdVerify_User_List() {
  459. urls := "/openapi/admin/list"
  460. T_name := c.GetString("T_name")
  461. signature, timestamp := lib.GenColdVerifySignature()
  462. resp, err := http.PostForm(conf.ColdVerify_OpenApi_Host+urls, url.Values{
  463. "T_name": {T_name},
  464. "X-API-KEY": {lib.ColdVerify_OpenApi_Key},
  465. "X-API-SIGNATURE": {signature},
  466. "X-API-TIMESTAMP": {timestamp},
  467. })
  468. if err != nil {
  469. fmt.Println("get请求失败:", err)
  470. }
  471. //延迟关闭(返回response的body尽量关闭,避免内存泄露)
  472. defer resp.Body.Close()
  473. if err != nil {
  474. c.Data["json"] = lib.JSONS{Code: 202, Msg: "请求冷链验证用户列表接口失败!"}
  475. c.ServeJSON()
  476. return
  477. }
  478. var res lib.R_JSONS
  479. body, _ := io.ReadAll(resp.Body)
  480. if err = json.Unmarshal(body, &res); err != nil {
  481. c.Data["json"] = lib.JSONS{Code: 202, Msg: "请求冷链验证用户列表接口失败!"}
  482. c.ServeJSON()
  483. return
  484. }
  485. c.Data["json"] = lib.JSONS{Data: res.Data, Code: 200, Msg: "ok!"}
  486. c.ServeJSON()
  487. return
  488. }