wechat.go 15 KB

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