User.go 13 KB

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