package bhomedbapi import ( "basic.com/pubsub/protomsg.git" jsoniter "github.com/json-iterator/go" ) 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) 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 } } func (api AppApi) Save(paramBody map[string]interface{}) bool { url := DATA_URL_PREFIX + "/app/save" netNode := getNetNode(url2Topic(Topic_AppCenter_Service, url)) client := NewClient(WithNodes(netNode)) 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 } //获取所有已安装的app列表,以package为key func (api AppApi) FindAppMap() map[string]protomsg.App { m := make(map[string]protomsg.App) apps := api.FindAll("") if apps != nil { for _, a := range apps { m[a.Package] = a } } return m } //获取带安装状态的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 } }