|
@@ -1,86 +1,85 @@
|
|
package models
|
|
package models
|
|
|
|
|
|
import (
|
|
import (
|
|
- "errors"
|
|
|
|
- "strconv"
|
|
|
|
"time"
|
|
"time"
|
|
-)
|
|
|
|
|
|
|
|
-var (
|
|
|
|
- UserList map[string]*User
|
|
|
|
|
|
+ "github.com/beego/beego/v2/client/orm"
|
|
)
|
|
)
|
|
|
|
|
|
-func init() {
|
|
|
|
- UserList = make(map[string]*User)
|
|
|
|
- u := User{"user_11111", "astaxie", "11111", Profile{"male", 20, "Singapore", "astaxie@gmail.com"}}
|
|
|
|
- UserList["user_11111"] = &u
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
type User struct {
|
|
type User struct {
|
|
- Id string
|
|
|
|
- Username string
|
|
|
|
- Password string
|
|
|
|
- Profile Profile
|
|
|
|
|
|
+ UserId int `orm:"pk" description:"用户id"`
|
|
|
|
+ OpenId string `description:"openid"`
|
|
|
|
+ UnionId string `description:"unionid"`
|
|
|
|
+ RealName string `description:"姓名"`
|
|
|
|
+ Phone string `description:"手机号"`
|
|
|
|
+ AreaCode string `description:"区号"`
|
|
|
|
+ Email string `description:"邮箱"`
|
|
|
|
+ SellerId int `description:"销售id(SysUserId)"`
|
|
|
|
+ Company string `description:"所属公司"`
|
|
|
|
+ ValidStartTime time.Time `description:"有效期开始时间"`
|
|
|
|
+ ValidEndTime time.Time `description:"有效期结束时间"`
|
|
|
|
+ Status int `description:"用户类型: 0表示禁用,1表示潜在客户,2表示正式客户"`
|
|
|
|
+ CreateTime time.Time `description:"系统中首次新增用户的时间"`
|
|
|
|
+ ModifyTime time.Time `description:"系统中用户信息变更的时间"`
|
|
|
|
+ RegisterTime time.Time `description:"用户首次登录小程序的时间"`
|
|
|
|
+ IsSubscribed bool `description:"是否关注公众号: 0表示没有关注,1表示关注"`
|
|
|
|
+ IsRegistered bool `description:"是否注册: 0表示没有注册,1表示注册"`
|
|
}
|
|
}
|
|
|
|
|
|
-type Profile struct {
|
|
|
|
- Gender string
|
|
|
|
- Age int
|
|
|
|
- Address string
|
|
|
|
- Email string
|
|
|
|
|
|
+func (u *User) Insert() (insertId int64, err error) {
|
|
|
|
+ o := orm.NewOrmUsingDB("master")
|
|
|
|
+ insertId, err = o.Insert(u)
|
|
|
|
+ return
|
|
}
|
|
}
|
|
|
|
|
|
-func AddUser(u User) string {
|
|
|
|
- u.Id = "user_" + strconv.FormatInt(time.Now().UnixNano(), 10)
|
|
|
|
- UserList[u.Id] = &u
|
|
|
|
- return u.Id
|
|
|
|
|
|
+type UserItem struct {
|
|
|
|
+ UserId int `description:"用户id"`
|
|
|
|
+ OpenId string `description:"open_id"`
|
|
|
|
+ UnionId string `description:"union_id"`
|
|
|
|
+ NickName string `description:"用户昵称"`
|
|
|
|
+ RealName string `description:"用户实际名称"`
|
|
|
|
+ Phone string `description:"手机号码"`
|
|
|
|
+ Componey string `description:"所属公司"`
|
|
|
|
+ AreaCode string `description:"区号"`
|
|
|
|
+ SellerId int `description:"销售id"`
|
|
|
|
+ Email string `description:"邮箱"`
|
|
|
|
+ Headimgurl string `description:"用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空"`
|
|
|
|
+ ValidEndTime time.Time `description:"服务截至时间"`
|
|
|
|
+ RegisterTime time.Time `description:"登录时间,用户首次登录小程序的时间"`
|
|
|
|
+ CreateTime time.Time `description:"系统中新增用户的时间"`
|
|
|
|
+ ModifyTime time.Time `description:"系统中用户信息更新的时间"`
|
|
|
|
+ IsRegistered bool `description:"是否注册:1:已注册,0:未注册"`
|
|
}
|
|
}
|
|
|
|
|
|
-func GetUser(uid string) (u *User, err error) {
|
|
|
|
- if u, ok := UserList[uid]; ok {
|
|
|
|
- return u, nil
|
|
|
|
- }
|
|
|
|
- return nil, errors.New("User not exists")
|
|
|
|
|
|
+// 变更联系人是否已注册状态
|
|
|
|
+func ModifyUserRegisterStatus(userId int, status bool, registerTime, modifyTime time.Time) (err error) {
|
|
|
|
+ o := orm.NewOrmUsingDB("master")
|
|
|
|
+ sql := `UPDATE user SET is_registered=?, register_time=?, modify_time=? WHERE user_id = ? `
|
|
|
|
+ _, err = o.Raw(sql, status, registerTime, modifyTime, userId).Exec()
|
|
|
|
+ return
|
|
}
|
|
}
|
|
|
|
|
|
-func GetAllUsers() map[string]*User {
|
|
|
|
- return UserList
|
|
|
|
|
|
+func GetUserById(userId int) (item *User, err error) {
|
|
|
|
+ o := orm.NewOrmUsingDB("master")
|
|
|
|
+ sql := `SELECT * FROM user WHERE user_id=? `
|
|
|
|
+ err = o.Raw(sql, userId).QueryRow(&item)
|
|
|
|
+ return
|
|
}
|
|
}
|
|
|
|
|
|
-func UpdateUser(uid string, uu *User) (a *User, err error) {
|
|
|
|
- if u, ok := UserList[uid]; ok {
|
|
|
|
- if uu.Username != "" {
|
|
|
|
- u.Username = uu.Username
|
|
|
|
- }
|
|
|
|
- if uu.Password != "" {
|
|
|
|
- u.Password = uu.Password
|
|
|
|
- }
|
|
|
|
- if uu.Profile.Age != 0 {
|
|
|
|
- u.Profile.Age = uu.Profile.Age
|
|
|
|
- }
|
|
|
|
- if uu.Profile.Address != "" {
|
|
|
|
- u.Profile.Address = uu.Profile.Address
|
|
|
|
- }
|
|
|
|
- if uu.Profile.Gender != "" {
|
|
|
|
- u.Profile.Gender = uu.Profile.Gender
|
|
|
|
- }
|
|
|
|
- if uu.Profile.Email != "" {
|
|
|
|
- u.Profile.Email = uu.Profile.Email
|
|
|
|
- }
|
|
|
|
- return u, nil
|
|
|
|
- }
|
|
|
|
- return nil, errors.New("User Not Exist")
|
|
|
|
|
|
+func GetUserItemByPhone(phone string) (item *UserItem, err error) {
|
|
|
|
+ o := orm.NewOrmUsingDB("master")
|
|
|
|
+ sql := `SELECT * FROM user WHERE phone=? `
|
|
|
|
+ err = o.Raw(sql, phone).QueryRow(&item)
|
|
|
|
+ return
|
|
}
|
|
}
|
|
|
|
|
|
-func Login(username, password string) bool {
|
|
|
|
- for _, u := range UserList {
|
|
|
|
- if u.Username == username && u.Password == password {
|
|
|
|
- return true
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- return false
|
|
|
|
|
|
+func GetUserItemByEmail(email string) (item *UserItem, err error) {
|
|
|
|
+ sql := `SELECT * FROM user WHERE email=? `
|
|
|
|
+ err = orm.NewOrm().Raw(sql, email).QueryRow(&item)
|
|
|
|
+ return
|
|
}
|
|
}
|
|
-
|
|
|
|
-func DeleteUser(uid string) {
|
|
|
|
- delete(UserList, uid)
|
|
|
|
|
|
+func GetUserItemByUserId(userId int) (item *UserItem, err error) {
|
|
|
|
+ sql := `SELECT * FROM user WHERE user_id=? `
|
|
|
|
+ err = orm.NewOrm().Raw(sql, userId).QueryRow(&item)
|
|
|
|
+ return
|
|
}
|
|
}
|