user.go 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  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. var title string
  596. tryType := req.TryType
  597. detailId := req.DetailId
  598. if tryType == "Article" {
  599. detail, err := models.GetArticleDetailById(detailId)
  600. if err != nil {
  601. br.Msg = "获取信息失败"
  602. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  603. return
  604. }
  605. title = detail.Title
  606. } else if tryType == "Activity" {
  607. detail, err := models.GetAddActivityInfoById(detailId)
  608. if err != nil {
  609. br.Msg = "操作失败"
  610. br.ErrMsg = "活动ID错误,不存在activityId:" + strconv.Itoa(detailId)
  611. return
  612. }
  613. title = detail.ActivityName
  614. }
  615. fmt.Println(title)
  616. //缓存校验
  617. cacheKey := fmt.Sprint("xygx:apply_record:add:", uid)
  618. ttlTime := utils.Rc.GetRedisTTL(cacheKey)
  619. if ttlTime > 0 {
  620. br.Msg = "申请失败,申请过于频繁"
  621. br.ErrMsg = "申请失败,申请过于频繁"
  622. return
  623. }
  624. utils.Rc.SetNX(cacheKey, user.Mobile, time.Second*10)
  625. //判断是否已经申请过
  626. applyCount, err := models.GetApplyRecordCount(uid)
  627. if err != nil && err.Error() != utils.ErrNoRow() {
  628. br.Msg = "获取信息失败"
  629. br.ErrMsg = "判断是否已申请过试用失败,Err:" + err.Error()
  630. return
  631. }
  632. if applyCount > 0 {
  633. br.Msg = "您已提交申请,请耐心等待。"
  634. br.IsSendEmail = false
  635. return
  636. }
  637. //判断是否存在申请
  638. var sellerMobile string
  639. if req.ApplyMethod == 2 {
  640. if req.BusinessCardUrl == "" {
  641. br.Msg = "请上传名片"
  642. return
  643. }
  644. if req.RealName == "" {
  645. br.Msg = "请输入姓名"
  646. return
  647. }
  648. if req.CompanyName == "" {
  649. br.Msg = "请输入公司名称"
  650. return
  651. }
  652. if req.BusinessCardUrl != "" && utils.RunMode == "release" {
  653. card, err := services.GetBusinessCard(req.BusinessCardUrl)
  654. if err != nil {
  655. br.Msg = "名片识别失败"
  656. br.ErrMsg = "名片识别失败,Err:" + err.Error()
  657. return
  658. }
  659. mobileStr := strings.Join(card.WordsResult.MOBILE, ",")
  660. isFlag := true
  661. if mobile != "" {
  662. if strings.Contains(mobileStr, mobile) || mobileStr == "" {
  663. isFlag = true
  664. } else {
  665. isFlag = false
  666. }
  667. }
  668. if !isFlag {
  669. //阿里云识别
  670. if utils.RunMode == "release" {
  671. aliyunResult, err := services.AliyunBusinessCard(req.BusinessCardUrl)
  672. if err != nil {
  673. br.Msg = "识别失败"
  674. br.ErrMsg = "识别失败,Err:" + err.Error()
  675. return
  676. }
  677. if !aliyunResult.Success {
  678. br.Msg = "识别失败"
  679. br.ErrMsg = "识别失败"
  680. return
  681. }
  682. mobileStr := strings.Join(aliyunResult.TelCell, ",")
  683. if mobile != "" {
  684. if strings.Contains(mobileStr, mobile) {
  685. isFlag = true
  686. } else {
  687. isFlag = false
  688. }
  689. }
  690. }
  691. }
  692. if !isFlag {
  693. br.Msg = "名片手机号与所填手机号不匹配,请重新填写"
  694. br.ErrMsg = "mobile:" + mobile
  695. return
  696. }
  697. }
  698. }
  699. //获取销售信息
  700. sellerItem, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
  701. if err != nil && err.Error() != utils.ErrNoRow() {
  702. br.Msg = "申请失败"
  703. br.ErrMsg = "获取销售信息失败,Err:" + err.Error()
  704. return
  705. }
  706. if sellerItem != nil {
  707. sellerMobile = sellerItem.Mobile
  708. //推送模板消息
  709. mobile := user.Mobile
  710. if mobile == "" {
  711. mobile = user.Email
  712. }
  713. }
  714. applyMethod := ""
  715. cnf, _ := models.GetConfigByCode("tpl_msg")
  716. if cnf != nil {
  717. if sellerItem != nil {
  718. cnf.ConfigValue = sellerItem.Mobile
  719. companyItem, err := models.GetCompanyDetailById(user.CompanyId)
  720. if err != nil && err.Error() != utils.ErrNoRow() {
  721. br.Msg = "获取信息失败"
  722. br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
  723. return
  724. }
  725. if companyItem != nil && companyItem.CompanyId > 0 {
  726. applyMethod = companyItem.Status + "客户申请"
  727. if detailId > 0 {
  728. companyProduct, err := models.GetCompanyProductDetail(user.CompanyId, 2)
  729. if err != nil && err.Error() != utils.ErrNoRow() {
  730. br.Msg = "获取信息失败"
  731. br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
  732. return
  733. }
  734. if companyProduct != nil && companyProduct.IsSuspend == 1 {
  735. applyMethod = "试用暂停客户"
  736. } else {
  737. if companyItem.Status == "正式" || companyItem.Status == "试用" {
  738. applyMethod = companyItem.Status + "客户申请,无对应权限"
  739. } else if companyItem.Status == "冻结" || companyItem.Status == "流失" {
  740. applyMethod = companyItem.Status + "客户"
  741. }
  742. }
  743. applyMethod = applyMethod + "," + title
  744. }
  745. }
  746. } else {
  747. //获取销售信息
  748. sellerItem, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 1)
  749. if err != nil && err.Error() != utils.ErrNoRow() {
  750. br.Msg = "申请失败"
  751. br.ErrMsg = "获取销售信息失败,Err:" + err.Error()
  752. return
  753. }
  754. if sellerItem != nil {
  755. applyMethod = "FICC客户"
  756. } else {
  757. applyMethod = "潜在客户"
  758. }
  759. if detailId > 0 {
  760. applyMethod = applyMethod + "," + title
  761. }
  762. }
  763. openIpItem, _ := models.GetUserRecordByMobile(4, cnf.ConfigValue)
  764. if openIpItem != nil && openIpItem.OpenId != "" {
  765. if req.ApplyMethod != 2 {
  766. req.RealName = user.RealName
  767. req.CompanyName = user.CompanyName
  768. }
  769. fmt.Println("推送消息", req.RealName, req.CompanyName, mobile, openIpItem.OpenId, applyMethod)
  770. utils.FileLog.Info("推送消息 %s %s,%s,%s,%s", req.RealName, req.CompanyName, mobile, openIpItem.OpenId, applyMethod)
  771. go services.SendPermissionApplyTemplateMsg(req.RealName, req.CompanyName, mobile, openIpItem.OpenId, applyMethod)
  772. }
  773. }
  774. err = models.AddApplyRecord(&req, user.Mobile, user.CompanyName, uid, user.CompanyId)
  775. if err != nil {
  776. br.Msg = "申请失败"
  777. br.ErrMsg = "申请失败,Err:" + err.Error()
  778. return
  779. }
  780. //添加成功后,设置5分钟缓存,不允许重复添加
  781. //utils.Rc.SetNX(cacheKey, user.Mobile, time.Second*60)
  782. br.Msg = "申请成功!"
  783. br.Ret = 200
  784. br.Success = true
  785. br.Data = sellerMobile
  786. }
  787. // @Title 是否需要填写区号
  788. // @Description 获取是否需要填写区号接口
  789. // @Success 200 {object} models.CountryCode
  790. // @router /countryCcode/isNeedAdd [get]
  791. func (this *UserController) CountryCcode() {
  792. br := new(models.BaseResponse).Init()
  793. defer func() {
  794. this.Data["json"] = br
  795. this.ServeJSON()
  796. }()
  797. user := this.User
  798. uid := user.UserId
  799. if user == nil {
  800. br.Msg = "请登录"
  801. br.ErrMsg = "请登录,用户信息为空"
  802. br.Ret = 408
  803. return
  804. }
  805. if uid == 0 {
  806. br.Msg = "请登录"
  807. br.ErrMsg = "请登录,用户信息为空"
  808. br.Ret = 408
  809. return
  810. }
  811. resp := new(models.CountryCode)
  812. if user.CountryCode == "" && len(user.Mobile) >= 11 {
  813. err := models.ChangeUserOutboundMobileByMobile(uid)
  814. if err != nil {
  815. br.Msg = "操作失败"
  816. br.ErrMsg = "操作失败,Err:" + err.Error()
  817. return
  818. }
  819. }
  820. if user.CountryCode == "" && user.Mobile != "" && len(user.Mobile) < 11 {
  821. resp.IsNeedAddCountryCode = true
  822. }
  823. if user.OutboundMobile != "" {
  824. resp.IsNeedAddCountryCode = false
  825. }
  826. br.Ret = 200
  827. br.Success = true
  828. br.Msg = "获取成功"
  829. br.Data = resp
  830. }
  831. // @Title 上传用户区号
  832. // @Description 上传用户区号接口
  833. // @Param request body models.CountryCodeItem true "type json string"
  834. // @Success Ret=200 新增成功
  835. // @router /countryCcode/Add [POST]
  836. func (this *UserController) AddCountryCcode() {
  837. br := new(models.BaseResponse).Init()
  838. defer func() {
  839. this.Data["json"] = br
  840. this.ServeJSON()
  841. }()
  842. user := this.User
  843. if user == nil {
  844. br.Msg = "请登录"
  845. br.ErrMsg = "请登录,用户信息为空"
  846. br.Ret = 408
  847. return
  848. }
  849. var req models.CountryCodeItem
  850. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  851. if err != nil {
  852. br.Msg = "参数解析异常!"
  853. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  854. return
  855. }
  856. err = models.AddCountryCode(req.CountryCode, user)
  857. if err != nil {
  858. br.Msg = "获取信息失败"
  859. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  860. return
  861. }
  862. br.Ret = 200
  863. br.Success = true
  864. br.Msg = "新增成功"
  865. }
  866. //// @Title 是否需要填写外呼手机号
  867. //// @Description 获取是否需要填写外呼手机号接口
  868. //// @Success 200 {object} models.CountryCode
  869. //// @router /countryCcode/isNeedAddOutbound [get]
  870. //func (this *UserController) OutboundMobile() {
  871. // br := new(models.BaseResponse).Init()
  872. // defer func() {
  873. // this.Data["json"] = br
  874. // this.ServeJSON()
  875. // }()
  876. // user := this.User
  877. // uid := user.UserId
  878. // if user == nil {
  879. // br.Msg = "请登录"
  880. // br.ErrMsg = "请登录,用户信息为空"
  881. // br.Ret = 408
  882. // return
  883. // }
  884. // if uid == 0 {
  885. // br.Msg = "请登录"
  886. // br.ErrMsg = "请登录,用户信息为空"
  887. // br.Ret = 408
  888. // return
  889. // }
  890. // resp := new(models.OutboundMobile)
  891. // if user.OutboundMobile == "" && user.CountryCode == "" {
  892. // resp.IsNeedAddOutboundMobile = true
  893. // }
  894. // br.Ret = 200
  895. // br.Success = true
  896. // br.Msg = "获取成功"
  897. // br.Data = resp
  898. //}
  899. // @Title 用户修改外呼手机号以及区号
  900. // @Description 用户修改外呼手机号以及区号接口
  901. // @Param request body models.OutboundMobileItem true "type json string"
  902. // @Success Ret=200 操作成功
  903. // @router /countryCcode/addOutboundMobile [POST]
  904. func (this *UserController) AddOutboundMobile() {
  905. br := new(models.BaseResponse).Init()
  906. defer func() {
  907. this.Data["json"] = br
  908. this.ServeJSON()
  909. }()
  910. user := this.User
  911. uid := user.UserId
  912. if user == nil {
  913. br.Msg = "请登录"
  914. br.ErrMsg = "请登录,用户信息为空"
  915. br.Ret = 408
  916. return
  917. }
  918. var req models.OutboundMobileItem
  919. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  920. if err != nil {
  921. br.Msg = "参数解析异常!"
  922. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  923. return
  924. }
  925. if !utils.ValidateFixedTelephoneFormatatEasy(req.OutboundMobile) {
  926. br.Msg = "号码格式有误,请重新填写!"
  927. br.ErrMsg = "号码格式有误,请重新填写" + req.OutboundMobile
  928. return
  929. }
  930. if req.OutboundMobile == "" {
  931. br.Msg = "请填写区号!"
  932. return
  933. }
  934. item := new(models.OutboundMobileItem)
  935. item.OutboundMobile = req.OutboundMobile
  936. item.OutboundCountryCode = req.OutboundCountryCode
  937. item.ActivityId = req.ActivityId
  938. if req.ActivityId == 0 {
  939. err = models.AddOutboundMobile(item, uid)
  940. } else {
  941. if user.Mobile == "" && user.OutboundMobile == "" {
  942. items := new(models.CygxActivitySignup)
  943. items.UserId = uid
  944. items.ActivityId = req.ActivityId
  945. items.CreateTime = time.Now()
  946. items.Mobile = user.Mobile
  947. items.Email = user.Email
  948. items.CompanyId = user.CompanyId
  949. items.CompanyName = user.CompanyName
  950. items.SignupType = 1
  951. items.FailType = 0
  952. items.DoFailType = 0
  953. items.OutboundMobile = req.OutboundMobile
  954. items.CountryCode = req.OutboundCountryCode
  955. _, err = models.AddActivitySignupFromEmail(items)
  956. } else {
  957. total, err := models.GetActivityCountByIdWithUid(item.ActivityId, uid)
  958. if total == 0 {
  959. br.Msg = "报名信息不存在"
  960. br.ErrMsg = "报名信息不存在,Err:" + "活动ActivityId:" + strconv.Itoa(item.ActivityId) + "用户Uid:" + strconv.Itoa(uid)
  961. return
  962. }
  963. if err != nil {
  964. br.Msg = "操作失败"
  965. br.ErrMsg = "操作失败,Err:" + err.Error()
  966. return
  967. }
  968. err = models.AddOutboundMobile(item, uid)
  969. }
  970. }
  971. if err != nil {
  972. br.Msg = "操作失败"
  973. br.ErrMsg = "操作失败,Err:" + err.Error()
  974. return
  975. }
  976. br.Ret = 200
  977. br.Success = true
  978. br.Msg = "操作成功"
  979. }
  980. // @Title 白名单同步
  981. // @Description 获取是否需要填写区号接口
  982. // @Param Keypd query string false "搜索主题 多个用 , 隔开 (空为活动主题,非空为更多主题)"
  983. // @Success 200 {object} models.CountryCode
  984. // @router /whiteUser [get]
  985. func (this *UserController) WhiteUser() {
  986. br := new(models.BaseResponse).Init()
  987. defer func() {
  988. this.Data["json"] = br
  989. this.ServeJSON()
  990. }()
  991. user := this.User
  992. uid := user.UserId
  993. if user == nil {
  994. br.Msg = "请登录"
  995. br.ErrMsg = "请登录,用户信息为空"
  996. br.Ret = 408
  997. return
  998. }
  999. if uid == 0 {
  1000. br.Msg = "请登录"
  1001. br.ErrMsg = "请登录,用户信息为空"
  1002. br.Ret = 408
  1003. return
  1004. }
  1005. keypd := this.GetString("Keypd")
  1006. if keypd != "888wqrerqesdfdsfsdwdsfasdqwerqwer" {
  1007. br.Msg = "密码错误"
  1008. return
  1009. }
  1010. item := new(models.WxUserWhite)
  1011. fmt.Println(item)
  1012. var fieldStr string
  1013. var condition string
  1014. fieldStr = ` u.mobile,u.country_code,u.real_name,c.company_name,u.company_id,cp.seller_name,u.created_time,cp.status,`
  1015. condition = ` AND cp.status IN ( '正式', '试用' ) AND u.mobile != '' `
  1016. list1, err := models.GetFormalUserWhiteList(fieldStr, condition)
  1017. if err != nil {
  1018. br.Msg = "获取失败,Err:" + err.Error()
  1019. return
  1020. }
  1021. 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,`
  1022. condition = ` AND u.mobile != u.outbound_mobile AND cp.status IN ( '正式', '试用' ) AND u.outbound_mobile != '' `
  1023. list2, err := models.GetFormalUserWhiteList(fieldStr, condition)
  1024. if err != nil {
  1025. br.Msg = "获取失败,Err:" + err.Error()
  1026. return
  1027. }
  1028. fmt.Println(len(list1))
  1029. for k, v := range list1 {
  1030. item.Mobile = v.Mobile
  1031. item.CountryCode = v.CountryCode
  1032. item.CompanyName = v.CompanyName
  1033. item.PermissionName = v.Permission
  1034. item.CreatedTime = time.Now()
  1035. item.UserCreatedTime = v.CreatedTime
  1036. item.RealName = v.RealName
  1037. item.SellerName = v.SellerName
  1038. item.Status = v.Status
  1039. _, err := models.AddWxUserWhite(item)
  1040. if err != nil {
  1041. br.Msg = "获取失败,Err:" + err.Error()
  1042. return
  1043. }
  1044. fmt.Println(k)
  1045. }
  1046. item = new(models.WxUserWhite)
  1047. fmt.Println(len(list2))
  1048. for _, v := range list2 {
  1049. item.OutboundMobile = v.Mobile
  1050. item.OutboundCountryCode = v.CountryCode
  1051. item.CreatedTime = time.Now()
  1052. item.CompanyName = v.CompanyName
  1053. item.PermissionName = v.Permission
  1054. item.UserCreatedTime = v.CreatedTime
  1055. item.RealName = v.RealName
  1056. item.SellerName = v.SellerName
  1057. item.Status = v.Status
  1058. _, err := models.AddWxUserWhite(item)
  1059. if err != nil {
  1060. br.Msg = "获取失败,Err:" + err.Error()
  1061. return
  1062. }
  1063. }
  1064. br.Ret = 200
  1065. br.Success = true
  1066. br.Msg = "获取成功"
  1067. }
  1068. // @Title 获取我的提问
  1069. // @Description 获取我的提问列表
  1070. // @Success 200 {object} models.CygxAskListResp
  1071. // @router /ask/list [get]
  1072. func (this *UserController) AskList() {
  1073. br := new(models.BaseResponse).Init()
  1074. defer func() {
  1075. this.Data["json"] = br
  1076. this.ServeJSON()
  1077. }()
  1078. user := this.User
  1079. if user == nil {
  1080. br.Msg = "请登录"
  1081. br.ErrMsg = "请登录,用户信息为空"
  1082. br.Ret = 408
  1083. return
  1084. }
  1085. userId := this.User.UserId
  1086. listActcivity, err := models.GetActivityAskList(userId)
  1087. if err != nil && err.Error() != utils.ErrNoRow() {
  1088. br.Msg = "获取失败"
  1089. br.ErrMsg = "获取活动问题失败,Err:" + err.Error()
  1090. return
  1091. }
  1092. for _, v := range listActcivity {
  1093. v.AskType = "Activity"
  1094. }
  1095. listArticle, err := models.GetArticleAskList(userId)
  1096. if err != nil && err.Error() != utils.ErrNoRow() {
  1097. br.Msg = "获取失败"
  1098. br.ErrMsg = "获取文章问题失败,Err:" + err.Error()
  1099. return
  1100. }
  1101. for _, v := range listArticle {
  1102. v.AskType = "Report"
  1103. listActcivity = append(listActcivity, v)
  1104. }
  1105. resp := new(models.CygxAskListResp)
  1106. resp.List = listActcivity
  1107. br.Msg = "获取成功!"
  1108. br.Ret = 200
  1109. br.Success = true
  1110. br.Data = resp
  1111. }