user.go 30 KB

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