heyujie
2021-11-30 89e847aafdc25922d97f2ca465391efae9c21640
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package bhomedbapi
 
import (
    "basic.com/pubsub/protomsg.git"
    "encoding/json"
)
 
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
    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
    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
    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
    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
    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
    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
    }
}