User.go 11 KB

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