User.go 12 KB

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