user.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_mini_api/models"
  5. "eta/eta_mini_api/models/request"
  6. "eta/eta_mini_api/models/response"
  7. "eta/eta_mini_api/services"
  8. "eta/eta_mini_api/services/wx_app"
  9. "eta/eta_mini_api/utils"
  10. "fmt"
  11. "strconv"
  12. "time"
  13. )
  14. type UserController struct {
  15. BaseCommonController
  16. }
  17. type UserAuthController struct {
  18. BaseAuthController
  19. }
  20. // @Title 用户登录接口
  21. // @Description 用户登录
  22. // @Param request body models.LoginReq true "type json string"
  23. // @Success 200 {object} models.LoginResp
  24. // @router /login [post]
  25. func (this *UserController) Login() {
  26. br := new(models.BaseResponse).Init()
  27. defer func() {
  28. if err := recover(); err != nil {
  29. fmt.Println(err)
  30. }
  31. this.Data["json"] = br
  32. this.ServeJSON()
  33. }()
  34. var req request.LoginReq
  35. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  36. if err != nil {
  37. br.Msg = "参数解析失败"
  38. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  39. return
  40. }
  41. if req.Code == "" {
  42. br.Msg = "授权码不存在"
  43. return
  44. }
  45. userInfo, err := wx_app.GetSession(req.Code)
  46. if err != nil {
  47. br.Msg = "登录失败,请重新尝试"
  48. br.ErrMsg = "用户信息获取失败,系统错误,Err:" + err.Error()
  49. return
  50. }
  51. switch req.LoginType {
  52. case 1:
  53. if req.Phone == "" {
  54. br.Msg = "请输入手机号"
  55. br.ErrMsg = "请输入手机号"
  56. return
  57. }
  58. if req.SmsCode == "" {
  59. br.Msg = "请输入验证码"
  60. br.ErrMsg = "请输入验证码"
  61. return
  62. }
  63. phone := req.AreaCode + req.Phone
  64. item, err := models.GetMsgCode(phone, req.SmsCode)
  65. if err != nil {
  66. if err.Error() == utils.ErrNoRow() {
  67. br.Msg = "验证码错误,请重新输入"
  68. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  69. return
  70. } else {
  71. br.Msg = "验证码错误,请重新输入"
  72. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  73. return
  74. }
  75. }
  76. if item == nil {
  77. br.Msg = "验证码错误,请重新输入"
  78. return
  79. }
  80. case 2:
  81. if req.Email == "" {
  82. br.Msg = "请输入手机号"
  83. br.ErrMsg = "请输入手机号"
  84. return
  85. }
  86. if req.SmsCode == "" {
  87. br.Msg = "请输入验证码"
  88. br.ErrMsg = "请输入验证码"
  89. return
  90. }
  91. item, err := models.GetMsgCode(req.Email, req.SmsCode)
  92. if err != nil {
  93. if err.Error() == utils.ErrNoRow() {
  94. br.Msg = "验证码错误,请重新输入"
  95. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  96. return
  97. } else {
  98. br.Msg = "验证码错误,请重新输入"
  99. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  100. return
  101. }
  102. }
  103. if item == nil {
  104. br.Msg = "验证码错误,请重新输入"
  105. return
  106. }
  107. }
  108. user, errMsg, err := services.BindWxUser(userInfo.UnionID, userInfo.OpenID, req.Phone, req.Email, req.AreaCode)
  109. if err != nil && err.Error() != utils.ErrNoRow() {
  110. br.Msg = "登录失败,系统处理中,请稍后重试"
  111. if errMsg != `` {
  112. br.Msg = errMsg
  113. }
  114. br.ErrMsg = "登录失败:" + err.Error()
  115. return
  116. }
  117. userId := user.UserId
  118. var token string
  119. tokenItem, err := models.GetTokenByOpenId(userInfo.OpenID)
  120. if err != nil && err.Error() != utils.ErrNoRow() {
  121. br.Msg = "登录失败"
  122. br.ErrMsg = "登录失败,获取token失败:" + err.Error()
  123. return
  124. }
  125. if tokenItem == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
  126. timeUnix := time.Now().Unix()
  127. timeUnixStr := strconv.FormatInt(timeUnix, 10)
  128. token := utils.MD5(strconv.Itoa(userId)) + utils.MD5(timeUnixStr)
  129. //新增session
  130. {
  131. session := new(models.WxSession)
  132. session.OpenId = userInfo.OpenID
  133. session.UserId = userId
  134. session.CreateTime = time.Now()
  135. session.LastUpdateTime = time.Now()
  136. session.ExpireTime = time.Now().AddDate(0, 3, 0)
  137. session.AccessToken = token
  138. err = session.AddWxSession()
  139. if err != nil {
  140. br.Msg = "登录失败"
  141. br.ErrMsg = "登录失败,新增用户session信息失败:" + err.Error()
  142. return
  143. }
  144. }
  145. } else {
  146. token = tokenItem.AccessToken
  147. _ = models.UpdateSession(tokenItem.WxSessionId, userId, time.Now().AddDate(0, 1, 0))
  148. }
  149. resp := new(response.LoginResp)
  150. resp.UserId = userId
  151. resp.Authorization = token
  152. br.Data = resp
  153. br.Msg = "登录成功"
  154. br.Success = true
  155. br.Ret = 200
  156. }
  157. // @Title 获取短信/邮箱验证码
  158. // @Description 用户登录
  159. // @Param request body models.LoginReq true "type json string"
  160. // @Success 200 {object} models.LoginResp
  161. // @router /getVerifyCode [post]
  162. func (this *UserController) GetVerifyCode() {
  163. br := new(models.BaseResponse).Init()
  164. defer func() {
  165. this.Data["json"] = br
  166. this.ServeJSON()
  167. }()
  168. var req request.VerifyCodeReq
  169. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  170. if err != nil {
  171. br.Msg = "参数解析异常!"
  172. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  173. return
  174. }
  175. if req.VerifyType != 1 && req.VerifyType != 2 {
  176. br.Msg = "验证方式有误"
  177. br.ErrMsg = fmt.Sprintf("验证方式异常<%d>", req.VerifyType)
  178. }
  179. code := utils.GetRandDigit(6)
  180. switch req.VerifyType {
  181. case 1:
  182. if req.AreaCode == "" {
  183. br.Msg = "请选择区号"
  184. return
  185. }
  186. if req.Phone == "" {
  187. br.Msg = "请输入手机号"
  188. return
  189. }
  190. if req.AreaCode == utils.TelAreaCodeHome && !utils.ValidateMobileFormatat(req.Phone) {
  191. br.Msg = "您的手机号输入有误, 请检查"
  192. return
  193. }
  194. var ok bool
  195. if req.AreaCode == "86" {
  196. ok = services.SendSmsCode(req.Phone, code)
  197. } else {
  198. ok = services.SendSmsCodeGj(req.Phone, code, req.AreaCode)
  199. }
  200. if !ok {
  201. br.ErrMsg = "短信验证码发送失败"
  202. return
  203. } else {
  204. item := new(models.MsgCode)
  205. item.OpenId = ""
  206. item.Code = code
  207. item.Mobile = req.AreaCode + req.Phone
  208. item.ExpiredIn = time.Now().Add(15 * time.Minute).Unix()
  209. item.CreateTime = time.Now()
  210. err = item.Insert()
  211. if err != nil {
  212. br.Msg = "发送失败"
  213. br.ErrMsg = "发送失败,Err:" + err.Error()
  214. return
  215. }
  216. br.Msg = "发送成功"
  217. }
  218. case 2:
  219. if req.Email == "" {
  220. br.Msg = "请输入邮箱"
  221. }
  222. if !utils.ValidateEmailFormatat(req.Email) {
  223. br.Msg = "您的邮箱格式输入有误, 请检查"
  224. return
  225. }
  226. date := time.Now()
  227. content := "尊敬的用户:</br>本次请求的验证码为:" + code + "(为了保障您账号的安全性,请在15分钟内完成验证。)</br>东吴期货研究团队 </br>" + fmt.Sprintf("%d年%02d月%02d日", date.Year(), date.Month(), date.Day())
  228. title := "东吴期货登录验证"
  229. result, err := utils.SendEmailByDw(title, content, req.Email)
  230. if err != nil {
  231. br.Msg = "发送失败"
  232. br.ErrMsg = "发送失败,Err:" + err.Error()
  233. return
  234. }
  235. if result {
  236. item := new(models.MsgCode)
  237. item.OpenId = ""
  238. item.Code = code
  239. item.Mobile = req.Email
  240. item.ExpiredIn = time.Now().Add(15 * time.Minute).Unix()
  241. item.CreateTime = time.Now()
  242. err = item.Insert()
  243. if err != nil {
  244. br.Msg = "发送失败"
  245. br.ErrMsg = "发送失败,Err:" + err.Error()
  246. return
  247. }
  248. br.Msg = "发送成功"
  249. } else {
  250. br.Msg = "发送失败"
  251. }
  252. }
  253. br.Ret = 200
  254. br.Success = true
  255. br.Msg = "发送成功"
  256. }
  257. // @Title 新增报告浏览记录
  258. // @Description 新增报告浏览记录接口
  259. // @Param request body models.ReportRecordReq true "type json string"
  260. // @Success 200 新增成功
  261. // @router /addReportRecord [post]
  262. func (this *UserAuthController) AddReportRecord() {
  263. br := new(models.BaseResponse).Init()
  264. defer func() {
  265. this.Data["json"] = br
  266. this.ServeJSON()
  267. }()
  268. user := this.User
  269. if user == nil {
  270. br.Msg = "请登录"
  271. br.ErrMsg = "请登录,用户信息为空"
  272. br.Ret = 408
  273. return
  274. }
  275. var req request.ReportRecordReq
  276. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  277. br.Msg = "参数解析异常!"
  278. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  279. return
  280. }
  281. if req.ReportId <= 0 {
  282. br.Msg = "参数错误"
  283. br.ErrMsg = "参数错误,报告id小于等于0"
  284. return
  285. }
  286. items, err := models.GetChartPermissionChapterMappingByReportId(req.ReportId)
  287. if err != nil {
  288. br.Msg = "添加阅读记录失败"
  289. br.ErrMsg = "获取研报品种错误,Err:" + err.Error()
  290. return
  291. }
  292. chartPermissionList, err := services.GetChartPermissionAllList()
  293. if err != nil {
  294. br.Msg = "添加阅读记录失败"
  295. br.ErrMsg = "获取研报品种列表失败,Err:" + err.Error()
  296. return
  297. }
  298. chartMap := make(map[int]*services.ChartPermission)
  299. for _, permission := range chartPermissionList.Data {
  300. chartMap[permission.ChartPermissionId] = permission
  301. }
  302. curTime := time.Now()
  303. insertIds := make([]int64, 0)
  304. if len(req.RecordId) <= 0 {
  305. // 如果不存在就新增一条记录
  306. for _, item := range items {
  307. curPermission := chartMap[item.ChartPermissionId]
  308. userReadRecord := &models.UserReadRecord{
  309. UserId: user.UserId,
  310. ReportId: req.ReportId,
  311. ReportTittle: req.ReportTittle,
  312. ChartPermissionId1: chartMap[curPermission.ParentId].ChartPermissionId,
  313. ChartPermissionId2: curPermission.ChartPermissionId,
  314. ChartPermissionName: curPermission.PermissionName,
  315. ClassifyId1: req.ClassifyIdFirst,
  316. ClassifyName1: req.ClassifyNameFirst,
  317. ClassifyId2: req.ClassifyIdSecond,
  318. ClassifyName2: req.ClassifyNameSecond,
  319. AreaCode: user.AreaCode,
  320. Phone: user.Phone,
  321. Email: user.Email,
  322. RealName: user.RealName,
  323. CompanyName: user.Company,
  324. Timestamp: int(curTime.Unix()),
  325. CreateTime: curTime,
  326. }
  327. insertId, _ := userReadRecord.Insert()
  328. insertIds = append(insertIds, insertId)
  329. }
  330. } else {
  331. // 如果存在就计算停留时间
  332. recordIds := make([]string, 0)
  333. for _, v := range req.RecordId {
  334. recordIds = append(recordIds, strconv.Itoa(v))
  335. }
  336. userRecordList, err := models.GetUserReadRecordListByRcordIds(recordIds)
  337. if err != nil {
  338. br.Msg = "更新阅读记录失败"
  339. br.ErrMsg = "更新阅读记录失败,Err:" + err.Error()
  340. return
  341. }
  342. if len(userRecordList) == 0 {
  343. br.Msg = "更新阅读记录不存在"
  344. return
  345. }
  346. stayTime := curTime.Unix() - int64(userRecordList[0].Timestamp)
  347. stayTimeStr := utils.SecondsToHMS(stayTime)
  348. err = models.UpdateUserReadRecordByRecordIds(recordIds, int(curTime.Unix()), int(stayTime), stayTimeStr)
  349. if err != nil {
  350. br.Msg = "更新阅读记录失败"
  351. br.ErrMsg = "更新阅读记录失败,Err:" + err.Error()
  352. return
  353. }
  354. }
  355. resp := new(response.UserReadRecordResp)
  356. resp.RecordIds = insertIds
  357. br.Msg = "添加阅读记录成功"
  358. br.Ret = 200
  359. br.Success = true
  360. br.Data = resp
  361. }