user.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package restful
  2. import (
  3. "fmt"
  4. "github.com/astaxie/beego/logs"
  5. )
  6. type User struct {
  7. Id int `xorm:"not null pk autoincr INT(10)"`
  8. Username string `xorm:"default '''' comment('账号') VARCHAR(50)"`
  9. Password string `xorm:"default '''' comment('密码') VARCHAR(50)"`
  10. Empid int `xorm:"default -1 INT(11)"`
  11. Sort int `xorm:"default 0 INT(11)"`
  12. Enable int `xorm:"default 1 TINYINT(4)"`
  13. CreatedOn int `xorm:"default NULL comment('创建时间') INT(11)"`
  14. ModifiedOn int `xorm:"default NULL comment('更新时间') INT(11)"`
  15. DeletedOn int `xorm:"default 0 comment('删除时间戳') INT(11)"`
  16. }
  17. /*
  18. type UserRole struct {
  19. Id int `xorm:"not null pk autoincr INT(10)"`
  20. UserId int `xorm:"default NULL comment('用户ID') INT(11)"`
  21. RoleId int `xorm:"default NULL comment('角色ID') INT(11)"`
  22. DeletedOn int `xorm:"default 0 comment('删除时间戳') INT(11)"`
  23. }
  24. type Role struct {
  25. Id int `xorm:"not null pk autoincr INT(10)"`
  26. Name string `xorm:"default '''' comment('名字') VARCHAR(50)"`
  27. Sort int `xorm:"default 0 INT(11)"`
  28. Enable int `xorm:"default 1 TINYINT(4)"`
  29. CreatedOn int `xorm:"default NULL comment('创建时间') INT(11)"`
  30. ModifiedOn int `xorm:"default NULL comment('更新时间') INT(11)"`
  31. DeletedOn int `xorm:"default 0 comment('删除时间戳') INT(11)"`
  32. }
  33. */
  34. func CheckUser(username, password string) (bool, error) {
  35. tqueryData, err := Dbs.Queryx("SELECT * FROM `user` WHERE `username` = ? and password = ? and `enable` >0 ", username, password)
  36. if err != nil {
  37. logs.Error(err)
  38. return false, err
  39. }
  40. valuesMap, err := rows2mapObjects(tqueryData)
  41. if err != nil || len(valuesMap) == 0 {
  42. logs.Error(err)
  43. return false, err
  44. }
  45. //if len(valuesMap) > 0 {
  46. //return true, nil
  47. //}
  48. return true, nil
  49. }
  50. func GetUserInfo(username string) map[string]interface{} {
  51. valuesMap := make([]map[string]interface{}, 1)
  52. valuesMap2 := make([]map[string]interface{}, 1)
  53. tqueryData, err := Dbs.Queryx("SELECT * FROM `user` WHERE `username` = ? and `enable` >0 ", username)
  54. if err != nil {
  55. fmt.Println(err)
  56. return nil
  57. }
  58. valuesMap, err = rows2mapObjects(tqueryData)
  59. if err != nil {
  60. return nil
  61. }
  62. if len(valuesMap) > 0 {
  63. tqueryData2, err := Dbs.Queryx("SELECT r.`name`,r.`id` FROM `user` u JOIN `user_role` ur ON u.`id`=ur.`user_id` JOIN `role` r ON r.id=ur.`role_id` WHERE r.`enable`>0 AND u.username=?", username)
  64. if err == nil {
  65. valuesMap2, err = rows2mapObjects(tqueryData2)
  66. if err == nil {
  67. valuesMap[0]["role"] = valuesMap2
  68. }
  69. }
  70. return valuesMap[0]
  71. }
  72. return nil
  73. }
  74. func GetUserVendor(username string) int64 {
  75. tqueryData, err := Dbs.Queryx(`SELECT
  76. ifnull(pa.vendor,0) vendor
  77. FROM
  78. emp e
  79. INNER JOIN USER u ON e.id = u.empid
  80. INNER JOIN department d ON e.deptid = d.id
  81. INNER join pasture pa on pa.id = d.pastureid
  82. WHERE
  83. u.username = ?`, username)
  84. if err == nil {
  85. valuesMap, err := rows2mapObjects(tqueryData)
  86. if err == nil {
  87. return valuesMap[0]["vendor"].(int64)
  88. }
  89. }
  90. return -1
  91. }