user.go 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hongze_cygx/models"
  6. "hongze/hongze_cygx/services"
  7. "hongze/hongze_cygx/utils"
  8. "rdluck_tools/paging"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. //用户
  14. type UserController struct {
  15. BaseAuthController
  16. }
  17. /*
  18. // @Title 登录
  19. // @Description 登录接口
  20. // @Param request body models.LoginReq true "type json string"
  21. // @Success 200 {object} models.LoginResp
  22. // @router /login [post]
  23. func (this *UserController) Login() {
  24. br := new(models.BaseResponse).Init()
  25. defer func() {
  26. this.Data["json"] = br
  27. this.ServeJSON()
  28. }()
  29. var req models.LoginReq
  30. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  31. if err != nil {
  32. br.Msg = "参数解析异常!"
  33. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  34. return
  35. }
  36. user := this.User
  37. if user == nil {
  38. br.Msg = "请登录"
  39. br.ErrMsg = "请登录"
  40. br.Ret = 408
  41. return
  42. }
  43. unionId := this.User.UnionId
  44. userId := this.User.UserId
  45. if unionId == "" {
  46. br.Msg = "参数错误"
  47. br.ErrMsg = "参数错误,unionId 为空"
  48. return
  49. }
  50. newUserId := 0
  51. if req.LoginType == 1 || req.LoginType == 3 {
  52. if req.Mobile == "" {
  53. br.Msg = "参数错误"
  54. br.ErrMsg = "参数错误,手机号为空 为空"
  55. return
  56. }
  57. if req.LoginType == 3 {
  58. item, err := models.GetMsgCode(req.Mobile, req.VCode)
  59. if err != nil {
  60. if err.Error() == utils.ErrNoRow() {
  61. br.Msg = "验证码错误,请重新输入"
  62. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  63. return
  64. } else {
  65. br.Msg = "验证码错误,请重新输入"
  66. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  67. return
  68. }
  69. }
  70. if item == nil {
  71. br.Msg = "验证码错误,请重新输入"
  72. return
  73. }
  74. }
  75. //BindMobile(openId, mobile string, userId, loginType int) (err error) {
  76. req.Mobile = strings.Trim(req.Mobile, " ")
  77. req.LoginType = 1
  78. newUserId, err = models.PcBindMobile(unionId, req.Mobile, userId, req.LoginType)
  79. } else if req.LoginType == 2 {
  80. if req.Email == "" {
  81. br.ErrMsg = "邮箱不能为空,请输入邮箱"
  82. br.Msg = "邮箱不能为空,请输入邮箱"
  83. return
  84. }
  85. if !utils.ValidateEmailFormatat(req.Email) {
  86. br.ErrMsg = "邮箱格式错误,请重新输入"
  87. br.Msg = "邮箱格式错误,请重新输入"
  88. return
  89. }
  90. item, err := models.GetMsgCode(req.Email, req.VCode)
  91. if err != nil {
  92. if err.Error() == utils.ErrNoRow() {
  93. br.Msg = "验证码错误,请重新输入"
  94. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  95. return
  96. } else {
  97. br.Msg = "验证码错误,请重新输入"
  98. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  99. return
  100. }
  101. }
  102. if item == nil {
  103. br.Msg = "验证码错误,请重新输入"
  104. return
  105. }
  106. newUserId, err = models.PcBindMobile(unionId, req.Email, userId, req.LoginType)
  107. } else {
  108. br.Msg = "无效的登录方式"
  109. br.ErrMsg = "无效的登录方式,Err:" + err.Error()
  110. return
  111. }
  112. var token string
  113. tokenItem, err := models.GetTokenByUid(newUserId)
  114. if err != nil && err.Error() != utils.ErrNoRow() {
  115. br.Msg = "登录失败"
  116. br.ErrMsg = "登录失败,获取token失败:" + err.Error()
  117. return
  118. }
  119. if tokenItem == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
  120. timeUnix := time.Now().Unix()
  121. timeUnixStr := strconv.FormatInt(timeUnix, 10)
  122. token = utils.MD5(strconv.Itoa(userId)) + utils.MD5(timeUnixStr)
  123. //新增session
  124. {
  125. session := new(models.CygxSession)
  126. session.OpenId = unionId
  127. session.UnionId = unionId
  128. session.UserId = userId
  129. session.CreatedTime = time.Now()
  130. session.LastUpdatedTime = time.Now()
  131. session.ExpireTime = time.Now().AddDate(0, 1, 0)
  132. session.AccessToken = token
  133. err = models.AddSession(session)
  134. if err != nil {
  135. br.Msg = "登录失败"
  136. br.ErrMsg = "登录失败,新增用户session信息失败:" + err.Error()
  137. return
  138. }
  139. }
  140. } else {
  141. token = tokenItem.AccessToken
  142. }
  143. //新增登录日志
  144. {
  145. loginLog := new(models.WxUserLog)
  146. loginLog.UserId = userId
  147. loginLog.OpenId = unionId
  148. loginLog.Mobile = req.Mobile
  149. loginLog.Email = req.Email
  150. loginLog.CreateTime = time.Now()
  151. loginLog.Handle = "wechat_user_login"
  152. loginLog.Remark = token
  153. go models.AddWxUserLog(loginLog)
  154. }
  155. resp := new(models.LoginResp)
  156. resp.UserId = newUserId
  157. resp.Authorization = token
  158. br.Ret = 200
  159. br.Success = true
  160. br.Data = resp
  161. br.Msg = "登录成功"
  162. }
  163. */
  164. // @Title 登录
  165. // @Description 登录接口
  166. // @Param request body models.LoginReq true "type json string"
  167. // @Success 200 {object} models.LoginResp
  168. // @router /login [post]
  169. func (this *UserController) Login() {
  170. br := new(models.BaseResponse).Init()
  171. defer func() {
  172. this.Data["json"] = br
  173. this.ServeJSON()
  174. }()
  175. var req models.LoginReq
  176. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  177. if err != nil {
  178. br.Msg = "参数解析异常!"
  179. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  180. return
  181. }
  182. user := this.User
  183. if user == nil {
  184. br.Msg = "请登录"
  185. br.ErrMsg = "请登录"
  186. br.Ret = 408
  187. return
  188. }
  189. unionId := this.User.UnionId
  190. openId := this.User.OpenId
  191. if unionId == "" {
  192. br.Msg = "参数错误"
  193. br.ErrMsg = "参数错误,unionId 为空"
  194. return
  195. }
  196. if req.LoginType == 1 || req.LoginType == 3 {
  197. if req.Mobile == "" {
  198. br.Msg = "参数错误"
  199. br.ErrMsg = "参数错误,手机号为空 为空"
  200. return
  201. }
  202. if req.LoginType == 3 {
  203. item, err := models.GetMsgCode(req.Mobile, req.VCode)
  204. if err != nil {
  205. if err.Error() == utils.ErrNoRow() {
  206. br.Msg = "验证码错误,请重新输入"
  207. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  208. return
  209. } else {
  210. br.Msg = "验证码错误,请重新输入"
  211. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  212. return
  213. }
  214. }
  215. if item == nil {
  216. br.Msg = "验证码错误,请重新输入"
  217. return
  218. }
  219. }
  220. req.Mobile = strings.Trim(req.Mobile, " ")
  221. } else if req.LoginType == 2 {
  222. if req.Email == "" {
  223. br.ErrMsg = "邮箱不能为空,请输入邮箱"
  224. br.Msg = "邮箱不能为空,请输入邮箱"
  225. return
  226. }
  227. if !utils.ValidateEmailFormatat(req.Email) {
  228. br.ErrMsg = "邮箱格式错误,请重新输入"
  229. br.Msg = "邮箱格式错误,请重新输入"
  230. return
  231. }
  232. item, err := models.GetMsgCode(req.Email, req.VCode)
  233. if err != nil {
  234. if err.Error() == utils.ErrNoRow() {
  235. br.Msg = "验证码错误,请重新输入"
  236. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  237. return
  238. } else {
  239. br.Msg = "验证码错误,请重新输入"
  240. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  241. return
  242. }
  243. }
  244. if item == nil {
  245. br.Msg = "验证码错误,请重新输入"
  246. return
  247. }
  248. } else {
  249. br.Msg = "无效的登录方式"
  250. br.ErrMsg = "无效的登录方式,Err:" + err.Error()
  251. return
  252. }
  253. user, err = services.BindWxUser(openId, req.Mobile, req.Email, req.CountryCode)
  254. userId := user.UserId
  255. var token string
  256. tokenItem, err := models.GetTokenByOpenId(openId)
  257. if err != nil && err.Error() != utils.ErrNoRow() {
  258. br.Msg = "登录失败"
  259. br.ErrMsg = "登录失败,获取token失败:" + err.Error()
  260. return
  261. }
  262. if tokenItem == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
  263. timeUnix := time.Now().Unix()
  264. timeUnixStr := strconv.FormatInt(timeUnix, 10)
  265. token = utils.MD5(openId) + utils.MD5(timeUnixStr)
  266. //新增session
  267. {
  268. session := new(models.CygxSession)
  269. session.OpenId = unionId
  270. session.UnionId = unionId
  271. session.UserId = userId
  272. session.CreatedTime = time.Now()
  273. session.LastUpdatedTime = time.Now()
  274. session.ExpireTime = time.Now().AddDate(0, 1, 0)
  275. session.AccessToken = token
  276. err = models.AddSession(session)
  277. if err != nil {
  278. br.Msg = "登录失败"
  279. br.ErrMsg = "登录失败,新增用户session信息失败:" + err.Error()
  280. return
  281. }
  282. }
  283. } else {
  284. token = tokenItem.AccessToken
  285. }
  286. //新增登录日志
  287. {
  288. loginLog := new(models.WxUserLog)
  289. loginLog.UserId = userId
  290. loginLog.OpenId = unionId
  291. loginLog.Mobile = req.Mobile
  292. loginLog.Email = req.Email
  293. loginLog.CreateTime = time.Now()
  294. loginLog.Handle = "wechat_user_login"
  295. loginLog.Remark = token
  296. go models.AddWxUserLog(loginLog)
  297. }
  298. resp := new(models.LoginResp)
  299. resp.UserId = userId
  300. resp.Authorization = token
  301. br.Ret = 200
  302. br.Success = true
  303. br.Data = resp
  304. br.Msg = "登录成功"
  305. }
  306. // @Title 获取用户详情
  307. // @Description 获取用户详情接口
  308. // @Success 200 {object} models.UserDetail
  309. // @router /detail [get]
  310. func (this *UserController) Detail() {
  311. br := new(models.BaseResponse).Init()
  312. defer func() {
  313. this.Data["json"] = br
  314. this.ServeJSON()
  315. }()
  316. user := this.User
  317. if user == nil {
  318. br.Msg = "请登录"
  319. br.ErrMsg = "请登录,用户信息为空"
  320. br.Ret = 408
  321. return
  322. }
  323. uid := user.UserId
  324. fmt.Println("uid:", uid)
  325. var hasPermission int
  326. detail := new(models.UserDetail)
  327. if uid > 0 {
  328. var err error
  329. detail, err = models.GetUserDetailByUserId(uid)
  330. if err != nil && err.Error() != utils.ErrNoRow() {
  331. br.Msg = "获取信息失败"
  332. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  333. return
  334. }
  335. userRecord, _ := models.GetUserRecordByUserId(uid, utils.WxPlatform)
  336. if userRecord != nil {
  337. detail.NickName = userRecord.NickName
  338. detail.Headimgurl = userRecord.Headimgurl
  339. if detail.Headimgurl == "" {
  340. userRecord, _ = models.GetUserRecordByUserId(uid, 1)
  341. if userRecord != nil {
  342. detail.NickName = userRecord.NickName
  343. detail.Headimgurl = userRecord.Headimgurl
  344. }
  345. }
  346. }
  347. if user.CompanyId > 1 {
  348. companyItem, err := models.GetCompanyDetailAllById(user.CompanyId)
  349. if err != nil && err.Error() != utils.ErrNoRow() {
  350. br.Msg = "获取信息失败"
  351. br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
  352. return
  353. }
  354. if companyItem != nil && companyItem.CompanyId > 0 {
  355. detail.CompanyName = companyItem.CompanyName
  356. if companyItem.Status == "试用" || companyItem.Status == "永续" || companyItem.Status == "正式" {
  357. permissionStr, err := models.GetCompanyPermission(companyItem.CompanyId)
  358. if err != nil {
  359. br.Msg = "获取信息失败"
  360. br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
  361. return
  362. }
  363. detail.PermissionName = permissionStr
  364. } else {
  365. hasPermission = 1
  366. }
  367. detail.SellerName = companyItem.SellerName
  368. detail.SellerMobile = companyItem.Mobile
  369. } else {
  370. hasPermission = 1
  371. }
  372. } else {
  373. //判断是否已经申请过
  374. applyCount, err := models.GetApplyRecordCount(uid)
  375. if err != nil && err.Error() != utils.ErrNoRow() {
  376. br.Msg = "获取信息失败"
  377. br.ErrMsg = "判断是否已申请过试用失败,Err:" + err.Error()
  378. return
  379. }
  380. if applyCount > 0 {
  381. hasPermission = 3
  382. } else {
  383. hasPermission = 2
  384. }
  385. detail.CompanyName = detail.Note
  386. }
  387. detail.HasPermission = hasPermission
  388. } else {
  389. ur, _ := models.GetUserRecordByOpenId(user.OpenId)
  390. if ur != nil {
  391. detail.NickName = ur.NickName
  392. detail.Email = ur.BindAccount
  393. detail.Mobile = ur.BindAccount
  394. detail.NickName = ur.NickName
  395. detail.Headimgurl = ur.Headimgurl
  396. }
  397. hasPermission = 2
  398. detail.HasPermission = hasPermission
  399. }
  400. br.Ret = 200
  401. br.Success = true
  402. br.Msg = "获取成功"
  403. br.Data = detail
  404. }
  405. // @Title 校验用户状态信息
  406. // @Description 校验用户状态信息
  407. // @Success 200 {object} models.CheckStatusResp
  408. // @router /check/status [get]
  409. func (this *UserController) CheckLogin() {
  410. br := new(models.BaseResponse).Init()
  411. defer func() {
  412. this.Data["json"] = br
  413. this.ServeJSON()
  414. }()
  415. user := this.User
  416. if user == nil {
  417. br.Msg = "请登录"
  418. br.ErrMsg = "请登录"
  419. br.Ret = 408
  420. return
  421. }
  422. uid := user.UserId
  423. resp := new(models.CheckStatusResp)
  424. if uid > 0 {
  425. //判断token是否过期
  426. userRecord, err := models.GetUserRecordByUserId(uid, utils.WxPlatform)
  427. if err != nil {
  428. br.Msg = "获取用户信息失败"
  429. br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
  430. return
  431. }
  432. permissionStr, err := models.GetCompanyPermission(user.CompanyId)
  433. if err != nil {
  434. br.Msg = "获取信息失败"
  435. br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
  436. return
  437. }
  438. resp.PermissionName = permissionStr
  439. if user.Mobile == "" && user.Email == "" {
  440. resp.IsBind = true
  441. }
  442. if userRecord.UnionId == "" {
  443. resp.IsAuth = true
  444. }
  445. } else {
  446. resp.IsBind = true
  447. if user.UnionId == "" {
  448. resp.IsAuth = true
  449. }
  450. resp.PermissionName = ""
  451. }
  452. br.Success = true
  453. br.Msg = "获取成功"
  454. br.Data = resp
  455. br.Ret = 200
  456. }
  457. //
  458. //// @Title 绑定手机号或邮箱
  459. //// @Description 绑定手机号或邮箱
  460. //// @Param request body models.WxGetPhoneNumberReq true "type json string"
  461. //// @Success 200 {object} models.WxGetPhoneNumberResp
  462. //// @router /bind [post]
  463. //func (this *WechatController) Bind() {
  464. // br := new(models.BaseResponse).Init()
  465. // defer func() {
  466. // this.Data["json"] = br
  467. // this.ServeJSON()
  468. // }()
  469. // var req models.WxGetPhoneNumberReq
  470. // err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  471. // if err != nil {
  472. // br.Msg = "参数解析异常!"
  473. // br.ErrMsg = "参数解析失败,Err:" + err.Error()
  474. // return
  475. // }
  476. // if req.EncryptedData == "" || req.Iv == "" {
  477. // br.Msg = "参数错误"
  478. // return
  479. // }
  480. // user := this.User
  481. // if user == nil {
  482. // br.Msg = "请登陆"
  483. // br.Ret = 408
  484. // return
  485. // }
  486. // sessionKey := user.SessionKey
  487. // wxMobile, err := weapp.DecryptMobile(sessionKey, req.EncryptedData, req.Iv)
  488. // if err != nil {
  489. // br.Msg = "解析用户手机号信息失败"
  490. // br.ErrMsg = "解析用户手机号信息失败,Err:" + err.Error()
  491. // return
  492. // }
  493. // err = models.ModifyUsersMobile(user.UserId, wxMobile.PurePhoneNumber)
  494. // if err != nil {
  495. // br.Msg = "获取失败"
  496. // br.ErrMsg = "获取失败,Err:" + err.Error()
  497. // return
  498. // }
  499. // resp := new(models.WxGetPhoneNumberResp)
  500. // resp.Authorization = this.Token
  501. // resp.PhoneNumber = wxMobile.PhoneNumber
  502. // resp.PurePhoneNumber = wxMobile.PurePhoneNumber
  503. // resp.CountryCode = wxMobile.CountryCode
  504. // br.Msg = "获取成功!"
  505. // br.Ret = 200
  506. // br.Success = true
  507. // br.Data = resp
  508. //}
  509. // @Title 获取我的收藏
  510. // @Description 获取我的收藏列表
  511. // @Param PageSize query int true "PageSize"
  512. // @Param CurrentIndex query int true "CurrentIndex"
  513. // @Success 200 {object} models.ArticleCollectListResp
  514. // @router /collect/list [get]
  515. func (this *UserController) CollectList() {
  516. br := new(models.BaseResponse).Init()
  517. defer func() {
  518. this.Data["json"] = br
  519. this.ServeJSON()
  520. }()
  521. userId := this.User.UserId
  522. var pageSize, currentIndex, startSize int
  523. pageSize, _ = this.GetInt("PageSize")
  524. currentIndex, _ = this.GetInt("CurrentIndex")
  525. if pageSize <= 0 {
  526. pageSize = utils.PageSize20
  527. }
  528. if currentIndex <= 0 {
  529. currentIndex = 1
  530. }
  531. startSize = utils.StartIndex(currentIndex, pageSize)
  532. total, err := models.GetArticleUserCollectCount(userId)
  533. if err != nil {
  534. br.Msg = "获取数据失败"
  535. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  536. return
  537. }
  538. list, err := models.GetArticleUserCollectList(startSize, pageSize, userId)
  539. if err != nil {
  540. br.Msg = "获取数据失败"
  541. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  542. return
  543. }
  544. var articleIds []string
  545. for _, v := range list {
  546. articleIds = append(articleIds, strconv.Itoa(v.ArticleId))
  547. }
  548. articleIdStr := strings.Join(articleIds, ",")
  549. articleMap := make(map[int]*models.ArticleDetail)
  550. if articleIdStr != "" {
  551. articleList, err := models.GetArticleDetailByIdStr(articleIdStr)
  552. if err != nil {
  553. br.Msg = "获取数据失败"
  554. br.ErrMsg = "获取报告详情信息失败,Err:" + err.Error()
  555. return
  556. }
  557. for _, v := range articleList {
  558. if _, ok := articleMap[v.ArticleId]; !ok {
  559. articleMap[v.ArticleId] = v
  560. }
  561. }
  562. }
  563. lenList := len(list)
  564. for i := 0; i < lenList; i++ {
  565. item := list[i]
  566. article := articleMap[item.ArticleId]
  567. list[i].Title = article.Title
  568. list[i].TitleEn = article.TitleEn
  569. list[i].UpdateFrequency = article.UpdateFrequency
  570. list[i].CreateDate = article.CreateDate
  571. list[i].PublishDate = article.PublishDate
  572. list[i].Body, _ = services.GetReportContentTextSub(article.Body)
  573. list[i].Abstract = article.Abstract
  574. list[i].CategoryName = article.CategoryName
  575. list[i].SubCategoryName = article.SubCategoryName
  576. }
  577. page := paging.GetPaging(currentIndex, pageSize, total)
  578. resp := new(models.ArticleCollectListResp)
  579. resp.List = list
  580. resp.Paging = page
  581. br.Msg = "获取成功!"
  582. br.Ret = 200
  583. br.Success = true
  584. br.Data = resp
  585. }
  586. // @Title 获取申请访谈列表
  587. // @Description 获取申请访谈列表
  588. // @Param PageSize query int true "PageSize"
  589. // @Param CurrentIndex query int true "CurrentIndex"
  590. // @Success 200 {object} models.ArticleInterviewApplyListResp
  591. // @router /interview/apply/list [get]
  592. func (this *UserController) InterviewApplyList() {
  593. br := new(models.BaseResponse).Init()
  594. defer func() {
  595. this.Data["json"] = br
  596. this.ServeJSON()
  597. }()
  598. userId := this.User.UserId
  599. var pageSize, currentIndex, startSize int
  600. pageSize, _ = this.GetInt("PageSize")
  601. currentIndex, _ = this.GetInt("CurrentIndex")
  602. if pageSize <= 0 {
  603. pageSize = utils.PageSize20
  604. }
  605. if currentIndex <= 0 {
  606. currentIndex = 1
  607. }
  608. startSize = utils.StartIndex(currentIndex, pageSize)
  609. total, err := models.GetArticleUserInterviewApplyCount(userId)
  610. if err != nil {
  611. br.Msg = "获取数据失败"
  612. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  613. return
  614. }
  615. list, err := models.GetArticleUserInterviewApplyList(startSize, pageSize, userId)
  616. if err != nil {
  617. br.Msg = "获取数据失败"
  618. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  619. return
  620. }
  621. var articleIds []string
  622. for _, v := range list {
  623. articleIds = append(articleIds, strconv.Itoa(v.ArticleId))
  624. }
  625. articleIdStr := strings.Join(articleIds, ",")
  626. articleMap := make(map[int]*models.ArticleDetail)
  627. if articleIdStr != "" {
  628. articleList, err := models.GetArticleDetailByIdStr(articleIdStr)
  629. if err != nil {
  630. br.Msg = "获取数据失败"
  631. br.ErrMsg = "获取报告详情信息失败,Err:" + err.Error()
  632. return
  633. }
  634. for _, v := range articleList {
  635. if _, ok := articleMap[v.ArticleId]; !ok {
  636. articleMap[v.ArticleId] = v
  637. }
  638. }
  639. }
  640. lenList := len(list)
  641. for i := 0; i < lenList; i++ {
  642. item := list[i]
  643. article := articleMap[item.ArticleId]
  644. bodySub, _ := services.GetReportContentTextSub(article.Body)
  645. list[i].Title = article.Title
  646. list[i].TitleEn = article.TitleEn
  647. list[i].UpdateFrequency = article.UpdateFrequency
  648. list[i].CreateDate = article.CreateDate
  649. list[i].PublishDate = article.PublishDate
  650. list[i].Body = bodySub
  651. list[i].Abstract = article.Abstract
  652. list[i].CategoryName = article.CategoryName
  653. list[i].SubCategoryName = article.SubCategoryName
  654. list[i].ExpertBackground = article.ExpertBackground
  655. list[i].ExpertNumber = article.ExpertNumber
  656. }
  657. page := paging.GetPaging(currentIndex, pageSize, total)
  658. resp := new(models.ArticleInterviewApplyListResp)
  659. resp.List = list
  660. resp.Paging = page
  661. br.Msg = "获取成功!"
  662. br.Ret = 200
  663. br.Success = true
  664. br.Data = resp
  665. }
  666. // @Title 获取浏览历史列表
  667. // @Description 获取浏览历史列表
  668. // @Param PageSize query int true "PageSize"
  669. // @Param CurrentIndex query int true "CurrentIndex"
  670. // @Success 200 {object} models.ArticleBrowseHistoryListResp
  671. // @router /browse/history/list [get]
  672. func (this *UserController) BrowseHistoryList() {
  673. br := new(models.BaseResponse).Init()
  674. defer func() {
  675. this.Data["json"] = br
  676. this.ServeJSON()
  677. }()
  678. userId := this.User.UserId
  679. var pageSize, currentIndex, startSize int
  680. pageSize, _ = this.GetInt("PageSize")
  681. currentIndex, _ = this.GetInt("CurrentIndex")
  682. if pageSize <= 0 {
  683. pageSize = utils.PageSize20
  684. }
  685. if currentIndex <= 0 {
  686. currentIndex = 1
  687. }
  688. startSize = utils.StartIndex(currentIndex, pageSize)
  689. endDate := time.Now().AddDate(0, -1, 0).Format(utils.FormatDate)
  690. total, err := models.GetArticleUserBrowseHistoryCount(userId, endDate)
  691. if err != nil {
  692. br.Msg = "获取数据失败"
  693. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  694. return
  695. }
  696. list, err := models.GetArticleUserBrowseHistoryList(startSize, pageSize, userId, endDate)
  697. if err != nil {
  698. br.Msg = "获取数据失败"
  699. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  700. return
  701. }
  702. var articleIds []string
  703. for _, v := range list {
  704. articleIds = append(articleIds, strconv.Itoa(v.ArticleId))
  705. }
  706. articleIdStr := strings.Join(articleIds, ",")
  707. articleMap := make(map[int]*models.ArticleDetail)
  708. if articleIdStr != "" {
  709. articleList, err := models.GetArticleDetailByIdStr(articleIdStr)
  710. if err != nil {
  711. br.Msg = "获取数据失败"
  712. br.ErrMsg = "获取报告详情信息失败,Err:" + err.Error()
  713. return
  714. }
  715. for _, v := range articleList {
  716. if _, ok := articleMap[v.ArticleId]; !ok {
  717. articleMap[v.ArticleId] = v
  718. }
  719. }
  720. }
  721. lenList := len(list)
  722. for i := 0; i < lenList; i++ {
  723. item := list[i]
  724. article := articleMap[item.ArticleId]
  725. if article != nil {
  726. list[i].Title = article.Title
  727. list[i].TitleEn = article.TitleEn
  728. list[i].UpdateFrequency = article.UpdateFrequency
  729. list[i].CreateDate = article.CreateDate
  730. list[i].PublishDate = article.PublishDate
  731. list[i].Body, _ = services.GetReportContentTextSub(article.Body)
  732. list[i].Abstract = article.Abstract
  733. list[i].CategoryName = article.CategoryName
  734. list[i].SubCategoryName = article.SubCategoryName
  735. }
  736. }
  737. page := paging.GetPaging(currentIndex, pageSize, total)
  738. resp := new(models.ArticleBrowseHistoryListResp)
  739. resp.List = list
  740. resp.Paging = page
  741. br.Msg = "获取成功!"
  742. br.Ret = 200
  743. br.Success = true
  744. br.Data = resp
  745. }
  746. // @Title 未付费申请试用
  747. // @Description 未付费申请试用
  748. // @Param request body models.ApplyTryReq true "type json string"
  749. // @Success 200
  750. // @router /apply/try [post]
  751. func (this *UserController) ApplyTryOut() {
  752. br := new(models.BaseResponse).Init()
  753. defer func() {
  754. this.Data["json"] = br
  755. this.ServeJSON()
  756. }()
  757. user := this.User
  758. if user == nil {
  759. br.Msg = "请登录"
  760. br.ErrMsg = "请登录,SysUser Is Empty"
  761. br.Ret = 408
  762. return
  763. }
  764. mobile := user.Mobile
  765. var req models.ApplyTryReq
  766. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  767. if err != nil {
  768. br.Msg = "参数解析异常!"
  769. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  770. return
  771. }
  772. if req.RealName == "" {
  773. req.RealName = user.RealName
  774. }
  775. if req.CompanyName == "" {
  776. req.CompanyName = user.CompanyName
  777. }
  778. uid := user.UserId
  779. //缓存校验
  780. cacheKey := fmt.Sprint("xygx:apply_record:add:", uid)
  781. ttlTime := utils.Rc.GetRedisTTL(cacheKey)
  782. if ttlTime > 0 {
  783. br.Msg = "申请失败,申请过于频繁"
  784. br.ErrMsg = "申请失败,申请过于频繁"
  785. return
  786. }
  787. utils.Rc.SetNX(cacheKey, user.Mobile, time.Second*10)
  788. //判断是否已经申请过
  789. applyCount, err := models.GetApplyRecordCount(uid)
  790. if err != nil && err.Error() != utils.ErrNoRow() {
  791. br.Msg = "获取信息失败"
  792. br.ErrMsg = "判断是否已申请过试用失败,Err:" + err.Error()
  793. return
  794. }
  795. if applyCount > 0 {
  796. br.Msg = "您已提交申请,请耐心等待。"
  797. br.IsSendEmail = false
  798. return
  799. }
  800. //判断是否存在申请
  801. var sellerMobile string
  802. if req.ApplyMethod == 2 {
  803. if req.BusinessCardUrl == "" {
  804. br.Msg = "请上传名片"
  805. return
  806. }
  807. if req.RealName == "" {
  808. br.Msg = "请输入姓名"
  809. return
  810. }
  811. if req.CompanyName == "" {
  812. br.Msg = "请输入公司名称"
  813. return
  814. }
  815. if req.BusinessCardUrl != "" && utils.RunMode == "release" {
  816. card, err := services.GetBusinessCard(req.BusinessCardUrl)
  817. if err != nil {
  818. br.Msg = "名片识别失败"
  819. br.ErrMsg = "名片识别失败,Err:" + err.Error()
  820. return
  821. }
  822. mobileStr := strings.Join(card.WordsResult.MOBILE, ",")
  823. isFlag := true
  824. if mobile != "" {
  825. if strings.Contains(mobileStr, mobile) || mobileStr == "" {
  826. isFlag = true
  827. } else {
  828. isFlag = false
  829. }
  830. }
  831. if !isFlag {
  832. //阿里云识别
  833. if utils.RunMode == "release" {
  834. aliyunResult, err := services.AliyunBusinessCard(req.BusinessCardUrl)
  835. if err != nil {
  836. br.Msg = "识别失败"
  837. br.ErrMsg = "识别失败,Err:" + err.Error()
  838. return
  839. }
  840. if !aliyunResult.Success {
  841. br.Msg = "识别失败"
  842. br.ErrMsg = "识别失败"
  843. return
  844. }
  845. mobileStr := strings.Join(aliyunResult.TelCell, ",")
  846. if mobile != "" {
  847. if strings.Contains(mobileStr, mobile) {
  848. isFlag = true
  849. } else {
  850. isFlag = false
  851. }
  852. }
  853. }
  854. }
  855. if !isFlag {
  856. br.Msg = "名片手机号与所填手机号不匹配,请重新填写"
  857. br.ErrMsg = "mobile:" + mobile
  858. return
  859. }
  860. }
  861. }
  862. //获取销售信息
  863. sellerItem, err := models.GetSellerByCompanyId(user.CompanyId)
  864. if err != nil && err.Error() != utils.ErrNoRow() {
  865. br.Msg = "申请失败"
  866. br.ErrMsg = "获取销售信息失败,Err:" + err.Error()
  867. return
  868. }
  869. if sellerItem != nil {
  870. sellerMobile = sellerItem.Mobile
  871. //推送模板消息
  872. mobile := user.Mobile
  873. if mobile == "" {
  874. mobile = user.Email
  875. }
  876. }
  877. applyMethod := ""
  878. if req.ApplyMethod == 1 {
  879. applyMethod = "已有客户申请"
  880. } else {
  881. applyMethod = "潜在客户申请"
  882. }
  883. cnf, _ := models.GetConfigByCode("tpl_msg")
  884. if cnf != nil {
  885. openIpItem, _ := models.GetUserRecordByMobile(1, cnf.ConfigValue)
  886. if openIpItem != nil && openIpItem.OpenId != "" {
  887. fmt.Println("推送消息", req.RealName, req.CompanyName, mobile, openIpItem.OpenId, applyMethod)
  888. utils.FileLog.Info("推送消息 %s %s,%s,%s,%s", req.RealName, req.CompanyName, mobile, openIpItem.OpenId, applyMethod)
  889. go services.SendPermissionApplyTemplateMsg(req.RealName, req.CompanyName, mobile, openIpItem.OpenId, applyMethod)
  890. }
  891. }
  892. err = models.AddApplyRecord(&req, user.Mobile, user.CompanyName, uid, user.CompanyId)
  893. if err != nil {
  894. br.Msg = "申请失败"
  895. br.ErrMsg = "申请失败,Err:" + err.Error()
  896. return
  897. }
  898. //添加成功后,设置5分钟缓存,不允许重复添加
  899. //utils.Rc.SetNX(cacheKey, user.Mobile, time.Second*60)
  900. br.Msg = "申请成功!"
  901. br.Ret = 200
  902. br.Success = true
  903. br.Data = sellerMobile
  904. }
  905. func sentMsgToSeller() {
  906. }
  907. //func init() {
  908. // fmt.Println("start")
  909. // realName:="沈涛"
  910. // companyName:="弘则研究"
  911. // mobile:="18767183922"
  912. // openId:="oN0jD1eTfIAf68Y2n24RrvIGXFw4"
  913. // applyMethod:="xxx"
  914. // services.SendPermissionApplyTemplateMsg(realName, companyName, mobile, openId, applyMethod)
  915. // return
  916. // fmt.Println("end")
  917. //}
  918. // @Title 是否需要填写区号
  919. // @Description 获取是否需要填写区号接口
  920. // @Success 200 {object} models.CountryCode
  921. // @router /countryCcode/isNeedAdd [get]
  922. func (this *UserController) CountryCcode() {
  923. br := new(models.BaseResponse).Init()
  924. defer func() {
  925. this.Data["json"] = br
  926. this.ServeJSON()
  927. }()
  928. user := this.User
  929. uid := user.UserId
  930. if user == nil {
  931. br.Msg = "请登录"
  932. br.ErrMsg = "请登录,用户信息为空"
  933. br.Ret = 408
  934. return
  935. }
  936. if uid == 0 {
  937. br.Msg = "请登录"
  938. br.ErrMsg = "请登录,用户信息为空"
  939. br.Ret = 408
  940. return
  941. }
  942. resp := new(models.CountryCode)
  943. if user.CountryCode == "" {
  944. resp.IsNeedAddCountryCode = true
  945. }
  946. br.Ret = 200
  947. br.Success = true
  948. br.Msg = "获取成功"
  949. br.Data = resp
  950. }
  951. // @Title 上传用户区号
  952. // @Description 上传用户区号接口
  953. // @Param request body models.CountryCodeItem true "type json string"
  954. // @Success Ret=200 新增成功
  955. // @router /countryCcode/Add [POST]
  956. func (this *UserController) AddCountryCcode() {
  957. br := new(models.BaseResponse).Init()
  958. defer func() {
  959. this.Data["json"] = br
  960. this.ServeJSON()
  961. }()
  962. user := this.User
  963. if user == nil {
  964. br.Msg = "请登录"
  965. br.ErrMsg = "请登录,用户信息为空"
  966. br.Ret = 408
  967. return
  968. }
  969. var req models.CountryCodeItem
  970. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  971. if err != nil {
  972. br.Msg = "参数解析异常!"
  973. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  974. return
  975. }
  976. err = models.AddCountryCode(req.CountryCode, user)
  977. if err != nil {
  978. br.Msg = "获取信息失败"
  979. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  980. return
  981. }
  982. br.Ret = 200
  983. br.Success = true
  984. br.Msg = "新增成功"
  985. }
  986. //// @Title 是否需要填写外呼手机号
  987. //// @Description 获取是否需要填写外呼手机号接口
  988. //// @Success 200 {object} models.CountryCode
  989. //// @router /countryCcode/isNeedAddOutbound [get]
  990. //func (this *UserController) OutboundMobile() {
  991. // br := new(models.BaseResponse).Init()
  992. // defer func() {
  993. // this.Data["json"] = br
  994. // this.ServeJSON()
  995. // }()
  996. // user := this.User
  997. // uid := user.UserId
  998. // if user == nil {
  999. // br.Msg = "请登录"
  1000. // br.ErrMsg = "请登录,用户信息为空"
  1001. // br.Ret = 408
  1002. // return
  1003. // }
  1004. // if uid == 0 {
  1005. // br.Msg = "请登录"
  1006. // br.ErrMsg = "请登录,用户信息为空"
  1007. // br.Ret = 408
  1008. // return
  1009. // }
  1010. // resp := new(models.OutboundMobile)
  1011. // if user.OutboundMobile == "" && user.CountryCode == "" {
  1012. // resp.IsNeedAddOutboundMobile = true
  1013. // }
  1014. // br.Ret = 200
  1015. // br.Success = true
  1016. // br.Msg = "获取成功"
  1017. // br.Data = resp
  1018. //}
  1019. // @Title 用户修改外呼手机号以及区号
  1020. // @Description 用户修改外呼手机号以及区号接口
  1021. // @Param request body models.OutboundMobileItem true "type json string"
  1022. // @Success Ret=200 操作成功
  1023. // @router /countryCcode/addOutboundMobile [POST]
  1024. func (this *UserController) AddOutboundMobile() {
  1025. br := new(models.BaseResponse).Init()
  1026. defer func() {
  1027. this.Data["json"] = br
  1028. this.ServeJSON()
  1029. }()
  1030. user := this.User
  1031. uid := user.UserId
  1032. if user == nil {
  1033. br.Msg = "请登录"
  1034. br.ErrMsg = "请登录,用户信息为空"
  1035. br.Ret = 408
  1036. return
  1037. }
  1038. var req models.OutboundMobileItem
  1039. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1040. if err != nil {
  1041. br.Msg = "参数解析异常!"
  1042. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1043. return
  1044. }
  1045. if !utils.ValidateFixedTelephoneFormatat(req.OutboundMobile) && !utils.ValidateMobileFormatat(req.OutboundMobile) {
  1046. br.Msg = "号码格式有误,请重新填写!"
  1047. br.ErrMsg = "号码格式有误,请重新填写" + req.OutboundMobile
  1048. return
  1049. }
  1050. if req.OutboundMobile == "" {
  1051. br.Msg = "请填写区号!"
  1052. return
  1053. }
  1054. item := new(models.OutboundMobileItem)
  1055. item.OutboundMobile = req.OutboundMobile
  1056. item.OutboundCountryCode = req.OutboundCountryCode
  1057. item.ActivityId = req.ActivityId
  1058. if req.ActivityId == 0 {
  1059. err = models.AddOutboundMobile(item, uid)
  1060. } else {
  1061. if user.Mobile == "" && user.OutboundMobile == "" {
  1062. items := new(models.CygxActivitySignup)
  1063. items.UserId = uid
  1064. items.ActivityId = req.ActivityId
  1065. items.CreateTime = time.Now()
  1066. items.Mobile = user.Mobile
  1067. items.Email = user.Email
  1068. items.CompanyId = user.CompanyId
  1069. items.CompanyName = user.CompanyName
  1070. items.SignupType = 1
  1071. items.FailType = 0
  1072. items.DoFailType = 0
  1073. items.OutboundMobile = req.OutboundMobile
  1074. items.CountryCode = req.OutboundCountryCode
  1075. _, errSignup := models.AddActivitySignup(items)
  1076. if errSignup != nil {
  1077. br.Msg = "操作失败"
  1078. br.ErrMsg = "操作失败,Err:" + errSignup.Error()
  1079. return
  1080. }
  1081. err = models.AddOutboundMobile(item, uid)
  1082. } else {
  1083. total, err := models.GetActivityCountByIdWithUid(item.ActivityId, uid)
  1084. if err != nil {
  1085. br.Msg = "操作失败"
  1086. br.ErrMsg = "操作失败,Err:" + err.Error()
  1087. return
  1088. }
  1089. err = models.AddOutboundMobile(item, uid)
  1090. if total == 0 {
  1091. br.Msg = "报名信息不存在"
  1092. br.ErrMsg = "报名信息不存在,Err:" + "活动ActivityId:" + strconv.Itoa(item.ActivityId) + "用户Uid:" + strconv.Itoa(uid)
  1093. return
  1094. }
  1095. }
  1096. }
  1097. if err != nil {
  1098. br.Msg = "操作失败"
  1099. br.ErrMsg = "操作失败,Err:" + err.Error()
  1100. return
  1101. }
  1102. br.Ret = 200
  1103. br.Success = true
  1104. br.Msg = "操作成功"
  1105. }