User.go 13 KB

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