wechat.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "encoding/xml"
  5. "fmt"
  6. "hongze/hongze_cygxzs/models"
  7. "hongze/hongze_cygxzs/services"
  8. "hongze/hongze_cygxzs/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 /loginByxzs [get]
  23. func (this *WechatCommonController) WechatLoginByxzs() {
  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.WxGetUserOpenIdByCodeXzs(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.GetWxAccessTokenByXzs()
  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.CygxUserRecord)
  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. } else {
  117. err = models.UpdateCygxUserRecordMobileByOpenId(user.UserId, user.Mobile, openId)
  118. if err != nil {
  119. br.Msg = "获取用户信息失败"
  120. br.ErrMsg = "更新openid绑定关系失败,Err:" + err.Error()
  121. return
  122. }
  123. }
  124. timeUnix := time.Now().Unix()
  125. timeUnixStr := strconv.FormatInt(timeUnix, 10)
  126. totalItem, err := models.GetTokenByOpenId(openId)
  127. if err != nil && err.Error() != utils.ErrNoRow() {
  128. br.Msg = "获取用户信息失败"
  129. br.ErrMsg = "查询数量失败,Err:" + err.Error()
  130. return
  131. }
  132. if totalItem == nil {
  133. token := utils.MD5(unionId) + utils.MD5(timeUnixStr)
  134. itemsSession := new(models.CygxXzsSession)
  135. itemsSession.UnionId = unionId
  136. itemsSession.OpenId = openId
  137. itemsSession.AccessToken = token
  138. itemsSession.CreatedTime = time.Now()
  139. itemsSession.LastUpdatedTime = time.Now()
  140. itemsSession.ExpireTime = time.Now().AddDate(0, 3, 0)
  141. if user != nil {
  142. itemsSession.UserId = user.UserId
  143. }
  144. err = models.AddCygxXzsSession(itemsSession)
  145. if err != nil {
  146. br.Msg = "获取用户信息失败"
  147. br.ErrMsg = "添加Token失败,Err:" + err.Error()
  148. return
  149. }
  150. } else {
  151. token = totalItem.AccessToken
  152. }
  153. if user == nil {
  154. resp.HasPermission = 3
  155. } else {
  156. permissionStr, err := models.GetCompanyPermission(user.CompanyId)
  157. if err != nil {
  158. br.Msg = "获取信息失败"
  159. br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
  160. return
  161. }
  162. if permissionStr != "" {
  163. resp.Permission = permissionStr
  164. resp.Mobile = user.Mobile
  165. resp.RealName = user.RealName
  166. resp.CompanyName = user.CompanyName
  167. resp.HasPermission = 1
  168. } else {
  169. resp.Mobile = user.Mobile
  170. resp.RealName = user.RealName
  171. resp.HasPermission = 2
  172. }
  173. resp.Headimgurl = user.HeadimgurlRecord
  174. }
  175. resp.Token = token
  176. br.Ret = 200
  177. br.Success = true
  178. br.Msg = "获取成功"
  179. br.Data = resp
  180. }
  181. // @Title 微信获取签名接口
  182. // @Description 微信获取签名接口
  183. // @Param Url query string true "url地址"
  184. // @Success 200 {object} models.WechatSign
  185. // @router /getWxSign [get]
  186. func (this *WechatCommonController) GetWxSign() {
  187. br := new(models.BaseResponse).Init()
  188. defer func() {
  189. this.Data["json"] = br
  190. this.ServeJSON()
  191. }()
  192. getUrl := this.GetString("Url")
  193. accessToken, err := services.GetWxAccessTokenByXzs()
  194. if err != nil {
  195. br.Msg = "获取用户信息失败"
  196. br.ErrMsg = "获取access_token失败,err:" + err.Error()
  197. return
  198. }
  199. if accessToken == "" {
  200. br.Msg = "获取用户信息失败"
  201. br.ErrMsg = "access_token 为空,"
  202. return
  203. }
  204. ticket, err := services.GetWxTicket(accessToken)
  205. if err != nil {
  206. br.Msg = "获取Ticket失败,请联系客服"
  207. br.ErrMsg = "获取Ticket失败,Err" + err.Error()
  208. return
  209. }
  210. if ticket == "" {
  211. br.Msg = "获取Ticket失败,请联系客服"
  212. br.ErrMsg = "ticket为空" + ticket
  213. return
  214. }
  215. nonceStr := utils.GetRandStringNoSpecialChar(16)
  216. signature, nonceString, timestamp := services.GetWxSignature(ticket, getUrl, nonceStr)
  217. resp := new(models.WechatSign)
  218. resp.AppId = utils.WxPublicAppId
  219. resp.NonceStr = nonceString
  220. resp.Timestamp = timestamp
  221. resp.Url = getUrl
  222. resp.Signature = signature
  223. br.Ret = 200
  224. br.Success = true
  225. br.Msg = "获取签名成功"
  226. br.Data = resp
  227. }
  228. // @Title 获取用户详情
  229. // @Description 获取用户详情接口
  230. // @Success 200 {object} models.UserDetailByUserLogin
  231. // @router /user/detail [get]
  232. func (this *WechatController) UserInfo() {
  233. br := new(models.BaseResponse).Init()
  234. defer func() {
  235. this.Data["json"] = br
  236. this.ServeJSON()
  237. }()
  238. user := this.User
  239. if user == nil {
  240. br.Msg = "请登录"
  241. br.ErrMsg = "请登录,用户信息为空"
  242. br.Ret = 408
  243. return
  244. }
  245. resp := new(models.UserDetailByUserLogin)
  246. if user.Mobile == "" {
  247. resp.HasPermission = 3
  248. } else {
  249. permissionStr, err := models.GetCompanyPermission(user.CompanyId)
  250. if err != nil {
  251. br.Msg = "获取信息失败"
  252. br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
  253. return
  254. }
  255. if permissionStr != "" {
  256. resp.Permission = permissionStr
  257. resp.Mobile = user.Mobile
  258. resp.RealName = user.RealName
  259. resp.CompanyName = user.CompanyName
  260. resp.HasPermission = 1
  261. //fmt.Println(user)
  262. go services.UpdateCygxUserRecordMobile(user.Mobile, user.UserId) //更新用户手机号与Openid绑定关系
  263. } else {
  264. resp.Mobile = user.Mobile
  265. resp.RealName = user.RealName
  266. resp.HasPermission = 2
  267. }
  268. resp.Headimgurl = user.Headimgurl
  269. }
  270. br.Ret = 200
  271. br.Success = true
  272. br.Msg = "获取成功"
  273. br.Data = resp
  274. }
  275. // @Title 微信获取签名接口
  276. // @Description 微信获取签名接口
  277. // @Param Url query string true "url地址"
  278. // @Success 200 {object} models.WechatSign
  279. // @router /notify [get,post]
  280. func (this *WechatCommonController) Notify() {
  281. echostr := this.GetString("echostr")
  282. method := this.Ctx.Input.Method()
  283. if method == "POST" {
  284. body := this.Ctx.Input.RequestBody
  285. utils.FileLog.Info("wechat notify:" + string(body))
  286. item := new(models.Notify)
  287. err := xml.Unmarshal(body, &item)
  288. if err != nil {
  289. utils.FileLog.Info("xml.Unmarshal:" + err.Error())
  290. }
  291. contactMsg := "感谢慧眼识金的你\r\n\r\n【登录】后就可以收到我们近期所有的研究更新与活动提醒哦~\r\n\r\n想降噪请点击【推送规则】,我们将只为您推送所关注赛道的最新内容\r\n\r\n过往所有纪要和可交互图表请戳【投研素材】 "
  292. var openId, returnResult string
  293. if item.MsgType != "" {
  294. openId = item.FromUserName
  295. }
  296. xmlTpl := `<xml>
  297. <ToUserName><![CDATA[%s]]></ToUserName>
  298. <FromUserName><![CDATA[%s]]></FromUserName>
  299. <CreateTime>%s</CreateTime>
  300. <MsgType><![CDATA[text]]></MsgType>
  301. <Content><![CDATA[%s]]></Content>
  302. </xml>`
  303. createTime := strconv.FormatInt(time.Now().Unix(), 10)
  304. xmlTpl = fmt.Sprintf(xmlTpl, openId, utils.WxId, createTime, contactMsg)
  305. total, err := models.GetCygxUserRecordCount(openId)
  306. if err != nil {
  307. utils.FileLog.Info("GetCygxUserRecordCount:" + err.Error())
  308. }
  309. var unionId string
  310. if total == 0 {
  311. accessToken, err := services.GetWxAccessTokenByXzs()
  312. if err != nil {
  313. utils.FileLog.Info("accessToken:" + err.Error())
  314. }
  315. if accessToken == "" {
  316. utils.FileLog.Info("access_token 为空 openId:" + openId)
  317. }
  318. wxUserInfo, err := services.WxGetUserInfo(openId, accessToken)
  319. if err != nil {
  320. utils.FileLog.Info("accessToken:" + err.Error())
  321. return
  322. }
  323. if wxUserInfo.Errcode != 0 {
  324. userInfoJson, _ := json.Marshal(wxUserInfo)
  325. utils.FileLog.Info("获取用户信息失败,err:" + string(userInfoJson))
  326. }
  327. unionId = wxUserInfo.Unionid
  328. if unionId != "" {
  329. services.AddCygxUserRecord(wxUserInfo)
  330. }
  331. } else {
  332. userRecordDetail, err := models.GetCygxUserRecordByOpenid(openId)
  333. if err != nil {
  334. utils.FileLog.Info("GetCygxUserRecordByOpenid:" + err.Error())
  335. return
  336. }
  337. unionId = userRecordDetail.UnionId
  338. }
  339. if unionId == "" {
  340. utils.FileLog.Info("获取unionid失败,openId:" + openId)
  341. return
  342. }
  343. wxUser, err := models.GetUserRecordByUnionId(unionId)
  344. if err != nil {
  345. utils.FileLog.Info("GetUserDetailBuOpenid:" + err.Error())
  346. }
  347. if wxUser == nil {
  348. utils.FileLog.Info("用户不存在openId:" + openId)
  349. return
  350. }
  351. if item.MsgType == "event" {
  352. switch item.Event {
  353. case "subscribe":
  354. fmt.Println("关注")
  355. go models.UserSubscribe(1, wxUser.UserId)
  356. go models.CygxUserSubscribe(1, unionId)
  357. break
  358. case "unsubscribe":
  359. fmt.Println("取消关注")
  360. go models.UserSubscribe(0, wxUser.UserId)
  361. go models.CygxUserSubscribe(0, unionId)
  362. break
  363. case "CLICK":
  364. returnResult = xmlTpl
  365. break
  366. default:
  367. utils.FileLog.Info("wechat notify event:" + item.Event)
  368. }
  369. this.Ctx.WriteString(xmlTpl)
  370. } else if item.MsgType == "text" {
  371. returnResult = xmlTpl
  372. this.Ctx.WriteString(returnResult)
  373. } else {
  374. returnResult = xmlTpl
  375. }
  376. this.Ctx.WriteString(returnResult)
  377. } else {
  378. this.Ctx.WriteString(echostr)
  379. }
  380. }