user_record.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package models
  2. import (
  3. "time"
  4. "github.com/beego/beego/v2/client/orm"
  5. )
  6. type UserRecord struct {
  7. UserRecordId int `orm:"pk"`
  8. OpenId string `description:"用户openid,最大长度:32"`
  9. UnionId string `description:"用户unionid,最大长度:64"`
  10. Subscribe int `description:"是否关注"`
  11. SubscribeTime time.Time `description:""`
  12. NickName string `descritpion:"用户昵称,最大长度:32"`
  13. RealName string `descritpion:"用户实际名称,最大长度:32"`
  14. Sex int `descritpion:"普通用户性别,1为男性,2为女性"`
  15. Province string `description:"普通用户个人资料填写的省份,最大长度:30"`
  16. City string `description:"普通用户个人资料填写的城市,最大长度:30"`
  17. Country string `description:"国家,如中国为CN,最大长度:30"`
  18. Headimgurl string `description:"用户第三方(微信)头像,最大长度:512"`
  19. CreateTime time.Time `description:"创建时间,关系添加时间、用户授权时间"`
  20. SessionKey string `description:"微信小程序会话密钥,最大长度:255"`
  21. UserId int `description:"用户id"`
  22. }
  23. func (item *UserRecord) Update(cols []string) (err error) {
  24. o := orm.NewOrm()
  25. _, err = o.Update(item, cols...)
  26. return
  27. }
  28. func (item *UserRecord) Insert() (insertId int64, err error) {
  29. o := orm.NewOrm()
  30. insertId, err = o.Insert(item)
  31. return
  32. }
  33. // 根据openid获取用户关系
  34. func GetUserRecordByOpenId(openId string) (item *UserRecord, err error) {
  35. sql := `SELECT * FROM user_record WHERE open_id=? `
  36. o := orm.NewOrm()
  37. err = o.Raw(sql, openId).QueryRow(&item)
  38. return
  39. }
  40. func UserSubscribe(subscribeType int, openId string) (err error) {
  41. o := orm.NewOrm()
  42. sql := `UPDATE user_record SET subscribe=?,subscribe_time=NOW() WHERE open_id = ? `
  43. _, err = o.Raw(sql, subscribeType, openId).Exec()
  44. return
  45. }
  46. // GetUserRecordByUnionId 通过unionid获取已绑定用户的user_record信息
  47. func GetUserRecordByUnionId(unionId string) (item *UserRecord, err error) {
  48. sql := `SELECT * FROM user_record WHERE union_id=? `
  49. err = orm.NewOrm().Raw(sql, unionId).QueryRow(&item)
  50. return
  51. }