作者 tangxvhui

添加 请求企业平台进行鉴权

... ... @@ -8,21 +8,22 @@ import (
//MyConfig 自定义配置选项
type MyConfig struct {
ConfigName string //配置名称
SqlConn string //数据库连接
RedisAddPort string //
RedisAuth string
RedisDB int
LogOutput string
LogFilename string
LogLevel string
UcenterCheckAlt string
UcenterBaseUrl string
UcenterSecret string
UcenterAppKey string
FileSavePath string
FileHost string
FileHostPath string
ConfigName string //配置名称
SqlConn string //数据库连接
RedisAddPort string //
RedisAuth string
RedisDB int
LogOutput string
LogFilename string
LogLevel string
UcenterCheckAlt string
UcenterBaseUrl string
UcenterSecret string
UcenterAppKey string
FileSavePath string
FileHost string
FileHostPath string
BusinessAdminUrl string
}
//MConfig
... ... @@ -39,21 +40,22 @@ func RestMyConfig() *MyConfig {
mysqlUser, mysqlPassword, mysqlHost, mysqlPort, mysqlDBname)
sqlconn = sqlconn + "?charset=utf8&loc=Asia%2FShanghai"
MConfig = &MyConfig{
ConfigName: beego.AppConfig.String("config_name"),
SqlConn: sqlconn,
RedisAddPort: fmt.Sprintf("%s:%s", beego.AppConfig.String("redis_add"), beego.AppConfig.String("redis_add_port")),
RedisAuth: beego.AppConfig.DefaultString("redis_auth", ""),
RedisDB: beego.AppConfig.DefaultInt("redis_db", 0),
LogOutput: beego.AppConfig.DefaultString("log_output", "console"),
LogFilename: beego.AppConfig.DefaultString("log_filename", "./log/ability.log"),
LogLevel: beego.AppConfig.DefaultString("log_Level", "debug"),
UcenterCheckAlt: beego.AppConfig.String("ucenter_check_alt"),
UcenterBaseUrl: beego.AppConfig.String("ucenter_base_url"),
UcenterSecret: beego.AppConfig.String("ucenter_secret"),
UcenterAppKey: beego.AppConfig.String("ucenter_app_key"),
FileSavePath: beego.AppConfig.String("file_save_path"),
FileHost: beego.AppConfig.String("file_host"),
FileHostPath: beego.AppConfig.String("file_host_path"),
ConfigName: beego.AppConfig.String("config_name"),
SqlConn: sqlconn,
RedisAddPort: fmt.Sprintf("%s:%s", beego.AppConfig.String("redis_add"), beego.AppConfig.String("redis_add_port")),
RedisAuth: beego.AppConfig.DefaultString("redis_auth", ""),
RedisDB: beego.AppConfig.DefaultInt("redis_db", 0),
LogOutput: beego.AppConfig.DefaultString("log_output", "console"),
LogFilename: beego.AppConfig.DefaultString("log_filename", "./log/ability.log"),
LogLevel: beego.AppConfig.DefaultString("log_Level", "debug"),
UcenterCheckAlt: beego.AppConfig.String("ucenter_check_alt"),
UcenterBaseUrl: beego.AppConfig.String("ucenter_base_url"),
UcenterSecret: beego.AppConfig.String("ucenter_secret"),
UcenterAppKey: beego.AppConfig.String("ucenter_app_key"),
FileSavePath: beego.AppConfig.String("file_save_path"),
FileHost: beego.AppConfig.String("file_host"),
FileHostPath: beego.AppConfig.String("file_host_path"),
BusinessAdminUrl: beego.AppConfig.String("business_admin_url"),
}
return MConfig
}
... ...
... ... @@ -32,6 +32,9 @@ ucenter_base_url = "http://suplus-ucenter-dev.fjmaimaimai.com"
ucenter_app_key = "39aefef9e22744a3b2d2d3791824ae7b"
ucenter_secret = "cykbjnfqgctn"
##企业平台相关配置
business_admin_url = "http://suplus-business-admin-dev.fjmaimaimai.com"
# 上传文件保存路径
file_save_path = "/var/www/oppmg/file/opportunity"
file_host = "http://mmm-oppmg-dev.fjmaimaimai.com"
... ...
... ... @@ -32,6 +32,9 @@ ucenter_base_url = "https://public-interface.fjmaimaimai.com/ucenter"
ucenter_app_key = "39aefef9e22744a3b2d2d3791824ae7b"
ucenter_secret = "cykbjnfqgctn"
##企业平台相关配置
business_admin_url = "http://suplus-business-admin-prd.fjmaimaimai.com"
# 上传文件保存路径
file_save_path = "/var/www/oppmg/file/opportunity"
file_host = "https://public-interface.fjmaimaimai.com/opportunitymg"
... ...
... ... @@ -32,6 +32,9 @@ ucenter_base_url = "http://suplus-ucenter-test.fjmaimaimai.com"
ucenter_app_key = "39aefef9e22744a3b2d2d3791824ae7b"
ucenter_secret = "cykbjnfqgctn"
##企业平台相关配置
business_admin_url = "http://suplus-business-admin-test.fjmaimaimai.com"
# 上传文件保存路径
file_save_path = "/var/www/oppmg/file/opportunity"
file_host = "http://mmm-oppmg-test.fjmaimaimai.com"
... ...
... ... @@ -8,6 +8,7 @@ import (
"oppmg/common/log"
"oppmg/models"
"oppmg/protocol"
serverplatform "oppmg/services/platform"
serverbac "oppmg/services/rbac"
"oppmg/services/ucenter"
"oppmg/storage/redisdata"
... ... @@ -593,6 +594,7 @@ func LoginAuthBySecretKey(secretKey string) (protocol.LoginAuthToken, error) {
},
}
}
userdata, err = models.GetUserByPhone(uclientReturn.Data.Phone)
if err != nil {
log.Debug("GetUserByPhone(%d) err:%s", uclientReturn.Data.Phone, err)
... ... @@ -613,6 +615,23 @@ func LoginAuthBySecretKey(secretKey string) (protocol.LoginAuthToken, error) {
return logintoken, protocol.NewErrWithMessage("10022")
}
businessAdminResp, err := serverplatform.GetuserAuthDo(ucompany.Id)
if err != nil {
log.Error("向企业平台获取鉴权结果失败,err:%s", err)
return logintoken, protocol.NewErrWithMessage("1")
}
if ok := businessAdminResp.IsOK(); !ok {
return logintoken, protocol.ErrWithMessage{
ErrorCode: protocol.ErrorCode{
Errno: fmt.Sprint(businessAdminResp.Code),
Errmsg: businessAdminResp.Msg,
},
}
}
if !businessAdminResp.Data.UserAuth {
log.Error("用户没有权限进行操作")
return logintoken, protocol.NewErrWithMessage("10080")
}
logintoken, _ = GenerateAuthToken(userdata.Id, companyData.Id, ucompany.Id)
//更新用户数据
userdata.Accid = uclientReturn.Data.Accid
... ...
... ... @@ -2,6 +2,8 @@ package platform
import "errors"
//提供给企业平台调用
type PlatformAction interface {
DoAction(string, []byte) error
}
... ...
package platform
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
mconfig "oppmg/common/config"
"oppmg/common/log"
"time"
)
//请求企业平台的接口
type IBusinessAdminParam interface {
Format() []byte
GetPath() (string, string) //返回请求路径path,请求方式mathod
}
type BusinessAdminClient struct {
baseUrl string
}
func NewBusinessAdminClient() *BusinessAdminClient {
return &BusinessAdminClient{
baseUrl: mconfig.MConfig.BusinessAdminUrl,
}
}
func (client BusinessAdminClient) buildHeader() http.Header {
var h = http.Header{}
h.Set("Content-Type", "application/json")
h.Set("Accept", "application/json")
return h
}
//httpDo post发送json
func (client BusinessAdminClient) httpDo(path string, mathod string, posts []byte) ([]byte, error) {
httpclient := http.Client{
Timeout: 10 * time.Second, //请求超时时间5秒
}
reqURL := client.baseUrl + path
req, err := http.NewRequest(mathod, reqURL, bytes.NewReader(posts))
if err != nil {
return nil, err
}
req.Header = client.buildHeader()
resp, err := httpclient.Do(req)
log.Info("====>Send To URL:%s", reqURL)
log.Info("====>Send To BusinessAdmin:%s", string(posts))
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
log.Info("<====BusinessAdmin Return:%s", string(body))
return body, nil
}
func (client BusinessAdminClient) Call(param IBusinessAdminParam) ([]byte, error) {
path, mathod := param.GetPath()
return client.httpDo(path, mathod, param.Format())
}
type CommResponse struct {
Code int `json:"code"`
Msg string `json:"msg"`
}
func (resp CommResponse) IsOK() bool {
return true
}
//GetuserAuth 企业后台鉴权
//userId 企业后台的用户id ,对应机会系统内的user_company表的字段id
//PlatformId 编号 机会系统固定值18
type RequestGetuserAuth struct {
UserId string `json:"userId"`
platformId string `json:"platformId"`
}
type ResponseGetUserAuth struct {
CommResponse
Data struct {
UserAuth bool `json:"userAuth"`
} `json:"data"`
}
func (r RequestGetuserAuth) Format() []byte {
r.platformId = "18"
var bt []byte
bt, _ = json.Marshal(r)
return bt
}
func (r RequestGetuserAuth) GetPath() (string, string) {
return "/auth/get-user-auth", "POST"
}
func GetuserAuthDo(userid int64) (ResponseGetUserAuth, error) {
param := RequestGetuserAuth{
UserId: fmt.Sprint(userid),
}
var resp ResponseGetUserAuth
uclient := NewBusinessAdminClient()
btBody, err := uclient.Call(param)
if err != nil {
log.Error("向企业平台发送请求失败 err:%s", err)
return resp, errors.New("向企业平台发送请求失败")
}
err = json.Unmarshal(btBody, &resp)
if err != nil {
log.Error("解析企业平台响应失败 err:%s", err)
return resp, errors.New("解析企业平台响应失败")
}
return resp, nil
}
... ...
... ... @@ -8,6 +8,8 @@ import (
"oppmg/protocol"
)
// 请求统一用户中心的接口
//RequestUCenterLogin 调用用户中心进行账号密码登录
func RequestUCenterLogin(account, password string) (*ResponseLogin, error) {
var uclientReturn ResponseLogin
... ... @@ -19,7 +21,7 @@ func RequestUCenterLogin(account, password string) (*ResponseLogin, error) {
uclient := NewUCenterClient()
btBody, err := uclient.Call(param)
if err != nil {
log.Error("统一用户中心请求失败 err:%s", err)
log.Error("向统一用户中心发送请求失败 err:%s", err)
return nil, protocol.NewErrWithMessage("1")
}
err = json.Unmarshal(btBody, &uclientReturn)
... ... @@ -27,7 +29,7 @@ func RequestUCenterLogin(account, password string) (*ResponseLogin, error) {
log.Error("解析统一用户中心响应失败 err:%s", err)
return nil, protocol.NewErrWithMessage("1")
}
if !(uclientReturn.Code == ResponseCode0) {
if !(uclientReturn.Code == ResponseCodeOk) {
return nil, protocol.NewErrWithMessage("10052")
}
return &uclientReturn, nil
... ... @@ -44,7 +46,7 @@ func RequestUCenterLoginBySecret(secret string) (*ResponseLogin, error) {
uclient := NewUCenterClient()
btBody, err := uclient.Call(param)
if err != nil {
log.Error("统一用户中心请求失败 err:%s", err)
log.Error("向统一用户中心发送请求失败 err:%s", err)
return nil, protocol.NewErrWithMessage("1")
}
err = json.Unmarshal(btBody, &uclientReturn)
... ... @@ -52,7 +54,7 @@ func RequestUCenterLoginBySecret(secret string) (*ResponseLogin, error) {
log.Error("解析统一用户中心响应失败 err:%s", err)
return nil, protocol.NewErrWithMessage("1")
}
if !(uclientReturn.Code == ResponseCode0) {
if !(uclientReturn.Code == ResponseCodeOk) {
return &uclientReturn, protocol.NewErrWithMessage("1")
}
return &uclientReturn, nil
... ... @@ -75,7 +77,7 @@ func RequestUCenterAddUser(phone string, nickname string, avatar string) (*Respo
uclient := NewUCenterClient()
btBody, err := uclient.Call(param)
if err != nil {
log.Error("统一用户中心请求失败 err:%s", err)
log.Error("向统一用户中心发送请求失败 err:%s", err)
return nil, protocol.NewErrWithMessage("1")
}
err = json.Unmarshal(btBody, &ucenterReturn)
... ... @@ -83,7 +85,7 @@ func RequestUCenterAddUser(phone string, nickname string, avatar string) (*Respo
log.Error("解析统一用户中心响应失败 err:%s", err)
return nil, protocol.NewErrWithMessage("1")
}
if !(ucenterReturn.Code == ResponseCode0) {
if !(ucenterReturn.Code == ResponseCodeOk) {
return nil, protocol.NewErrWithMessage("10052")
}
return &ucenterReturn, nil
... ... @@ -100,7 +102,7 @@ func RequestUCenterSmsCode(phone string) error {
uclient := NewUCenterClient()
btBody, err := uclient.Call(param)
if err != nil {
log.Error("统一用户中心请求失败 err:%s", err)
log.Error("向统一用户中心发送请求失败 err:%s", err)
return protocol.NewErrWithMessage("1")
}
var ucenterReturn CommResponse
... ... @@ -109,7 +111,7 @@ func RequestUCenterSmsCode(phone string) error {
log.Error("解析统一用户中心响应失败 err:%s", err)
return protocol.NewErrWithMessage("1")
}
if !(ucenterReturn.Code == ResponseCode0) {
if !(ucenterReturn.Code == ResponseCodeOk) {
return protocol.NewErrWithMessage("10052")
}
return nil
... ... @@ -125,7 +127,7 @@ func RequestUCenterLoginSms(phone string, captcha string) (*ResponseLoginSms, er
uclient := NewUCenterClient()
btBody, err := uclient.Call(param)
if err != nil {
log.Error("统一用户中心请求失败 err:%s", err)
log.Error("向统一用户中心发送请求失败 err:%s", err)
return nil, protocol.NewErrWithMessage("1")
}
var ucenterReturn ResponseLoginSms
... ... @@ -134,7 +136,7 @@ func RequestUCenterLoginSms(phone string, captcha string) (*ResponseLoginSms, er
log.Error("解析统一用户中心响应失败 err:%s", err)
return nil, protocol.NewErrWithMessage("1")
}
if !(ucenterReturn.Code == ResponseCode0) {
if !(ucenterReturn.Code == ResponseCodeOk) {
return &ucenterReturn, protocol.NewErrWithMessage("10052")
}
return &ucenterReturn, nil
... ... @@ -148,7 +150,7 @@ func RequestUCenterSmsCodeCheck(phone string, captcha string) (*ResponseSmsCodeC
uclient := NewUCenterClient()
btBody, err := uclient.Call(param)
if err != nil {
log.Error("统一用户中心请求失败 err:%s", err)
log.Error("向统一用户中心发送请求失败 err:%s", err)
return nil, protocol.NewErrWithMessage("1")
}
var ucenterReturn ResponseSmsCodeCheck
... ... @@ -157,7 +159,7 @@ func RequestUCenterSmsCodeCheck(phone string, captcha string) (*ResponseSmsCodeC
log.Error("解析统一用户中心响应失败 err:%s", err)
return nil, protocol.NewErrWithMessage("1")
}
if !(ucenterReturn.Code == ResponseCode0) {
if !(ucenterReturn.Code == ResponseCodeOk) {
return &ucenterReturn, protocol.NewErrWithMessage("10026")
}
return &ucenterReturn, nil
... ... @@ -173,7 +175,7 @@ func RequestUCenterRestPassword(phone, newPwd, confirmPwd, certificate string) e
uclient := NewUCenterClient()
btBody, err := uclient.Call(param)
if err != nil {
log.Error("统一用户中心请求失败 err:%s", err)
log.Error("向统一用户中心发送请求失败 err:%s", err)
return protocol.NewErrWithMessage("1")
}
var ucenterReturn CommResponse
... ... @@ -182,7 +184,7 @@ func RequestUCenterRestPassword(phone, newPwd, confirmPwd, certificate string) e
log.Error("解析统一用户中心响应失败 err:%s", err)
return protocol.NewErrWithMessage("1")
}
if !(ucenterReturn.Code == ResponseCode0) {
if !(ucenterReturn.Code == ResponseCodeOk) {
return protocol.NewErrWithMessage("10052")
}
... ...
... ... @@ -12,7 +12,7 @@ import (
)
const (
ResponseCode0 int = 0
ResponseCodeOk int = 0
)
type IUCenterParam interface {
... ...