user_login.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package services
  2. import (
  3. "encoding/json"
  4. "eta/eta_mobile/models"
  5. "eta/eta_mobile/models/company"
  6. "eta/eta_mobile/models/system"
  7. "eta/eta_mobile/utils"
  8. "fmt"
  9. "github.com/go-ldap/ldap"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. // SendAdminMobileVerifyCode 发送用户手机验证码
  15. func SendAdminMobileVerifyCode(source int, mobile, areaCode string) (ok bool, err error) {
  16. defer func() {
  17. if err != nil {
  18. tips := fmt.Sprintf("SendAdminMobileVerifyCode ErrMsg: %s", err.Error())
  19. utils.FileLog.Info(tips)
  20. fmt.Println(tips)
  21. }
  22. }()
  23. smsClient, e := NewSmsClient()
  24. if e != nil {
  25. err = fmt.Errorf("NewSmsClient err: %s", e.Error())
  26. return
  27. }
  28. verifyCode := utils.GetRandDigit(6)
  29. record := new(system.AdminVerifyCodeRecord)
  30. record.VerifyType = system.AdminVerifyCodeRecordTypeMobile
  31. record.Mobile = mobile
  32. record.Source = source
  33. record.Code = verifyCode
  34. record.ExpiredTime = time.Now().Add(utils.VerifyCodeExpireMinute * time.Minute)
  35. record.CreateTime = time.Now().Local()
  36. record.ModifyTime = time.Now().Local()
  37. if e := record.Create(); e != nil {
  38. err = fmt.Errorf("新增验证码记录失败, Err: %s", e.Error())
  39. return
  40. }
  41. var smsReq UserLoginSmsCodeReq
  42. smsReq.Mobile = mobile
  43. smsReq.TelAreaCode = areaCode
  44. smsReq.VerifyCode = verifyCode
  45. smsResult, e := smsClient.SendUserLoginCode(smsReq)
  46. if e != nil {
  47. err = fmt.Errorf("SendUserLoginCode err: %s", e.Error())
  48. return
  49. }
  50. ok = smsResult.Success
  51. record.SendStatus = system.AdminVerifyCodeRecordStatusSuccess
  52. if !ok {
  53. record.SendStatus = system.AdminVerifyCodeRecordStatusFail
  54. }
  55. record.RequestId = smsResult.RequestId
  56. cols := []string{"SendStatus", "RequestId"}
  57. if e := record.Update(cols); e != nil {
  58. err = fmt.Errorf("更新验证码记录失败, Err: %s", e.Error())
  59. }
  60. return
  61. }
  62. // SendAdminEmailVerifyCode 发送用户邮箱验证码
  63. func SendAdminEmailVerifyCode(source int, email string) (ok bool, err error) {
  64. defer func() {
  65. if err != nil {
  66. tips := fmt.Sprintf("SendAdminEmailVerifyCode ErrMsg: %s", err.Error())
  67. utils.FileLog.Info(tips)
  68. fmt.Println(tips)
  69. }
  70. }()
  71. // 读取配置
  72. confMap, e := models.GetBusinessConf()
  73. if e != nil {
  74. err = fmt.Errorf("GetBusinessConf err: %s", e.Error())
  75. return
  76. }
  77. subjectConf := confMap[models.BusinessConfLoginEmailTemplateSubject]
  78. contentConf := confMap[models.BusinessConfLoginEmailTemplateContent]
  79. if subjectConf == "" {
  80. err = fmt.Errorf("请先配置邮件模版主题")
  81. return
  82. }
  83. if contentConf == "" {
  84. err = fmt.Errorf("请先配置邮件模版内容")
  85. return
  86. }
  87. verifyCode := utils.GetRandDigit(6)
  88. t := time.Now().Format("2006年01月02日")
  89. emailContent := contentConf
  90. emailContent = strings.Replace(emailContent, "{{VERIFY_CODE}}", verifyCode, 1)
  91. emailContent = strings.Replace(emailContent, "{{EXPIRED_MINUTE}}", strconv.Itoa(utils.VerifyCodeExpireMinute), 1)
  92. emailContent = strings.Replace(emailContent, "{{DATE_TIME}}", t, 1)
  93. // 验证码记录
  94. record := new(system.AdminVerifyCodeRecord)
  95. record.VerifyType = system.AdminVerifyCodeRecordTypeEmail
  96. record.Email = email
  97. record.Source = source
  98. record.Code = verifyCode
  99. record.ExpiredTime = time.Now().Add(utils.VerifyCodeExpireMinute * time.Minute)
  100. record.CreateTime = time.Now().Local()
  101. record.ModifyTime = time.Now().Local()
  102. if e := record.Create(); e != nil {
  103. err = fmt.Errorf("新增验证码记录失败, Err: %s", e.Error())
  104. return
  105. }
  106. var result string
  107. if confMap[models.BusinessConfEmailClient] == models.BusinessConfEmailClientSmtp {
  108. // 普通邮箱
  109. var emailReq SendEmailReq
  110. emailReq.Title = subjectConf
  111. emailReq.Content = emailContent
  112. emailReq.ToUser = append(emailReq.ToUser, email)
  113. ok, e = SendEmail(emailReq)
  114. if e != nil {
  115. err = fmt.Errorf("邮箱推送失败, Err: %s", e.Error())
  116. return
  117. }
  118. } else {
  119. // 默认阿里云邮箱
  120. // 读取发信人昵称配置...后面可以优化一下
  121. authKey := "english_report_email_conf"
  122. emailConf, e := company.GetConfigDetailByCode(authKey)
  123. if e != nil {
  124. err = fmt.Errorf("获取群发邮件权限失败, Err: %s", e.Error())
  125. return
  126. }
  127. if emailConf.ConfigValue == "" {
  128. err = fmt.Errorf("邮件配置为空, 不可推送")
  129. return
  130. }
  131. conf := new(models.EnglishReportEmailConf)
  132. if e = json.Unmarshal([]byte(emailConf.ConfigValue), &conf); e != nil {
  133. err = fmt.Errorf("邮件配置有误, 不可推送")
  134. return
  135. }
  136. req := new(EnglishReportSendEmailRequest)
  137. req.Subject = subjectConf
  138. req.Email = email
  139. req.FromAlias = conf.FromAlias // 发信人昵称
  140. req.HtmlBody = emailContent
  141. aliEmail := new(AliyunEmail)
  142. o, r, e := aliEmail.SendEmail(req)
  143. if e != nil {
  144. err = fmt.Errorf("阿里云邮箱推送失败, Err: %s", e.Error())
  145. return
  146. }
  147. ok = o
  148. result = r
  149. }
  150. record.SendStatus = system.AdminVerifyCodeRecordStatusSuccess
  151. if !ok {
  152. record.SendStatus = system.AdminVerifyCodeRecordStatusFail
  153. }
  154. record.SendResult = result
  155. cols := []string{"SendStatus", "SendResult"}
  156. if e = record.Update(cols); e != nil {
  157. err = fmt.Errorf("更新验证码记录失败, Err: %s", e.Error())
  158. }
  159. return
  160. }
  161. // LdapUserCheck AD域用户校验
  162. func LdapUserCheck(userName, password string) (pass bool, err error) {
  163. defer func() {
  164. if err != nil {
  165. tips := fmt.Sprintf("LdapUserCheck ErrMsg: %s", err.Error())
  166. utils.FileLog.Info(tips)
  167. fmt.Println(tips)
  168. }
  169. }()
  170. if userName == "" || password == "" {
  171. err = fmt.Errorf("账号密码有误")
  172. return
  173. }
  174. confMap, e := models.GetBusinessConf()
  175. if e != nil {
  176. err = fmt.Errorf("GetBusinessConf err: %s", e.Error())
  177. return
  178. }
  179. if confMap[models.BusinessConfLdapHost] == "" || confMap[models.BusinessConfLdapBase] == "" {
  180. err = fmt.Errorf("AD域配置有误")
  181. return
  182. }
  183. ldapPort, _ := strconv.Atoi(confMap[models.BusinessConfLdapPort])
  184. if ldapPort <= 0 {
  185. err = fmt.Errorf("AD域端口号有误, Port: %d", ldapPort)
  186. return
  187. }
  188. // 连接ldap
  189. addr := fmt.Sprintf("%s:%d", confMap[models.BusinessConfLdapHost], ldapPort)
  190. conn, e := ldap.Dial("tcp", addr)
  191. if e != nil {
  192. err = fmt.Errorf("ldap Dial err: %s", e.Error())
  193. return
  194. }
  195. defer conn.Close()
  196. // 绑定用户
  197. bindUserName := fmt.Sprintf("%s%s", userName, confMap[models.BusinessConfLdapBindUserSuffix])
  198. if e = conn.Bind(bindUserName, password); e != nil {
  199. err = fmt.Errorf("ldap Bind err: %s", e.Error())
  200. return
  201. }
  202. // 鉴权操作
  203. searchRequest := ldap.NewSearchRequest(
  204. confMap[models.BusinessConfLdapBase],
  205. ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
  206. fmt.Sprintf(confMap[models.BusinessConfLdapUserFilter], userName),
  207. []string{"dn"},
  208. nil,
  209. )
  210. //b, _ := json.Marshal(searchRequest)
  211. //fmt.Println("searchRequest: ", string(b))
  212. sr, e := conn.Search(searchRequest)
  213. if e != nil {
  214. err = fmt.Errorf("ldap Search err: %s", e.Error())
  215. return
  216. }
  217. // 验证结果
  218. if len(sr.Entries) != 1 {
  219. utils.FileLog.Info("ldap check fail: user does not exist or too many entries returned")
  220. return
  221. }
  222. pass = true
  223. return
  224. }