wechat.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta_gn/eta_report/models"
  6. "eta_gn/eta_report/services/wechat"
  7. "eta_gn/eta_report/utils"
  8. "fmt"
  9. "github.com/rdlucklib/rdluck_tools/http"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. func GetWxTicket(accessToken string) (int, string, error) {
  15. Url := strings.Join([]string{"https://api.weixin.qq.com/cgi-bin/ticket/getticket",
  16. "?access_token=", accessToken,
  17. "&type=jsapi"}, "")
  18. infoBody, err := http.Get(Url)
  19. if err != nil {
  20. return 0, "", err
  21. }
  22. atr := models.WxTicket{}
  23. err = json.Unmarshal(infoBody, &atr)
  24. fmt.Println("ticket result:", string(infoBody))
  25. if err != nil {
  26. return atr.Errcode, atr.Errmsg, err
  27. } else {
  28. return 0, atr.Ticket, nil
  29. }
  30. }
  31. func GetWxSignature(ticket, url, noncestr string) (string, string, int64) {
  32. timestamp := time.Now().Unix()
  33. signStr := strings.Join([]string{"jsapi_ticket=", ticket,
  34. "&noncestr=", noncestr,
  35. "&timestamp=", strconv.FormatInt(timestamp, 10), "&url=", url}, "")
  36. signature := utils.Sha1(signStr)
  37. fmt.Println("signStr", signStr)
  38. return signature, noncestr, timestamp
  39. }
  40. func GetDefaultWxAccessToken() (accessToken string, err error, errMsg string) {
  41. conf, e := models.GetBusinessConf()
  42. if e != nil {
  43. errMsg = "获取配置信息失败, Err: " + e.Error()
  44. err = errors.New(errMsg)
  45. return
  46. }
  47. appId := ""
  48. appSecret := ""
  49. if v, ok := conf[models.BusinessConfWxAppId]; ok {
  50. appId = v
  51. }
  52. if v, ok := conf[models.BusinessConfWxAppSecret]; ok {
  53. appSecret = v
  54. }
  55. if appId == "" || appSecret == "" {
  56. errMsg = "获取微信公众号未配置"
  57. err = errors.New(errMsg)
  58. return
  59. }
  60. req := wechat.WxTokenReq{
  61. WxAppId: appId,
  62. WxAppSecret: appSecret,
  63. }
  64. accessToken, err, errMsg = wechat.GetAccessToken(req)
  65. return
  66. }