user_login.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_api/models"
  6. "eta/eta_api/models/company"
  7. "eta/eta_api/models/system"
  8. "eta/eta_api/utils"
  9. "fmt"
  10. "io"
  11. "net/http"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. // SendAdminMobileVerifyCode 发送用户手机验证码
  17. func SendAdminMobileVerifyCode(source int, mobile, areaCode string) (ok bool, err error) {
  18. verifyCode := utils.GetRandDigit(6)
  19. record := new(system.AdminVerifyCodeRecord)
  20. record.VerifyType = system.AdminVerifyCodeRecordTypeMobile
  21. record.Mobile = mobile
  22. record.Source = source
  23. record.Code = verifyCode
  24. record.ExpiredTime = time.Now().Add(utils.VerifyCodeExpireMinute * time.Minute)
  25. record.CreateTime = time.Now().Local()
  26. record.ModifyTime = time.Now().Local()
  27. if e := record.Create(); e != nil {
  28. err = fmt.Errorf("新增验证码记录失败, Err: %s", e.Error())
  29. return
  30. }
  31. // 获取配置好的短信模版
  32. smsCond := ` AND conf_key in (?,?) `
  33. smsPars := make([]interface{}, 0)
  34. smsPars = append(smsPars, "LoginSmsTpId", "LoginSmsGjTpId")
  35. conf := new(models.BusinessConf)
  36. confList, e := conf.GetItemsByCondition(smsCond, smsPars, []string{"conf_key", "conf_val"}, "")
  37. if e != nil {
  38. if e.Error() == utils.ErrNoRow() {
  39. err = fmt.Errorf("请先配置短信模版")
  40. return
  41. }
  42. err = fmt.Errorf("获取短信模版失败, Err: %s", e.Error())
  43. return
  44. }
  45. tplId := ""
  46. gjTplId := ""
  47. for _, v := range confList {
  48. if v.ConfKey == "LoginSmsTpId" {
  49. tplId = v.ConfVal
  50. } else if v.ConfKey == "LoginSmsGjTpId" {
  51. gjTplId = v.ConfVal
  52. }
  53. }
  54. if tplId == "" {
  55. err = fmt.Errorf("请先配置短信模版")
  56. return
  57. }
  58. if areaCode == "86" {
  59. ok = SendSmsCode(mobile, verifyCode, tplId)
  60. } else {
  61. if gjTplId == "" {
  62. err = fmt.Errorf("请先配置国际短信模版")
  63. return
  64. }
  65. ok = SendSmsCodeGj(mobile, verifyCode, areaCode, gjTplId)
  66. }
  67. record.SendStatus = system.AdminVerifyCodeRecordStatusSuccess
  68. if !ok {
  69. record.SendStatus = system.AdminVerifyCodeRecordStatusFail
  70. }
  71. cols := []string{"SendStatus"}
  72. if e := record.Update(cols); e != nil {
  73. err = fmt.Errorf("更新验证码记录失败, Err: %s", e.Error())
  74. }
  75. return
  76. }
  77. // SendAdminEmailVerifyCode 发送用户邮箱验证码
  78. func SendAdminEmailVerifyCode(source int, email string) (ok bool, err error) {
  79. verifyCode := utils.GetRandDigit(6)
  80. record := new(system.AdminVerifyCodeRecord)
  81. record.VerifyType = system.AdminVerifyCodeRecordTypeEmail
  82. record.Email = email
  83. record.Source = source
  84. record.Code = verifyCode
  85. record.ExpiredTime = time.Now().Add(utils.VerifyCodeExpireMinute * time.Minute)
  86. record.CreateTime = time.Now().Local()
  87. record.ModifyTime = time.Now().Local()
  88. if e := record.Create(); e != nil {
  89. err = fmt.Errorf("新增验证码记录失败, Err: %s", e.Error())
  90. return
  91. }
  92. // 获取邮件配置
  93. authKey := "english_report_email_conf"
  94. emailConf, e := company.GetConfigDetailByCode(authKey)
  95. if e != nil {
  96. err = fmt.Errorf("获取群发邮件权限失败, Err: %s", e.Error())
  97. return
  98. }
  99. if emailConf.ConfigValue == "" {
  100. err = fmt.Errorf("邮件配置为空, 不可推送")
  101. return
  102. }
  103. conf := new(models.EnglishReportEmailConf)
  104. if e = json.Unmarshal([]byte(emailConf.ConfigValue), &conf); e != nil {
  105. err = fmt.Errorf("邮件配置有误, 不可推送")
  106. return
  107. }
  108. // 获取邮箱模板
  109. // 获取配置好的短信模版
  110. cond := ` AND (conf_key = ? OR conf_key = ?)`
  111. pars := make([]interface{}, 0)
  112. pars = append(pars, "LoginEmailTemplateSubject", "LoginEmailTemplateContent")
  113. busiConf := new(models.BusinessConf)
  114. emailConfList, e := busiConf.GetItemsByCondition(cond, pars, []string{"conf_key, conf_val"}, "")
  115. if e != nil {
  116. if e.Error() == utils.ErrNoRow() {
  117. err = fmt.Errorf("请先配置邮件模版")
  118. return
  119. }
  120. err = fmt.Errorf("获取邮件模版失败, Err: %s", e.Error())
  121. return
  122. }
  123. var emaiContent, emailSubject string
  124. for _, v := range emailConfList {
  125. if v.ConfKey == "LoginEmailTemplateContent" {
  126. emaiContent = v.ConfVal
  127. } else if v.ConfKey == "LoginEmailTemplateSubject" {
  128. emailSubject = v.ConfVal
  129. }
  130. }
  131. if emailSubject == "" {
  132. err = fmt.Errorf("请先配置邮件模版主题")
  133. return
  134. }
  135. if emaiContent == "" {
  136. err = fmt.Errorf("请先配置邮件模版内容")
  137. return
  138. }
  139. req := new(EnglishReportSendEmailRequest)
  140. req.Subject = emailSubject
  141. req.Email = email
  142. // todo 发信人昵称
  143. req.FromAlias = conf.FromAlias
  144. // 填充模板
  145. t := time.Now().Format("2006年01月02日")
  146. ct := emaiContent
  147. ct = strings.Replace(ct, "{{VERIFY_CODE}}", verifyCode, 1)
  148. ct = strings.Replace(ct, "{{EXPIRED_MINUTE}}", strconv.Itoa(utils.VerifyCodeExpireMinute), 1)
  149. ct = strings.Replace(ct, "{{DATE_TIME}}", t, 1)
  150. req.HtmlBody = ct
  151. aliEmail := new(AliyunEmail)
  152. o, result, e := aliEmail.SendEmail(req)
  153. if e != nil {
  154. err = fmt.Errorf("邮箱推送失败, Err: %s", e.Error())
  155. return
  156. }
  157. ok = o
  158. record.SendStatus = system.AdminVerifyCodeRecordStatusSuccess
  159. if !ok {
  160. record.SendStatus = system.AdminVerifyCodeRecordStatusFail
  161. }
  162. record.SendResult = result
  163. cols := []string{"SendStatus", "SendResult"}
  164. if e = record.Update(cols); e != nil {
  165. err = fmt.Errorf("更新验证码记录失败, Err: %s", e.Error())
  166. }
  167. return
  168. }
  169. func ThirdLogin(req map[string]interface{}) (data GetCrmTokenData, err error, errMsg string) {
  170. if utils.BusinessCode == utils.BusinessCodeRelease || utils.BusinessCode == utils.BusinessCodeSandbox {
  171. authCode, ok := req["AuthCode"]
  172. if !ok {
  173. errMsg = "参数有误"
  174. err = errors.New("参数缺失, AuthCode")
  175. return
  176. }
  177. authCodeStr := fmt.Sprint(authCode)
  178. if authCodeStr == "" {
  179. errMsg = "参数有误"
  180. err = errors.New("参数缺失, AuthCode")
  181. return
  182. }
  183. data, err = CodeLoginFromMiddleServer(authCodeStr)
  184. return
  185. }
  186. data, err = ThirdCodeLoginFromMiddleServer(req)
  187. // 普通的第三方
  188. return
  189. }
  190. // ThirdCodeLoginFromMiddleServer 中间服务-编码登录
  191. func ThirdCodeLoginFromMiddleServer(param map[string]interface{}) (tokenResp GetCrmTokenData, err error) {
  192. data, e := json.Marshal(param)
  193. if e != nil {
  194. err = fmt.Errorf("data json marshal err: %s", e.Error())
  195. return
  196. }
  197. body := io.NopCloser(strings.NewReader(string(data)))
  198. client := &http.Client{}
  199. req, e := http.NewRequest("POST", utils.EtaBridgeLoginUrl, body)
  200. if e != nil {
  201. err = fmt.Errorf("http create request err: %s", e.Error())
  202. return
  203. }
  204. contentType := "application/json;charset=utf-8"
  205. req.Header.Set("Content-Type", contentType)
  206. req.Header.Set("Authorization", utils.CrmEtaAuthorization)
  207. resp, e := client.Do(req)
  208. if e != nil {
  209. err = fmt.Errorf("http client do err: %s", e.Error())
  210. return
  211. }
  212. defer func() {
  213. _ = resp.Body.Close()
  214. }()
  215. b, e := io.ReadAll(resp.Body)
  216. if e != nil {
  217. err = fmt.Errorf("resp body read err: %s", e.Error())
  218. return
  219. }
  220. if len(b) == 0 {
  221. err = fmt.Errorf("resp body is empty")
  222. return
  223. }
  224. // 生产环境解密, 注意有个坑前后的双引号
  225. if utils.RunMode == "release" {
  226. str := string(b)
  227. str = strings.Trim(str, `"`)
  228. b = utils.DesBase64Decrypt([]byte(str), utils.DesKey)
  229. }
  230. result := new(GetCrmTokenDataResp)
  231. if e = json.Unmarshal(b, &result); e != nil {
  232. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  233. return
  234. }
  235. if result.Code != 200 {
  236. err = fmt.Errorf("result: %s", string(b))
  237. return
  238. }
  239. tokenResp = result.Data
  240. return
  241. }