322 lines
9.4 KiB
Go
322 lines
9.4 KiB
Go
package mtwmapi
|
||
|
||
import (
|
||
"encoding/json"
|
||
"fmt"
|
||
"git.rosy.net.cn/baseapi/utils"
|
||
"github.com/gazeboxu/mapstructure"
|
||
"github.com/go-redis/redis"
|
||
"github.com/gorilla/websocket"
|
||
"io"
|
||
"net/http/httptest"
|
||
"sync"
|
||
"testing"
|
||
"time"
|
||
)
|
||
|
||
const (
|
||
MTIMPushUrl = "wss://wpush.meituan.com/websocket"
|
||
TestAppID = "589_WMOPEN"
|
||
TestToken = "wo589i4VsZHFH2fh4uVsr6Dtc3k6vG8Xu0vxpreBQFy6QAvg"
|
||
|
||
TestMTIM589 = "wss://wpush.meituan.com/websocket/589_WMOPEN/wo589i4VsZHFH2fh4uVsr6Dtc3k6vG8Xu0vxpreBQFy6QAvg"
|
||
TestMTIM4123 = "wss://wpush.meituan.com/websocket/4123_WMOPEN/wo4123aAVXDUkZDYucMoTDAZgsMzjrR_porZcLGv2GmWRNOiw"
|
||
TestWssUrl = "wss://www.jxc4.com:443/v2/event/TestWebsocket"
|
||
TestWssUrl1 = "wss://www-jxgy.jxc4.com:443/v2/im/StartWebSocket"
|
||
)
|
||
|
||
type ClientManager struct {
|
||
ClientIdMap map[string]*Client // 全部的连接
|
||
ClientIdMapLock sync.RWMutex // 读写锁
|
||
|
||
Connect chan *Client // 连接处理
|
||
DisConnect chan *Client // 断开连接处理
|
||
|
||
GroupLock sync.RWMutex
|
||
Groups map[string][]string
|
||
|
||
//SystemClientsLock sync.RWMutex
|
||
//SystemClients map[string][]string
|
||
Clients map[string]*Client // 保存连接
|
||
Accounts map[string][]string // 账号和连接关系,map的key是账号id即:AccountId,这里主要考虑到一个账号多个连接
|
||
mu *sync.Mutex
|
||
}
|
||
|
||
var Manager = NewClientManager()
|
||
|
||
func NewClientManager() (clientManager *ClientManager) {
|
||
clientManager = &ClientManager{
|
||
Accounts: make(map[string][]string),
|
||
ClientIdMap: make(map[string]*Client, 100),
|
||
Connect: make(chan *Client, 10000),
|
||
DisConnect: make(chan *Client, 10000),
|
||
mu: new(sync.Mutex),
|
||
}
|
||
|
||
return
|
||
}
|
||
|
||
var RegisterChan = make(chan *Client, 100)
|
||
|
||
type Client struct {
|
||
ID string // 连接ID
|
||
AccountId string // 账号id, 一个账号可能有多个连接
|
||
Socket *websocket.Conn // 连接
|
||
HeartbeatTime int64 // 前一次心跳时间
|
||
}
|
||
|
||
var rdb = redis.NewClient(&redis.Options{
|
||
//Addr: "www.jxc4.com:6379",
|
||
//Password: "",
|
||
Addr: "127.0.0.1:6379",
|
||
Password: "123456",
|
||
DB: 0,
|
||
})
|
||
|
||
type TestStr struct {
|
||
VendorID int `json:"vendorID"` //平台品牌 10-美团 11-饿了么
|
||
UserID int `json:"userID"` //用户ID
|
||
NewMessageNum int `json:"NewMessageNum"` //新消息数量
|
||
LatestMsg string `json:"latestMsg"` //最新一条消息
|
||
LatestTime int `json:"latestTime"` //最新一条消息发送时间
|
||
}
|
||
|
||
func TestCacher_RPush(t *testing.T) {
|
||
//ans := TestStr{
|
||
// VendorID: 22,
|
||
// UserID: 2222222222,
|
||
// NewMessageNum: 222,
|
||
// LatestMsg: "22222222222",
|
||
// LatestTime: 22222222222,
|
||
//}
|
||
//str, _ := json.Marshal(ans)
|
||
//err := rdb.RPush("test", string(str))
|
||
keys := []string{"589:7954977:10", "test"}
|
||
retVal := make(map[string][]interface{}, 0)
|
||
for _, key := range keys {
|
||
temp := rdb.LRange(key, 0, -1).Val()
|
||
for _, v := range temp {
|
||
retVal[key] = append(retVal[key], v)
|
||
}
|
||
}
|
||
fmt.Printf("%s", utils.Format4Output(retVal, false))
|
||
//if err != nil {
|
||
// fmt.Print(err)
|
||
//}
|
||
}
|
||
|
||
//测试心跳
|
||
func TestHeartCheck(t *testing.T) {
|
||
var clientID = make(map[string]*websocket.Conn)
|
||
//conn, resp, err := websocket.DefaultDialer.Dial(TestMTIMPushUrl, nil)
|
||
|
||
//str := "~#HHhehHBBB#~"
|
||
//data := []byte(str)
|
||
conn1, resp1, err1 := websocket.DefaultDialer.Dial(TestMTIM4123, nil)
|
||
fmt.Println(conn1, resp1, err1)
|
||
clientID["1"] = conn1
|
||
|
||
conn, resp, err := websocket.DefaultDialer.Dial(TestMTIM589, nil)
|
||
clientID["2"] = conn
|
||
fmt.Println(conn, resp, err)
|
||
|
||
if err != nil || resp.StatusCode != 101 {
|
||
fmt.Printf("连接失败:%v http响应不成功", err)
|
||
}
|
||
//关闭
|
||
defer func(conn *websocket.Conn) {
|
||
err := conn.Close()
|
||
if err != nil {
|
||
return
|
||
}
|
||
}(conn)
|
||
|
||
if err := conn.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(time.Second)); err != nil {
|
||
fmt.Println(err)
|
||
}
|
||
|
||
//err = conn.WriteMessage(websocket.TextMessage, data)
|
||
//if err != nil {
|
||
// fmt.Println(err)
|
||
//}
|
||
|
||
for {
|
||
_, msg, err := conn.ReadMessage()
|
||
temp := string(msg)
|
||
if err != nil || temp != "HB" {
|
||
break
|
||
}
|
||
fmt.Printf("%s receive: %s\n", conn.RemoteAddr(), string(msg))
|
||
}
|
||
}
|
||
|
||
func TestGetConnectionToken(t *testing.T) {
|
||
resp, err := api.GetConnectionToken()
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
retVal := GetConnTokenResp{}
|
||
err = mapstructure.Decode(resp, &retVal)
|
||
fmt.Println(err)
|
||
fmt.Println(utils.Format4Output(retVal, false))
|
||
}
|
||
|
||
//解密测试
|
||
func TestAesCBCDecrypt(t *testing.T) {
|
||
secret := "a81eb3df418d83d6a1a4b7c572156d2f"
|
||
key := secret[:16]
|
||
str := "qodhoVd4IGtgPKrvYwq6QrzBecJZkeSUPYR88iGRUsCRFmCFxDHpUhqsbBztNXQx"
|
||
//str := "Vv+Y/K8vfS42W+P7xq26aIb6uoaG/nL0ZoMMXpitc5QQ3XJm3Roh10NuSoojYrG/3JZwbzgtYA+kBvodoY2eJV00f9MBY+kLkxToP+aSofsYva9tHbipvjVtexebc+eP7aQMtzbwU4BNNnuRG6e7TkXP+BLdtiGsyvHolGfky+p2fZgWes9R6JIxkuRCXW/yBhUo8F+wWCZ2YQl/szp5lHJ3cmneD6cwem36E0FBcvxZNB9an4pRkBrqi1p43V8QBLO719oXkQ+dqTqJMi1/xDSBrCDYN8QORnARP8+j1oDuqE34Kklcse4WL9rwTJ2sOmOu/O2h6Gx3ZaFaMaWRXBDYv8JpzTZjCbRrLSENlEHTof29BmvXTJ0QZ7qi6iAD"
|
||
data, err := Decrypt(str, []byte(key))
|
||
//data, err := DecryptAES(key, str)
|
||
fmt.Println(data)
|
||
fmt.Println(err)
|
||
}
|
||
|
||
var wsList []*websocket.Conn
|
||
|
||
type RetData struct {
|
||
Code int `json:"code"` //响应code
|
||
Msg string `json:"msg"` //响应msg success/fail
|
||
Data interface{} `json:"data"` //信息
|
||
}
|
||
|
||
func TestWebSocketClient(t *testing.T) {
|
||
var retData RetData
|
||
|
||
retData.Code = 0
|
||
retData.Msg = "success"
|
||
retData.Data = "发送信息成功"
|
||
|
||
retJson, _ := json.Marshal(retData)
|
||
str := string(retJson)
|
||
|
||
w := httptest.NewRecorder()
|
||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||
_, _ = io.WriteString(w, str)
|
||
return
|
||
}
|
||
|
||
func TestPUSH(t *testing.T) {
|
||
key := "589:7954977:10"
|
||
rdb.RPush(key, "1111111111")
|
||
//rdb.RPush(key, "{\"vendorID\":10,\"userID\":11158569333,\"NewMessageNum\":3,\"latestMsg\":\"hhhhhhhhhhh\",\"latestTime\":1681983980}")
|
||
rdb.RPush(key, "{\"vendorID\":10,\"userID\":11158569333,\"NewMessageNum\":3,\"latestMsg\":\"oooooooooo\",\"latestTime\":1681983980}")
|
||
rdb.RPush(key, "2222222222222")
|
||
rdb.RPush(key, "{\"vendorID\":10,\"userID\":11158569333,\"NewMessageNum\":4,\"latestMsg\":\"成功插入新数据,看下cnt\",\"latestTime\":1681983980}")
|
||
rdb.RPush(key, "{\"vendorID\":10,\"userID\":11158569333,\"NewMessageNum\":5,\"latestMsg\":\"成功插入新数据,看下cnt\",\"latestTime\":1681983980}")
|
||
}
|
||
|
||
//用户消息列表
|
||
type UserMessageList struct {
|
||
VendorID int `json:"vendorID"` //平台品牌 10-美团 11-饿了么
|
||
UserID int `json:"userID"` //用户ID
|
||
NewMessageNum int `json:"NewMessageNum"` //新消息数量
|
||
LatestMsg string `json:"latestMsg"` //最新一条消息
|
||
LatestTime int `json:"latestTime"` //最新一条消息发送时间
|
||
}
|
||
|
||
func TestNewRedis(t *testing.T) {
|
||
var (
|
||
err error
|
||
flag = 11158569333
|
||
key = "589:7954977:10"
|
||
temp = UserMessageList{}
|
||
)
|
||
if n := rdb.Exists(key).Val(); n > 0 {
|
||
s2 := rdb.LRange(key, 0, -1).Val()
|
||
for i := 0; i < len(s2); i++ {
|
||
v := UserMessageList{}
|
||
_ = json.Unmarshal([]byte(s2[i]), &v)
|
||
if v.UserID == flag {
|
||
//删除此条数据
|
||
err = rdb.LSet(key, int64(i), "del").Err()
|
||
err = rdb.LRem(key, 0, "del").Err()
|
||
s2 = append(s2[:i], s2[i+1:]...)
|
||
i--
|
||
//cnt=0 重新赋值
|
||
temp = UserMessageList{
|
||
VendorID: v.VendorID,
|
||
UserID: v.UserID,
|
||
NewMessageNum: 0,
|
||
LatestMsg: v.LatestMsg,
|
||
LatestTime: v.LatestTime,
|
||
}
|
||
}
|
||
}
|
||
str, _ := json.Marshal(temp)
|
||
err = rdb.RPush(key, str).Err()
|
||
}
|
||
fmt.Print(err)
|
||
//s2 := rdb.LRange(key, 0, -1).Val()
|
||
//fmt.Printf("before len %d\n", len(s2))
|
||
//fmt.Printf("before ans %s\n", s2)
|
||
//cnt := 0
|
||
//n := rdb.Exists(key).Val()
|
||
//if n > 0 {
|
||
// for i := 0; i < len(s2); i++ {
|
||
// v := UserMessageList{}
|
||
// _ = json.Unmarshal([]byte(s2[i]), &v)
|
||
// if v.UserID == flag {
|
||
// rdb.LSet(key, int64(i), "del")
|
||
// rdb.LRem(key, 0, "del")
|
||
// s2 = append(s2[:i], s2[i+1:]...)
|
||
// i--
|
||
// if v.NewMessageNum == 0 { //目前为首条
|
||
// cnt++ //赋值1
|
||
// } else {
|
||
// cnt = v.NewMessageNum
|
||
// }
|
||
// }
|
||
// }
|
||
//}
|
||
//fmt.Printf("after cnt %d\n", cnt)
|
||
//fmt.Printf("after len %d\n", len(s2))
|
||
//fmt.Printf("after ans %s\n", s2)
|
||
////存入flag数据
|
||
//ans := UserMessageList{
|
||
// VendorID: 10,
|
||
// UserID: 11158569333,
|
||
// NewMessageNum: cnt,
|
||
// LatestMsg: "成功插入新数据,看下cnt",
|
||
// LatestTime: 1681983980,
|
||
//}
|
||
//param, _ := json.Marshal(ans)
|
||
//rdb.RPush(key, param)
|
||
}
|
||
|
||
// 根据账号获取连接
|
||
func TestGetClient(t *testing.T) {
|
||
accountId := "QW+r2FtsRKGGLJnlgyDNlChzcKcSZ8Kfgh0qw//ONuQCDKzky4x+nlbnx3k1JX13"
|
||
clients := make([]*Client, 0)
|
||
|
||
Manager.mu.Lock()
|
||
defer Manager.mu.Unlock()
|
||
|
||
if len(Manager.Accounts[accountId]) > 0 {
|
||
for _, clientId := range Manager.Accounts[accountId] {
|
||
if c, ok := Manager.Clients[clientId]; ok {
|
||
clients = append(clients, c)
|
||
}
|
||
}
|
||
}
|
||
|
||
fmt.Printf(utils.Format4Output(clients, false))
|
||
}
|
||
func TestMal(t *testing.T) {
|
||
|
||
}
|
||
|
||
func TestGetWayBillFee(t *testing.T) {
|
||
//order, _ := api.OrderGetOrderDetail(1100486451772280163, false)
|
||
//globals.SugarLogger.Debugf("order:==%s", utils.Format4Output(order, false)) 2002
|
||
api.GetWayBillFee("1100486451772280163", 2)
|
||
api.GetWayBillFee("1100486451772280163", 1)
|
||
}
|
||
|
||
// 商家没有接入众包配送,无法进行众包配送相关操作
|
||
func TestGetShippingFeeList(t *testing.T) {
|
||
api.GetShippingFeeList("1300486314174032613,1100487300210228389", 1)
|
||
api.GetShippingFeeList("1300486314174032613,1100487300210228389", 2)
|
||
}
|