wechat.go 15 KB

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