wechat.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "encoding/xml"
  5. "fmt"
  6. "hongze/hongze_mfyx_gzh/models"
  7. "hongze/hongze_mfyx_gzh/services"
  8. "hongze/hongze_mfyx_gzh/utils"
  9. "strconv"
  10. "time"
  11. )
  12. type WechatController struct {
  13. BaseAuthController
  14. }
  15. type WechatCommonController struct {
  16. BaseCommonController
  17. }
  18. // @Title 微信登录公众号接口
  19. // @Description 微信登录公众号接口
  20. // @Param Code query string true "微信唯一编码code"
  21. // @Success 200 {object} models.UserDetailByUserLogin
  22. // @router /loginByGzh [get]
  23. func (this *WechatCommonController) WechatLoginByGzh() {
  24. br := new(models.BaseResponse).Init()
  25. defer func() {
  26. this.Data["json"] = br
  27. this.ServeJSON()
  28. }()
  29. code := this.GetString("Code")
  30. if code == "" {
  31. br.Msg = "参数错误"
  32. br.ErrMsg = "Code 为空"
  33. return
  34. }
  35. var token string
  36. item, err := services.WxGetUserOpenIdByCodeGzh(code)
  37. if err != nil {
  38. br.Msg = "获取用户信息失败"
  39. br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
  40. return
  41. }
  42. if item.Errcode != 0 {
  43. br.Msg = "获取用户信息失败"
  44. br.ErrMsg = "获取access_token 失败 errcode:" + strconv.Itoa(item.Errcode) + " ;errmsg:" + item.Errmsg
  45. return
  46. }
  47. openId := item.Openid
  48. if openId == "" {
  49. br.Msg = "获取用户信息失败"
  50. br.ErrMsg = "获取openid失败,openid:" + item.Openid
  51. return
  52. }
  53. resp := new(models.UserDetailByUserLogin)
  54. accessToken, err := services.GetWxAccessTokenByGzh()
  55. if err != nil {
  56. br.Msg = "获取用户信息失败"
  57. br.ErrMsg = "获取access_token失败,err:" + err.Error()
  58. return
  59. }
  60. if accessToken == "" {
  61. br.Msg = "获取用户信息失败"
  62. br.ErrMsg = "access_token 为空,"
  63. return
  64. }
  65. wxUserInfo, err := services.WxGetUserInfo(openId, accessToken)
  66. if err != nil {
  67. br.Msg = "获取用户信息失败"
  68. br.ErrMsg = "获取微信用户信息失败,Err:" + err.Error()
  69. return
  70. }
  71. if wxUserInfo.Errcode != 0 {
  72. userInfoJson, _ := json.Marshal(wxUserInfo)
  73. br.Msg = "登录失败"
  74. br.ErrMsg = "获取用户信息失败,err:" + string(userInfoJson)
  75. return
  76. }
  77. unionId := wxUserInfo.Unionid
  78. if unionId == "" {
  79. br.Msg = "获取用户信息失败"
  80. br.ErrMsg = "获取unionid失败,unionid:" + wxUserInfo.Unionid
  81. return
  82. }
  83. total, err := models.GetCygxUserRecordCount(openId)
  84. if err != nil {
  85. br.Msg = "获取用户信息失败"
  86. br.ErrMsg = "查询数量失败,Err:" + err.Error()
  87. return
  88. }
  89. user, err := models.GetWxUserItemByUserUnionId(unionId)
  90. if err != nil && err.Error() != utils.ErrNoRow() {
  91. br.Msg = "获取用户信息失败"
  92. br.ErrMsg = "获取本地用户信息失败,Err:" + err.Error()
  93. return
  94. }
  95. items := new(models.CygxMfyxGzhUserRecord)
  96. items.OpenId = openId
  97. items.UnionId = unionId
  98. items.NickName = wxUserInfo.Nickname
  99. items.Sex = wxUserInfo.Sex
  100. items.Province = wxUserInfo.Province
  101. items.City = wxUserInfo.City
  102. items.Country = wxUserInfo.Country
  103. items.Headimgurl = wxUserInfo.Headimgurl
  104. items.CreateTime = time.Now()
  105. if user != nil {
  106. items.CygxUserId = user.UserId
  107. items.CygxBindAccount = user.Mobile
  108. }
  109. if total == 0 {
  110. _, err = models.AddCygxUserRecord(items)
  111. if err != nil {
  112. br.Msg = "获取用户信息失败"
  113. br.ErrMsg = "添加openid失败,Err:" + err.Error()
  114. return
  115. }
  116. }
  117. timeUnix := time.Now().Unix()
  118. timeUnixStr := strconv.FormatInt(timeUnix, 10)
  119. totalItem, err := models.GetTokenByOpenId(openId)
  120. if err != nil && err.Error() != utils.ErrNoRow() {
  121. br.Msg = "获取用户信息失败"
  122. br.ErrMsg = "查询数量失败,Err:" + err.Error()
  123. return
  124. }
  125. if totalItem == nil {
  126. token := utils.MD5(unionId) + utils.MD5(timeUnixStr)
  127. itemsSession := new(models.CygxMfyxGzhSession)
  128. itemsSession.UnionId = unionId
  129. itemsSession.OpenId = openId
  130. itemsSession.AccessToken = token
  131. itemsSession.CreatedTime = time.Now()
  132. itemsSession.LastUpdatedTime = time.Now()
  133. itemsSession.ExpireTime = time.Now().AddDate(0, 3, 0)
  134. if user != nil {
  135. itemsSession.UserId = user.UserId
  136. }
  137. err = models.AddCygxXzsSession(itemsSession)
  138. if err != nil {
  139. br.Msg = "获取用户信息失败"
  140. br.ErrMsg = "添加Token失败,Err:" + err.Error()
  141. return
  142. }
  143. } else {
  144. token = totalItem.AccessToken
  145. }
  146. if user == nil {
  147. resp.HasPermission = 3
  148. } else {
  149. permissionStr, err := models.GetCompanyPermission(user.CompanyId)
  150. if err != nil {
  151. br.Msg = "获取信息失败"
  152. br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
  153. return
  154. }
  155. if permissionStr != "" {
  156. resp.Permission = permissionStr
  157. resp.Mobile = user.Mobile
  158. resp.RealName = user.RealName
  159. resp.CompanyName = user.CompanyName
  160. resp.HasPermission = 1
  161. } else {
  162. resp.Mobile = user.Mobile
  163. resp.RealName = user.RealName
  164. resp.HasPermission = 2
  165. }
  166. resp.Headimgurl = user.HeadimgurlRecord
  167. }
  168. resp.Token = token
  169. br.Ret = 200
  170. br.Success = true
  171. br.Msg = "获取成功"
  172. br.Data = resp
  173. }
  174. // @Title 微信获取签名接口
  175. // @Description 微信获取签名接口
  176. // @Param Url query string true "url地址"
  177. // @Success 200 {object} models.WechatSign
  178. // @router /getWxSign [get]
  179. func (this *WechatCommonController) GetWxSign() {
  180. br := new(models.BaseResponse).Init()
  181. defer func() {
  182. this.Data["json"] = br
  183. this.ServeJSON()
  184. }()
  185. getUrl := this.GetString("Url")
  186. accessToken, err := services.GetWxAccessTokenByGzh()
  187. if err != nil {
  188. br.Msg = "获取用户信息失败"
  189. br.ErrMsg = "获取access_token失败,err:" + err.Error()
  190. return
  191. }
  192. if accessToken == "" {
  193. br.Msg = "获取用户信息失败"
  194. br.ErrMsg = "access_token 为空,"
  195. return
  196. }
  197. ticket, err := services.GetWxTicket(accessToken)
  198. if err != nil {
  199. br.Msg = "获取Ticket失败,请联系客服"
  200. br.ErrMsg = "获取Ticket失败,Err" + err.Error()
  201. return
  202. }
  203. if ticket == "" {
  204. br.Msg = "获取Ticket失败,请联系客服"
  205. br.ErrMsg = "ticket为空" + ticket
  206. return
  207. }
  208. nonceStr := utils.GetRandStringNoSpecialChar(16)
  209. signature, nonceString, timestamp := services.GetWxSignature(ticket, getUrl, nonceStr)
  210. resp := new(models.WechatSign)
  211. resp.AppId = utils.WxPublicAppId
  212. resp.NonceStr = nonceString
  213. resp.Timestamp = timestamp
  214. resp.Url = getUrl
  215. resp.Signature = signature
  216. br.Ret = 200
  217. br.Success = true
  218. br.Msg = "获取签名成功"
  219. br.Data = resp
  220. }
  221. // @Title 获取用户详情
  222. // @Description 获取用户详情接口
  223. // @Success 200 {object} models.UserDetailByUserLogin
  224. // @router /user/detail [get]
  225. func (this *WechatController) UserInfo() {
  226. br := new(models.BaseResponse).Init()
  227. defer func() {
  228. this.Data["json"] = br
  229. this.ServeJSON()
  230. }()
  231. user := this.User
  232. if user == nil {
  233. br.Msg = "请登录"
  234. br.ErrMsg = "请登录,用户信息为空"
  235. br.Ret = 408
  236. return
  237. }
  238. uid := user.UserId
  239. var hasPermission int
  240. detail := new(models.UserDetail)
  241. if uid > 0 {
  242. var err error
  243. detail, err = models.GetUserDetailByUserId(uid)
  244. if err != nil && err.Error() != utils.ErrNoRow() {
  245. br.Msg = "获取信息失败"
  246. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  247. return
  248. }
  249. if detail == nil {
  250. br.Msg = "获取信息失败"
  251. br.ErrMsg = "获取信息失败,Err:用户不存在"
  252. return
  253. }
  254. userRecord, _ := models.GetUserRecordByUserId(uid, utils.WxPlatform)
  255. if userRecord != nil {
  256. detail.NickName = userRecord.NickName
  257. if detail.Headimgurl == "" {
  258. detail.Headimgurl = userRecord.Headimgurl
  259. }
  260. }
  261. if user.CompanyId > 1 {
  262. companyItem, err := models.GetCompanyDetailById(user.CompanyId)
  263. if err != nil && err.Error() != utils.ErrNoRow() {
  264. br.Msg = "获取信息失败"
  265. br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
  266. return
  267. }
  268. if companyItem != nil && companyItem.CompanyId > 1 {
  269. detail.CompanyName = companyItem.CompanyName
  270. detail.SellerName = companyItem.SellerName
  271. detail.SellerMobile = companyItem.Mobile
  272. // 获取用户所在公司剩余的点
  273. companyPointsNum, err := models.GetCompanyPoints(user.CompanyId)
  274. if err != nil && err.Error() != utils.ErrNoRow() {
  275. br.Msg = "获取信息失败"
  276. br.ErrMsg = "获取用户所在公司剩余的点失败,Err:" + err.Error()
  277. return
  278. }
  279. detail.CompanyPointsNum = companyPointsNum
  280. userYanxunaDetail, err := models.GetCygxUserYanxuanPermissionDetailByUserId(user.UserId)
  281. if err != nil && err.Error() != utils.ErrNoRow() {
  282. br.Msg = "获取信息失败"
  283. br.ErrMsg = "获取信息失败,GetCygxUserYanxuanPermissionDetailByUserId Err:" + err.Error()
  284. return
  285. }
  286. if userYanxunaDetail != nil {
  287. detail.PermissionStatus = userYanxunaDetail.Status
  288. detail.StartDate = userYanxunaDetail.StartDate
  289. detail.EndDate = userYanxunaDetail.EndDate
  290. detail.PermissionName = utils.CHART_PERMISSION_NAME_MF_YANXUAN
  291. } else {
  292. //查询研选的权限状态
  293. var condition string
  294. var pars []interface{}
  295. condition += " AND company_id = ? AND status IN ('正式','试用') AND chart_permission_id = ? ORDER BY company_report_permission_id DESC LIMIT 1 "
  296. pars = append(pars, user.CompanyId, utils.CHART_PERMISSION_ID_YANXUAN)
  297. companyReportPermissionDetail, err := models.GetCompanyReportPermissionDetailByCondition(condition, pars)
  298. if err != nil && err.Error() != utils.ErrNoRow() {
  299. br.Msg = "获取信息失败"
  300. br.ErrMsg = "获取用户所在公司剩余的点失败,Err:" + err.Error()
  301. return
  302. }
  303. companyProduct, err := models.GetCompanyProductDetail(user.CompanyId, 2)
  304. if err != nil {
  305. br.Msg = "获取信息失败"
  306. br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
  307. return
  308. }
  309. if companyReportPermissionDetail == nil || companyProduct.IsSuspend == 1 {
  310. hasPermission = 1
  311. } else {
  312. detail.PermissionStatus = companyReportPermissionDetail.Status
  313. detail.StartDate = companyReportPermissionDetail.StartDate
  314. detail.EndDate = companyReportPermissionDetail.EndDate
  315. }
  316. }
  317. } else {
  318. hasPermission = 1
  319. }
  320. } else {
  321. //判断是否已经申请过
  322. applyCount, err := models.GetApplyRecordCount(uid)
  323. if err != nil && err.Error() != utils.ErrNoRow() {
  324. br.Msg = "获取信息失败"
  325. br.ErrMsg = "判断是否已申请过试用失败,Err:" + err.Error()
  326. return
  327. }
  328. if applyCount > 0 {
  329. hasPermission = 3
  330. } else {
  331. hasPermission = 2
  332. }
  333. detail.CompanyName = detail.Note
  334. }
  335. detail.HasPermission = hasPermission
  336. } else {
  337. ur, _ := models.GetUserRecordByOpenId(user.OpenId)
  338. if ur != nil {
  339. detail.NickName = ur.NickName
  340. detail.Email = ur.BindAccount
  341. detail.Mobile = ur.BindAccount
  342. detail.NickName = ur.NickName
  343. detail.Headimgurl = ur.Headimgurl
  344. }
  345. hasPermission = 2
  346. detail.HasPermission = hasPermission
  347. }
  348. if detail.Headimgurl == "" {
  349. detail.Headimgurl = utils.DefaultHeadimgurl
  350. }
  351. br.Ret = 200
  352. br.Success = true
  353. br.Msg = "获取成功"
  354. br.Data = detail
  355. }
  356. // @Title 微信获取签名接口
  357. // @Description 微信获取签名接口
  358. // @Param Url query string true "url地址"
  359. // @Success 200 {object} models.WechatSign
  360. // @router /notify [get,post]
  361. func (this *WechatCommonController) Notify() {
  362. //echostr := this.GetString("echostr")
  363. method := this.Ctx.Input.Method()
  364. if method == "POST" {
  365. body := this.Ctx.Input.RequestBody
  366. utils.FileLog.Info("wechat notify:" + string(body))
  367. item := new(models.Notify)
  368. err := xml.Unmarshal(body, &item)
  369. if err != nil {
  370. utils.FileLog.Info("xml.Unmarshal:" + err.Error())
  371. }
  372. contactMsg := "感谢慧眼识金的你\r\n\r\n【登录】后就可以收到我们近期所有的研究更新与活动提醒哦~\r\n\r\n想降噪请点击【推送规则】,我们将只为您推送所关注赛道的最新内容\r\n\r\n过往所有纪要和可交互图表请戳【投研素材】 "
  373. var openId, returnResult string
  374. if item.MsgType != "" {
  375. openId = item.FromUserName
  376. }
  377. xmlTpl := `<xml>
  378. <ToUserName><![CDATA[%s]]></ToUserName>
  379. <FromUserName><![CDATA[%s]]></FromUserName>
  380. <CreateTime>%s</CreateTime>
  381. <MsgType><![CDATA[text]]></MsgType>
  382. <Content><![CDATA[%s]]></Content>
  383. </xml>`
  384. createTime := strconv.FormatInt(time.Now().Unix(), 10)
  385. xmlTpl = fmt.Sprintf(xmlTpl, openId, utils.WxId, createTime, contactMsg)
  386. total, err := models.GetCygxUserRecordCount(openId)
  387. if err != nil {
  388. utils.FileLog.Info("GetCygxUserRecordCount:" + err.Error())
  389. }
  390. var unionId string
  391. if total == 0 {
  392. accessToken, err := services.GetWxAccessTokenByGzh()
  393. if err != nil {
  394. utils.FileLog.Info("accessToken:" + err.Error())
  395. }
  396. if accessToken == "" {
  397. utils.FileLog.Info("access_token 为空 openId:" + openId)
  398. }
  399. wxUserInfo, err := services.WxGetUserInfo(openId, accessToken)
  400. if err != nil {
  401. utils.FileLog.Info("accessToken:" + err.Error())
  402. return
  403. }
  404. if wxUserInfo.Errcode != 0 {
  405. userInfoJson, _ := json.Marshal(wxUserInfo)
  406. utils.FileLog.Info("获取用户信息失败,err:" + string(userInfoJson))
  407. }
  408. unionId = wxUserInfo.Unionid
  409. if unionId != "" {
  410. services.AddCygxUserRecord(wxUserInfo)
  411. }
  412. } else {
  413. userRecordDetail, err := models.GetCygxUserRecordByOpenid(openId)
  414. if err != nil {
  415. utils.FileLog.Info("GetCygxUserRecordByOpenid:" + err.Error())
  416. return
  417. }
  418. unionId = userRecordDetail.UnionId
  419. }
  420. if unionId == "" {
  421. utils.FileLog.Info("获取unionid失败,openId:" + openId)
  422. return
  423. }
  424. wxUser, err := models.GetUserRecordByUnionId(unionId)
  425. if err != nil {
  426. utils.FileLog.Info("GetUserDetailBuOpenid:" + err.Error())
  427. }
  428. if wxUser == nil {
  429. utils.FileLog.Info("用户不存在openId:" + openId)
  430. return
  431. }
  432. if item.MsgType == "event" {
  433. switch item.Event {
  434. case "subscribe":
  435. fmt.Println("关注")
  436. go models.UserSubscribe(1, wxUser.UserId)
  437. go models.CygxUserSubscribe(1, unionId)
  438. break
  439. case "unsubscribe":
  440. fmt.Println("取消关注")
  441. go models.UserSubscribe(0, wxUser.UserId)
  442. go models.CygxUserSubscribe(0, unionId)
  443. break
  444. case "CLICK":
  445. returnResult = xmlTpl
  446. break
  447. default:
  448. utils.FileLog.Info("wechat notify event:" + item.Event)
  449. }
  450. this.Ctx.WriteString(xmlTpl)
  451. } else if item.MsgType == "text" {
  452. returnResult = xmlTpl
  453. this.Ctx.WriteString(returnResult)
  454. } else {
  455. returnResult = xmlTpl
  456. }
  457. returnResult = "hongze"
  458. this.Ctx.WriteString(returnResult)
  459. } else {
  460. this.Ctx.WriteString("hongze")
  461. }
  462. }