user.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hongze_clpt/models"
  6. "hongze/hongze_clpt/services"
  7. "hongze/hongze_clpt/utils"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. type UserController struct {
  13. BaseAuthController
  14. }
  15. type UserCommonController struct {
  16. BaseCommonController
  17. }
  18. // @Title 登录
  19. // @Description 登录接口
  20. // @Param request body models.LoginReq true "type json string"
  21. // @Success 200 {object} models.LoginResp
  22. // @router /login [post]
  23. func (this *UserCommonController) Login() {
  24. br := new(models.BaseResponse).Init()
  25. defer func() {
  26. this.Data["json"] = br
  27. this.ServeJSON()
  28. }()
  29. var token string
  30. var req models.LoginReq
  31. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  32. if err != nil {
  33. br.Msg = "参数解析异常!"
  34. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  35. return
  36. }
  37. mobile := req.Mobile
  38. req.Mobile = strings.Trim(req.Mobile, " ")
  39. if req.Mobile == "" {
  40. br.Msg = "参数错误"
  41. br.ErrMsg = "参数错误,手机号为空"
  42. return
  43. }
  44. code := req.VCode
  45. if code == "" {
  46. br.Msg = "参数错误"
  47. br.ErrMsg = "Code 为空"
  48. return
  49. }
  50. authorization := req.Token
  51. item, err := models.GetMsgCode(req.Mobile, req.VCode)
  52. if err != nil {
  53. if err.Error() == utils.ErrNoRow() {
  54. br.Msg = "验证码错误,请重新输入"
  55. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  56. return
  57. } else {
  58. br.Msg = "验证码错误,请重新输入"
  59. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  60. return
  61. }
  62. }
  63. if item == nil {
  64. br.Msg = "验证码错误,请重新输入"
  65. return
  66. }
  67. user, err := services.BindWxUser(mobile, "86")
  68. //user, err := models.GetWxUserItemByMobile(mobile)
  69. if err != nil {
  70. br.Msg = "登录失败"
  71. br.ErrMsg = "获取用户信息失败,GetUserDetailByMobile Err:" + err.Error()
  72. return
  73. }
  74. if authorization != "" {
  75. session, err := models.GetSessionByToken(authorization)
  76. if err != nil {
  77. br.Msg = "登录失败"
  78. br.ErrMsg = "绑定失败,GetSessionByToken err:" + err.Error()
  79. return
  80. }
  81. openid := session.OpenId
  82. if openid == "" {
  83. br.Msg = "登录失败"
  84. br.ErrMsg = "获取用户信息失败,绑定用户openid失败,OpenId不能为空 "
  85. return
  86. }
  87. err = models.BindSessionMobile(mobile, authorization)
  88. if err != nil {
  89. br.Msg = "登录失败"
  90. br.ErrMsg = "获取用户信息失败,BindSessionMobile Err:" + err.Error()
  91. return
  92. }
  93. err = models.BindUserRecordByOpenid(user.UserId, openid, mobile)
  94. if err != nil {
  95. br.Msg = "登录失败"
  96. br.ErrMsg = "获取用户信息失败,BindUserRecordByOpenid Err:" + err.Error()
  97. return
  98. }
  99. token = authorization
  100. } else {
  101. timeUnix := time.Now().Unix()
  102. timeUnixStr := strconv.FormatInt(timeUnix, 10)
  103. token = utils.MD5(mobile) + utils.MD5(timeUnixStr)
  104. itemsSession := new(models.CygxClptSession)
  105. itemsSession.UserId = user.UserId
  106. itemsSession.Mobile = mobile
  107. itemsSession.AccessToken = token
  108. itemsSession.CreatedTime = time.Now()
  109. itemsSession.LastUpdatedTime = time.Now()
  110. itemsSession.ExpireTime = time.Now().AddDate(0, 3, 0)
  111. err = models.AddCygxClptSession(itemsSession)
  112. if err != nil {
  113. br.Msg = "获取用户信息失败"
  114. br.ErrMsg = "添加Token失败,Err:" + err.Error()
  115. return
  116. }
  117. }
  118. resp := new(models.LoginResp)
  119. resp.UserId = user.UserId
  120. resp.Headimgurl = user.Headimgurl
  121. resp.Mobile = user.Mobile
  122. resp.Email = user.Email
  123. resp.CompanyName = user.CompanyName
  124. resp.Authorization = token
  125. br.Ret = 200
  126. br.Success = true
  127. br.Msg = "获取成功"
  128. br.Data = resp
  129. }
  130. // @Title 获取用户详情
  131. // @Description 获取用户详情接口
  132. // @Success 200 {object} models.UserDetailResp
  133. // @router /detail [get]
  134. func (this *UserController) Detail() {
  135. br := new(models.BaseResponse).Init()
  136. defer func() {
  137. this.Data["json"] = br
  138. this.ServeJSON()
  139. }()
  140. user := this.User
  141. if user == nil {
  142. br.Msg = "请登录"
  143. br.ErrMsg = "请登录,用户信息为空"
  144. br.Ret = 408
  145. return
  146. }
  147. resp := new(models.UserDetailResp)
  148. resp.UserId = user.UserId
  149. resp.Headimgurl = user.Headimgurl
  150. resp.Mobile = user.Mobile
  151. resp.Email = user.Email
  152. resp.CompanyName = user.CompanyName
  153. if resp.Headimgurl == "" {
  154. resp.Headimgurl = utils.DefaultHeadimgurl
  155. }
  156. br.Ret = 200
  157. br.Success = true
  158. br.Msg = "获取成功"
  159. br.Data = resp
  160. }
  161. // @Title 未付费申请试用
  162. // @Description 未付费申请试用
  163. // @Param request body models.ApplyTryReq true "type json string"
  164. // @Success 200
  165. // @router /apply/try [post]
  166. func (this *UserController) ApplyTryOut() {
  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 = "请登录,SysUser Is Empty"
  176. br.Ret = 408
  177. return
  178. }
  179. mobile := user.Mobile
  180. var req models.ApplyTryReq
  181. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  182. if err != nil {
  183. br.Msg = "参数解析异常!"
  184. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  185. return
  186. }
  187. if req.RealName == "" {
  188. req.RealName = user.RealName
  189. }
  190. if req.CompanyName == "" {
  191. req.CompanyName = user.CompanyName
  192. }
  193. uid := user.UserId
  194. var title string
  195. tryType := req.TryType
  196. detailId := req.DetailId
  197. if tryType == "Article" {
  198. detail, err := models.GetArticleDetailById(detailId)
  199. if err != nil {
  200. br.Msg = "获取信息失败"
  201. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  202. return
  203. }
  204. title = detail.Title
  205. }
  206. fmt.Println(title)
  207. //缓存校验
  208. cacheKey := fmt.Sprint("xygx:apply_record:add:", uid)
  209. ttlTime := utils.Rc.GetRedisTTL(cacheKey)
  210. if ttlTime > 0 {
  211. br.Msg = "申请失败,申请过于频繁"
  212. br.ErrMsg = "申请失败,申请过于频繁"
  213. return
  214. }
  215. utils.Rc.SetNX(cacheKey, user.Mobile, time.Second*10)
  216. //判断是否已经申请过
  217. applyCount, err := models.GetApplyRecordCount(uid)
  218. if err != nil && err.Error() != utils.ErrNoRow() {
  219. br.Msg = "获取信息失败"
  220. br.ErrMsg = "判断是否已申请过试用失败,Err:" + err.Error()
  221. return
  222. }
  223. if applyCount > 0 {
  224. br.Msg = "您已提交申请,请耐心等待。"
  225. br.IsSendEmail = false
  226. return
  227. }
  228. //判断是否存在申请
  229. var sellerMobile string
  230. if req.ApplyMethod == 2 {
  231. if req.BusinessCardUrl == "" {
  232. br.Msg = "请上传名片"
  233. return
  234. }
  235. if req.RealName == "" {
  236. br.Msg = "请输入姓名"
  237. return
  238. }
  239. if req.CompanyName == "" {
  240. br.Msg = "请输入公司名称"
  241. return
  242. }
  243. if req.BusinessCardUrl != "" && utils.RunMode == "release" {
  244. card, err := services.GetBusinessCard(req.BusinessCardUrl)
  245. if err != nil {
  246. br.Msg = "名片识别失败"
  247. br.ErrMsg = "名片识别失败,Err:" + err.Error()
  248. return
  249. }
  250. mobileStr := strings.Join(card.WordsResult.MOBILE, ",")
  251. isFlag := true
  252. if mobile != "" {
  253. if strings.Contains(mobileStr, mobile) || mobileStr == "" {
  254. isFlag = true
  255. } else {
  256. isFlag = false
  257. }
  258. }
  259. if !isFlag {
  260. //阿里云识别
  261. if utils.RunMode == "release" {
  262. aliyunResult, err := services.AliyunBusinessCard(req.BusinessCardUrl)
  263. if err != nil {
  264. br.Msg = "识别失败"
  265. br.ErrMsg = "识别失败,Err:" + err.Error()
  266. return
  267. }
  268. if !aliyunResult.Success {
  269. br.Msg = "识别失败"
  270. br.ErrMsg = "识别失败"
  271. return
  272. }
  273. mobileStr := strings.Join(aliyunResult.TelCell, ",")
  274. if mobile != "" {
  275. if strings.Contains(mobileStr, mobile) {
  276. isFlag = true
  277. } else {
  278. isFlag = false
  279. }
  280. }
  281. }
  282. }
  283. if !isFlag {
  284. br.Msg = "名片手机号与所填手机号不匹配,请重新填写"
  285. br.ErrMsg = "mobile:" + mobile
  286. return
  287. }
  288. }
  289. }
  290. //获取销售信息
  291. sellerItem, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
  292. if err != nil && err.Error() != utils.ErrNoRow() {
  293. br.Msg = "申请失败"
  294. br.ErrMsg = "获取销售信息失败,Err:" + err.Error()
  295. return
  296. }
  297. if sellerItem != nil {
  298. sellerMobile = sellerItem.Mobile
  299. //推送模板消息
  300. mobile := user.Mobile
  301. if mobile == "" {
  302. mobile = user.Email
  303. }
  304. }
  305. //用户状态,1:潜在客户 、2:现有客户 、3:FICC客户 、4:现有客户(正式,无对应权限) 、5:现有客户(试用,无对应权限) 、6:现有客户(试用暂停) 、7:现有客户(冻结) 、8:现有客户(流失)?
  306. CompanyIdType := 1
  307. applyMethod := ""
  308. cnf, _ := models.GetConfigByCode("tpl_msg")
  309. if cnf != nil {
  310. if sellerItem != nil {
  311. cnf.ConfigValue = sellerItem.Mobile
  312. companyItem, err := models.GetCompanyDetailById(user.CompanyId)
  313. if err != nil && err.Error() != utils.ErrNoRow() {
  314. br.Msg = "获取信息失败"
  315. br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
  316. return
  317. }
  318. if companyItem != nil && companyItem.CompanyId > 0 {
  319. companyProduct, err := models.GetCompanyProductDetail(user.CompanyId, 2)
  320. if err != nil && err.Error() != utils.ErrNoRow() {
  321. br.Msg = "获取信息失败"
  322. br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
  323. return
  324. }
  325. if companyProduct != nil && companyProduct.IsSuspend == 1 {
  326. CompanyIdType = 6
  327. } else {
  328. switch companyItem.Status {
  329. case "正式":
  330. CompanyIdType = 4
  331. case "试用":
  332. CompanyIdType = 5
  333. case "冻结":
  334. CompanyIdType = 7
  335. case "流失":
  336. CompanyIdType = 8
  337. }
  338. }
  339. applyMethod = companyItem.Status + "客户申请"
  340. if detailId > 0 {
  341. if companyProduct != nil && companyProduct.IsSuspend == 1 {
  342. applyMethod = "试用暂停客户"
  343. } else {
  344. if companyItem.Status == "正式" || companyItem.Status == "试用" {
  345. applyMethod = companyItem.Status + "客户申请,无对应权限"
  346. } else if companyItem.Status == "冻结" || companyItem.Status == "流失" {
  347. applyMethod = companyItem.Status + "客户"
  348. }
  349. }
  350. applyMethod = applyMethod + "," + title
  351. }
  352. }
  353. } else {
  354. //获取销售信息
  355. sellerItem, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 1)
  356. if err != nil && err.Error() != utils.ErrNoRow() {
  357. br.Msg = "申请失败"
  358. br.ErrMsg = "获取销售信息失败,Err:" + err.Error()
  359. return
  360. }
  361. if sellerItem != nil {
  362. CompanyIdType = 3
  363. applyMethod = "FICC客户"
  364. } else {
  365. CompanyIdType = 1
  366. applyMethod = "潜在客户"
  367. }
  368. if detailId > 0 {
  369. applyMethod = applyMethod + "," + title
  370. }
  371. }
  372. openIpItem, _ := models.GetUserRecordByMobile(4, cnf.ConfigValue)
  373. if openIpItem != nil && openIpItem.OpenId != "" {
  374. if req.ApplyMethod != 2 {
  375. req.RealName = user.RealName
  376. req.CompanyName = user.CompanyName
  377. }
  378. utils.FileLog.Info("推送消息 %s %s,%s,%s,%s", req.RealName, req.CompanyName, mobile, openIpItem.OpenId, applyMethod)
  379. go services.SendPermissionApplyTemplateMsg(req.RealName, req.CompanyName, mobile, applyMethod, openIpItem)
  380. }
  381. }
  382. err = models.AddApplyRecord(&req, user.Mobile, user.CompanyName, uid, user.CompanyId, CompanyIdType)
  383. if err != nil {
  384. br.Msg = "申请失败"
  385. br.ErrMsg = "申请失败,Err:" + err.Error()
  386. return
  387. }
  388. //添加成功后,设置5分钟缓存,不允许重复添加
  389. //utils.Rc.SetNX(cacheKey, user.Mobile, time.Second*60)
  390. br.Msg = "申请成功!"
  391. br.Ret = 200
  392. br.Success = true
  393. br.Data = sellerMobile
  394. }