user.go 10 KB

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