user_record.go 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package models
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "time"
  5. )
  6. type UserRecord struct {
  7. UserRecordId int `orm:"column(user_record_id);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. BindAccount string `descritpion:"绑定时的账号,最大长度:128"`
  15. Sex int `descritpion:"普通用户性别,1为男性,2为女性"`
  16. Province string `description:"普通用户个人资料填写的省份,最大长度:30"`
  17. City string `description:"普通用户个人资料填写的城市,最大长度:30"`
  18. Country string `description:"国家,如中国为CN,最大长度:30"`
  19. Headimgurl string `description:"用户第三方(微信)头像,最大长度:512"`
  20. CreateTime time.Time `description:"创建时间,关系添加时间、用户授权时间"`
  21. CreatePlatform int `description:"注册平台,1:日度点评公众号,2:管理后台,3:pc端网站,4:查研观向小程序;默认:1"`
  22. SessionKey string `description:"微信小程序会话密钥,最大长度:255"`
  23. UserId int `description:"用户id"`
  24. }
  25. //根据openid获取用户关系
  26. func GetUserRecordByOpenId(openId string) (item *UserRecord, err error) {
  27. sql := `SELECT * FROM user_record WHERE open_id=? `
  28. err = orm.NewOrm().Raw(sql, openId).QueryRow(&item)
  29. return
  30. }
  31. //根据用户id和平台id获取用户关系
  32. func GetUserRecordByUserId(userId, platform int) (item *UserRecord, err error) {
  33. sql := `SELECT * FROM user_record WHERE user_id=? AND create_platform = ?`
  34. err = orm.NewOrm().Raw(sql, userId, platform).QueryRow(&item)
  35. return
  36. }
  37. // AddUserRecord 添加用户关系
  38. func AddUserRecord(record *UserRecord) (recordId int64, err error) {
  39. o := orm.NewOrm()
  40. recordId, err = o.Insert(record)
  41. record.UserRecordId = int(recordId)
  42. return
  43. }
  44. //根据openid绑定用户关系
  45. func BindUserRecordByOpenid(userId int, openId, bindAccount string) (err error) {
  46. o := orm.NewOrm()
  47. msql := " UPDATE user_record SET user_id = ?,bind_account=? WHERE open_id = ? "
  48. _, err = o.Raw(msql, userId, bindAccount, openId).Exec()
  49. return
  50. }
  51. //根据openid解除绑定用户关系
  52. func UnBindUserRecordByOpenid(openId string) (err error) {
  53. o := orm.NewOrm()
  54. msql := ` UPDATE user_record SET user_id = 0,bind_account="" WHERE open_id = ? `
  55. _, err = o.Raw(msql, openId).Exec()
  56. return
  57. }
  58. //修改用户微信信息
  59. func ModifyUserRecordInfo(openId, nickName, headimgUrl, city, province, country string, sex, userId int) (err error) {
  60. o := orm.NewOrm()
  61. sql := `UPDATE user_record SET nick_name=?,headimgurl=?,sex=?,city=?,province=?,country=? WHERE user_id=? and openid=? `
  62. _, err = o.Raw(sql, nickName, headimgUrl, sex, city, province, country, userId, openId).Exec()
  63. return
  64. }
  65. //获取该用户第一个的 三方信息(微信头像信息)
  66. func GetUserThirdRecordByUserId(userId int) (item *UserRecord, err error) {
  67. sql := `SELECT * FROM user_record WHERE user_id = ? order by user_record_id asc`
  68. err = orm.NewOrm().Raw(sql, userId).QueryRow(&item)
  69. return
  70. }
  71. // GetBindUserRecordByUnionId 通过unionid获取已绑定用户的user_record信息
  72. func GetBindUserRecordByUnionId(unionId string) (item *UserRecord, err error) {
  73. sql := `SELECT * FROM user_record WHERE union_id=? AND user_id >0`
  74. err = orm.NewOrm().Raw(sql, unionId).QueryRow(&item)
  75. return
  76. }
  77. func (item *UserRecord) Update(cols []string) (err error) {
  78. o := orm.NewOrm()
  79. _, err = o.Update(item, cols...)
  80. return
  81. }