User.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. data.Code = 200
  101. data.Msg = "ok!"
  102. c.Data["json"] = data
  103. c.ServeJSON()
  104. return
  105. }
  106. // 添加用户信息
  107. func (c *UserController) Add() {
  108. T_power := c.GetString("T_power")
  109. T_name := c.GetString("T_name")
  110. T_user := c.GetString("T_user")
  111. T_pass := c.GetString("T_pass")
  112. T_dept, _ := c.GetInt("T_dept")
  113. T_post, _ := c.GetInt("T_post")
  114. T_dept_leader, _ := c.GetInt("T_dept_leader")
  115. T_sex, _ := c.GetInt("T_sex")
  116. T_id_card := c.GetString("T_id_card")
  117. T_nation := c.GetString("T_nation")
  118. T_school := c.GetString("T_school")
  119. T_major := c.GetString("T_major")
  120. T_education := c.GetString("T_education")
  121. T_phone := c.GetString("T_phone")
  122. T_marry, _ := c.GetInt("T_marry")
  123. T_spouse_name := c.GetString("T_spouse_name")
  124. T_spouse_phone := c.GetString("T_spouse_phone")
  125. T_entry_time := c.GetString("T_entry_time")
  126. T_positive_time := c.GetString("T_positive_time")
  127. T_entry_type := c.GetString("T_entry_type")
  128. T_contract_start_time := c.GetString("T_contract_start_time")
  129. T_contract_end_time := c.GetString("T_contract_end_time")
  130. T_expire, _ := c.GetInt("T_expire")
  131. T_remark := c.GetString("T_remark")
  132. T_verify_cold_uuid := c.GetString("T_verify_cold_uuid")
  133. T_verify_perf_target, _ := c.GetInt("T_verify_perf_target")
  134. var_ := Account.User{
  135. T_power: T_power,
  136. T_name: T_name,
  137. T_user: T_user,
  138. T_pass: T_pass,
  139. T_dept: T_dept,
  140. T_post: T_post,
  141. T_dept_leader: T_dept_leader,
  142. T_sex: T_sex,
  143. T_id_card: T_id_card,
  144. T_nation: T_nation,
  145. T_school: T_school,
  146. T_major: T_major,
  147. T_education: T_education,
  148. T_phone: T_phone,
  149. T_marry: T_marry,
  150. T_spouse_name: T_spouse_name,
  151. T_spouse_phone: T_spouse_phone,
  152. T_entry_time: T_entry_time,
  153. T_positive_time: T_positive_time,
  154. T_entry_type: T_entry_type,
  155. T_contract_start_time: T_contract_start_time,
  156. T_contract_end_time: T_contract_end_time,
  157. T_expire: T_expire,
  158. T_remark: T_remark,
  159. T_verify_cold_uuid: T_verify_cold_uuid,
  160. T_verify_perf_target: T_verify_perf_target,
  161. }
  162. if len(T_power) < 1 {
  163. c.Data["json"] = lib.JSONS{Code: 204, Msg: "权限异常!"}
  164. c.ServeJSON()
  165. return
  166. }
  167. _, err := Account.Read_Power_ByT_id(T_power)
  168. if err != nil {
  169. c.Data["json"] = lib.JSONS{Code: 205, Msg: "参数异常!"}
  170. c.ServeJSON()
  171. return
  172. }
  173. if len(var_.T_name) < 3 {
  174. c.Data["json"] = lib.JSONS{Code: 206, Msg: "名字 长度太短 < 3!"}
  175. c.ServeJSON()
  176. return
  177. }
  178. if len(var_.T_user) < 3 {
  179. c.Data["json"] = lib.JSONS{Code: 207, Msg: "用户名 长度太短 < 3!"}
  180. c.ServeJSON()
  181. return
  182. }
  183. if len(var_.T_pass) < 3 {
  184. c.Data["json"] = lib.JSONS{Code: 208, Msg: "密码异常!"}
  185. c.ServeJSON()
  186. return
  187. }
  188. _, err = Account.Add_User(var_)
  189. if err != nil {
  190. c.Data["json"] = lib.JSONS{Code: 209, Msg: "添加失败!"}
  191. c.ServeJSON()
  192. return
  193. }
  194. var_.T_pass = "******"
  195. System.Add_UserLogs_T(c.User.T_uuid, "用户", "新增", var_)
  196. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  197. c.ServeJSON()
  198. return
  199. }
  200. // 修改个人信息
  201. func (c *UserController) Post() {
  202. T_pass := c.GetString("T_pass")
  203. user := c.User
  204. if len(T_pass) > 0 {
  205. if len(T_pass) < 8 {
  206. c.Data["json"] = lib.JSONS{Code: 206, Msg: "密码格式不正确!"}
  207. c.ServeJSON()
  208. return
  209. }
  210. user.T_pass = T_pass
  211. }
  212. if err := Account.Update_User(user, "T_pass"); err != nil {
  213. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  214. c.ServeJSON()
  215. return
  216. }
  217. System.Add_UserLogs_T(c.User.T_uuid, "用户", "修改登录密码", "")
  218. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  219. c.ServeJSON()
  220. return
  221. }
  222. // 修改用户信息
  223. func (c *UserController) Edit() {
  224. T_uuid := c.GetString("T_uuid")
  225. T_power := c.GetString("T_power")
  226. T_name := c.GetString("T_name")
  227. T_pass := c.GetString("T_pass")
  228. T_dept, _ := c.GetInt("T_dept")
  229. T_post, _ := c.GetInt("T_post")
  230. T_dept_leader, T_dept_leader_err := c.GetInt("T_dept_leader")
  231. T_sex, T_sex_err := c.GetInt("T_sex")
  232. T_id_card := c.GetString("T_id_card")
  233. T_nation := c.GetString("T_nation")
  234. T_school := c.GetString("T_school")
  235. T_major := c.GetString("T_major")
  236. T_education := c.GetString("T_education")
  237. T_phone := c.GetString("T_phone")
  238. T_marry, T_marry_err := c.GetInt("T_marry")
  239. T_spouse_name := c.GetString("T_spouse_name")
  240. T_spouse_phone := c.GetString("T_spouse_phone")
  241. T_entry_time := c.GetString("T_entry_time")
  242. T_positive_time := c.GetString("T_positive_time")
  243. T_entry_type := c.GetString("T_entry_type")
  244. T_contract_start_time := c.GetString("T_contract_start_time")
  245. T_contract_end_time := c.GetString("T_contract_end_time")
  246. T_expire, T_expire_err := c.GetInt("T_expire")
  247. T_remark := c.GetString("T_remark")
  248. T_verify_cold_uuid := c.GetString("T_verify_cold_uuid")
  249. T_verify_perf_target, _ := c.GetInt("T_verify_perf_target")
  250. var err error
  251. var user Account.User
  252. var cols []string
  253. if len(T_uuid) > 0 {
  254. user, err = Account.Read_User_ByT_uuid(T_uuid)
  255. if err != nil {
  256. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  257. c.ServeJSON()
  258. return
  259. }
  260. }
  261. if len(T_name) > 0 {
  262. if len(T_name) < 2 {
  263. c.Data["json"] = lib.JSONS{Code: 204, Msg: "名字格式不正确!"}
  264. c.ServeJSON()
  265. return
  266. }
  267. user.T_name = T_name
  268. cols = append(cols, "T_name")
  269. }
  270. if len(T_pass) > 0 {
  271. if len(T_pass) < 8 {
  272. c.Data["json"] = lib.JSONS{Code: 206, Msg: "密码格式不正确!"}
  273. c.ServeJSON()
  274. return
  275. }
  276. user.T_pass = T_pass
  277. cols = append(cols, "T_pass")
  278. }
  279. if len(T_power) > 0 {
  280. _, err = Account.Read_Power_ByT_id(T_power)
  281. if err != nil {
  282. c.Data["json"] = lib.JSONS{Code: 208, Msg: "T_power Err!"}
  283. c.ServeJSON()
  284. return
  285. }
  286. user.T_power = T_power
  287. cols = append(cols, "T_power")
  288. }
  289. if T_dept > 0 {
  290. user.T_dept = T_dept
  291. cols = append(cols, "T_dept")
  292. }
  293. if T_post > 0 {
  294. user.T_post = T_post
  295. cols = append(cols, "T_post")
  296. }
  297. if T_dept_leader_err == nil {
  298. user.T_dept_leader = T_dept_leader
  299. cols = append(cols, "T_dept_leader")
  300. }
  301. if T_sex_err == nil {
  302. user.T_sex = T_sex
  303. cols = append(cols, "T_sex")
  304. }
  305. if len(T_id_card) > 0 {
  306. user.T_id_card = T_id_card
  307. cols = append(cols, "T_id_card")
  308. }
  309. if len(T_nation) > 0 {
  310. user.T_nation = T_nation
  311. cols = append(cols, "T_nation")
  312. }
  313. if len(T_school) > 0 {
  314. user.T_school = T_school
  315. cols = append(cols, "T_school")
  316. }
  317. if len(T_major) > 0 {
  318. user.T_major = T_major
  319. cols = append(cols, "T_major")
  320. }
  321. if len(T_education) > 0 {
  322. user.T_education = T_education
  323. cols = append(cols, "T_education")
  324. }
  325. if len(T_phone) > 0 {
  326. user.T_phone = T_phone
  327. cols = append(cols, "T_phone")
  328. }
  329. if T_marry_err == nil {
  330. user.T_marry = T_marry
  331. cols = append(cols, "T_marry")
  332. }
  333. if len(T_spouse_name) > 0 {
  334. user.T_spouse_name = T_spouse_name
  335. cols = append(cols, "T_spouse_name")
  336. }
  337. if len(T_spouse_phone) > 0 {
  338. user.T_spouse_phone = T_spouse_phone
  339. cols = append(cols, "T_spouse_phone")
  340. }
  341. if len(T_entry_time) > 0 {
  342. user.T_entry_time = T_entry_time
  343. cols = append(cols, "T_entry_time")
  344. }
  345. if len(T_positive_time) > 0 {
  346. user.T_positive_time = T_positive_time
  347. cols = append(cols, "T_positive_time")
  348. }
  349. if len(T_entry_type) > 0 {
  350. user.T_entry_type = T_entry_type
  351. cols = append(cols, "T_entry_type")
  352. }
  353. if len(T_contract_start_time) > 0 {
  354. user.T_contract_start_time = T_contract_start_time
  355. cols = append(cols, "T_contract_start_time")
  356. }
  357. if len(T_contract_end_time) > 0 {
  358. user.T_contract_end_time = T_contract_end_time
  359. cols = append(cols, "T_contract_end_time")
  360. }
  361. if T_expire_err == nil {
  362. user.T_expire = T_expire
  363. cols = append(cols, "T_expire")
  364. }
  365. if len(T_remark) > 0 {
  366. user.T_remark = T_remark
  367. cols = append(cols, "T_remark")
  368. }
  369. if len(T_verify_cold_uuid) > 0 {
  370. user.T_verify_cold_uuid = T_verify_cold_uuid
  371. cols = append(cols, "T_verify_cold_uuid")
  372. }
  373. if T_verify_perf_target > 0 {
  374. user.T_verify_perf_target = T_verify_perf_target
  375. cols = append(cols, "T_verify_perf_target")
  376. }
  377. if err = Account.Update_User(user, cols...); err != nil {
  378. c.Data["json"] = lib.JSONS{Code: 208, Msg: "修改失败!"}
  379. c.ServeJSON()
  380. return
  381. }
  382. user.T_pass = "******"
  383. System.Add_UserLogs_T(c.User.T_uuid, "用户", "修改个人信息", user)
  384. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  385. c.ServeJSON()
  386. return
  387. }
  388. // 删除用户信息
  389. func (c *UserController) Del() {
  390. T_uuid := c.GetString("T_uuid")
  391. if len(T_uuid) == 0 {
  392. c.Data["json"] = lib.JSONS{Code: 201, Msg: "T_uuid Err!"}
  393. c.ServeJSON()
  394. return
  395. }
  396. user, err := Account.Read_User_ByT_uuid(T_uuid)
  397. if err != nil {
  398. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  399. c.ServeJSON()
  400. return
  401. }
  402. if user.Id == 1 {
  403. c.Data["json"] = lib.JSONS{Code: 202, Msg: "禁止删除超级管理员!"}
  404. c.ServeJSON()
  405. return
  406. }
  407. if err = Account.Delete_User(user); err != nil {
  408. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  409. c.ServeJSON()
  410. return
  411. }
  412. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  413. c.ServeJSON()
  414. return
  415. }
  416. func (c *UserController) Dept_List() {
  417. var r_jsons lib.R_JSONS
  418. list := []string{
  419. "人事财务部",
  420. "实施",
  421. "研发部-软件组",
  422. "研发部-硬件组",
  423. }
  424. r_jsons.Data = list
  425. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  426. c.ServeJSON()
  427. return
  428. }
  429. // 修改个人信息
  430. func (c *UserController) Leave() {
  431. T_uuid := c.GetString("T_uuid")
  432. if len(T_uuid) == 0 {
  433. c.Data["json"] = lib.JSONS{Code: 201, Msg: "T_uuid Err!"}
  434. c.ServeJSON()
  435. return
  436. }
  437. user, err := Account.Read_User_ByT_uuid(T_uuid)
  438. if err != nil {
  439. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  440. c.ServeJSON()
  441. return
  442. }
  443. if user.Id == 1 {
  444. c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权操作!"}
  445. c.ServeJSON()
  446. return
  447. }
  448. user.T_State = 2
  449. if err := Account.Update_User(user, "T_State"); err != nil {
  450. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  451. c.ServeJSON()
  452. return
  453. }
  454. System.Add_UserLogs_T(c.User.T_uuid, "用户", "离职", "")
  455. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  456. c.ServeJSON()
  457. return
  458. }
  459. // 获取冷链验证用户列表
  460. func (c *UserController) ColdVerify_User_List() {
  461. urls := "/openapi/admin/list"
  462. T_name := c.GetString("T_name")
  463. signature, timestamp := lib.GenColdVerifySignature()
  464. resp, err := http.PostForm(conf.ColdVerify_OpenApi_Host+urls, url.Values{
  465. "T_name": {T_name},
  466. "X-API-KEY": {lib.ColdVerify_OpenApi_Key},
  467. "X-API-SIGNATURE": {signature},
  468. "X-API-TIMESTAMP": {timestamp},
  469. })
  470. if err != nil {
  471. fmt.Println("get请求失败:", err)
  472. }
  473. //延迟关闭(返回response的body尽量关闭,避免内存泄露)
  474. defer resp.Body.Close()
  475. if err != nil {
  476. c.Data["json"] = lib.JSONS{Code: 202, Msg: "请求冷链验证用户列表接口失败!"}
  477. c.ServeJSON()
  478. return
  479. }
  480. var res lib.R_JSONS
  481. body, _ := io.ReadAll(resp.Body)
  482. if err = json.Unmarshal(body, &res); err != nil {
  483. c.Data["json"] = lib.JSONS{Code: 202, Msg: "请求冷链验证用户列表接口失败!"}
  484. c.ServeJSON()
  485. return
  486. }
  487. c.Data["json"] = lib.JSONS{Data: res.Data, Code: 200, Msg: "ok!"}
  488. c.ServeJSON()
  489. return
  490. }