enterprise
This commit is contained in:
parent
9e09eece6f
commit
10a63309ee
|
|
@ -0,0 +1,19 @@
|
||||||
|
package global
|
||||||
|
|
||||||
|
type Enterprise struct {
|
||||||
|
token string
|
||||||
|
baseUrl string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Enterprise) GetHeader() map[string]string {
|
||||||
|
return map[string]string{
|
||||||
|
"x-token": e.token,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Enterprise) GetBaseUrl() string {
|
||||||
|
if e.baseUrl != "" {
|
||||||
|
return e.baseUrl
|
||||||
|
}
|
||||||
|
return "http://e.batiao8.com"
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,76 @@
|
||||||
|
package global
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"git.u8t.cn/open/goutil"
|
||||||
|
"github.com/tidwall/gjson"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
corpId = 1000
|
||||||
|
)
|
||||||
|
|
||||||
|
type VerifyCode struct {
|
||||||
|
Username string `json:"username"`
|
||||||
|
Timestamp string `json:"timestamp"`
|
||||||
|
}
|
||||||
|
type StaffInfo struct {
|
||||||
|
Username string `json:"username"`
|
||||||
|
Realname string `json:"realname"`
|
||||||
|
Phone string `json:"phone"`
|
||||||
|
}
|
||||||
|
type StaffUser struct {
|
||||||
|
Enterprise
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewStaffUser(baseUrl, token string) *StaffUser {
|
||||||
|
return &StaffUser{
|
||||||
|
Enterprise: Enterprise{
|
||||||
|
baseUrl: baseUrl,
|
||||||
|
token: token,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *StaffUser) SendVerifyCode(username, scene string) (*VerifyCode, error) {
|
||||||
|
var reqBody string
|
||||||
|
reqBody = fmt.Sprintf(`{"username":"%s","scene":"%s"}`, username, scene)
|
||||||
|
reqUrl := e.GetBaseUrl() + "/api/staff/verify/code"
|
||||||
|
|
||||||
|
body, err := goutil.HttpPost(reqUrl, e.GetHeader(), []byte(reqBody))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
g := gjson.ParseBytes(body)
|
||||||
|
if g.Get("code").Int() != 0 {
|
||||||
|
return nil, errors.New(string(body))
|
||||||
|
}
|
||||||
|
r := new(VerifyCode)
|
||||||
|
r.Timestamp = g.Get("data.timestamp").String()
|
||||||
|
r.Username = g.Get("data.username").String()
|
||||||
|
|
||||||
|
return r, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *StaffUser) Login(username, timestamp, code string) (*StaffInfo, error) {
|
||||||
|
reqBody := fmt.Sprintf(`{"username":"%s","timestamp":"%s","code":"%s"}`, username, timestamp, code)
|
||||||
|
reqUrl := e.GetBaseUrl() + "/api/staff/login"
|
||||||
|
body, err := goutil.HttpPost(reqUrl, e.GetHeader(), []byte(reqBody))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
g := gjson.ParseBytes(body)
|
||||||
|
if g.Get("code").Int() != 0 {
|
||||||
|
return nil, errors.New(string(body))
|
||||||
|
}
|
||||||
|
|
||||||
|
r := new(StaffInfo)
|
||||||
|
r.Realname = g.Get("data.realname").String()
|
||||||
|
r.Username = g.Get("data.username").String()
|
||||||
|
r.Phone = g.Get("data.phone").String()
|
||||||
|
|
||||||
|
return r, nil
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue