正在显示
7 个修改的文件
包含
326 行增加
和
2 行删除
| 1 | package models | 1 | package models |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | + "errors" | ||
| 4 | "fmt" | 5 | "fmt" |
| 6 | + "reflect" | ||
| 7 | + "strings" | ||
| 5 | "time" | 8 | "time" |
| 6 | 9 | ||
| 7 | "github.com/astaxie/beego/orm" | 10 | "github.com/astaxie/beego/orm" |
| @@ -13,6 +16,9 @@ type User struct { | @@ -13,6 +16,9 @@ type User struct { | ||
| 13 | Phone string `orm:"column(phone);size(40)" description:"手机号码"` | 16 | Phone string `orm:"column(phone);size(40)" description:"手机号码"` |
| 14 | Passwd string `orm:"column(passwd);size(128)" description:"密码"` | 17 | Passwd string `orm:"column(passwd);size(128)" description:"密码"` |
| 15 | Icon string `orm:"column(icon);size(255)" description:"头像"` | 18 | Icon string `orm:"column(icon);size(255)" description:"头像"` |
| 19 | + CsAccount int64 `orm:"column(cs_account)" description:"客服有话说ID"` | ||
| 20 | + IsKefu int8 `orm:"column(is_kefu)" description:"是否是客服 0:否 1:是"` | ||
| 21 | + ImToken string `orm:"column(im_token);size(128)" description:"网易云token"` | ||
| 16 | LastLoginTime time.Time `orm:"column(last_login_time);type(timestamp)" description:"最后一次登录时间"` | 22 | LastLoginTime time.Time `orm:"column(last_login_time);type(timestamp)" description:"最后一次登录时间"` |
| 17 | CreateAt time.Time `orm:"column(create_at);type(timestamp);auto_now_add" description:"创建时间"` | 23 | CreateAt time.Time `orm:"column(create_at);type(timestamp);auto_now_add" description:"创建时间"` |
| 18 | EnableStatus int8 `orm:"column(enable_status)" description:"是否有效"` | 24 | EnableStatus int8 `orm:"column(enable_status)" description:"是否有效"` |
| @@ -45,6 +51,84 @@ func GetUserById(id int) (v *User, err error) { | @@ -45,6 +51,84 @@ func GetUserById(id int) (v *User, err error) { | ||
| 45 | return nil, err | 51 | return nil, err |
| 46 | } | 52 | } |
| 47 | 53 | ||
| 54 | +// GetAllUser retrieves all User matches certain condition. Returns empty list if | ||
| 55 | +// no records exist | ||
| 56 | +func GetAllUser(query map[string]string, fields []string, sortby []string, order []string, | ||
| 57 | + offset int64, limit int64) (ml []interface{}, err error) { | ||
| 58 | + o := orm.NewOrm() | ||
| 59 | + qs := o.QueryTable(new(User)) | ||
| 60 | + // query k=v | ||
| 61 | + for k, v := range query { | ||
| 62 | + // rewrite dot-notation to Object__Attribute | ||
| 63 | + k = strings.Replace(k, ".", "__", -1) | ||
| 64 | + if strings.Contains(k, "isnull") { | ||
| 65 | + qs = qs.Filter(k, (v == "true" || v == "1")) | ||
| 66 | + } else { | ||
| 67 | + qs = qs.Filter(k, v) | ||
| 68 | + } | ||
| 69 | + } | ||
| 70 | + // order by: | ||
| 71 | + var sortFields []string | ||
| 72 | + if len(sortby) != 0 { | ||
| 73 | + if len(sortby) == len(order) { | ||
| 74 | + // 1) for each sort field, there is an associated order | ||
| 75 | + for i, v := range sortby { | ||
| 76 | + orderby := "" | ||
| 77 | + if order[i] == "desc" { | ||
| 78 | + orderby = "-" + v | ||
| 79 | + } else if order[i] == "asc" { | ||
| 80 | + orderby = v | ||
| 81 | + } else { | ||
| 82 | + return nil, errors.New("Error: Invalid order. Must be either [asc|desc]") | ||
| 83 | + } | ||
| 84 | + sortFields = append(sortFields, orderby) | ||
| 85 | + } | ||
| 86 | + qs = qs.OrderBy(sortFields...) | ||
| 87 | + } else if len(sortby) != len(order) && len(order) == 1 { | ||
| 88 | + // 2) there is exactly one order, all the sorted fields will be sorted by this order | ||
| 89 | + for _, v := range sortby { | ||
| 90 | + orderby := "" | ||
| 91 | + if order[0] == "desc" { | ||
| 92 | + orderby = "-" + v | ||
| 93 | + } else if order[0] == "asc" { | ||
| 94 | + orderby = v | ||
| 95 | + } else { | ||
| 96 | + return nil, errors.New("Error: Invalid order. Must be either [asc|desc]") | ||
| 97 | + } | ||
| 98 | + sortFields = append(sortFields, orderby) | ||
| 99 | + } | ||
| 100 | + } else if len(sortby) != len(order) && len(order) != 1 { | ||
| 101 | + return nil, errors.New("Error: 'sortby', 'order' sizes mismatch or 'order' size is not 1") | ||
| 102 | + } | ||
| 103 | + } else { | ||
| 104 | + if len(order) != 0 { | ||
| 105 | + return nil, errors.New("Error: unused 'order' fields") | ||
| 106 | + } | ||
| 107 | + } | ||
| 108 | + | ||
| 109 | + var l []User | ||
| 110 | + qs = qs.OrderBy(sortFields...) | ||
| 111 | + if _, err = qs.Limit(limit, offset).All(&l, fields...); err == nil { | ||
| 112 | + if len(fields) == 0 { | ||
| 113 | + for _, v := range l { | ||
| 114 | + ml = append(ml, v) | ||
| 115 | + } | ||
| 116 | + } else { | ||
| 117 | + // trim unused fields | ||
| 118 | + for _, v := range l { | ||
| 119 | + m := make(map[string]interface{}) | ||
| 120 | + val := reflect.ValueOf(v) | ||
| 121 | + for _, fname := range fields { | ||
| 122 | + m[fname] = val.FieldByName(fname).Interface() | ||
| 123 | + } | ||
| 124 | + ml = append(ml, m) | ||
| 125 | + } | ||
| 126 | + } | ||
| 127 | + return ml, nil | ||
| 128 | + } | ||
| 129 | + return nil, err | ||
| 130 | +} | ||
| 131 | + | ||
| 48 | // UpdateUser updates User by Id and returns error if | 132 | // UpdateUser updates User by Id and returns error if |
| 49 | // the record to be updated doesn't exist | 133 | // the record to be updated doesn't exist |
| 50 | func UpdateUserById(m *User) (err error) { | 134 | func UpdateUserById(m *User) (err error) { |
models/user_auth.go
0 → 100644
| 1 | +package models | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "errors" | ||
| 5 | + "fmt" | ||
| 6 | + "reflect" | ||
| 7 | + "strings" | ||
| 8 | + "time" | ||
| 9 | + | ||
| 10 | + "github.com/astaxie/beego/orm" | ||
| 11 | +) | ||
| 12 | + | ||
| 13 | +type UserAuth struct { | ||
| 14 | + Id int `orm:"column(id);auto"` | ||
| 15 | + UserId int64 `orm:"column(user_id)" description:"表user.id "` | ||
| 16 | + RefreshToken string `orm:"column(refresh_token);size(64)" description:"refresh token "` | ||
| 17 | + RefreshTokenExp time.Time `orm:"column(refresh_token_exp);type(timestamp)" description:"refresh token 过期时间"` | ||
| 18 | + AccessToken string `orm:"column(access_token);size(64)" description:"access_token "` | ||
| 19 | + AccessTokenExp time.Time `orm:"column(access_token_exp);type(timestamp)" description:"access token 过期时间"` | ||
| 20 | + AuthCode string `orm:"column(auth_code);size(64)" description:"auth_code"` | ||
| 21 | + AuthCodeExp time.Time `orm:"column(auth_code_exp);type(timestamp)" description:"auth_code过期时间"` | ||
| 22 | + DeviceType int8 `orm:"column(device_type)" description:"设备类型 0:ios 1:安卓 2:web "` | ||
| 23 | + ClientId string `orm:"column(client_id);size(100)" description:"设备识别码 推送标识"` | ||
| 24 | + DeviceToken string `orm:"column(device_token);size(100)" description:"设备识别码 推送标识"` | ||
| 25 | + CreateAt time.Time `orm:"column(create_at);type(timestamp)" description:"创建时间"` | ||
| 26 | + UpdateAt time.Time `orm:"column(update_at);type(timestamp)" description:"更新时间"` | ||
| 27 | +} | ||
| 28 | + | ||
| 29 | +func (t *UserAuth) TableName() string { | ||
| 30 | + return "user_auth" | ||
| 31 | +} | ||
| 32 | + | ||
| 33 | +func init() { | ||
| 34 | + orm.RegisterModel(new(UserAuth)) | ||
| 35 | +} | ||
| 36 | + | ||
| 37 | +// AddUserAuth insert a new UserAuth into database and returns | ||
| 38 | +// last inserted Id on success. | ||
| 39 | +func AddUserAuth(m *UserAuth) (id int64, err error) { | ||
| 40 | + o := orm.NewOrm() | ||
| 41 | + id, err = o.Insert(m) | ||
| 42 | + return | ||
| 43 | +} | ||
| 44 | + | ||
| 45 | +// GetUserAuthById retrieves UserAuth by Id. Returns error if | ||
| 46 | +// Id doesn't exist | ||
| 47 | +func GetUserAuthById(id int) (v *UserAuth, err error) { | ||
| 48 | + o := orm.NewOrm() | ||
| 49 | + v = &UserAuth{Id: id} | ||
| 50 | + if err = o.Read(v); err == nil { | ||
| 51 | + return v, nil | ||
| 52 | + } | ||
| 53 | + return nil, err | ||
| 54 | +} | ||
| 55 | + | ||
| 56 | +// GetAllUserAuth retrieves all UserAuth matches certain condition. Returns empty list if | ||
| 57 | +// no records exist | ||
| 58 | +func GetAllUserAuth(query map[string]string, fields []string, sortby []string, order []string, | ||
| 59 | + offset int64, limit int64) (ml []interface{}, err error) { | ||
| 60 | + o := orm.NewOrm() | ||
| 61 | + qs := o.QueryTable(new(UserAuth)) | ||
| 62 | + // query k=v | ||
| 63 | + for k, v := range query { | ||
| 64 | + // rewrite dot-notation to Object__Attribute | ||
| 65 | + k = strings.Replace(k, ".", "__", -1) | ||
| 66 | + if strings.Contains(k, "isnull") { | ||
| 67 | + qs = qs.Filter(k, (v == "true" || v == "1")) | ||
| 68 | + } else { | ||
| 69 | + qs = qs.Filter(k, v) | ||
| 70 | + } | ||
| 71 | + } | ||
| 72 | + // order by: | ||
| 73 | + var sortFields []string | ||
| 74 | + if len(sortby) != 0 { | ||
| 75 | + if len(sortby) == len(order) { | ||
| 76 | + // 1) for each sort field, there is an associated order | ||
| 77 | + for i, v := range sortby { | ||
| 78 | + orderby := "" | ||
| 79 | + if order[i] == "desc" { | ||
| 80 | + orderby = "-" + v | ||
| 81 | + } else if order[i] == "asc" { | ||
| 82 | + orderby = v | ||
| 83 | + } else { | ||
| 84 | + return nil, errors.New("Error: Invalid order. Must be either [asc|desc]") | ||
| 85 | + } | ||
| 86 | + sortFields = append(sortFields, orderby) | ||
| 87 | + } | ||
| 88 | + qs = qs.OrderBy(sortFields...) | ||
| 89 | + } else if len(sortby) != len(order) && len(order) == 1 { | ||
| 90 | + // 2) there is exactly one order, all the sorted fields will be sorted by this order | ||
| 91 | + for _, v := range sortby { | ||
| 92 | + orderby := "" | ||
| 93 | + if order[0] == "desc" { | ||
| 94 | + orderby = "-" + v | ||
| 95 | + } else if order[0] == "asc" { | ||
| 96 | + orderby = v | ||
| 97 | + } else { | ||
| 98 | + return nil, errors.New("Error: Invalid order. Must be either [asc|desc]") | ||
| 99 | + } | ||
| 100 | + sortFields = append(sortFields, orderby) | ||
| 101 | + } | ||
| 102 | + } else if len(sortby) != len(order) && len(order) != 1 { | ||
| 103 | + return nil, errors.New("Error: 'sortby', 'order' sizes mismatch or 'order' size is not 1") | ||
| 104 | + } | ||
| 105 | + } else { | ||
| 106 | + if len(order) != 0 { | ||
| 107 | + return nil, errors.New("Error: unused 'order' fields") | ||
| 108 | + } | ||
| 109 | + } | ||
| 110 | + | ||
| 111 | + var l []UserAuth | ||
| 112 | + qs = qs.OrderBy(sortFields...) | ||
| 113 | + if _, err = qs.Limit(limit, offset).All(&l, fields...); err == nil { | ||
| 114 | + if len(fields) == 0 { | ||
| 115 | + for _, v := range l { | ||
| 116 | + ml = append(ml, v) | ||
| 117 | + } | ||
| 118 | + } else { | ||
| 119 | + // trim unused fields | ||
| 120 | + for _, v := range l { | ||
| 121 | + m := make(map[string]interface{}) | ||
| 122 | + val := reflect.ValueOf(v) | ||
| 123 | + for _, fname := range fields { | ||
| 124 | + m[fname] = val.FieldByName(fname).Interface() | ||
| 125 | + } | ||
| 126 | + ml = append(ml, m) | ||
| 127 | + } | ||
| 128 | + } | ||
| 129 | + return ml, nil | ||
| 130 | + } | ||
| 131 | + return nil, err | ||
| 132 | +} | ||
| 133 | + | ||
| 134 | +// UpdateUserAuth updates UserAuth by Id and returns error if | ||
| 135 | +// the record to be updated doesn't exist | ||
| 136 | +func UpdateUserAuthById(m *UserAuth) (err error) { | ||
| 137 | + o := orm.NewOrm() | ||
| 138 | + v := UserAuth{Id: m.Id} | ||
| 139 | + // ascertain id exists in the database | ||
| 140 | + if err = o.Read(&v); err == nil { | ||
| 141 | + var num int64 | ||
| 142 | + if num, err = o.Update(m); err == nil { | ||
| 143 | + fmt.Println("Number of records updated in database:", num) | ||
| 144 | + } | ||
| 145 | + } | ||
| 146 | + return | ||
| 147 | +} | ||
| 148 | + | ||
| 149 | +// DeleteUserAuth deletes UserAuth by Id and returns error if | ||
| 150 | +// the record to be deleted doesn't exist | ||
| 151 | +func DeleteUserAuth(id int) (err error) { | ||
| 152 | + o := orm.NewOrm() | ||
| 153 | + v := UserAuth{Id: id} | ||
| 154 | + // ascertain id exists in the database | ||
| 155 | + if err = o.Read(&v); err == nil { | ||
| 156 | + var num int64 | ||
| 157 | + if num, err = o.Delete(&UserAuth{Id: id}); err == nil { | ||
| 158 | + fmt.Println("Number of records deleted in database:", num) | ||
| 159 | + } | ||
| 160 | + } | ||
| 161 | + return | ||
| 162 | +} |
| 1 | package protocol | 1 | package protocol |
| 2 | 2 | ||
| 3 | +//指定的请求头字段 | ||
| 3 | const ( | 4 | const ( |
| 4 | HeaderAccessToken string = "x-mmm-accesstoken" | 5 | HeaderAccessToken string = "x-mmm-accesstoken" |
| 5 | HeaderRefreshToken string = "x-mmm-refreshtoken" | 6 | HeaderRefreshToken string = "x-mmm-refreshtoken" |
| @@ -23,3 +24,21 @@ type BaseHeader struct { | @@ -23,3 +24,21 @@ type BaseHeader struct { | ||
| 23 | Timestamp int | 24 | Timestamp int |
| 24 | UID int | 25 | UID int |
| 25 | } | 26 | } |
| 27 | + | ||
| 28 | +//RequestLogin 登录请求 | ||
| 29 | +type RequestLogin struct { | ||
| 30 | + Account string `json:"account"` | ||
| 31 | + Password string `json:"password"` | ||
| 32 | +} | ||
| 33 | + | ||
| 34 | +//ResponseLogin 登录响应 | ||
| 35 | +type ResponseLogin struct { | ||
| 36 | +} | ||
| 37 | + | ||
| 38 | +//RequestSwapCompany 切换公司 | ||
| 39 | +type RequestSwapCompany struct { | ||
| 40 | +} | ||
| 41 | + | ||
| 42 | +// ResponseSwapCompany ... | ||
| 43 | +type ResponseSwapCompany struct { | ||
| 44 | +} |
| @@ -11,6 +11,7 @@ type RequestRoleAdd struct { | @@ -11,6 +11,7 @@ type RequestRoleAdd struct { | ||
| 11 | // return nil | 11 | // return nil |
| 12 | // } | 12 | // } |
| 13 | 13 | ||
| 14 | +//RequestRoleDelete 删除一个角色数据 | ||
| 14 | type RequestRoleDelete struct { | 15 | type RequestRoleDelete struct { |
| 15 | CompanyID int `json:"company_id"` | 16 | CompanyID int `json:"company_id"` |
| 16 | RoleID int `json:"role_id"` | 17 | RoleID int `json:"role_id"` |
| @@ -24,6 +25,26 @@ type RequestRoleEdit struct { | @@ -24,6 +25,26 @@ type RequestRoleEdit struct { | ||
| 24 | Descript string `json:"descript"` | 25 | Descript string `json:"descript"` |
| 25 | } | 26 | } |
| 26 | 27 | ||
| 28 | +//RequestRoleOne 获取一个角色数据 | ||
| 29 | +type RequestRoleOne struct { | ||
| 30 | + CompanyID int `json:"company_id"` | ||
| 31 | + RoleID int `json:"role_id` | ||
| 32 | +} | ||
| 33 | + | ||
| 34 | +//ResponseRoleInfo 响应数据 | ||
| 35 | +type ResponseRoleInfo struct { | ||
| 36 | + RoleID int `json:"role_id"` | ||
| 37 | + RoleName string `json:"role_name"` | ||
| 38 | + Descript string `json:"descript"` | ||
| 39 | +} | ||
| 40 | + | ||
| 41 | +//RequestRoleList 分页获取角色列表 | ||
| 42 | +type RequestRoleList struct { | ||
| 43 | + PageIndex int `json:"page_index"` | ||
| 44 | + PageSize int `json:"page_size"` | ||
| 45 | + keyword string `json:"keyword"` // 搜索关键字 | ||
| 46 | +} | ||
| 47 | + | ||
| 27 | type RequestRolePermission struct { | 48 | type RequestRolePermission struct { |
| 28 | CompanyID int `json:"company_id"` | 49 | CompanyID int `json:"company_id"` |
| 29 | RoleID int `json:"role_id"` | 50 | RoleID int `json:"role_id"` |
| @@ -71,10 +71,19 @@ func RoleEdit(param protocol.RequestRoleEdit) error { | @@ -71,10 +71,19 @@ func RoleEdit(param protocol.RequestRoleEdit) error { | ||
| 71 | return nil | 71 | return nil |
| 72 | } | 72 | } |
| 73 | 73 | ||
| 74 | -func RoleGetOne() error { | 74 | +func RoleGetOne(param protocol.RequestRoleOne) error { |
| 75 | + | ||
| 76 | + return nil | ||
| 77 | +} | ||
| 78 | + | ||
| 79 | +func RoleGetByPage(param protocol.RequestRoleList) error { | ||
| 80 | + return nil | ||
| 81 | +} | ||
| 82 | + | ||
| 83 | +func RoleHasPermission() error { | ||
| 75 | return nil | 84 | return nil |
| 76 | } | 85 | } |
| 77 | 86 | ||
| 78 | -func RoleGetByPage() error { | 87 | +func PermissionHasRole() error { |
| 79 | return nil | 88 | return nil |
| 80 | } | 89 | } |
| 1 | package serveauth | 1 | package serveauth |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | + "crypto/sha1" | ||
| 5 | + "fmt" | ||
| 6 | + "io" | ||
| 4 | "oppmg/common/config" | 7 | "oppmg/common/config" |
| 5 | "oppmg/common/log" | 8 | "oppmg/common/log" |
| 6 | "oppmg/protocol" | 9 | "oppmg/protocol" |
| 10 | + "strings" | ||
| 7 | ) | 11 | ) |
| 8 | 12 | ||
| 13 | +//GetAccessToken 获取accessToken | ||
| 9 | func GetAccessToken(param protocol.RequestCheckSmsCode) (*protocol.DataUserInfo, error) { | 14 | func GetAccessToken(param protocol.RequestCheckSmsCode) (*protocol.DataUserInfo, error) { |
| 10 | data := &protocol.DataUserInfo{} | 15 | data := &protocol.DataUserInfo{} |
| 11 | err := protocol.NewErrWithMessage("00000") | 16 | err := protocol.NewErrWithMessage("00000") |
| @@ -13,3 +18,26 @@ func GetAccessToken(param protocol.RequestCheckSmsCode) (*protocol.DataUserInfo, | @@ -13,3 +18,26 @@ func GetAccessToken(param protocol.RequestCheckSmsCode) (*protocol.DataUserInfo, | ||
| 13 | log.Info("%+v", config.MConfig) | 18 | log.Info("%+v", config.MConfig) |
| 14 | return data, err | 19 | return data, err |
| 15 | } | 20 | } |
| 21 | + | ||
| 22 | +//ValidatePassword ... | ||
| 23 | +//from:待校验的密码;to:比对用的密文 | ||
| 24 | +func ValidatePassword(from, to string) bool { | ||
| 25 | + //密码加密方式sha1 | ||
| 26 | + h := sha1.New() | ||
| 27 | + io.WriteString(h, from) | ||
| 28 | + str := fmt.Sprintf("%x", h.Sum(nil)) | ||
| 29 | + if strings.Compare(str, to) == 0 { | ||
| 30 | + return true | ||
| 31 | + } | ||
| 32 | + return false | ||
| 33 | +} | ||
| 34 | + | ||
| 35 | +//LoginAuth 登录认证 | ||
| 36 | +func LoginAuth(account, password string) error { | ||
| 37 | + return nil | ||
| 38 | +} | ||
| 39 | + | ||
| 40 | +//RefreshAccessToken 刷新token | ||
| 41 | +func RefreshAccessToken(account string, token string) error { | ||
| 42 | + return nil | ||
| 43 | +} |
-
请 注册 或 登录 后发表评论