app.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package service
  2. import (
  3. "context"
  4. "crypto/md5"
  5. "encoding/hex"
  6. "math/rand"
  7. "strconv"
  8. "time"
  9. "go-common/app/admin/main/open/model"
  10. "go-common/library/log"
  11. )
  12. // AddApp represents add an app.
  13. func (s *Service) AddApp(c context.Context, appname string) (err error) {
  14. // Generate the secret and key
  15. timestamp := strconv.Itoa((int)(time.Now().Unix()) + rand.Intn(1))
  16. hashsec := md5.New()
  17. hashsec.Write([]byte(timestamp))
  18. appsecret := hex.EncodeToString(hashsec.Sum(nil))
  19. hashkey := md5.New()
  20. timestamp += "biliappkey"
  21. hashkey.Write([]byte(timestamp))
  22. appkey := hex.EncodeToString(hashkey.Sum(nil))
  23. g := &model.App{
  24. AppName: appname,
  25. AppKey: appkey,
  26. AppSecret: appsecret,
  27. Enabled: 1,
  28. }
  29. if err = s.dao.AddApp(c, g); err != nil {
  30. log.Error("s.dao.AddApp(%+v) error(%v)", g, err)
  31. }
  32. return
  33. }
  34. // DelApp represents delete an app.
  35. func (s *Service) DelApp(c context.Context, appid int64) (err error) {
  36. if err = s.dao.DelApp(c, appid); err != nil {
  37. log.Error("s.DelApp(%d) error(%v)", appid, err)
  38. }
  39. return
  40. }
  41. // UpdateApp represents update an app.
  42. func (s *Service) UpdateApp(c context.Context, arg *model.AppParams) (err error) {
  43. if err = s.dao.UpdateApp(c, arg); err != nil {
  44. log.Error("s.UpdateApps id (%d) appname(%s) error", arg.AppID, arg.AppName)
  45. }
  46. return
  47. }
  48. // ListApp represents search an app.
  49. func (s *Service) ListApp(c context.Context, t *model.AppListParams) (res []*model.App, total int64, err error) {
  50. if res, err = s.dao.ListApp(c, t); err != nil {
  51. log.Error("s.dao.ListApp error (%v)", err)
  52. return
  53. }
  54. // output the data
  55. total = int64(len(res))
  56. start := (t.PN - 1) * t.PS
  57. if start >= total {
  58. res = []*model.App{}
  59. return
  60. }
  61. end := start + t.PS
  62. if end > total {
  63. end = total
  64. }
  65. res = res[start:end]
  66. return
  67. }