weixin_service_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "testing"
  6. "xiaoniaokuaiyan.com/xiaoniao/config"
  7. "xiaoniaokuaiyan.com/xiaoniao/pay/wx"
  8. )
  9. func TestSendMsg(t *testing.T) {
  10. wxSrv := &WeixinService{
  11. Weixin: wx.NewWeixin(config.IniConf.Section("weixin").Key("wx.appid").Value(), config.IniConf.Section("weixin").Key("wx.secret").Value()),
  12. JsSdk: wx.NewJsSdk(),
  13. }
  14. tokenStr, err := wxSrv.GetWXAccessToken("")
  15. if err != nil {
  16. t.Fatal(err)
  17. }
  18. msg := wx.Message{
  19. "touser": "onNpDwyL0Tg2n9vYis7mwUBC0VZM",
  20. "msgtype": "text",
  21. "text": map[string]string{"content": `测试<a href="http://m.xiaoniaokuaiyan.com">hello world</a>小鸟快验欢迎你`},
  22. }
  23. err = wxSrv.Weixin.Send(msg, tokenStr.(string))
  24. if err != nil {
  25. t.Fatal(err)
  26. }
  27. }
  28. func TestSendTpl(t *testing.T) {
  29. wxSrv := &WeixinService{
  30. Weixin: wx.NewWeixin(config.IniConf.Section("weixin").Key("wx.appid").Value(), config.IniConf.Section("weixin").Key("wx.secret").Value()),
  31. JsSdk: wx.NewJsSdk(),
  32. }
  33. msg := map[string]interface{}{
  34. "touser": "onNpDwyL0Tg2n9vYis7mwUBC0VZM",
  35. "template_id": "og_C32ekCtZDIG86FodplOJ-Agj0pYYjTlQLJxt3j_0",
  36. "url": "http://m.xiaoniaokuaiyan.com",
  37. "data": map[string]interface{}{
  38. "first": map[string]interface{}{
  39. "value": "订单支付成功",
  40. "color": "#173177",
  41. },
  42. "orderMoneySum": map[string]interface{}{
  43. "value": "200元",
  44. "color": "#173177",
  45. },
  46. "orderProductName": map[string]interface{}{
  47. "value": "胃幽",
  48. "color": "#173177",
  49. },
  50. "Remark": map[string]interface{}{
  51. "value": "感谢您使用小鸟快验!",
  52. "color": "#173177",
  53. },
  54. },
  55. }
  56. buf, _ := json.Marshal(msg)
  57. _, err := wxSrv.SendTpl(string(buf), "")
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. }
  62. func TestScanPay(t *testing.T) {
  63. wxSrv := &WeixinService{
  64. Weixin: wx.NewWeixin(config.IniConf.Section("weixin").Key("wx.appid").Value(), config.IniConf.Section("weixin").Key("wx.secret").Value()),
  65. JsSdk: wx.NewJsSdk(),
  66. }
  67. rel, err := wxSrv.ScanPay(map[string]string{
  68. "ip": "127.0.0.1",
  69. "device_info": "123",
  70. "trade_type": "JSAPI",
  71. "openid": "onNpDwwktNS1lD34UOOQ3Yj0ToZo",
  72. "total_fee": "100",
  73. "usefor": "藏医院 核磁",
  74. })
  75. if err != nil {
  76. t.Fatal(err)
  77. }
  78. fmt.Println(rel)
  79. }
  80. func BenchmarkString(b *testing.B) {
  81. for i := 0; i < b.N; i++ {
  82. b.StopTimer()
  83. b.StartTimer()
  84. b.ResetTimer()
  85. }
  86. }