微信接口的RPC包
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
1.7 KiB

  1. package weixinrpc
  2. import (
  3. "encoding/json"
  4. "strconv"
  5. "time"
  6. "git.tetele.net/tgo/crypter"
  7. "github.com/golang/protobuf/proto"
  8. )
  9. //公众号模板消息
  10. type MpTempMsg struct {
  11. Appid string
  12. TemplateId string
  13. Url string
  14. Miniprogram string
  15. Data string
  16. }
  17. type UniformMessageReq struct {
  18. MiniAppid string
  19. Touser string
  20. MpTemplateMsg MpTempMsg
  21. }
  22. /**
  23. * 发送小程序统一服务消息,默认发送公众号消息
  24. * appid,secret 小程序ID 密钥
  25. * touser 小程序openid
  26. * mp_appid 关联公众号appid要求与小程序有绑定且同主体
  27. * temp_id 公众号模板id
  28. * redirect_url 跳转的url
  29. * mini_program 公众号模板消息所要跳转的小程序小程序的必须与公众号具有绑定关系
  30. * data 公众号模板消息的数据
  31. */
  32. func SendUniformMessage(dbname, site_id string, appid, secret, touser, mp_appid, temp_id, redirect_url, mini_program, data string, url ...string) (*WxApiRes, error) {
  33. conn, err := rpc_server_conn(url...)
  34. data := UniformMessageReq{}
  35. data.SiteId = site_id
  36. data.Dbname = dbname
  37. data.Appid = appid
  38. data.Secret = secret
  39. data.Touser = touser
  40. data.MpAppid = mp_appid
  41. data.TemplateId = temp_id
  42. data.Url = redirect_url
  43. data.MiniProgram = mini_program
  44. data.Data = data
  45. data_json, err := json.Marshal(data)
  46. if err != nil {
  47. return nil, err
  48. }
  49. encryData := crypter.DesEn(string(data_json), DES_KEY)
  50. now_int64 := time.Now().Unix()
  51. now := strconv.FormatInt(now_int64, 10)
  52. sign := Sign(encryData, now)
  53. req := &Request{
  54. proto.String(encryData),
  55. proto.String(now),
  56. proto.String(sign),
  57. nil}
  58. res := &Response{}
  59. err = conn.SendUniformMessage(req, res)
  60. if err != nil {
  61. return nil, err
  62. }
  63. return HandleRes(res)
  64. }