-
Notifications
You must be signed in to change notification settings - Fork 2
/
ums.go
137 lines (114 loc) · 3.66 KB
/
ums.go
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
package main
import (
"chnvideo.com/cloud/common/core"
"fmt"
"net/url"
"net/http"
"strings"
"io/ioutil"
"encoding/json"
"strconv"
)
type UserInfo struct {
Id int `json:"id"`
UserName string `json:"username"`
Email string `json:"email"`
CellPhone string `json:"cellphone"`
}
type MenuSubContent struct {
Status int `json:"status"` //1-启用,0-禁用
NeedVerify int `json:"need_verify"` //是否需要审核 0-不需要,1-需要(针对 轮播频道分类)
Id int `json:"id"`
Name string `json:"name"`
Level int `json:"level"`
Url string `json:"url"`
Selected bool `json:"selected"`
ParentId int `json:"parent_id"`
Desc string `json:"desc"`
Order int `json:"order"`
WorkFlowId int `json:"wf_id"` //工作流ID
}
type MenuContent struct {
MenuSubContent
ChileMenu []*MenuSubContent `json:"child_menu"`
}
type Menu struct {
MenuContent
ChildMenu []*MenuContent `json:"child_menu"`
}
type Ums struct {
}
func (v *Ums) postForm(sid, url string, form url.Values) (body []byte, err error) {
var resp *http.Response
var req *http.Request
if req, err = http.NewRequest("POST", url, strings.NewReader(form.Encode())); err != nil {
core.LoggerError.Println("http post", url, "failed. err is", err)
return
}
req.Header.Set("Accept", "*/*")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.AddCookie(&http.Cookie{Name: "sessionid", Value: sid})
core.LoggerTrace.Println("ums post to", url, "values", form.Encode())
client := &http.Client{}
if resp, err = client.Do(req); err != nil {
core.LoggerError.Println("do request failed, err is", err)
return
}
defer resp.Body.Close()
if body, err = ioutil.ReadAll(resp.Body); err != nil {
core.LoggerError.Println("read body from", url, "failed. err is", err)
return
}
status := &struct {
Status string `json:"status"`
}{}
if err = json.Unmarshal(body, status); err != nil {
core.LoggerError.Println(fmt.Sprintf("parse ums status failed, body:%v, err is %v", string(body), err))
return
}
if status.Status != "success" {
err = fmt.Errorf("response=%v", string(body))
core.LoggerError.Println(err.Error())
return
}
return
}
func (v *Ums) MenuList(m_type, sid string, uid int) (menu []*Menu, err error) {
api := fmt.Sprintf("http://%s%s", Config().Playout.Ums, UmsGetMenuListApi)
form := make(url.Values)
form.Set("m_type", m_type)
form.Set("user_id", strconv.Itoa(uid))
form.Set("system", ProductSystem)
var body []byte
if body, err = v.postForm(sid, api, form); err != nil {
return
}
result := &struct {
Result []*Menu `json:"result"`
}{}
if err = json.Unmarshal(body, result); err != nil {
core.LoggerError.Println(fmt.Sprintf("parse ums result failed, body:%v, err is %v", string(body), err))
return
}
menu = result.Result
return
}
func (v *Ums) UserInfo(sid string) (user *UserInfo, err error) {
api := fmt.Sprintf("http://%s%s", Config().Playout.Ums, UmsGetUserApi)
form := make(url.Values)
form.Set("token", fmt.Sprintf("sessionid=%s", sid))
form.Set("fr", "ajax")
var body []byte
if body, err = v.postForm(sid, api, form); err != nil {
return
}
result := &struct {
Result *UserInfo `json:"result"`
}{}
if err = json.Unmarshal(body, result); err != nil {
core.LoggerError.Println(fmt.Sprintf("parse ums result failed, body:%v, err is %v", string(body), err))
return
}
user = result.Result
return
}