user_pc.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/mojocn/base64Captcha"
  6. "hongze/hongze_api/services"
  7. "hongze/hongze_api/utils"
  8. "strconv"
  9. "hongze/hongze_api/models"
  10. "image/color"
  11. "time"
  12. )
  13. // @Title 获取图形验证码
  14. // @Description 获取图形验证码
  15. // @Success 200 {object} models.GraphicVerifyCodeResp
  16. // @router /pc/getGraphicVerifyCode [get]
  17. func (this *UserCommonController) GetGraphicVerifyCode() {
  18. br := new(models.BaseResponse).Init()
  19. defer func() {
  20. this.Data["json"] = br
  21. this.ServeJSON()
  22. }()
  23. var configC = base64Captcha.ConfigCharacter{
  24. Height: 26,
  25. Width: 67,
  26. Mode: base64Captcha.CaptchaModeNumber,
  27. ComplexOfNoiseText: base64Captcha.CaptchaComplexLower,
  28. ComplexOfNoiseDot: base64Captcha.CaptchaComplexLower,
  29. IsUseSimpleFont: true,
  30. IsShowHollowLine: false,
  31. IsShowNoiseDot: true,
  32. IsShowNoiseText: true,
  33. IsShowSlimeLine: false,
  34. IsShowSineLine: false,
  35. CaptchaLen: 4,
  36. BgColor: &color.RGBA{255, 255, 255, 1},
  37. }
  38. char := base64Captcha.EngineCharCreate(configC)
  39. base64stringC := base64Captcha.CaptchaWriteToBase64Encoding(char)
  40. verifyId := fmt.Sprintf("%d", time.Now().Unix())
  41. item := new(models.GraphicVerifyCode)
  42. item.VerifyId = verifyId
  43. item.VerifyCode = char.CaptchaItem.VerifyValue
  44. item.CreateTime=time.Now()
  45. err := models.AddGraphicVerifyCode(item)
  46. if err != nil {
  47. br.Msg = "获取失败"
  48. br.ErrMsg = "获取失败,Err:" + err.Error()
  49. return
  50. }
  51. resp := new(models.GraphicVerifyCodeResp)
  52. resp.VerifyId = verifyId
  53. resp.VerifyCode = base64stringC
  54. br.Ret = 200
  55. br.Data = resp
  56. br.Msg = "获取成功"
  57. br.Success = true
  58. }
  59. // @Title 登录
  60. // @Description 登录接口
  61. // @Param request body models.LoginReq true "type json string"
  62. // @Success 200 {object} models.LoginResp
  63. // @router /pc/login [post]
  64. func (this *UserCommonController) Login() {
  65. br := new(models.BaseResponse).Init()
  66. defer func() {
  67. this.Data["json"] = br
  68. this.ServeJSON()
  69. }()
  70. var req models.PcLoginReq
  71. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  72. if err != nil {
  73. br.Msg = "参数解析异常!"
  74. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  75. return
  76. }
  77. newUserId := 0
  78. userId:=0
  79. var isAdd bool
  80. if req.LoginType == 1 {
  81. if req.VerifyId == "" {
  82. br.Msg = "参数错误!"
  83. return
  84. }
  85. if req.VerifyCode == "" {
  86. br.Msg="请输入验证码"
  87. return
  88. }
  89. item,err:=models.GetGraphicVerifyById(req.VerifyId)
  90. if err!=nil {
  91. if err.Error()==utils.ErrNoRow() {
  92. br.Msg = "验证码不存在,请刷新页面!"
  93. return
  94. }
  95. br.Msg = "校验验证码失败!"
  96. return
  97. }
  98. if item.VerifyCode!=req.VerifyCode {
  99. br.Msg = "验证码错误,请重新输入!"
  100. return
  101. }
  102. checkoutTime:=time.Now().Add(15*time.Minute)
  103. if item.CreateTime.After(checkoutTime) {
  104. br.Msg = "验证码过期,请重新输入!"
  105. return
  106. }
  107. //BindMobile(openId, mobile string, userId, loginType int) (err error) {
  108. //newUserId, err = models.BindMobile(openId, req.Mobile, userId, req.LoginType)
  109. } else if req.LoginType == 2 {
  110. if req.Email == "" {
  111. br.ErrMsg = "邮箱不能为空,请输入邮箱"
  112. br.Msg = "邮箱不能为空,请输入邮箱"
  113. return
  114. }
  115. if !utils.ValidateEmailFormatat(req.Email) {
  116. br.ErrMsg = "邮箱格式错误,请重新输入"
  117. br.Msg = "邮箱格式错误,请重新输入"
  118. return
  119. }
  120. wxUser, err := models.GetWxUserItemByEmail(req.Email)
  121. if err!=nil {
  122. if err.Error()==utils.ErrNoRow() {
  123. isAdd=true
  124. }else{
  125. br.Msg = "登陆失败"
  126. br.ErrMsg = "根据邮箱获取用户信息失败,Err:" + err.Error()
  127. return
  128. }
  129. }
  130. if wxUser==nil {
  131. isAdd=true
  132. }else{
  133. userId=wxUser.UserId
  134. }
  135. } else {
  136. br.Msg = "无效的登录方式"
  137. br.ErrMsg = "无效的登录方式,Err:" + err.Error()
  138. return
  139. }
  140. if isAdd {
  141. user := new(models.WxUser)
  142. user.CompanyId = 1
  143. user.CreatedTime = time.Now()
  144. user.FirstLogin = 1
  145. user.Enabled = 1
  146. user.RegisterTime=time.Now()
  147. lastId,err := models.AddWxUser(user)
  148. if err!=nil {
  149. br.Msg = "登录失败"
  150. br.ErrMsg = "登录失败,Err:" + err.Error()
  151. return
  152. }
  153. userId=int(lastId)
  154. }
  155. timeUnix := time.Now().Unix()
  156. timeUnixStr := strconv.FormatInt(timeUnix, 10)
  157. token := utils.MD5(strconv.Itoa(userId)) + utils.MD5(timeUnixStr)
  158. //新增session
  159. {
  160. session := new(models.Session)
  161. session.UserId = userId
  162. session.CreatedTime = time.Now()
  163. session.LastUpdatedTime = time.Now()
  164. session.ExpireTime = time.Now().AddDate(1, 0, 0)
  165. session.AccessToken = token
  166. err = models.AddSession(session)
  167. if err != nil {
  168. br.Msg = "登录失败"
  169. br.ErrMsg = "登录失败,新增用户session信息失败:" + err.Error()
  170. return
  171. }
  172. }
  173. userPermission, err := services.CheckUserPermission(newUserId)
  174. if err != nil {
  175. br.Msg = "登录失败"
  176. br.ErrMsg = "登录失败,判断权限失败:" + err.Error()
  177. return
  178. }
  179. err = models.ModifyFirstLogin(userId)
  180. if err != nil {
  181. br.Msg = "登录失败"
  182. br.ErrMsg = "登录失败,判断权限失败:" + err.Error()
  183. return
  184. }
  185. resp := new(models.LoginResp)
  186. resp.UserId = newUserId
  187. resp.UserPermission = userPermission
  188. resp.Authorization = token
  189. br.Ret = 200
  190. br.Success = true
  191. br.Data = resp
  192. br.Msg = "登录成功"
  193. }
  194. /*
  195. var (
  196. searcher = riot.New("zh")
  197. )
  198. func init() {
  199. fmt.Sprintf("start")
  200. data := types.DocData{Content: `I wonder how, I wonder why
  201. , I wonder where they are`}
  202. data1 := types.DocData{Content: "所以, 你好, 再见"}
  203. data2 := types.DocData{Content: "没有理由"}
  204. data3 := types.DocData{Content: "你好,中国"}
  205. data4 := types.DocData{Content: "晚上好"}
  206. searcher.Index("1", data)
  207. searcher.Index("2", data1)
  208. searcher.IndexDoc("3", data2)
  209. searcher.IndexDoc("4", data3)
  210. searcher.IndexDoc("5", data4)
  211. searcher.Flush()
  212. req := types.SearchReq{Text: "你好"}
  213. search := searcher.Search(req)
  214. jsonStr,_:=json.Marshal(search)
  215. fmt.Println(string(jsonStr))
  216. utils.FileLog.Info("%s",string(jsonStr))
  217. fmt.Sprintf("end")
  218. }
  219. */