xcx_api_controller.go 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package xcx_mobile_api_controller_go
  2. import (
  3. "Xcx_New/controllers"
  4. "Xcx_New/enums"
  5. "Xcx_New/models"
  6. "Xcx_New/service"
  7. "Xcx_New/utils"
  8. "fmt"
  9. "github.com/astaxie/beego"
  10. "github.com/jinzhu/gorm"
  11. "time"
  12. )
  13. func XcxApiControllersRegisterRouters() {
  14. //传送codeinit
  15. beego.Router("/m/api/code", &XcxApiController{}, "Get:GetCodeInit")
  16. //获取验证码
  17. beego.Router("/xcx/api/mobile/code", &XcxApiController{}, "Get:GetCodeInfo")
  18. //用户绑定
  19. beego.Router("/xcx/api/mobile/register", &XcxApiController{}, "Get:GetUserRegister")
  20. //登录
  21. beego.Router("/xcx/api/mobile/register", &XcxApiController{}, "Get:GetLoginInfor")
  22. }
  23. type XcxApiController struct {
  24. controllers.BaseAuthAPIController
  25. }
  26. func (this *XcxApiController) GetCodeInit() {
  27. redisClient := service.RedisClient()
  28. defer redisClient.Close()
  29. req := this.Ctx.Request
  30. addr := utils.GetIP(req)
  31. cur_time := time.Now().Format("2006-01-02")
  32. _, err := redisClient.Get("ip:host_" + cur_time + "_" + addr).Result()
  33. if err != nil {
  34. redisClient.Set("ip:host_"+cur_time+"_"+addr, 0, time.Second*24*60*60)
  35. }
  36. //将客户端的ip加密传给前端,作为短信验证的密钥,来验证短信发送的IP地址
  37. aespass := utils.AESEncrypt(addr)
  38. fmt.Println("hhhhhhh3223323232332", aespass)
  39. this.ServeSuccessJSON(map[string]interface{}{
  40. "aespass": aespass,
  41. })
  42. }
  43. func (this *XcxApiController) GetUserRegister() {
  44. //用户绑定
  45. name := this.GetString("name")
  46. id_card_no := this.GetString("id_card_no")
  47. mobile := this.GetString("mobile")
  48. code := this.GetString("code")
  49. role := models.XcxAdminUserRole{
  50. PatientName: name,
  51. IdCardNo: id_card_no,
  52. Mobile: mobile,
  53. Code: code,
  54. PatientId: 0,
  55. UserOrgId: 0,
  56. Status: 0,
  57. Ctime: 0,
  58. Mtime: 0,
  59. Appid: "",
  60. Appsecret: "",
  61. SessionKey: "",
  62. }
  63. //查找该电话号码是否存在
  64. _, errcode := service.GetXcxMobileInformation(mobile)
  65. if errcode == gorm.ErrRecordNotFound {
  66. err := service.CreateXcxAdminUser(role)
  67. if err == nil {
  68. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  69. return
  70. }
  71. this.ServeSuccessJSON(map[string]interface{}{
  72. "role": role,
  73. })
  74. } else if errcode == nil {
  75. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  76. return
  77. }
  78. fmt.Println("roler", role)
  79. }
  80. func (this *XcxApiController) GetCodeInfo() {
  81. mobile := this.GetString("phone")
  82. aespass := this.GetString("aespass")
  83. utils.TraceLog("mobile:%v aespass:%v", mobile, aespass)
  84. if utils.CellPhoneRegexp().MatchString(mobile) == false {
  85. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeMobileFormat)
  86. this.ServeJSON()
  87. return
  88. }
  89. adminUser, _ := service.GetValidAdminUserByMobileReturnErr(mobile)
  90. if adminUser != nil {
  91. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeRegisterExist)
  92. this.ServeJSON()
  93. return
  94. }
  95. if err := service.SendVerificationCodeSMS(mobile, aespass); err != nil {
  96. this.Data["json"] = enums.MakeFailResponseJSON(err.Error(), 600)
  97. this.ServeJSON()
  98. } else {
  99. this.Data["json"] = enums.MakeSuccessResponseJSON(map[string]interface{}{
  100. "msg": "短信发送成功,有效期为10分钟",
  101. })
  102. this.ServeJSON()
  103. }
  104. }
  105. func (this *XcxApiController) GetLoginInfor() {
  106. mobile := this.GetString("mobile")
  107. role, err := service.GetLoginInfor(mobile)
  108. if err == nil {
  109. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  110. return
  111. }
  112. this.ServeSuccessJSON(map[string]interface{}{
  113. "role": role,
  114. })
  115. }