wechat.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "encoding/xml"
  5. "fmt"
  6. "hongze/hongze_api/models"
  7. "hongze/hongze_api/services"
  8. "hongze/hongze_api/utils"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. type WechatController struct {
  14. BaseAuthController
  15. }
  16. type WechatCommonController struct {
  17. BaseCommonController
  18. }
  19. // @Title 微信登录接口
  20. // @Description 微信登录接口
  21. // @Param Code query string true "微信唯一编码code"
  22. // @Success 200 {object} models.WxLoginResp
  23. // @router /login [get]
  24. func (this *WechatCommonController) WechatLogin() {
  25. br := new(models.BaseResponse).Init()
  26. defer func() {
  27. this.Data["json"] = br
  28. this.ServeJSON()
  29. }()
  30. resp := new(models.WxLoginResp)
  31. code := this.GetString("Code")
  32. fmt.Println("code:", code)
  33. utils.FileLog.Info("WechatLogin code:%s", code)
  34. wxCodeInfo, err := models.GetWxUserCode(code)
  35. if err == nil && wxCodeInfo != nil && wxCodeInfo.Id > 0 {
  36. utils.FileLog.Info("WechatLogin code exist:%s", code)
  37. resp.UserId = wxCodeInfo.UserId
  38. resp.Code = 0
  39. resp.FirstLogin = wxCodeInfo.FirstLogin
  40. resp.Authorization = wxCodeInfo.Authorization
  41. resp.UserPermission = wxCodeInfo.UserPermission
  42. br.Ret = 200
  43. br.Success = true
  44. br.Msg = "登录成功"
  45. br.Data = resp
  46. return
  47. }
  48. item, err := services.WxGetUserOpenIdByCode(code)
  49. if err != nil {
  50. br.Msg = "获取用户信息失败"
  51. br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
  52. return
  53. }
  54. if item.Errcode != 0 {
  55. br.Msg = "获取用户信息失败"
  56. br.ErrMsg = "获取access_token 失败 errcode:" + strconv.Itoa(item.Errcode) + " ;errmsg:" + item.Errmsg
  57. return
  58. }
  59. openId := item.Openid
  60. if openId == "" {
  61. br.Msg = "获取用户信息失败"
  62. br.ErrMsg = "获取openid失败,openid:" + item.Openid
  63. return
  64. }
  65. accessToken, err, errMsg := services.GetDefaultWxAccessToken()
  66. if err != nil {
  67. br.Msg = "获取用户信息失败"
  68. br.ErrMsg = errMsg
  69. return
  70. }
  71. //获取用户信息
  72. wxUserInfo, err := services.WxGetUserInfo(openId, accessToken)
  73. if err != nil {
  74. br.Msg = "获取用户信息失败"
  75. br.ErrMsg = "获取微信用户信息失败,Err:" + err.Error()
  76. return
  77. }
  78. if wxUserInfo.Errcode != 0 {
  79. userInfoJson, _ := json.Marshal(wxUserInfo)
  80. br.Msg = "登录失败"
  81. br.ErrMsg = "获取用户信息失败,err:" + string(userInfoJson)
  82. return
  83. }
  84. token, userId, firstLogin, permission, err := services.WxLogin(utils.WxPlatform, code, item, wxUserInfo)
  85. if err != nil {
  86. br.Msg = "微信登录失败"
  87. br.ErrMsg = "微信登录失败,err:" + err.Error()
  88. return
  89. }
  90. resp.UserId = userId
  91. resp.Code = 0
  92. resp.FirstLogin = firstLogin
  93. resp.Authorization = token
  94. resp.UserPermission = permission
  95. br.Ret = 200
  96. br.Success = true
  97. br.Msg = "登录成功"
  98. br.Data = resp
  99. //登录日志
  100. {
  101. returnResult, err := json.Marshal(br)
  102. if err != nil {
  103. utils.FileLog.Info(this.Ctx.Input.URI() + " Err:%s" + err.Error())
  104. }
  105. utils.FileLog.Info(this.Ctx.Input.URI()+" code: %s , return data: %s", code, string(returnResult))
  106. }
  107. }
  108. //作废于2021-03-29 10:14:54
  109. //func (this *WechatCommonController) WechatLoginV1() {
  110. // br := new(models.BaseResponse).Init()
  111. // defer func() {
  112. // this.Data["json"] = br
  113. // this.ServeJSON()
  114. // }()
  115. // resp := new(models.WxLoginResp)
  116. //
  117. // code := this.GetString("Code")
  118. // fmt.Println("code:", code)
  119. // utils.FileLog.Info("WechatLogin code:%s", code)
  120. // wxCodeInfo, err := models.GetWxUserCode(code)
  121. // if err == nil && wxCodeInfo != nil && wxCodeInfo.Id > 0 {
  122. // utils.FileLog.Info("WechatLogin code exist:%s", code)
  123. // resp.UserId = wxCodeInfo.UserId
  124. // resp.Code = 0
  125. // resp.FirstLogin = wxCodeInfo.FirstLogin
  126. // resp.Authorization = wxCodeInfo.Authorization
  127. // resp.UserPermission = wxCodeInfo.UserPermission
  128. // br.Ret = 200
  129. // br.Success = true
  130. // br.Msg = "登录成功"
  131. // br.Data = resp
  132. // return
  133. // }
  134. //
  135. // item, err := services.WxGetUserOpenIdByCode(code)
  136. // if err != nil {
  137. // br.Msg = "获取用户信息失败"
  138. // br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
  139. // return
  140. // }
  141. // if item.Errcode != 0 {
  142. // br.Msg = "获取用户信息失败"
  143. // br.ErrMsg = "获取access_token 失败 errcode:" + strconv.Itoa(item.Errcode) + " ;errmsg:" + item.Errmsg
  144. // return
  145. // }
  146. // openId := item.Openid
  147. // accessToken, err := services.WxGetAccessToken()
  148. // if err != nil {
  149. // br.Msg = "获取用户信息失败"
  150. // br.ErrMsg = "获取access_token失败,err:" + err.Error()
  151. // return
  152. // }
  153. // //获取用户信息
  154. // wxUserInfo, err := services.WxGetUserInfo(openId, accessToken)
  155. // if err != nil {
  156. // br.Msg = "获取用户信息失败"
  157. // br.ErrMsg = "获取微信用户信息失败,Err:" + err.Error()
  158. // return
  159. // }
  160. // if wxUserInfo.Errcode != 0 {
  161. // userInfoJson, _ := json.Marshal(wxUserInfo)
  162. // br.Msg = "登录失败"
  163. // br.ErrMsg = "获取用户信息失败,err:" + string(userInfoJson)
  164. // return
  165. // }
  166. //
  167. // unionid := item.Unionid
  168. // if unionid == "" {
  169. // unionid = wxUserInfo.Unionid
  170. // }
  171. // firstLogin := 1
  172. // userId := 0
  173. // utils.FileLog.Info("openId:%s", openId)
  174. // utils.FileLog.Info("unionid:%s", unionid)
  175. // //获取成功
  176. // if openId != "" {
  177. // wxUser, err := models.GetWxUserItemByOpenId(openId)
  178. // if err != nil && err.Error() != utils.ErrNoRow() {
  179. // br.Msg = "获取用户信息失败"
  180. // br.ErrMsg = "根据openid获取用户信息失败,Eerr:" + err.Error()
  181. // return
  182. // }
  183. // if wxUser == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
  184. // user := new(models.WxUser)
  185. // user.OpenId = openId
  186. // user.CompanyId = 1
  187. // user.CreatedTime = time.Now()
  188. // user.UnionId = unionid
  189. // user.Unionid = unionid
  190. // user.NickName = wxUserInfo.Nickname
  191. // user.Sex = wxUserInfo.Sex
  192. // user.City = wxUserInfo.City
  193. // user.Province = wxUserInfo.Province
  194. // user.Country = wxUserInfo.Country
  195. // user.Headimgurl = wxUserInfo.Headimgurl
  196. // user.FirstLogin = 1
  197. // user.Enabled = 1
  198. // user.RegisterPlatform = 1
  199. // user.RegisterTime = time.Now()
  200. // _, err = models.AddWxUser(user)
  201. // wxUser, err = models.GetWxUserItemByOpenId(openId)
  202. // if err != nil {
  203. // br.Msg = "获取用户信息失败"
  204. // br.ErrMsg = "unionid登录,获取微信用户信息失败,Err:" + err.Error()
  205. // return
  206. // }
  207. // userId = wxUser.UserId
  208. // } else {
  209. // firstLogin = wxUser.FirstLogin
  210. // userId = wxUser.UserId
  211. // }
  212. // } else {
  213. // br.Msg = "获取用户信息失败"
  214. // br.ErrMsg = "获取openid失败,openid:" + item.Openid
  215. // return
  216. // }
  217. // permission, err := services.CheckUserPermission(userId)
  218. // if err != nil {
  219. // utils.FileLog.Info("userId:%s,err:%s", strconv.Itoa(userId), err)
  220. // }
  221. // //if err != nil {
  222. // // br.Msg = "登录失败"
  223. // // br.ErrMsg = "登录失败,判断权限失败:" + err.Error()
  224. // // return
  225. // //}
  226. // var token string
  227. // tokenItem, err := models.GetTokenByUid(userId)
  228. // if err != nil && err.Error() != utils.ErrNoRow() {
  229. // br.Msg = "登录失败"
  230. // br.ErrMsg = "登录失败,获取token失败:" + err.Error()
  231. // return
  232. // }
  233. //
  234. // if tokenItem == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
  235. // timeUnix := time.Now().Unix()
  236. // timeUnixStr := strconv.FormatInt(timeUnix, 10)
  237. // token = utils.MD5(strconv.Itoa(userId)) + utils.MD5(timeUnixStr)
  238. // //新增session
  239. // {
  240. // session := new(models.Session)
  241. // session.OpenId = openId
  242. // session.UserId = userId
  243. // session.CreatedTime = time.Now()
  244. // session.LastUpdatedTime = time.Now()
  245. // session.ExpireTime = time.Now().AddDate(0, 3, 0)
  246. // session.AccessToken = token
  247. // err = models.AddSession(session)
  248. // if err != nil {
  249. // br.Msg = "登录失败"
  250. // br.ErrMsg = "登录失败,新增用户session信息失败:" + err.Error()
  251. // return
  252. // }
  253. // }
  254. // } else {
  255. // token = tokenItem.AccessToken
  256. // }
  257. //
  258. // if wxUserInfo != nil {
  259. // go models.ModifyWxUserInfo(wxUserInfo.Nickname, wxUserInfo.Headimgurl, wxUserInfo.City, wxUserInfo.Province, wxUserInfo.Country, wxUserInfo.Sex, userId)
  260. // }
  261. // //firstLogin==1,强制绑定手机号或者邮箱
  262. // {
  263. // newItem, _ := models.GetWxUserItemByUserId(userId)
  264. // if newItem.Mobile == "" && newItem.Email == "" {
  265. // firstLogin = 1
  266. // }
  267. // }
  268. // //新增登录日志
  269. // {
  270. // loginLog := new(models.WxUserLog)
  271. // loginLog.UserId = userId
  272. // loginLog.OpenId = openId
  273. // loginLog.UnionId = unionid
  274. // loginLog.CreateTime = time.Now()
  275. // loginLog.Handle = "wechat_login"
  276. // loginLog.Remark = token
  277. // go models.AddWxUserLog(loginLog)
  278. // }
  279. //
  280. // {
  281. // codeLog := new(models.WxUserCode)
  282. // codeLog.WxCode = code
  283. // codeLog.UserId = userId
  284. // codeLog.Code = 0
  285. // codeLog.FirstLogin = firstLogin
  286. // codeLog.Authorization = token
  287. // codeLog.UserPermission = permission
  288. // codeLog.CreateTime=time.Now()
  289. // models.AddWxUserCode(codeLog)
  290. // }
  291. //
  292. // resp.UserId = userId
  293. // resp.Code = 0
  294. // resp.FirstLogin = firstLogin
  295. // resp.Authorization = token
  296. // resp.UserPermission = permission
  297. // br.Ret = 200
  298. // br.Success = true
  299. // br.Msg = "登录成功"
  300. // br.Data = resp
  301. // //登录日志
  302. // {
  303. // returnResult, err := json.Marshal(br)
  304. // if err != nil {
  305. // utils.FileLog.Info(this.Ctx.Input.URI() + " Err:%s" + err.Error())
  306. // }
  307. // utils.FileLog.Info(this.Ctx.Input.URI()+" code: %s , return data: %s", code, string(returnResult))
  308. // }
  309. //}
  310. // @Title 微信获取签名接口
  311. // @Description 微信获取签名接口
  312. // @Param Url query string true "url地址"
  313. // @Success 200 {object} models.WechatSign
  314. // @router /getWxSign [get]
  315. func (this *WechatController) GetWxSign() {
  316. br := new(models.BaseResponse).Init()
  317. defer func() {
  318. this.Data["json"] = br
  319. this.ServeJSON()
  320. }()
  321. // 微信配置信息
  322. conf, e := models.GetBusinessConf()
  323. if e != nil {
  324. br.Msg = "获取失败"
  325. br.ErrMsg = "获取配置失败, Err: " + e.Error()
  326. return
  327. }
  328. appId := ""
  329. appSecret := ""
  330. if v, ok := conf[models.BusinessConfWxAppId]; ok {
  331. appId = v
  332. }
  333. if v, ok := conf[models.BusinessConfWxAppSecret]; ok {
  334. appSecret = v
  335. }
  336. if appId == "" || appSecret == "" {
  337. br.Msg = "微信公众号信息未配置"
  338. return
  339. }
  340. getUrl := this.GetString("Url")
  341. fmt.Println("getUrl:", getUrl)
  342. accessToken, err, errMsg := services.GetWxAccessToken(appId, appSecret)
  343. if err != nil {
  344. br.Msg = "获取用户信息失败"
  345. br.ErrMsg = "获取access_token失败,err:" + errMsg
  346. return
  347. }
  348. ticket, err := services.GetWxTicket(accessToken)
  349. if err != nil {
  350. br.Msg = "获取Ticket失败,请联系客服"
  351. br.ErrMsg = "获取Ticket失败,Err" + err.Error()
  352. return
  353. }
  354. if ticket == "" {
  355. br.Msg = "获取Ticket失败,请联系客服"
  356. br.ErrMsg = "ticket为空" + ticket
  357. return
  358. }
  359. nonceStr := utils.GetRandStringNoSpecialChar(16)
  360. signature, nonceString, timestamp := services.GetWxSignature(ticket, getUrl, nonceStr)
  361. resp := new(models.WechatSign)
  362. resp.AppId = appId
  363. resp.NonceStr = nonceString
  364. resp.Timestamp = timestamp
  365. resp.Url = getUrl
  366. resp.Signature = signature
  367. br.Ret = 200
  368. br.Success = true
  369. br.Msg = "获取签名成功"
  370. br.Data = resp
  371. }
  372. // @Title 微信获取签名接口
  373. // @Description 微信获取签名接口
  374. // @Param Url query string true "url地址"
  375. // @Success 200 {object} models.WechatSign
  376. // @router /notify [get,post]
  377. func (this *WechatCommonController) Notify() {
  378. echostr := this.GetString("echostr")
  379. method := this.Ctx.Input.Method()
  380. if method == "POST" {
  381. body := this.Ctx.Input.RequestBody
  382. utils.FileLog.Info("wechat notify:" + string(body))
  383. item := new(Notify)
  384. err := xml.Unmarshal(body, &item)
  385. if err != nil {
  386. utils.FileLog.Info("xml.Unmarshal:" + err.Error())
  387. }
  388. contactMsg := "感谢关注弘则研究。\r\n公司地址:上海市世纪大道210号21世纪中心大厦12层1206室\r\n\r\n业务合作:\r\n电话:86-21-61645300\r\n邮箱:service@hzinsights.com\r\n邮编:200120\r\n\r\n海外业务:\r\n邮箱:yyu@hzinsights.com "
  389. var openId, returnResult string
  390. if item.MsgType != "" {
  391. openId = item.FromUserName
  392. }
  393. xmlTpl := `<xml>
  394. <ToUserName><![CDATA[%s]]></ToUserName>
  395. <FromUserName><![CDATA[%s]]></FromUserName>
  396. <CreateTime>%s</CreateTime>
  397. <MsgType><![CDATA[text]]></MsgType>
  398. <Content><![CDATA[%s]]></Content>
  399. </xml>`
  400. createTime := strconv.FormatInt(time.Now().Unix(), 10)
  401. xmlTpl = fmt.Sprintf(xmlTpl, openId, utils.WxId, createTime, contactMsg)
  402. if item.MsgType == "event" {
  403. switch item.Event {
  404. case "subscribe":
  405. fmt.Println("关注")
  406. go subscribe(openId)
  407. break
  408. case "unsubscribe":
  409. fmt.Println("取消关注")
  410. go models.UserSubscribe(0, openId)
  411. break
  412. case "CLICK":
  413. returnResult = xmlTpl
  414. break
  415. default:
  416. utils.FileLog.Info("wechat notify event:" + item.Event)
  417. }
  418. this.Ctx.WriteString(xmlTpl)
  419. } else if item.MsgType == "text" {
  420. textXmlTpl := `<xml>
  421. <ToUserName><![CDATA[%s]]></ToUserName>
  422. <FromUserName><![CDATA[%s]]></FromUserName>
  423. <CreateTime>%s</CreateTime>
  424. <MsgType><![CDATA[text]]></MsgType>
  425. <Content><![CDATA[%s]]></Content>
  426. </xml>`
  427. createTime := strconv.FormatInt(time.Now().Unix(), 10)
  428. classifyArr := utils.ClassifyArr
  429. var flag bool
  430. for _, v := range classifyArr {
  431. if strings.Contains(v, item.Content) || strings.Contains(item.Content, v) {
  432. flag = true
  433. }
  434. }
  435. if flag {
  436. contactMsg = `请点击研究报告-FICC研报,查看报告`
  437. textXmlTpl = fmt.Sprintf(textXmlTpl, item.FromUserName, utils.WxId, createTime, contactMsg)
  438. this.Ctx.WriteString(textXmlTpl)
  439. return
  440. }
  441. reportNameArr := utils.ReportNameArr
  442. for _, v := range reportNameArr {
  443. if strings.Contains(v, item.Content) || strings.Contains(item.Content, v) {
  444. flag = true
  445. }
  446. }
  447. if flag {
  448. contactMsg = `请点击研究报告-FICC研报-研报(左上),查看报告`
  449. textXmlTpl = fmt.Sprintf(textXmlTpl, item.FromUserName, utils.WxId, createTime, contactMsg)
  450. this.Ctx.WriteString(textXmlTpl)
  451. return
  452. }
  453. if strings.Contains("解绑", item.Content) || strings.Contains(item.Content, "解绑") ||
  454. strings.Contains("手机号", item.Content) || strings.Contains(item.Content, "手机号") {
  455. flag = true
  456. }
  457. if flag {
  458. contactMsg = `请通过电话联系我们或者联系销售人员处理`
  459. textXmlTpl = fmt.Sprintf(textXmlTpl, item.FromUserName, utils.WxId, createTime, contactMsg)
  460. this.Ctx.WriteString(textXmlTpl)
  461. return
  462. }
  463. returnResult = xmlTpl
  464. this.Ctx.WriteString(returnResult)
  465. } else {
  466. returnResult = xmlTpl
  467. }
  468. this.Ctx.WriteString(returnResult)
  469. } else {
  470. this.Ctx.WriteString(echostr)
  471. }
  472. }
  473. type Notify struct {
  474. ToUserName string `xml:"ToUserName"`
  475. FromUserName string `xml:"FromUserName"`
  476. CreateTime int `xml:"CreateTime"`
  477. MsgType string `xml:"MsgType"`
  478. Event string `xml:"Event"`
  479. EventKey string `xml:"EventKey"`
  480. Content string `xml:"Content"`
  481. }
  482. // subscribe 关注后的处理逻辑
  483. func subscribe(openId string) {
  484. accessToken, err, _ := services.GetDefaultWxAccessToken()
  485. if err != nil {
  486. fmt.Println("获取access_token失败,err:" + err.Error())
  487. return
  488. }
  489. userRecord, err := models.GetUserRecordByOpenId(openId)
  490. if err != nil && err.Error() != utils.ErrNoRow() {
  491. fmt.Println("通过openid获取user_record记录失败,err:" + err.Error())
  492. return
  493. }
  494. err = nil
  495. // openId已存在
  496. if userRecord != nil {
  497. if userRecord.UserId > 0 { //已经绑定了的话,那么就去修改用户状态
  498. models.UserSubscribe(1, openId)
  499. } else {
  500. // 没有绑定的话,那么校验下unionid,然后再去修改
  501. unionId := userRecord.UnionId
  502. if unionId == `` {
  503. wxUserItem, err := services.WxGetUserInfo(openId, accessToken)
  504. if err != nil {
  505. fmt.Println("获取用户信息失败,err:" + err.Error())
  506. return
  507. }
  508. if wxUserItem.Unionid != `` {
  509. unionId = wxUserItem.Unionid
  510. }
  511. }
  512. updateCol := make([]string, 0)
  513. userRecord.Subscribe = 1
  514. userRecord.SubscribeTime = time.Now()
  515. updateCol = append(updateCol, "Subscribe")
  516. if unionId != `` {
  517. userRecord.UnionId = unionId
  518. // 通过unionid获取已绑定用户的user_record信息
  519. bindUserRecord, _ := models.GetBindUserRecordByUnionId(unionId)
  520. if bindUserRecord != nil {
  521. userRecord.UserId = bindUserRecord.UserId
  522. userRecord.RealName = bindUserRecord.RealName
  523. userRecord.BindAccount = bindUserRecord.BindAccount
  524. userRecord.Sex = bindUserRecord.Sex
  525. updateCol = append(updateCol, "UserId", "RealName", "BindAccount")
  526. }
  527. }
  528. err = userRecord.Update(updateCol)
  529. if err != nil {
  530. fmt.Println("关注后,通过openid更新user_record异常,ERR:", err)
  531. }
  532. }
  533. return
  534. }
  535. // 没有记录,那么需要获取下unionid
  536. wxUserItem, err := services.WxGetUserInfo(openId, accessToken)
  537. if err != nil {
  538. fmt.Println("获取用户信息失败,err:" + err.Error())
  539. return
  540. }
  541. newUserRecord := &models.UserRecord{
  542. UserRecordId: 0,
  543. OpenId: openId,
  544. UnionId: wxUserItem.Unionid,
  545. Subscribe: 1,
  546. SubscribeTime: time.Now(),
  547. NickName: wxUserItem.Nickname,
  548. Sex: wxUserItem.Sex,
  549. Province: wxUserItem.Province,
  550. City: wxUserItem.City,
  551. Country: wxUserItem.Country,
  552. Headimgurl: wxUserItem.Headimgurl,
  553. CreateTime: time.Now(),
  554. CreatePlatform: 1,
  555. SessionKey: "",
  556. }
  557. if wxUserItem.Unionid != `` {
  558. // 通过unionid获取已绑定用户的user_record信息
  559. bindUserRecord, _ := models.GetBindUserRecordByUnionId(wxUserItem.Unionid)
  560. if bindUserRecord != nil {
  561. newUserRecord.UserId = bindUserRecord.UserId
  562. newUserRecord.RealName = bindUserRecord.RealName
  563. newUserRecord.BindAccount = bindUserRecord.BindAccount
  564. newUserRecord.Sex = bindUserRecord.Sex
  565. newUserRecord.Province = bindUserRecord.Province
  566. newUserRecord.City = bindUserRecord.City
  567. newUserRecord.Country = bindUserRecord.Country
  568. newUserRecord.Headimgurl = bindUserRecord.Headimgurl
  569. }
  570. }
  571. _, err = models.AddUserRecord(newUserRecord)
  572. if err != nil {
  573. fmt.Println("关注后,添加user_record信息失败,err:" + err.Error())
  574. return
  575. }
  576. }
  577. // @Title 微信获取签名接口-无token校验
  578. // @Description 微信获取签名接口
  579. // @Param Url query string true "url地址"
  580. // @Success 200 {object} models.WechatSign
  581. // @router /open/getWxSign [get]
  582. func (this *WechatCommonController) GetWxSign() {
  583. br := new(models.BaseResponse).Init()
  584. defer func() {
  585. this.Data["json"] = br
  586. this.ServeJSON()
  587. }()
  588. // 微信配置信息
  589. conf, e := models.GetBusinessConf()
  590. if e != nil {
  591. br.Msg = "获取失败"
  592. br.ErrMsg = "获取配置信息失败, Err: " + e.Error()
  593. return
  594. }
  595. appId := ""
  596. appSecret := ""
  597. if v, ok := conf[models.BusinessConfWxAppId]; ok {
  598. appId = v
  599. }
  600. if v, ok := conf[models.BusinessConfWxAppSecret]; ok {
  601. appSecret = v
  602. }
  603. if appId == "" || appSecret == "" {
  604. br.Msg = "微信公众号信息未配置"
  605. return
  606. }
  607. getUrl := this.GetString("Url")
  608. fmt.Println("getUrl:", getUrl)
  609. accessToken, err, errMsg := services.GetWxAccessToken(appId, appSecret)
  610. if err != nil {
  611. br.Msg = "获取用户信息失败"
  612. br.ErrMsg = "获取access_token失败,err:" + errMsg
  613. return
  614. }
  615. ticket, err := services.GetWxTicket(accessToken)
  616. if err != nil {
  617. br.Msg = "获取Ticket失败,请联系客服"
  618. br.ErrMsg = "获取Ticket失败,Err" + err.Error()
  619. return
  620. }
  621. if ticket == "" {
  622. br.Msg = "获取Ticket失败,请联系客服"
  623. br.ErrMsg = "ticket为空" + ticket
  624. return
  625. }
  626. nonceStr := utils.GetRandStringNoSpecialChar(16)
  627. signature, nonceString, timestamp := services.GetWxSignature(ticket, getUrl, nonceStr)
  628. resp := new(models.WechatSign)
  629. resp.AppId = appId
  630. resp.NonceStr = nonceString
  631. resp.Timestamp = timestamp
  632. resp.Url = getUrl
  633. resp.Signature = signature
  634. br.Ret = 200
  635. br.Success = true
  636. br.Msg = "获取签名成功"
  637. br.Data = resp
  638. }