wechat.go 11 KB

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