auth.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**********************************************
  2. ** @Des: 权限因子
  3. ** @Author: haodaquan
  4. ** @Date: 2017-09-09 20:50:36
  5. ** @Last Modified by: haodaquan
  6. ** @Last Modified time: 2017-09-17 21:42:08
  7. ***********************************************/
  8. package backgroundm
  9. import (
  10. "fmt"
  11. "github.com/astaxie/beego/orm"
  12. "wuyebaoxiuapi/models"
  13. )
  14. type Auth struct {
  15. Id int
  16. AuthName string
  17. AuthUrl string
  18. UserId int
  19. Pid int
  20. Sort int
  21. Icon string
  22. IsShow int
  23. Status int
  24. CreateId int
  25. UpdateId int
  26. CreateTime int64
  27. UpdateTime int64
  28. }
  29. func (a *Auth) TableName() string {
  30. return models.TableName("uc_auth")
  31. }
  32. func AuthGetList(page, pageSize int, filters ...interface{}) ([]*Auth, int64) {
  33. offset := (page - 1) * pageSize
  34. list := make([]*Auth, 0)
  35. query := orm.NewOrm().QueryTable(models.TableName("uc_auth"))
  36. if len(filters) > 0 {
  37. l := len(filters)
  38. for k := 0; k < l; k += 2 {
  39. query = query.Filter(filters[k].(string), filters[k+1])
  40. }
  41. }
  42. total, _ := query.Count()
  43. query.OrderBy("pid", "sort").Limit(pageSize, offset).All(&list)
  44. return list, total
  45. }
  46. func AuthGetListByIds(authIds string, userId int) ([]*Auth, error) {
  47. list1 := make([]*Auth, 0)
  48. var list []orm.Params
  49. //list:=[]orm.Params
  50. var err error
  51. if userId == 1 {
  52. //超级管理员
  53. _, err = orm.NewOrm().Raw("select id,auth_name,auth_url,pid,icon,is_show from pp_uc_auth where status=? order by pid asc,sort asc", 1).Values(&list)
  54. } else {
  55. _, err = orm.NewOrm().Raw("select id,auth_name,auth_url,pid,icon,is_show from pp_uc_auth where status=1 and id in("+authIds+") order by pid asc,sort asc", authIds).Values(&list)
  56. }
  57. for k, v := range list {
  58. fmt.Println(k, v)
  59. }
  60. fmt.Println(list)
  61. return list1, err
  62. }
  63. func AuthAdd(auth *Auth) (int64, error) {
  64. return orm.NewOrm().Insert(auth)
  65. }
  66. func init() {
  67. orm.RegisterModel(new(Auth))
  68. }
  69. func AuthGetById(id int) (*Auth, error) {
  70. a := new(Auth)
  71. err := orm.NewOrm().QueryTable(models.TableName("uc_auth")).Filter("id", id).One(a)
  72. if err != nil {
  73. return nil, err
  74. }
  75. return a, nil
  76. }
  77. func (a *Auth) Update(fields ...string) error {
  78. if _, err := orm.NewOrm().Update(a, fields...); err != nil {
  79. return err
  80. }
  81. return nil
  82. }