Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
杨树贤
/
kefu_server
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
211eb670
authored
Mar 11, 2020
by
keith
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
update
parent
f3517e22
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
78 additions
and
11 deletions
conf/app.conf
controllers/auth_controller.go
controllers/robot_controller.go
filters/index.go
models/auth_types_base.go
models/auths_base.go
routers/router.go
services/robot_service.go
utils/http_request.go
conf/app.conf
View file @
211eb670
...
...
@@ -36,7 +36,7 @@ kf_driver_name= "mysql"
kf_mysql_host
=
"aissz.com"
kf_mysql_port
=
"3636"
kf_mysql_user
=
"root"
kf_mysql_db
=
"kefu_server"
kf_mysql_db
=
"kefu_server
_dev
"
kf_mysql_pwd
=
"chenxianqi"
...
...
controllers/auth_controller.go
View file @
211eb670
...
...
@@ -173,7 +173,6 @@ func (c *AuthController) RobotFetchToken() {
for
_
,
err
:=
range
valid
.
Errors
{
c
.
JSON
(
configs
.
ResponseFail
,
err
.
Message
,
nil
)
}
}
// MD5
...
...
@@ -191,12 +190,36 @@ func (c *AuthController) RobotFetchToken() {
// check
if
reqyestSecret
!=
currentAppSecret
{
c
.
JSON
(
configs
.
ResponseFail
,
"server error~"
,
currentAppSecret
)
c
.
JSON
(
configs
.
ResponseFail
,
"server error~"
,
reqyestSecret
)
c
.
JSON
(
configs
.
ResponseFail
,
"server error~"
,
nil
)
}
// create token
newToken
:=
utils
.
GenerateToken
(
models
.
JwtKeyDto
{
ID
:
_AppID
,
UserName
:
_AppKey
,
AuthType
:
0
})
auth
:=
c
.
AuthsRepository
.
GetAuthInfoWithTypeAndUID
(
0
,
0
)
if
auth
==
nil
{
newAuth
:=
models
.
Auths
{
Token
:
newToken
,
UID
:
0
,
AuthType
:
0
,
UpdateAt
:
time
.
Now
()
.
Unix
(),
CreateAt
:
time
.
Now
()
.
Unix
(),
}
if
_
,
err
:=
c
.
AuthsRepository
.
Add
(
&
newAuth
);
err
!=
nil
{
c
.
JSON
(
configs
.
ResponseFail
,
"授权失败!"
,
nil
)
}
}
else
{
_
,
err
:=
c
.
AuthsRepository
.
Update
(
auth
.
ID
,
orm
.
Params
{
"Token"
:
newToken
,
"UpdateAt"
:
time
.
Now
()
.
Unix
(),
})
if
err
!=
nil
{
c
.
JSON
(
configs
.
ResponseFail
,
"授权失败!"
,
nil
)
}
}
c
.
JSON
(
configs
.
ResponseSucess
,
"授权成功!"
,
&
newToken
)
...
...
controllers/robot_controller.go
View file @
211eb670
...
...
@@ -235,3 +235,15 @@ func (c *RobotController) List() {
c
.
JSON
(
configs
.
ResponseSucess
,
"success"
,
&
robots
)
}
// GetOnlineAll get all ofline robot
func
(
c
*
RobotController
)
GetOnlineAll
()
{
// query
robots
,
err
:=
c
.
RobotRepository
.
GetRobotOnlineAll
()
if
err
!=
nil
{
c
.
JSON
(
configs
.
ResponseFail
,
"fail"
,
nil
)
}
c
.
JSON
(
configs
.
ResponseSucess
,
"success"
,
&
robots
)
}
filters/index.go
View file @
211eb670
...
...
@@ -9,6 +9,8 @@ import (
"strings"
"time"
"github.com/astaxie/beego/logs"
"github.com/astaxie/beego/context"
"github.com/astaxie/beego/orm"
)
...
...
@@ -27,9 +29,11 @@ var FilterToken = func(ctx *context.Context) {
whitelist
:=
[]
string
{
"/v1/auth/login"
,
"/v1/auth/logout"
,
"/v1/auth/token"
,
}
oldToken
:=
ctx
.
Input
.
Header
(
"Authorization"
)
logs
.
Info
(
"oldTokenoldToken==="
,
oldToken
)
isExistInSlice
:=
utils
.
InExistInSlice
(
ctx
.
Request
.
RequestURI
,
whitelist
)
isMatch
,
_
:=
regexp
.
MatchString
(
`^Bearer\s`
,
oldToken
)
if
(
isExistInSlice
==
false
&&
oldToken
==
""
)
||
!
isMatch
{
...
...
models/auth_types_base.go
View file @
211eb670
package
models
// AuthTypes 登录配置模
// 目前有一种情况是机器人客户端使用的 0类型
type
AuthTypes
struct
{
ID
int64
`orm:"auto;pk;column(id)" json:"id"`
// ID
Title
string
`orm:"type(char);column(title)" json:"title"`
// 标题
...
...
models/auths_base.go
View file @
211eb670
package
models
// Auths 登录授权信息
// 目前有一种情况是机器人客户端使用的 UID=0 进行登录
type
Auths
struct
{
ID
int64
`orm:"auto;pk;column(id)" json:"id"`
// 客服(管理员)ID
AuthType
int64
`orm:"column(auth_type)" json:"auth_type"`
// AuthType登录配置模ID
...
...
routers/router.go
View file @
211eb670
...
...
@@ -95,6 +95,7 @@ func init() {
beego
.
NSBefore
(
filters
.
FilterToken
),
beego
.
NSRouter
(
"/?:id"
,
&
controllers
.
RobotController
{}),
beego
.
NSRouter
(
"/list"
,
&
controllers
.
RobotController
{},
"get:List"
),
beego
.
NSRouter
(
"/online/all"
,
&
controllers
.
RobotController
{},
"get:GetOnlineAll"
),
),
// shortcut
...
...
services/robot_service.go
View file @
211eb670
...
...
@@ -15,6 +15,7 @@ type RobotRepositoryInterface interface {
GetRobot
(
id
int64
)
*
models
.
Robot
GetRobotWithNickName
(
nickName
string
)
*
models
.
Robot
GetRobotWithOnline
(
platformID
int64
)
(
*
models
.
Robot
,
error
)
GetRobotOnlineAll
()
([]
*
models
.
Robot
,
error
)
GetRobotWithRandomOnline
()
*
models
.
Robot
GetRobots
()
([]
models
.
Robot
,
error
)
GetRobotWithInIds
(
ids
...
int64
)
([]
models
.
Robot
,
error
)
...
...
@@ -121,6 +122,20 @@ func (r *RobotRepository) GetRobotWithRandomOnline() (*models.Robot, error) {
return
robot
,
nil
}
// GetRobotOnlineAll get one Robot with Random Online
func
(
r
*
RobotRepository
)
GetRobotOnlineAll
()
([]
*
models
.
Robot
,
error
)
{
var
robots
[]
*
models
.
Robot
if
_
,
err
:=
r
.
q
.
Filter
(
"switch"
,
1
)
.
All
(
&
robots
);
err
!=
nil
{
logs
.
Warn
(
"GetRobotWithRandomOnline get one Robot with Random Online------------"
,
err
)
return
nil
,
err
}
for
index
:=
range
robots
{
robots
[
index
]
.
Artificial
=
strings
.
Trim
(
robots
[
index
]
.
Artificial
,
"|"
)
robots
[
index
]
.
KeyWord
=
strings
.
Trim
(
robots
[
index
]
.
KeyWord
,
"|"
)
}
return
robots
,
nil
}
// GetRobotWithOnline get one Robot with Online
func
(
r
*
RobotRepository
)
GetRobotWithOnline
(
platformID
int64
)
(
*
models
.
Robot
,
error
)
{
var
robots
[]
*
models
.
Robot
...
...
utils/http_request.go
View file @
211eb670
package
utils
import
(
"bytes"
"encoding/json"
"io/ioutil"
"log"
"math/rand"
"net/http"
"net/url"
"strings"
"time"
"github.com/astaxie/beego/logs"
...
...
@@ -44,10 +45,17 @@ type HTTPResponse struct {
// method post, get 等
// data body 数据
// token 授权token
func
HTTPRequest
(
path
string
,
method
string
,
bodyData
url
.
Values
,
token
string
)
*
HTTPResponse
{
func
HTTPRequest
(
path
string
,
method
string
,
data
interface
{}
,
token
string
)
*
HTTPResponse
{
client
:=
&
http
.
Client
{}
response
:=
new
(
HTTPResponse
)
req
,
err
:=
http
.
NewRequest
(
method
,
path
,
strings
.
NewReader
(
bodyData
.
Encode
()))
bodyJSON
,
err
:=
json
.
Marshal
(
data
)
if
err
!=
nil
{
log
.
Println
(
err
)
response
.
Code
=
400
response
.
Message
=
"json error"
return
response
}
req
,
err
:=
http
.
NewRequest
(
method
,
path
,
bytes
.
NewBuffer
(
bodyJSON
))
if
err
!=
nil
{
response
.
Code
=
500
response
.
Message
=
"链接错误"
...
...
@@ -57,7 +65,7 @@ func HTTPRequest(path string, method string, bodyData url.Values, token string)
req
.
Header
.
Add
(
"Accept-Language"
,
"zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3"
)
req
.
Header
.
Add
(
"Connection"
,
"keep-alive"
)
req
.
Header
.
Add
(
"User-Agent"
,
GetRandomUserAgent
())
req
.
Header
.
Set
(
"Content-Type"
,
"application/
x-www-form-urlencoded
"
)
req
.
Header
.
Set
(
"Content-Type"
,
"application/
json
"
)
req
.
Header
.
Set
(
"Access-Control-Max-Age"
,
"2592000"
)
req
.
Header
.
Set
(
"Authorization"
,
token
)
req
.
Header
.
Set
(
"usertype"
,
"cmp_app"
)
...
...
@@ -66,14 +74,16 @@ func HTTPRequest(path string, method string, bodyData url.Values, token string)
defer
resp
.
Body
.
Close
()
body
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
)
if
err
!=
nil
{
response
.
Code
=
400
response
.
Message
=
"请求错误"
logs
.
Error
(
err
)
return
response
}
if
resp
.
StatusCode
!=
200
{
response
.
Code
=
400
response
.
Message
=
"请求错误"
return
response
}
response
.
Data
=
string
(
body
)
json
.
Unmarshal
(
body
,
response
)
// response.Data = string(body)
return
response
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment