liuxiaolong
2022-03-04 f64dd9f191dff341b4eb430d7bacc44a3db9a279
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
package controllers
 
import (
    "car-service/extend/code"
    "car-service/models"
    "car-service/service"
    "car-service/vo"
    "encoding/json"
    "fmt"
    "github.com/astaxie/beego"
    "net/http"
)
 
// Operations about Users
type UserController struct {
    beego.Controller
}
 
 
// @Title Update
// @Description update the user
// @Param    uid        path     string    true        "The uid you want to update"
// @Param    body        body     models.User    true        "body for user content"
// @Success 200 {object} models.User
// @Failure 403 {string} json ":uid is not int"
// @router /:uid [put]
func (u *UserController) Put() {
    uid := u.GetString(":uid")
    if uid != "" {
        var user models.User
        u.Data["json"] = user
 
    }
    u.ServeJSON()
}
 
// @Title Delete
// @Description delete the user
// @Param    uid        path     string    true        "The uid you want to delete"
// @Success 200 {string} delete success!
// @Failure 403 {string} json  "uid is empty"
// @router /:uid [delete]
func (u *UserController) Delete() {
    uid := u.GetString(":uid")
    var user = models.User{
        Id: uid,
    }
 
    i, err := user.DeleteById()
    if err ==nil && i >0 {
        u.Data["json"] = "delete success!"
    }
    u.ServeJSON()
}
 
// @Title Login
// @Description Logs user into the system
// @Param    username        query     string    true        "The username for login"
// @Param    password        query     string    true        "The password for login"
// @Success 200 {string} login success
// @Failure 403 {string} json  "user not exist"
// @router /login [get]
func (u *UserController) Login() {
    phoneNum := u.GetString("phoneNum")
    cod := u.GetString("code")
    cid := u.GetString("cid")  //unipush clientid
    resp := code.Code{}
    fmt.Println("phoneNum:", phoneNum, "code:", cod, "clientId:", cid)
    if phoneNum == "" || cod == ""{
        resp.Success= false
        resp.Status= http.StatusBadRequest
        resp.Data= "参数有误"
    } else {
        var sv service.UserService
        b, info, e := sv.Login(phoneNum, cod, cid)
 
        if b {
            resp.Success= true
            resp.Status= http.StatusOK
            resp.Data= *info
        } else {
            resp.Success= false
            resp.Status= http.StatusBadRequest
            resp.Data= e.Error()
        }
    }
 
    u.Data["json"] = resp
    u.ServeJSON()
}
 
// @Title logout
// @Description Logs out current logged in user session
// @Success 200 {string} logout success
// @router /logout [get]
func (u *UserController) Logout() {
    cid := u.GetString("cid")  //unipush clientid
    fmt.Println("clientId:", cid)
 
    if cid != "" {
        go func() {
            unbindB, unE := service.UnbindAlias(cid)
            fmt.Println("unbind result:", unbindB, "err:", unE)
            var uc models.UserClient
            i, e := uc.DeleteByCid(cid)
            fmt.Println("deleteByCid affected:", i, "err:", e)
        }()
    }
    resp := code.Code{}
    resp.Success = true
    resp.Status = http.StatusOK
    resp.Data = "退出成功"
    u.Data["json"] = resp
    u.ServeJSON()
}
 
// @Param reqBody body vo.AddPlateNoVo true "绑定车牌号参数"
// @router /addPlateNo [post]
func (u *UserController) AddPlateNo() {
    var reqBody vo.AddPlateNoVo
    body := u.Ctx.Input.RequestBody
    err := json.Unmarshal(body, &reqBody)
    resp := code.Code{}
    if err != nil {
        resp.Success = false
        resp.Status = http.StatusBadRequest
        resp.Data = "参数有误"
    } else {
        var sv service.UserService
        if sv.AddPlateNo(reqBody.UserId, reqBody.PlateNo) {
            resp.Success = true
            resp.Status = http.StatusOK
            resp.Data = "添加成功"
        } else {
            resp.Success = false
            resp.Status = http.StatusBadRequest
            resp.Data = "添加失败"
        }
    }
    u.Data["json"] = resp
    u.ServeJSON()
}
 
// @router /myPlateNos [get]
func (u *UserController) MyPlateNos() {
    userId := u.GetString("userId")
    fmt.Println("MyPlateNos userId", userId)
    sv :=  service.NewCarService()
    vehicleList := sv.GetVehicleListByPerson(userId)
    var nos = make([]string, 0)
    if vehicleList != nil {
        for _,p := range vehicleList {
            nos = append(nos, p.PlateNo)
        }
    }
    resp := code.Code{
        Success: true,
        Status: http.StatusOK,
        Data: nos,
    }
    u.Data["json"] = resp
    u.ServeJSON()
}
 
// @router /delPlateNo [delete]
func (u *UserController) DelPlateNo() {
    userId := u.GetString("userId")
    plateNo := u.GetString("plateNo")
    resp := code.Code{}
    var uc models.UserCar
    i,e := uc.Delete(userId, plateNo)
    fmt.Println("delPlateNo i:",i, "e:", e)
    if e ==nil && i >0 {
        resp.Success = true
        resp.Status = http.StatusOK
        resp.Data = "删除成功"
    } else {
        resp.Success = false
        resp.Status = http.StatusInternalServerError
        resp.Data = "删除失败"
    }
    u.Data["json"] = resp
    u.ServeJSON()
}
 
// @router /all [get]
func (u *UserController) GetUserAll() {
    resp := code.Code{}
    sv := service.NewCarService()
    personAll := sv.GetHikPersonList()
    if personAll != nil {
        resp.Success = true
        resp.Status = http.StatusOK
        resp.Data = personAll
    }
    u.Data["json"] = resp
    u.ServeJSON()
}
 
 
// @router /sync [get]
func (u *UserController) Sync() {
    resp := code.Code{}
    syncCount := service.SyncHikPerson()
 
    resp.Success = true
    resp.Status = http.StatusOK
    resp.Data = map[string]interface{}{
        "syncCount": syncCount,
    }
 
    u.Data["json"] = resp
    u.ServeJSON()
}