Commit 211eb670 by keith

update

parent f3517e22
...@@ -36,7 +36,7 @@ kf_driver_name= "mysql" ...@@ -36,7 +36,7 @@ kf_driver_name= "mysql"
kf_mysql_host = "aissz.com" kf_mysql_host = "aissz.com"
kf_mysql_port = "3636" kf_mysql_port = "3636"
kf_mysql_user = "root" kf_mysql_user = "root"
kf_mysql_db = "kefu_server" kf_mysql_db = "kefu_server_dev"
kf_mysql_pwd = "chenxianqi" kf_mysql_pwd = "chenxianqi"
......
...@@ -173,7 +173,6 @@ func (c *AuthController) RobotFetchToken() { ...@@ -173,7 +173,6 @@ func (c *AuthController) RobotFetchToken() {
for _, err := range valid.Errors { for _, err := range valid.Errors {
c.JSON(configs.ResponseFail, err.Message, nil) c.JSON(configs.ResponseFail, err.Message, nil)
} }
} }
// MD5 // MD5
...@@ -191,12 +190,36 @@ func (c *AuthController) RobotFetchToken() { ...@@ -191,12 +190,36 @@ func (c *AuthController) RobotFetchToken() {
// check // check
if reqyestSecret != currentAppSecret { if reqyestSecret != currentAppSecret {
c.JSON(configs.ResponseFail, "server error~", currentAppSecret) c.JSON(configs.ResponseFail, "server error~", nil)
c.JSON(configs.ResponseFail, "server error~", reqyestSecret)
} }
// create token // create token
newToken := utils.GenerateToken(models.JwtKeyDto{ID: _AppID, UserName: _AppKey, AuthType: 0}) 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) c.JSON(configs.ResponseSucess, "授权成功!", &newToken)
......
...@@ -235,3 +235,15 @@ func (c *RobotController) List() { ...@@ -235,3 +235,15 @@ func (c *RobotController) List() {
c.JSON(configs.ResponseSucess, "success", &robots) 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)
}
...@@ -9,6 +9,8 @@ import ( ...@@ -9,6 +9,8 @@ import (
"strings" "strings"
"time" "time"
"github.com/astaxie/beego/logs"
"github.com/astaxie/beego/context" "github.com/astaxie/beego/context"
"github.com/astaxie/beego/orm" "github.com/astaxie/beego/orm"
) )
...@@ -27,9 +29,11 @@ var FilterToken = func(ctx *context.Context) { ...@@ -27,9 +29,11 @@ var FilterToken = func(ctx *context.Context) {
whitelist := []string{ whitelist := []string{
"/v1/auth/login", "/v1/auth/login",
"/v1/auth/logout", "/v1/auth/logout",
"/v1/auth/token",
} }
oldToken := ctx.Input.Header("Authorization") oldToken := ctx.Input.Header("Authorization")
logs.Info("oldTokenoldToken===", oldToken)
isExistInSlice := utils.InExistInSlice(ctx.Request.RequestURI, whitelist) isExistInSlice := utils.InExistInSlice(ctx.Request.RequestURI, whitelist)
isMatch, _ := regexp.MatchString(`^Bearer\s`, oldToken) isMatch, _ := regexp.MatchString(`^Bearer\s`, oldToken)
if (isExistInSlice == false && oldToken == "") || !isMatch { if (isExistInSlice == false && oldToken == "") || !isMatch {
......
package models package models
// AuthTypes 登录配置模 // AuthTypes 登录配置模
// 目前有一种情况是机器人客户端使用的 0类型
type AuthTypes struct { type AuthTypes struct {
ID int64 `orm:"auto;pk;column(id)" json:"id"` // ID ID int64 `orm:"auto;pk;column(id)" json:"id"` // ID
Title string `orm:"type(char);column(title)" json:"title"` // 标题 Title string `orm:"type(char);column(title)" json:"title"` // 标题
......
package models package models
// Auths 登录授权信息 // Auths 登录授权信息
// 目前有一种情况是机器人客户端使用的 UID=0 进行登录
type Auths struct { type Auths struct {
ID int64 `orm:"auto;pk;column(id)" json:"id"` // 客服(管理员)ID ID int64 `orm:"auto;pk;column(id)" json:"id"` // 客服(管理员)ID
AuthType int64 `orm:"column(auth_type)" json:"auth_type"` // AuthType登录配置模ID AuthType int64 `orm:"column(auth_type)" json:"auth_type"` // AuthType登录配置模ID
......
...@@ -95,6 +95,7 @@ func init() { ...@@ -95,6 +95,7 @@ func init() {
beego.NSBefore(filters.FilterToken), beego.NSBefore(filters.FilterToken),
beego.NSRouter("/?:id", &controllers.RobotController{}), beego.NSRouter("/?:id", &controllers.RobotController{}),
beego.NSRouter("/list", &controllers.RobotController{}, "get:List"), beego.NSRouter("/list", &controllers.RobotController{}, "get:List"),
beego.NSRouter("/online/all", &controllers.RobotController{}, "get:GetOnlineAll"),
), ),
// shortcut // shortcut
......
...@@ -15,6 +15,7 @@ type RobotRepositoryInterface interface { ...@@ -15,6 +15,7 @@ type RobotRepositoryInterface interface {
GetRobot(id int64) *models.Robot GetRobot(id int64) *models.Robot
GetRobotWithNickName(nickName string) *models.Robot GetRobotWithNickName(nickName string) *models.Robot
GetRobotWithOnline(platformID int64) (*models.Robot, error) GetRobotWithOnline(platformID int64) (*models.Robot, error)
GetRobotOnlineAll() ([]*models.Robot, error)
GetRobotWithRandomOnline() *models.Robot GetRobotWithRandomOnline() *models.Robot
GetRobots() ([]models.Robot, error) GetRobots() ([]models.Robot, error)
GetRobotWithInIds(ids ...int64) ([]models.Robot, error) GetRobotWithInIds(ids ...int64) ([]models.Robot, error)
...@@ -121,6 +122,20 @@ func (r *RobotRepository) GetRobotWithRandomOnline() (*models.Robot, error) { ...@@ -121,6 +122,20 @@ func (r *RobotRepository) GetRobotWithRandomOnline() (*models.Robot, error) {
return robot, nil 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 // GetRobotWithOnline get one Robot with Online
func (r *RobotRepository) GetRobotWithOnline(platformID int64) (*models.Robot, error) { func (r *RobotRepository) GetRobotWithOnline(platformID int64) (*models.Robot, error) {
var robots []*models.Robot var robots []*models.Robot
......
package utils package utils
import ( import (
"bytes"
"encoding/json"
"io/ioutil" "io/ioutil"
"log"
"math/rand" "math/rand"
"net/http" "net/http"
"net/url"
"strings"
"time" "time"
"github.com/astaxie/beego/logs" "github.com/astaxie/beego/logs"
...@@ -44,10 +45,17 @@ type HTTPResponse struct { ...@@ -44,10 +45,17 @@ type HTTPResponse struct {
// method post, get 等 // method post, get 等
// data body 数据 // data body 数据
// token 授权token // 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{} client := &http.Client{}
response := new(HTTPResponse) 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 { if err != nil {
response.Code = 500 response.Code = 500
response.Message = "链接错误" response.Message = "链接错误"
...@@ -57,7 +65,7 @@ func HTTPRequest(path string, method string, bodyData url.Values, token string) ...@@ -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("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("Connection", "keep-alive")
req.Header.Add("User-Agent", GetRandomUserAgent()) 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("Access-Control-Max-Age", "2592000")
req.Header.Set("Authorization", token) req.Header.Set("Authorization", token)
req.Header.Set("usertype", "cmp_app") req.Header.Set("usertype", "cmp_app")
...@@ -66,14 +74,16 @@ func HTTPRequest(path string, method string, bodyData url.Values, token string) ...@@ -66,14 +74,16 @@ func HTTPRequest(path string, method string, bodyData url.Values, token string)
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
response.Code = 400
response.Message = "请求错误" response.Message = "请求错误"
logs.Error(err)
return response return response
} }
if resp.StatusCode != 200 { if resp.StatusCode != 200 {
response.Code = 400
response.Message = "请求错误" response.Message = "请求错误"
return response return response
} }
response.Data = string(body) json.Unmarshal(body, response)
// response.Data = string(body)
return response return response
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment