user.go 29 KB

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