User.go 13 KB

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