account.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. #*****************************************************************************
  3. # @file account.go
  4. # @author MakerYang(https://www.makeryang.com)
  5. # @statement 免费课程配套开源项目,任何形式收费均为盗版
  6. #*****************************************************************************
  7. */
  8. package AccountInterface
  9. import (
  10. "Game/framework/database"
  11. "Game/framework/database/game_account_data"
  12. "Game/framework/utils"
  13. "encoding/json"
  14. "fmt"
  15. "github.com/gin-gonic/gin"
  16. "io/ioutil"
  17. )
  18. type requestRegister struct {
  19. Account string `json:"account"`
  20. Password string `json:"password"`
  21. Name string `json:"name"`
  22. Number string `json:"number"`
  23. QuestionA string `json:"question_a"`
  24. AnswerA string `json:"answer_a"`
  25. QuestionB string `json:"question_b"`
  26. AnswerB string `json:"answer_b"`
  27. }
  28. type responseRegister struct {
  29. Token string `json:"token"`
  30. }
  31. func Register(c *gin.Context) {
  32. returnData := responseRegister{}
  33. jsonData := requestRegister{}
  34. requestJson, _ := ioutil.ReadAll(c.Request.Body)
  35. err := json.Unmarshal(requestJson, &jsonData)
  36. if err != nil {
  37. Utils.Error(c, returnData)
  38. return
  39. }
  40. accountDatabase := Database.New(GameAccountData.TableName)
  41. accountData := GameAccountData.Data{}
  42. where := fmt.Sprintf("account_account = %q", jsonData.Account)
  43. err = accountDatabase.GetData(&accountData, where, "")
  44. if err == nil && accountData.AccountId > 0 {
  45. Utils.Warning(c, 10001, "邮箱已经被注册", returnData)
  46. return
  47. }
  48. setData := &GameAccountData.Data{}
  49. setData.AccountAccount = jsonData.Account
  50. setData.AccountPassword = Utils.MD5Hash(jsonData.Password)
  51. setData.AccountName = jsonData.Name
  52. setData.AccountNumber = jsonData.Number
  53. setData.AccountQuestionA = jsonData.QuestionA
  54. setData.AccountAnswerA = jsonData.AnswerA
  55. setData.AccountQuestionB = jsonData.QuestionB
  56. setData.AccountAnswerB = jsonData.AnswerB
  57. setData.AccountStatus = 2
  58. err = accountDatabase.CreateData(setData)
  59. if err != nil {
  60. Utils.Error(c, accountData)
  61. return
  62. }
  63. if setData.AccountId == 0 {
  64. Utils.Error(c, accountData)
  65. return
  66. }
  67. returnData.Token = Utils.EncodeId(128, setData.AccountId)
  68. Utils.Success(c, returnData)
  69. return
  70. }