From 01dfd9dc8de7b19f9dfa4284722e01bbd5837801 Mon Sep 17 00:00:00 2001 From: zhangmeng <775834166@qq.com> Date: 星期五, 19 一月 2024 09:10:30 +0800 Subject: [PATCH] replace json to json-iterator --- appApi.go | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 105 insertions(+), 13 deletions(-) diff --git a/appApi.go b/appApi.go index b08ec4f..ff4069b 100644 --- a/appApi.go +++ b/appApi.go @@ -2,29 +2,33 @@ import ( "basic.com/pubsub/protomsg.git" - "encoding/json" + jsoniter "github.com/json-iterator/go" ) -type AppApi struct{ - +type AppApi struct { +} +type AppWithShop struct { + protomsg.App + RemoteVersion string `json:"remoteVersion"` //鍟嗗煄浠撳簱鐗堟湰鍙� + Installed bool `json:"installed"` //鏄惁宸插畨瑁� + IsUpgrade bool `json:"isUpgrade"` //鏄惁闇�瑕佸崌绾� + ProgressMsg string `json:"progressMsg"` //瀹夎鎴栧崌绾ц繘搴� } func (api AppApi) FindAll(appName string) (list []protomsg.App) { url := DATA_URL_PREFIX + "/app/findAllApp" netNode := getNetNode(url2Topic(Topic_AppCenter_Service, url)) logPrint("netNode:", netNode) - if netNode == nil { - return nil - } client := NewClient(WithNodes(netNode)) paramMap := make(map[string]string, 0) paramMap["appName"] = appName respBody, err := client.DoGetRequest(url, paramMap, nil) logPrint("DoGetRequest err:", err) - if err !=nil { + if err != nil { return nil } var res Result + var json = jsoniter.ConfigCompatibleWithStandardLibrary if err = json.Unmarshal(respBody, &res); err != nil { logPrint(err) return nil @@ -42,20 +46,79 @@ func (api AppApi) Save(paramBody map[string]interface{}) bool { url := DATA_URL_PREFIX + "/app/save" netNode := getNetNode(url2Topic(Topic_AppCenter_Service, url)) - if netNode == nil { - return false - } client := NewClient(WithNodes(netNode)) - body, err := client.DoPostRequest(url,CONTENT_TYPE_JSON,paramBody,nil,nil) + body, err := client.DoPostRequest(url, CONTENT_TYPE_JSON, paramBody, nil, nil) if err != nil { return false } var res Result + var json = jsoniter.ConfigCompatibleWithStandardLibrary if err = json.Unmarshal(body, &res); err != nil { return false } return res.Success +} + +func (api AppApi) GetAppInfo(id string) (flag bool, d interface{}) { + url := DATA_URL_PREFIX + "/app/getAppInfo" + netNode := getNetNode(url2Topic(Topic_AppCenter_Service, url)) + client := NewClient(WithNodes(netNode)) + paramMap := map[string]string{ + "id": id, + } + body, err := client.DoGetRequest(url, paramMap, nil) + if err != nil { + return false, err.Error() + } + + var res Result + var json = jsoniter.ConfigCompatibleWithStandardLibrary + if err = json.Unmarshal(body, &res); err != nil { + return false, err.Error() + } + return res.Success, res.Data +} + +func (api AppApi) Delete(appId string) (bool, interface{}) { + url := DATA_URL_PREFIX + "/app/delete" + netNode := getNetNode(url2Topic(Topic_AppCenter_Service, url)) + client := NewClient(WithNodes(netNode)) + paramBody := map[string]interface{}{ + "appId": appId, + } + body, err := client.DoPostRequest(url, CONTENT_TYPE_JSON, paramBody, nil, nil) + if err != nil { + return false, err.Error() + } + + var res Result + var json = jsoniter.ConfigCompatibleWithStandardLibrary + if err = json.Unmarshal(body, &res); err != nil { + return false, err.Error() + } + return res.Success, res.Data +} + +// id浼犵畻娉曟垨鑰呭簲鐢ㄧ殑id锛屼笅杞藉畨瑁呮垨鍗囩骇 +func (api AppApi) Upgrade(id string) (bool, interface{}) { + url := DATA_URL_PREFIX + "/sdk/sdkDownload" + netNode := getNetNode(url2Topic(Topic_AppCenter_Service, url)) + client := NewClient(WithNodes(netNode)) + paramMap := map[string]string{ + "path": id, + } + body, err := client.DoGetRequest(url, paramMap, nil) + if err != nil { + return false, err.Error() + } + + var res Result + var json = jsoniter.ConfigCompatibleWithStandardLibrary + if err = json.Unmarshal(body, &res); err != nil { + return false, err.Error() + } + return res.Success, res.Data } //鑾峰彇鎵�鏈夊凡瀹夎鐨刟pp鍒楄〃,浠ackage涓簁ey @@ -63,10 +126,39 @@ m := make(map[string]protomsg.App) apps := api.FindAll("") if apps != nil { - for _,a := range apps { + for _, a := range apps { m[a.Package] = a } } return m -} \ No newline at end of file +} + +//鑾峰彇甯﹀畨瑁呯姸鎬佺殑app鍒楄〃 +func (api AppApi) FindAppWithInstallStatus(appName string) (list []AppWithShop) { + url := DATA_URL_PREFIX + "/app/findAllApp" + netNode := getNetNode(url2Topic(Topic_AppCenter_Service, url)) + logPrint("netNode:", netNode) + client := NewClient(WithNodes(netNode)) + paramMap := make(map[string]string, 0) + paramMap["appName"] = appName + respBody, err := client.DoGetRequest(url, paramMap, nil) + logPrint("DoGetRequest err:", err) + if err != nil { + return nil + } + var res Result + var json = jsoniter.ConfigCompatibleWithStandardLibrary + if err = json.Unmarshal(respBody, &res); err != nil { + logPrint(err) + return nil + } + bytes, _ := json.Marshal(res.Data) + err = json.Unmarshal(bytes, &list) + if err == nil { + return list + } else { + logPrint("unmarshal err:", err) + return nil + } +} -- Gitblit v1.8.0