wechat.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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+"appid:" + utils.WxPublicAppId+"secret:"+utils.WxPublicAppSecret
  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. mfyxUserRecord, err := models.GetCygxUserRecordByOpenid(openId)
  84. if err != nil && err.Error() != utils.ErrNoRow() {
  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. items.Subscribe = 1
  106. items.SubscribeTime = time.Now()
  107. if user != nil {
  108. items.CygxUserId = user.UserId
  109. items.CygxBindAccount = user.Mobile
  110. if mfyxUserRecord != nil && mfyxUserRecord.CygxUserId != user.UserId {
  111. err = models.UpdateCygxUserRecordMobile(user.UserId, user.Mobile, unionId)
  112. if err != nil {
  113. br.Msg = "修复用户信息失败"
  114. br.ErrMsg = "修复用户信息失败,Err:" + err.Error()
  115. return
  116. }
  117. }
  118. }
  119. userRecord := &models.UserRecord{
  120. OpenId: openId, //用户open_id
  121. UnionId: unionId, //用户union_id
  122. Subscribe: 0,
  123. NickName: wxUserInfo.Nickname, //用户昵称,最大长度:32
  124. RealName: "", //用户实际名称,最大长度:32
  125. Sex: wxUserInfo.Sex, //普通用户性别,1为男性,2为女性
  126. Province: wxUserInfo.Province, //普通用户个人资料填写的省份,最大长度:30
  127. City: wxUserInfo.City, //普通用户个人资料填写的城市,最大长度:30
  128. Country: wxUserInfo.Country, //国家,如中国为CN,最大长度:30
  129. Headimgurl: wxUserInfo.Headimgurl, //用户第三方(微信)头像,最大长度:512
  130. CreateTime: time.Now(), //创建时间,关系添加时间、用户授权时间
  131. CreatePlatform: 12, //注册平台,1:日度点评公众号,2:管理后台,3:pc端网站,4:查研观向小程序;默认:1
  132. SessionKey: wxUserInfo.SessionKey, //微信小程序会话密钥,最大长度:255
  133. }
  134. if mfyxUserRecord == nil {
  135. _, err = models.AddMfyxGzhUserRecord(items)
  136. if err != nil {
  137. br.Msg = "获取用户信息失败"
  138. br.ErrMsg = "添加openid失败,Err:" + err.Error()
  139. return
  140. }
  141. _, err = models.AddUserRecord(userRecord)
  142. if err != nil {
  143. return
  144. }
  145. }
  146. timeUnix := time.Now().Unix()
  147. timeUnixStr := strconv.FormatInt(timeUnix, 10)
  148. totalItem, err := models.GetTokenByOpenId(openId)
  149. if err != nil && err.Error() != utils.ErrNoRow() {
  150. br.Msg = "获取用户信息失败"
  151. br.ErrMsg = "查询数量失败,Err:" + err.Error()
  152. return
  153. }
  154. if totalItem == nil {
  155. token := utils.MD5(unionId) + utils.MD5(timeUnixStr)
  156. itemsSession := new(models.CygxMfyxGzhSession)
  157. itemsSession.UnionId = unionId
  158. itemsSession.OpenId = openId
  159. itemsSession.AccessToken = token
  160. itemsSession.CreatedTime = time.Now()
  161. itemsSession.LastUpdatedTime = time.Now()
  162. itemsSession.ExpireTime = time.Now().AddDate(0, 3, 0)
  163. if user != nil {
  164. itemsSession.UserId = user.UserId
  165. }
  166. err = models.AddCygxXzsSession(itemsSession)
  167. if err != nil {
  168. br.Msg = "获取用户信息失败"
  169. br.ErrMsg = "添加Token失败,Err:" + err.Error()
  170. return
  171. }
  172. } else {
  173. token = totalItem.AccessToken
  174. }
  175. if user == nil {
  176. resp.HasPermission = 3
  177. } else {
  178. permissionStr, err := models.GetCompanyPermission(user.CompanyId)
  179. if err != nil {
  180. br.Msg = "获取信息失败"
  181. br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
  182. return
  183. }
  184. if permissionStr != "" {
  185. resp.Permission = permissionStr
  186. resp.Mobile = user.Mobile
  187. resp.RealName = user.RealName
  188. resp.CompanyName = user.CompanyName
  189. resp.HasPermission = 1
  190. } else {
  191. resp.Mobile = user.Mobile
  192. resp.RealName = user.RealName
  193. resp.HasPermission = 2
  194. }
  195. resp.Headimgurl = user.HeadimgurlRecord
  196. }
  197. if resp.Headimgurl == "" {
  198. resp.Headimgurl = utils.DefaultHeadimgurl
  199. }
  200. resp.Token = token
  201. br.Ret = 200
  202. br.Success = true
  203. br.Msg = "获取成功"
  204. br.Data = resp
  205. }
  206. // @Title 微信获取签名接口
  207. // @Description 微信获取签名接口
  208. // @Param Url query string true "url地址"
  209. // @Success 200 {object} models.WechatSign
  210. // @router /getWxSign [get]
  211. func (this *WechatCommonController) GetWxSign() {
  212. br := new(models.BaseResponse).Init()
  213. defer func() {
  214. this.Data["json"] = br
  215. this.ServeJSON()
  216. }()
  217. getUrl := this.GetString("Url")
  218. accessToken, err := services.GetWxAccessTokenByGzh()
  219. if err != nil {
  220. br.Msg = "获取用户信息失败"
  221. br.ErrMsg = "获取access_token失败,err:" + err.Error()
  222. return
  223. }
  224. if accessToken == "" {
  225. br.Msg = "获取用户信息失败"
  226. br.ErrMsg = "access_token 为空,"
  227. return
  228. }
  229. ticket, err := services.GetWxTicket(accessToken)
  230. if err != nil {
  231. br.Msg = "获取Ticket失败,请联系客服"
  232. br.ErrMsg = "获取Ticket失败,Err" + err.Error()
  233. return
  234. }
  235. if ticket == "" {
  236. br.Msg = "获取Ticket失败,请联系客服"
  237. br.ErrMsg = "ticket为空" + ticket
  238. return
  239. }
  240. nonceStr := utils.GetRandStringNoSpecialChar(16)
  241. signature, nonceString, timestamp := services.GetWxSignature(ticket, getUrl, nonceStr)
  242. resp := new(models.WechatSign)
  243. resp.AppId = utils.WxPublicAppId
  244. resp.NonceStr = nonceString
  245. resp.Timestamp = timestamp
  246. resp.Url = getUrl
  247. resp.Signature = signature
  248. br.Ret = 200
  249. br.Success = true
  250. br.Msg = "获取签名成功"
  251. br.Data = resp
  252. }
  253. // @Title 获取用户详情
  254. // @Description 获取用户详情接口
  255. // @Success 200 {object} models.UserDetailByUserLogin
  256. // @router /user/detail [get]
  257. func (this *WechatController) UserInfo() {
  258. br := new(models.BaseResponse).Init()
  259. defer func() {
  260. this.Data["json"] = br
  261. this.ServeJSON()
  262. }()
  263. user := this.User
  264. if user == nil {
  265. br.Msg = "请登录"
  266. br.ErrMsg = "请登录,用户信息为空"
  267. br.Ret = 408
  268. return
  269. }
  270. uid := user.UserId
  271. var hasPermission int
  272. detail := new(models.UserDetail)
  273. if uid > 0 {
  274. var err error
  275. detail, err = models.GetUserDetailByUserId(uid)
  276. if err != nil && err.Error() != utils.ErrNoRow() {
  277. br.Msg = "获取信息失败"
  278. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  279. return
  280. }
  281. if detail == nil {
  282. br.Msg = "获取信息失败"
  283. br.ErrMsg = "获取信息失败,Err:用户不存在"
  284. return
  285. }
  286. userRecord, _ := models.GetUserRecordByUserId(uid, utils.WxPlatform)
  287. if userRecord != nil {
  288. detail.NickName = userRecord.NickName
  289. if detail.Headimgurl == "" {
  290. detail.Headimgurl = userRecord.Headimgurl
  291. }
  292. }
  293. if user.CompanyId > 1 {
  294. companyItem, err := models.GetCompanyDetailById(user.CompanyId)
  295. if err != nil && err.Error() != utils.ErrNoRow() {
  296. br.Msg = "获取信息失败"
  297. br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
  298. return
  299. }
  300. if companyItem != nil && companyItem.CompanyId > 1 {
  301. detail.CompanyName = companyItem.CompanyName
  302. detail.SellerName = companyItem.SellerName
  303. detail.SellerMobile = companyItem.Mobile
  304. // 获取用户所在公司剩余的点
  305. companyPointsNum, err := models.GetCompanyPoints(user.CompanyId)
  306. if err != nil && err.Error() != utils.ErrNoRow() {
  307. br.Msg = "获取信息失败"
  308. br.ErrMsg = "获取用户所在公司剩余的点失败,Err:" + err.Error()
  309. return
  310. }
  311. detail.CompanyPointsNum = companyPointsNum
  312. userYanxunaDetail, err := models.GetCygxUserYanxuanPermissionDetailByUserId(user.UserId)
  313. if err != nil && err.Error() != utils.ErrNoRow() {
  314. br.Msg = "获取信息失败"
  315. br.ErrMsg = "获取信息失败,GetCygxUserYanxuanPermissionDetailByUserId Err:" + err.Error()
  316. return
  317. }
  318. if userYanxunaDetail != nil {
  319. detail.PermissionStatus = userYanxunaDetail.Status
  320. detail.StartDate = userYanxunaDetail.StartDate
  321. detail.EndDate = userYanxunaDetail.EndDate
  322. detail.PermissionName = utils.CHART_PERMISSION_NAME_MF_YANXUAN
  323. } else {
  324. //查询研选的权限状态
  325. var condition string
  326. var pars []interface{}
  327. condition += " AND company_id = ? AND status IN ('正式','试用') AND chart_permission_id = ? ORDER BY company_report_permission_id DESC LIMIT 1 "
  328. pars = append(pars, user.CompanyId, utils.CHART_PERMISSION_ID_YANXUAN)
  329. companyReportPermissionDetail, err := models.GetCompanyReportPermissionDetailByCondition(condition, pars)
  330. if err != nil && err.Error() != utils.ErrNoRow() {
  331. br.Msg = "获取信息失败"
  332. br.ErrMsg = "获取用户所在公司剩余的点失败,Err:" + err.Error()
  333. return
  334. }
  335. companyProduct, err := models.GetCompanyProductDetail(user.CompanyId, 2)
  336. if err != nil {
  337. br.Msg = "获取信息失败"
  338. br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
  339. return
  340. }
  341. if companyReportPermissionDetail == nil || companyProduct.IsSuspend == 1 {
  342. hasPermission = 1
  343. } else {
  344. detail.PermissionStatus = companyReportPermissionDetail.Status
  345. detail.StartDate = companyReportPermissionDetail.StartDate
  346. detail.EndDate = companyReportPermissionDetail.EndDate
  347. }
  348. }
  349. } else {
  350. hasPermission = 1
  351. }
  352. } else {
  353. //判断是否已经申请过
  354. applyCount, err := models.GetApplyRecordCount(uid)
  355. if err != nil && err.Error() != utils.ErrNoRow() {
  356. br.Msg = "获取信息失败"
  357. br.ErrMsg = "判断是否已申请过试用失败,Err:" + err.Error()
  358. return
  359. }
  360. if applyCount > 0 {
  361. hasPermission = 3
  362. } else {
  363. hasPermission = 2
  364. }
  365. detail.CompanyName = detail.Note
  366. }
  367. detail.HasPermission = hasPermission
  368. } else {
  369. ur, _ := models.GetCygxUserRecordByOpenid(user.OpenId)
  370. if ur != nil {
  371. detail.NickName = ur.NickName
  372. detail.Email = ur.CygxBindAccount
  373. detail.Mobile = ur.CygxBindAccount
  374. detail.NickName = ur.NickName
  375. detail.Headimgurl = ur.Headimgurl
  376. }
  377. hasPermission = 2
  378. detail.HasPermission = hasPermission
  379. }
  380. if detail.Headimgurl == "" {
  381. detail.Headimgurl = utils.DefaultHeadimgurl
  382. }
  383. br.Ret = 200
  384. br.Success = true
  385. br.Msg = "获取成功"
  386. br.Data = detail
  387. }
  388. // @Title 微信获取签名接口
  389. // @Description 微信获取签名接口
  390. // @Param Url query string true "url地址"
  391. // @Success 200 {object} models.WechatSign
  392. // @router /notify [get,post]
  393. func (this *WechatCommonController) Notify() {
  394. //echostr := this.GetString("echostr")
  395. method := this.Ctx.Input.Method()
  396. if method == "POST" {
  397. body := this.Ctx.Input.RequestBody
  398. utils.FileLog.Info("wechat notify:" + string(body))
  399. item := new(models.Notify)
  400. err := xml.Unmarshal(body, &item)
  401. if err != nil {
  402. utils.FileLog.Info("xml.Unmarshal:" + err.Error())
  403. }
  404. contactMsg := "感谢慧眼识金的你\r\n\r\n【登录】后就可以收到我们近期所有的研究更新与活动提醒哦~\r\n\r\n想降噪请点击【推送规则】,我们将只为您推送所关注赛道的最新内容\r\n\r\n过往所有纪要和可交互图表请戳【投研素材】 "
  405. var openId, returnResult string
  406. if item.MsgType != "" {
  407. openId = item.FromUserName
  408. }
  409. xmlTpl := `<xml>
  410. <ToUserName><![CDATA[%s]]></ToUserName>
  411. <FromUserName><![CDATA[%s]]></FromUserName>
  412. <CreateTime>%s</CreateTime>
  413. <MsgType><![CDATA[text]]></MsgType>
  414. <Content><![CDATA[%s]]></Content>
  415. </xml>`
  416. createTime := strconv.FormatInt(time.Now().Unix(), 10)
  417. xmlTpl = fmt.Sprintf(xmlTpl, openId, utils.WxId, createTime, contactMsg)
  418. total, err := models.GetCygxUserRecordCount(openId)
  419. if err != nil {
  420. utils.FileLog.Info("GetCygxUserRecordCount:" + err.Error())
  421. }
  422. var unionId string
  423. if total == 0 {
  424. accessToken, err := services.GetWxAccessTokenByGzh()
  425. if err != nil {
  426. utils.FileLog.Info("accessToken:" + err.Error())
  427. }
  428. if accessToken == "" {
  429. utils.FileLog.Info("access_token 为空 openId:" + openId)
  430. }
  431. wxUserInfo, err := services.WxGetUserInfo(openId, accessToken)
  432. if err != nil {
  433. utils.FileLog.Info("accessToken:" + err.Error())
  434. return
  435. }
  436. if wxUserInfo.Errcode != 0 {
  437. userInfoJson, _ := json.Marshal(wxUserInfo)
  438. utils.FileLog.Info("获取用户信息失败,err:" + string(userInfoJson))
  439. }
  440. unionId = wxUserInfo.Unionid
  441. if unionId != "" {
  442. services.AddCygxUserRecord(wxUserInfo)
  443. }
  444. } else {
  445. userRecordDetail, err := models.GetCygxUserRecordByOpenid(openId)
  446. if err != nil {
  447. utils.FileLog.Info("GetCygxUserRecordByOpenid:" + err.Error())
  448. return
  449. }
  450. unionId = userRecordDetail.UnionId
  451. }
  452. if unionId == "" {
  453. utils.FileLog.Info("获取unionid失败,openId:" + openId)
  454. return
  455. }
  456. wxUser, err := models.GetUserRecordByUnionId(unionId)
  457. if err != nil {
  458. utils.FileLog.Info("GetUserDetailBuOpenid:" + err.Error())
  459. }
  460. if wxUser == nil {
  461. utils.FileLog.Info("用户不存在openId:" + openId)
  462. return
  463. }
  464. if item.MsgType == "event" {
  465. switch item.Event {
  466. case "subscribe":
  467. fmt.Println("关注")
  468. go models.UserSubscribe(1, wxUser.UserId)
  469. go models.CygxUserSubscribe(1, unionId)
  470. break
  471. case "unsubscribe":
  472. fmt.Println("取消关注")
  473. go models.UserSubscribe(0, wxUser.UserId)
  474. go models.CygxUserSubscribe(0, unionId)
  475. break
  476. case "CLICK":
  477. returnResult = xmlTpl
  478. break
  479. default:
  480. utils.FileLog.Info("wechat notify event:" + item.Event)
  481. }
  482. this.Ctx.WriteString(xmlTpl)
  483. } else if item.MsgType == "text" {
  484. returnResult = xmlTpl
  485. this.Ctx.WriteString(returnResult)
  486. } else {
  487. returnResult = xmlTpl
  488. }
  489. returnResult = "hongze"
  490. this.Ctx.WriteString(returnResult)
  491. } else {
  492. this.Ctx.WriteString("hongze")
  493. }
  494. }