zhangzengfei
2023-09-07 55aa27a6ad0e012d62dcea2db37528a1b18836fb
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package controller
 
import (
    "basic.com/valib/bhomedbapi.git"
    "basic.com/valib/licence.git"
    "basic.com/valib/logger.git"
    "encoding/json"
    "errors"
    "github.com/gin-gonic/gin"
    "io/ioutil"
    "net/http"
    "os"
    "strings"
    "time"
    "vamicro/config"
    "vamicro/extend/code"
    "vamicro/extend/util"
    "vamicro/version-control/service"
)
 
/*
      在线激活smartai
    1.登录商城
    2.展示商城订单
    3.选择订单进行激活
*/
 
func pRet(b []byte, err error, c *gin.Context, cb func(data interface{}) (interface{}, error)) {
    if err != nil {
        logger.Error("pRet err:", err)
        util.ResponseFormat(c, code.ComError, err.Error())
        return
    }
    var ret bhomedbapi.Result
    err = json.Unmarshal(b, &ret)
    if err != nil {
        logger.Error("pRet err:", err)
        util.ResponseFormat(c, code.ComError, err.Error())
        return
    }
    if !ret.Success {
        logger.Error("!ret.Success ret:", ret)
        c.JSON(http.StatusInternalServerError, ret)
        return
    }
    if cb != nil {
        var rd interface{}
        rd, err = cb(ret.Data)
        if err != nil {
            util.ResponseFormat(c, code.ComError, err.Error())
            return
        } else {
            util.ResponseFormat(c, code.Success, rd)
            return
        }
    }
    c.JSON(http.StatusOK, ret)
}
 
//请求商城端发送短信验证码
func shopMakSmsHandle(c *gin.Context) {
    phoneNum := c.Query("phoneNum")
    if phoneNum == "" {
        util.ResponseFormat(c, code.RequestParamError, "")
        return
    }
    url := util.GetShopUrl() + "/data/api-u/user/makeVerifyCode"
    if !strings.HasPrefix(url, "http://") {
        url = "http://" + url
    }
    m := map[string]string{
        "phoneNum": phoneNum,
    }
    b, err := util.DoGetRequest(url, m, nil)
    pRet(b, err, c, nil)
}
 
//调用商城端登录,获取token
func shopCenterLoginHandle(c *gin.Context) {
    phoneNum := c.PostForm("phoneNum")
    verifyCode := c.PostForm("verifyCode")
 
    if phoneNum == "" || verifyCode == "" {
        util.ResponseFormat(c, code.RequestParamError, "")
        return
    }
 
    url := util.GetShopUrl() + "/data/api-u/user/login"
    if !strings.HasPrefix(url, "http://") {
        url = "http://" + url
    }
    m := map[string]interface{}{
        "phoneNum":   phoneNum,
        "verifyCode": verifyCode,
    }
 
    b, err := util.DoPostRequest(url, util.CONTENT_TYPE_FORM, m, nil, nil, time.Second*5)
    pRet(b, err, c, func(data interface{}) (interface{}, error) {
        d, e := json.Marshal(data)
        if e != nil {
            return nil, e
        }
        type sld struct {
            AccessToken string `json:"access_token"`
        }
        var u sld
        e = json.Unmarshal(d, &u)
        if e != nil {
            return nil, e
        }
        if !strings.HasPrefix(u.AccessToken, "Bearer ") {
            u.AccessToken = "Bearer " + u.AccessToken
        }
        return u, nil
    })
}
 
//获取可用于激活的订单列表
func shopCenterGetOrderListHandle(c *gin.Context) {
    token := c.PostForm("token")
    if token == "" {
        util.ResponseFormat(c, code.RequestParamError, "")
        return
    }
    if !strings.HasPrefix(token, "Bearer ") {
        token = "Bearer " + token
    }
    url := util.GetShopUrl() + "/data/api-s/order/getActiveOrder"
    if !strings.HasPrefix(url, "http://") {
        url = "http://" + url
    }
    header := map[string]string{
        "Authorization": token,
    }
    m := map[string]interface{}{
        "page": 1,
        "size": 100,
    }
    b, err := util.DoPostRequest(url, util.CONTENT_TYPE_JSON, m, nil, header, time.Second*5)
    pRet(b, err, c, nil)
}
 
//请求商城使用订单进行激活,返回内容:
//请求码  通道数量  摄像机数量  设备数量  服务时长   产品密钥
func authActiveByOrderHandle(c *gin.Context) {
    token := c.PostForm("token")
    orderId := c.PostForm("orderId")
    productId := c.PostForm("productId")
    activateCode := c.PostForm("activateCode") //25位激活码
    if token == "" || activateCode == "" {
        util.ResponseFormat(c, code.RequestParamError, "")
        return
    }
    if !strings.HasPrefix(token, "Bearer ") {
        token = "Bearer " + token
    }
    url := util.GetShopUrl() + "/data/api-s/q_authorization"
    if !strings.HasPrefix(url, "http://") {
        url = "http://" + url
    }
    header := map[string]string{
        "Authorization": token,
    }
 
    var rd = util.AuthorizationInfo{
        DevId:        config.Server.AnalyServerId,
        MachineCode:  licence.GetMachineCode(),
        DeviceType:   config.Server.DeviceType,
        DeviceMode:   config.Server.DeviceModel,
        ActivateCode: activateCode,
    }
 
    var sysInitApi bhomedbapi.SysInitApi
    b, rInfo := sysInitApi.GetRegInfo()
    if b {
        rbd, e := json.Marshal(rInfo)
        if e == nil {
            var sysRI service.RegUserInfo
            if e = json.Unmarshal(rbd, &sysRI); e == nil {
                rd.UserType = sysRI.UserType
                rd.PhoneNum = sysRI.PhoneNum
                rd.Name = sysRI.Name
                rd.ProvinceId = sysRI.ProvinceId
                rd.CityId = sysRI.CityId
                rd.CountyId = sysRI.CountyId
                rd.Email = sysRI.Email
            }
        }
    }
 
    q, err := service.HackAuthorizationInfo(rd)
    if err != nil {
        logger.Error("Haq err:", err)
        util.ResponseFormat(c, code.ComError, err.Error())
        return
    }
 
    body := map[string]interface{}{
        "sn":           q,
        "orderId":      orderId,
        "productId":    productId,
        "activateCode": activateCode,
    }
 
    rb, err := util.DoPostRequest(url, util.CONTENT_TYPE_JSON, body, nil, header, time.Second*5)
    cb := func(data interface{}) (interface{}, error) {
        rb, err := json.Marshal(data)
        if err != nil {
            logger.Error("json.Marshal err:", err)
            return nil, err
        }
        type rs struct {
            AuthInfo string `json:"authInfo"`
        }
        var rai rs
        err = json.Unmarshal(rb, &rai)
        if err != nil {
            logger.Error("json.Unmarshal err:", err)
            return nil, err
        }
        authInfo, e := util.GetAuthorizationInfo(rai.AuthInfo)
        if e != nil {
            logger.Error("GetAuthorizationInfo e:", e)
            return nil, e
        }
        machineCode := licence.GetMachineCode()
        if machineCode != authInfo.MachineCode {
            logger.Error("GetAuthorization machineCode not match, local:", machineCode, " remote:", authInfo.MachineCode)
            return nil, errors.New("机器码不匹配")
        }
 
        sn := util.GetVamicroPath() + "/sn.txt"
        pathAuth := util.GetVamicroPath() + "/auth.txt"
        ioutil.WriteFile(sn, []byte(""), os.ModePerm)
        ioutil.WriteFile(pathAuth, []byte(rai.AuthInfo), os.ModePerm)
        rm := map[string]interface{}{
            "sn":          authInfo.ActivateCode,
            "edition":     authInfo.Edition,
            "chCount":     authInfo.ChCount,
            "cameraCount": authInfo.CameraCount,
            "devCount":    authInfo.DevCount,
            "serveYear":   authInfo.ServeYear,
            "authInfo":    rai.AuthInfo,
        }
        return rm, nil
    }
    pRet(rb, err, c, cb)
}