member.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. package model
  2. import (
  3. "crypto/hmac"
  4. "crypto/sha1"
  5. "encoding/base64"
  6. "fmt"
  7. "math/rand"
  8. "net/url"
  9. "path/filepath"
  10. "strconv"
  11. "strings"
  12. "time"
  13. xtime "go-common/library/time"
  14. )
  15. const (
  16. _expMulti = 100
  17. level1 = 1
  18. level2 = 200
  19. level3 = 1500
  20. level4 = 4500
  21. level5 = 10800
  22. level6 = 28800
  23. levelMax = -1
  24. _URLNoFace = "http://static.hdslb.com/images/member/noface.gif"
  25. // ManagerLogID manager log id.
  26. ManagerLogID = 121
  27. //FaceCheckLogID is.
  28. FaceCheckLogID = 161
  29. // bfs facepri bucket
  30. _facepriKeyID = "8923aff2e1124bb2"
  31. _facepriKeySecret = "b237e8927823cc2984aee980123cb0"
  32. )
  33. // base audit type const.
  34. const (
  35. BaseAuditType = iota
  36. BaseAuditTypeFace
  37. BaseAuditTypeSign
  38. BaseAuditTypeName
  39. )
  40. // Base is.
  41. type Base struct {
  42. Mid int64 `json:"mid" gorm:"column:mid"`
  43. Name string `json:"name" gorm:"column:name"`
  44. Sex int64 `json:"sex" gorm:"column:sex"`
  45. Face string `json:"face" gorm:"column:face"`
  46. Sign string `json:"sign" gorm:"column:sign"`
  47. Rank int64 `json:"rank" gorm:"column:rank"`
  48. Birthday xtime.Time `json:"birthday" gorm:"column:birthday"`
  49. CTime xtime.Time `json:"ctime" gorm:"column:ctime"`
  50. MTime xtime.Time `json:"mtime" gorm:"column:mtime"`
  51. }
  52. // RandFaceURL get face URL
  53. func (b *Base) RandFaceURL() {
  54. if b.Face == "" {
  55. b.Face = _URLNoFace
  56. return
  57. }
  58. b.Face = fmt.Sprintf("http://i%d.hdslb.com%s", rand.Int63n(3), b.Face)
  59. }
  60. // Detail is.
  61. type Detail struct {
  62. Mid int64 `json:"mid" gorm:"column:mid"`
  63. Birthday xtime.Time `json:"birthday" gorm:"column:birthday"`
  64. Place int64 `json:"place" gorm:"column:place"`
  65. Marital int64 `json:"marital" gorm:"column:marital"`
  66. Dating int64 `json:"dating" gorm:"column:dating"`
  67. Tags string `json:"tags" gorm:"column:tags"`
  68. CTime xtime.Time `json:"ctime" gorm:"column:ctime"`
  69. MTime xtime.Time `json:"mtime" gorm:"column:mtime"`
  70. }
  71. // Exp is.
  72. type Exp struct {
  73. Mid int64 `json:"mid" gorm:"column:mid"`
  74. Exp int64 `json:"exp" gorm:"column:exp"`
  75. Flag uint32 `json:"flag" gorm:"column:flag"`
  76. AddTime xtime.Time `json:"addtime" gorm:"column:addtime"`
  77. CTime xtime.Time `json:"ctime" gorm:"column:ctime"`
  78. MTime xtime.Time `json:"mtime" gorm:"column:mtime"`
  79. }
  80. // Moral is.
  81. type Moral struct {
  82. Mid int64 `json:"mid" gorm:"column:mid"`
  83. Moral int64 `json:"moral" gorm:"column:moral"`
  84. Added int64 `json:"added" gorm:"column:added"`
  85. Deducted int64 `json:"deducted" gorm:"column:deducted"`
  86. LastRecoverDate xtime.Time `json:"last_recover_date" gorm:"colum:last_recover_date"`
  87. CTime xtime.Time `json:"ctime" gorm:"column:ctime"`
  88. MTime xtime.Time `json:"mtime" gorm:"column:mtime"`
  89. }
  90. // UserAddit is.
  91. type UserAddit struct {
  92. ID int64 `json:"id" gorm:"column:id"`
  93. Mid int64 `json:"mid" gorm:"column:mid"`
  94. FaceReject int64 `json:"face_reject" gorm:"colum:face_reject"`
  95. ViolationCount int64 `json:"violation_count" gorm:"colum:violation_count"`
  96. CTime xtime.Time `json:"ctime" gorm:"column:ctime"`
  97. MTime xtime.Time `json:"mtime" gorm:"column:mtime"`
  98. Remark string `json:"remark" gorm:"column:remark"`
  99. }
  100. // Level is.
  101. type Level struct {
  102. CurrentLevel int32 `json:"current_level"`
  103. CurrentMin int32 `json:"current_min"`
  104. CurrentExp int32 `json:"current_exp"`
  105. NextExp int32 `json:"next_exp"`
  106. }
  107. // Profile is.
  108. type Profile struct {
  109. Base Base `json:"base"`
  110. Detail Detail `json:"detail"`
  111. Exp Exp `json:"exp"`
  112. Level Level `json:"level"`
  113. Moral Moral `json:"moral"`
  114. Official Official `json:"official"`
  115. Coin Coin `json:"coin"`
  116. Addit UserAddit `json:"addit"`
  117. Realanme Realname `json:"realname"`
  118. }
  119. // Coin is.
  120. type Coin struct {
  121. Coins float64 `json:"coins"`
  122. }
  123. // UserLog is.
  124. type UserLog struct {
  125. Mid int64 `json:"mid"`
  126. IP string `json:"ip"`
  127. TS int64 `json:"ts"`
  128. Content map[string]string `json:"content"`
  129. }
  130. // FaceRecord is.
  131. type FaceRecord struct {
  132. ID int64 `json:"id"`
  133. Mid int64 `json:"mid"`
  134. ModifyTime xtime.Time `json:"modify_time"`
  135. ApplyTime xtime.Time `json:"apply_time"`
  136. NewFace string `json:"new_face"`
  137. OldFace string `json:"old_face"`
  138. Operator string `json:"operator"`
  139. Status int8 `json:"status"`
  140. }
  141. // BaseReview is.
  142. type BaseReview struct {
  143. Base
  144. Addit UserAddit `json:"addit"`
  145. Logs []AuditLog `json:"logs"`
  146. }
  147. // AddExpMsg is.
  148. type AddExpMsg struct {
  149. Event string `json:"event,omitempty"`
  150. Mid int64 `json:"mid,omitempty"`
  151. IP string `json:"ip,omitempty"`
  152. Ts int64 `json:"ts,omitempty"`
  153. }
  154. // BuildFaceURL is.
  155. func BuildFaceURL(raw string) string {
  156. if raw == "" {
  157. return _URLNoFace
  158. }
  159. ori, err := url.Parse(raw)
  160. if err != nil {
  161. return raw
  162. }
  163. if ori.Path == "/images/member/noface.gif" {
  164. return _URLNoFace
  165. }
  166. if strings.HasPrefix(ori.Path, "/bfs/facepri") {
  167. token := authorize(_facepriKeyID, _facepriKeySecret, "GET", "facepri", filepath.Base(ori.Path), time.Now().Unix())
  168. p := url.Values{}
  169. p.Set("token", token)
  170. ori.RawQuery = p.Encode()
  171. }
  172. if ori.Hostname() == "" {
  173. ori.Host = fmt.Sprintf("i%d.hdslb.com", rand.Int63n(3))
  174. ori.Scheme = "http"
  175. }
  176. return ori.String()
  177. }
  178. // authorize returns authorization for upload file to bfs
  179. func authorize(key, secret, method, bucket, filename string, expire int64) string {
  180. content := fmt.Sprintf("%s\n%s\n%s\n%d\n", method, bucket, filename, expire)
  181. mac := hmac.New(sha1.New, []byte(secret))
  182. mac.Write([]byte(content))
  183. signature := base64.StdEncoding.EncodeToString(mac.Sum(nil))
  184. return fmt.Sprintf("%s:%s:%d", key, signature, expire)
  185. }
  186. // BuildFaceURL is.
  187. func (fr *FaceRecord) BuildFaceURL() {
  188. fr.NewFace = BuildFaceURL(fr.NewFace)
  189. fr.OldFace = BuildFaceURL(fr.OldFace)
  190. }
  191. // ParseStatus is.
  192. func ParseStatus(s string) int8 {
  193. st, _ := strconv.ParseInt(s, 10, 8)
  194. return int8(st)
  195. }
  196. // ParseLogTime is.
  197. func ParseLogTime(ts string) (xt xtime.Time, err error) {
  198. var (
  199. t time.Time
  200. )
  201. if t, err = time.ParseInLocation("2006-01-02 15:04:05", ts, time.Local); err != nil {
  202. return
  203. }
  204. xt.Scan(t)
  205. return
  206. }
  207. // ParseApplyTime is.
  208. func ParseApplyTime(ts string) xtime.Time {
  209. ti, _ := strconv.ParseInt(ts, 10, 64)
  210. return xtime.Time(ti)
  211. }
  212. // NewProfile is.
  213. func NewProfile() *Profile {
  214. return &Profile{}
  215. }
  216. // FaceRecordList is
  217. type FaceRecordList []*FaceRecord
  218. // Filter is
  219. func (frl FaceRecordList) Filter(con func(*FaceRecord) bool) FaceRecordList {
  220. res := make(FaceRecordList, 0)
  221. for _, fr := range frl {
  222. if con(fr) {
  223. res = append(res, fr)
  224. }
  225. }
  226. return res
  227. }
  228. // Paginate is
  229. func (frl FaceRecordList) Paginate(skip int, size int) FaceRecordList {
  230. if skip > len(frl) {
  231. skip = len(frl)
  232. }
  233. end := skip + size
  234. if end > len(frl) {
  235. end = len(frl)
  236. }
  237. return frl[skip:end]
  238. }
  239. // FromExp is.
  240. func (lv *Level) FromExp(e *Exp) {
  241. exp := e.Exp / _expMulti
  242. switch {
  243. case exp < level1:
  244. lv.CurrentLevel = 0
  245. lv.CurrentMin = 0
  246. lv.NextExp = level1
  247. case exp < level2:
  248. lv.CurrentLevel = 1
  249. lv.CurrentMin = level1
  250. lv.NextExp = level2
  251. case exp < level3:
  252. lv.CurrentLevel = 2
  253. lv.CurrentMin = level2
  254. lv.NextExp = level3
  255. case exp < level4:
  256. lv.CurrentLevel = 3
  257. lv.CurrentMin = level3
  258. lv.NextExp = level4
  259. case exp < level5:
  260. lv.CurrentLevel = 4
  261. lv.CurrentMin = level4
  262. lv.NextExp = level5
  263. case exp < level6:
  264. lv.CurrentLevel = 5
  265. lv.CurrentMin = level5
  266. lv.NextExp = level6
  267. default:
  268. lv.CurrentLevel = 6
  269. lv.CurrentMin = level6
  270. lv.NextExp = levelMax
  271. }
  272. lv.CurrentExp = int32(exp)
  273. }