作者 tangxvhui

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

@@ -23,6 +23,7 @@ type MyConfig struct { @@ -23,6 +23,7 @@ type MyConfig struct {
23 FileSavePath string 23 FileSavePath string
24 FileHost string 24 FileHost string
25 FileHostPath string 25 FileHostPath string
  26 + BusinessAdminUrl string
26 } 27 }
27 28
28 //MConfig 29 //MConfig
@@ -54,6 +55,7 @@ func RestMyConfig() *MyConfig { @@ -54,6 +55,7 @@ func RestMyConfig() *MyConfig {
54 FileSavePath: beego.AppConfig.String("file_save_path"), 55 FileSavePath: beego.AppConfig.String("file_save_path"),
55 FileHost: beego.AppConfig.String("file_host"), 56 FileHost: beego.AppConfig.String("file_host"),
56 FileHostPath: beego.AppConfig.String("file_host_path"), 57 FileHostPath: beego.AppConfig.String("file_host_path"),
  58 + BusinessAdminUrl: beego.AppConfig.String("business_admin_url"),
57 } 59 }
58 return MConfig 60 return MConfig
59 } 61 }
@@ -32,6 +32,9 @@ ucenter_base_url = "http://suplus-ucenter-dev.fjmaimaimai.com" @@ -32,6 +32,9 @@ ucenter_base_url = "http://suplus-ucenter-dev.fjmaimaimai.com"
32 ucenter_app_key = "39aefef9e22744a3b2d2d3791824ae7b" 32 ucenter_app_key = "39aefef9e22744a3b2d2d3791824ae7b"
33 ucenter_secret = "cykbjnfqgctn" 33 ucenter_secret = "cykbjnfqgctn"
34 34
  35 +##企业平台相关配置
  36 +business_admin_url = "http://suplus-business-admin-dev.fjmaimaimai.com"
  37 +
35 # 上传文件保存路径 38 # 上传文件保存路径
36 file_save_path = "/var/www/oppmg/file/opportunity" 39 file_save_path = "/var/www/oppmg/file/opportunity"
37 file_host = "http://mmm-oppmg-dev.fjmaimaimai.com" 40 file_host = "http://mmm-oppmg-dev.fjmaimaimai.com"
@@ -32,6 +32,9 @@ ucenter_base_url = "https://public-interface.fjmaimaimai.com/ucenter" @@ -32,6 +32,9 @@ ucenter_base_url = "https://public-interface.fjmaimaimai.com/ucenter"
32 ucenter_app_key = "39aefef9e22744a3b2d2d3791824ae7b" 32 ucenter_app_key = "39aefef9e22744a3b2d2d3791824ae7b"
33 ucenter_secret = "cykbjnfqgctn" 33 ucenter_secret = "cykbjnfqgctn"
34 34
  35 +##企业平台相关配置
  36 +business_admin_url = "http://suplus-business-admin-prd.fjmaimaimai.com"
  37 +
35 # 上传文件保存路径 38 # 上传文件保存路径
36 file_save_path = "/var/www/oppmg/file/opportunity" 39 file_save_path = "/var/www/oppmg/file/opportunity"
37 file_host = "https://public-interface.fjmaimaimai.com/opportunitymg" 40 file_host = "https://public-interface.fjmaimaimai.com/opportunitymg"
@@ -32,6 +32,9 @@ ucenter_base_url = "http://suplus-ucenter-test.fjmaimaimai.com" @@ -32,6 +32,9 @@ ucenter_base_url = "http://suplus-ucenter-test.fjmaimaimai.com"
32 ucenter_app_key = "39aefef9e22744a3b2d2d3791824ae7b" 32 ucenter_app_key = "39aefef9e22744a3b2d2d3791824ae7b"
33 ucenter_secret = "cykbjnfqgctn" 33 ucenter_secret = "cykbjnfqgctn"
34 34
  35 +##企业平台相关配置
  36 +business_admin_url = "http://suplus-business-admin-test.fjmaimaimai.com"
  37 +
35 # 上传文件保存路径 38 # 上传文件保存路径
36 file_save_path = "/var/www/oppmg/file/opportunity" 39 file_save_path = "/var/www/oppmg/file/opportunity"
37 file_host = "http://mmm-oppmg-test.fjmaimaimai.com" 40 file_host = "http://mmm-oppmg-test.fjmaimaimai.com"
@@ -8,6 +8,7 @@ import ( @@ -8,6 +8,7 @@ import (
8 "oppmg/common/log" 8 "oppmg/common/log"
9 "oppmg/models" 9 "oppmg/models"
10 "oppmg/protocol" 10 "oppmg/protocol"
  11 + serverplatform "oppmg/services/platform"
11 serverbac "oppmg/services/rbac" 12 serverbac "oppmg/services/rbac"
12 "oppmg/services/ucenter" 13 "oppmg/services/ucenter"
13 "oppmg/storage/redisdata" 14 "oppmg/storage/redisdata"
@@ -593,6 +594,7 @@ func LoginAuthBySecretKey(secretKey string) (protocol.LoginAuthToken, error) { @@ -593,6 +594,7 @@ func LoginAuthBySecretKey(secretKey string) (protocol.LoginAuthToken, error) {
593 }, 594 },
594 } 595 }
595 } 596 }
  597 +
596 userdata, err = models.GetUserByPhone(uclientReturn.Data.Phone) 598 userdata, err = models.GetUserByPhone(uclientReturn.Data.Phone)
597 if err != nil { 599 if err != nil {
598 log.Debug("GetUserByPhone(%d) err:%s", uclientReturn.Data.Phone, err) 600 log.Debug("GetUserByPhone(%d) err:%s", uclientReturn.Data.Phone, err)
@@ -613,6 +615,23 @@ func LoginAuthBySecretKey(secretKey string) (protocol.LoginAuthToken, error) { @@ -613,6 +615,23 @@ func LoginAuthBySecretKey(secretKey string) (protocol.LoginAuthToken, error) {
613 return logintoken, protocol.NewErrWithMessage("10022") 615 return logintoken, protocol.NewErrWithMessage("10022")
614 } 616 }
615 617
  618 + businessAdminResp, err := serverplatform.GetuserAuthDo(ucompany.Id)
  619 + if err != nil {
  620 + log.Error("向企业平台获取鉴权结果失败,err:%s", err)
  621 + return logintoken, protocol.NewErrWithMessage("1")
  622 + }
  623 + if ok := businessAdminResp.IsOK(); !ok {
  624 + return logintoken, protocol.ErrWithMessage{
  625 + ErrorCode: protocol.ErrorCode{
  626 + Errno: fmt.Sprint(businessAdminResp.Code),
  627 + Errmsg: businessAdminResp.Msg,
  628 + },
  629 + }
  630 + }
  631 + if !businessAdminResp.Data.UserAuth {
  632 + log.Error("用户没有权限进行操作")
  633 + return logintoken, protocol.NewErrWithMessage("10080")
  634 + }
616 logintoken, _ = GenerateAuthToken(userdata.Id, companyData.Id, ucompany.Id) 635 logintoken, _ = GenerateAuthToken(userdata.Id, companyData.Id, ucompany.Id)
617 //更新用户数据 636 //更新用户数据
618 userdata.Accid = uclientReturn.Data.Accid 637 userdata.Accid = uclientReturn.Data.Accid
@@ -2,6 +2,8 @@ package platform @@ -2,6 +2,8 @@ package platform
2 2
3 import "errors" 3 import "errors"
4 4
  5 +//提供给企业平台调用
  6 +
5 type PlatformAction interface { 7 type PlatformAction interface {
6 DoAction(string, []byte) error 8 DoAction(string, []byte) error
7 } 9 }
  1 +package platform
  2 +
  3 +import (
  4 + "bytes"
  5 + "encoding/json"
  6 + "errors"
  7 + "fmt"
  8 + "io/ioutil"
  9 + "net/http"
  10 + mconfig "oppmg/common/config"
  11 + "oppmg/common/log"
  12 + "time"
  13 +)
  14 +
  15 +//请求企业平台的接口
  16 +type IBusinessAdminParam interface {
  17 + Format() []byte
  18 + GetPath() (string, string) //返回请求路径path,请求方式mathod
  19 +}
  20 +
  21 +type BusinessAdminClient struct {
  22 + baseUrl string
  23 +}
  24 +
  25 +func NewBusinessAdminClient() *BusinessAdminClient {
  26 + return &BusinessAdminClient{
  27 + baseUrl: mconfig.MConfig.BusinessAdminUrl,
  28 + }
  29 +}
  30 +
  31 +func (client BusinessAdminClient) buildHeader() http.Header {
  32 + var h = http.Header{}
  33 + h.Set("Content-Type", "application/json")
  34 + h.Set("Accept", "application/json")
  35 + return h
  36 +}
  37 +
  38 +//httpDo post发送json
  39 +func (client BusinessAdminClient) httpDo(path string, mathod string, posts []byte) ([]byte, error) {
  40 + httpclient := http.Client{
  41 + Timeout: 10 * time.Second, //请求超时时间5秒
  42 + }
  43 + reqURL := client.baseUrl + path
  44 + req, err := http.NewRequest(mathod, reqURL, bytes.NewReader(posts))
  45 + if err != nil {
  46 + return nil, err
  47 + }
  48 + req.Header = client.buildHeader()
  49 + resp, err := httpclient.Do(req)
  50 + log.Info("====>Send To URL:%s", reqURL)
  51 + log.Info("====>Send To BusinessAdmin:%s", string(posts))
  52 + if err != nil {
  53 + return nil, err
  54 + }
  55 + defer resp.Body.Close()
  56 + body, err := ioutil.ReadAll(resp.Body)
  57 + if err != nil {
  58 + return nil, err
  59 + }
  60 + log.Info("<====BusinessAdmin Return:%s", string(body))
  61 + return body, nil
  62 +}
  63 +
  64 +func (client BusinessAdminClient) Call(param IBusinessAdminParam) ([]byte, error) {
  65 + path, mathod := param.GetPath()
  66 + return client.httpDo(path, mathod, param.Format())
  67 +}
  68 +
  69 +type CommResponse struct {
  70 + Code int `json:"code"`
  71 + Msg string `json:"msg"`
  72 +}
  73 +
  74 +func (resp CommResponse) IsOK() bool {
  75 +
  76 + return true
  77 +}
  78 +
  79 +//GetuserAuth 企业后台鉴权
  80 +//userId 企业后台的用户id ,对应机会系统内的user_company表的字段id
  81 +//PlatformId 编号 机会系统固定值18
  82 +type RequestGetuserAuth struct {
  83 + UserId string `json:"userId"`
  84 + platformId string `json:"platformId"`
  85 +}
  86 +
  87 +type ResponseGetUserAuth struct {
  88 + CommResponse
  89 + Data struct {
  90 + UserAuth bool `json:"userAuth"`
  91 + } `json:"data"`
  92 +}
  93 +
  94 +func (r RequestGetuserAuth) Format() []byte {
  95 + r.platformId = "18"
  96 + var bt []byte
  97 + bt, _ = json.Marshal(r)
  98 + return bt
  99 +}
  100 +
  101 +func (r RequestGetuserAuth) GetPath() (string, string) {
  102 + return "/auth/get-user-auth", "POST"
  103 +}
  104 +
  105 +func GetuserAuthDo(userid int64) (ResponseGetUserAuth, error) {
  106 + param := RequestGetuserAuth{
  107 + UserId: fmt.Sprint(userid),
  108 + }
  109 + var resp ResponseGetUserAuth
  110 + uclient := NewBusinessAdminClient()
  111 + btBody, err := uclient.Call(param)
  112 + if err != nil {
  113 + log.Error("向企业平台发送请求失败 err:%s", err)
  114 + return resp, errors.New("向企业平台发送请求失败")
  115 + }
  116 + err = json.Unmarshal(btBody, &resp)
  117 + if err != nil {
  118 + log.Error("解析企业平台响应失败 err:%s", err)
  119 + return resp, errors.New("解析企业平台响应失败")
  120 + }
  121 + return resp, nil
  122 +}
@@ -8,6 +8,8 @@ import ( @@ -8,6 +8,8 @@ import (
8 "oppmg/protocol" 8 "oppmg/protocol"
9 ) 9 )
10 10
  11 +// 请求统一用户中心的接口
  12 +
11 //RequestUCenterLogin 调用用户中心进行账号密码登录 13 //RequestUCenterLogin 调用用户中心进行账号密码登录
12 func RequestUCenterLogin(account, password string) (*ResponseLogin, error) { 14 func RequestUCenterLogin(account, password string) (*ResponseLogin, error) {
13 var uclientReturn ResponseLogin 15 var uclientReturn ResponseLogin
@@ -19,7 +21,7 @@ func RequestUCenterLogin(account, password string) (*ResponseLogin, error) { @@ -19,7 +21,7 @@ func RequestUCenterLogin(account, password string) (*ResponseLogin, error) {
19 uclient := NewUCenterClient() 21 uclient := NewUCenterClient()
20 btBody, err := uclient.Call(param) 22 btBody, err := uclient.Call(param)
21 if err != nil { 23 if err != nil {
22 - log.Error("统一用户中心请求失败 err:%s", err) 24 + log.Error("向统一用户中心发送请求失败 err:%s", err)
23 return nil, protocol.NewErrWithMessage("1") 25 return nil, protocol.NewErrWithMessage("1")
24 } 26 }
25 err = json.Unmarshal(btBody, &uclientReturn) 27 err = json.Unmarshal(btBody, &uclientReturn)
@@ -27,7 +29,7 @@ func RequestUCenterLogin(account, password string) (*ResponseLogin, error) { @@ -27,7 +29,7 @@ func RequestUCenterLogin(account, password string) (*ResponseLogin, error) {
27 log.Error("解析统一用户中心响应失败 err:%s", err) 29 log.Error("解析统一用户中心响应失败 err:%s", err)
28 return nil, protocol.NewErrWithMessage("1") 30 return nil, protocol.NewErrWithMessage("1")
29 } 31 }
30 - if !(uclientReturn.Code == ResponseCode0) { 32 + if !(uclientReturn.Code == ResponseCodeOk) {
31 return nil, protocol.NewErrWithMessage("10052") 33 return nil, protocol.NewErrWithMessage("10052")
32 } 34 }
33 return &uclientReturn, nil 35 return &uclientReturn, nil
@@ -44,7 +46,7 @@ func RequestUCenterLoginBySecret(secret string) (*ResponseLogin, error) { @@ -44,7 +46,7 @@ func RequestUCenterLoginBySecret(secret string) (*ResponseLogin, error) {
44 uclient := NewUCenterClient() 46 uclient := NewUCenterClient()
45 btBody, err := uclient.Call(param) 47 btBody, err := uclient.Call(param)
46 if err != nil { 48 if err != nil {
47 - log.Error("统一用户中心请求失败 err:%s", err) 49 + log.Error("向统一用户中心发送请求失败 err:%s", err)
48 return nil, protocol.NewErrWithMessage("1") 50 return nil, protocol.NewErrWithMessage("1")
49 } 51 }
50 err = json.Unmarshal(btBody, &uclientReturn) 52 err = json.Unmarshal(btBody, &uclientReturn)
@@ -52,7 +54,7 @@ func RequestUCenterLoginBySecret(secret string) (*ResponseLogin, error) { @@ -52,7 +54,7 @@ func RequestUCenterLoginBySecret(secret string) (*ResponseLogin, error) {
52 log.Error("解析统一用户中心响应失败 err:%s", err) 54 log.Error("解析统一用户中心响应失败 err:%s", err)
53 return nil, protocol.NewErrWithMessage("1") 55 return nil, protocol.NewErrWithMessage("1")
54 } 56 }
55 - if !(uclientReturn.Code == ResponseCode0) { 57 + if !(uclientReturn.Code == ResponseCodeOk) {
56 return &uclientReturn, protocol.NewErrWithMessage("1") 58 return &uclientReturn, protocol.NewErrWithMessage("1")
57 } 59 }
58 return &uclientReturn, nil 60 return &uclientReturn, nil
@@ -75,7 +77,7 @@ func RequestUCenterAddUser(phone string, nickname string, avatar string) (*Respo @@ -75,7 +77,7 @@ func RequestUCenterAddUser(phone string, nickname string, avatar string) (*Respo
75 uclient := NewUCenterClient() 77 uclient := NewUCenterClient()
76 btBody, err := uclient.Call(param) 78 btBody, err := uclient.Call(param)
77 if err != nil { 79 if err != nil {
78 - log.Error("统一用户中心请求失败 err:%s", err) 80 + log.Error("向统一用户中心发送请求失败 err:%s", err)
79 return nil, protocol.NewErrWithMessage("1") 81 return nil, protocol.NewErrWithMessage("1")
80 } 82 }
81 err = json.Unmarshal(btBody, &ucenterReturn) 83 err = json.Unmarshal(btBody, &ucenterReturn)
@@ -83,7 +85,7 @@ func RequestUCenterAddUser(phone string, nickname string, avatar string) (*Respo @@ -83,7 +85,7 @@ func RequestUCenterAddUser(phone string, nickname string, avatar string) (*Respo
83 log.Error("解析统一用户中心响应失败 err:%s", err) 85 log.Error("解析统一用户中心响应失败 err:%s", err)
84 return nil, protocol.NewErrWithMessage("1") 86 return nil, protocol.NewErrWithMessage("1")
85 } 87 }
86 - if !(ucenterReturn.Code == ResponseCode0) { 88 + if !(ucenterReturn.Code == ResponseCodeOk) {
87 return nil, protocol.NewErrWithMessage("10052") 89 return nil, protocol.NewErrWithMessage("10052")
88 } 90 }
89 return &ucenterReturn, nil 91 return &ucenterReturn, nil
@@ -100,7 +102,7 @@ func RequestUCenterSmsCode(phone string) error { @@ -100,7 +102,7 @@ func RequestUCenterSmsCode(phone string) error {
100 uclient := NewUCenterClient() 102 uclient := NewUCenterClient()
101 btBody, err := uclient.Call(param) 103 btBody, err := uclient.Call(param)
102 if err != nil { 104 if err != nil {
103 - log.Error("统一用户中心请求失败 err:%s", err) 105 + log.Error("向统一用户中心发送请求失败 err:%s", err)
104 return protocol.NewErrWithMessage("1") 106 return protocol.NewErrWithMessage("1")
105 } 107 }
106 var ucenterReturn CommResponse 108 var ucenterReturn CommResponse
@@ -109,7 +111,7 @@ func RequestUCenterSmsCode(phone string) error { @@ -109,7 +111,7 @@ func RequestUCenterSmsCode(phone string) error {
109 log.Error("解析统一用户中心响应失败 err:%s", err) 111 log.Error("解析统一用户中心响应失败 err:%s", err)
110 return protocol.NewErrWithMessage("1") 112 return protocol.NewErrWithMessage("1")
111 } 113 }
112 - if !(ucenterReturn.Code == ResponseCode0) { 114 + if !(ucenterReturn.Code == ResponseCodeOk) {
113 return protocol.NewErrWithMessage("10052") 115 return protocol.NewErrWithMessage("10052")
114 } 116 }
115 return nil 117 return nil
@@ -125,7 +127,7 @@ func RequestUCenterLoginSms(phone string, captcha string) (*ResponseLoginSms, er @@ -125,7 +127,7 @@ func RequestUCenterLoginSms(phone string, captcha string) (*ResponseLoginSms, er
125 uclient := NewUCenterClient() 127 uclient := NewUCenterClient()
126 btBody, err := uclient.Call(param) 128 btBody, err := uclient.Call(param)
127 if err != nil { 129 if err != nil {
128 - log.Error("统一用户中心请求失败 err:%s", err) 130 + log.Error("向统一用户中心发送请求失败 err:%s", err)
129 return nil, protocol.NewErrWithMessage("1") 131 return nil, protocol.NewErrWithMessage("1")
130 } 132 }
131 var ucenterReturn ResponseLoginSms 133 var ucenterReturn ResponseLoginSms
@@ -134,7 +136,7 @@ func RequestUCenterLoginSms(phone string, captcha string) (*ResponseLoginSms, er @@ -134,7 +136,7 @@ func RequestUCenterLoginSms(phone string, captcha string) (*ResponseLoginSms, er
134 log.Error("解析统一用户中心响应失败 err:%s", err) 136 log.Error("解析统一用户中心响应失败 err:%s", err)
135 return nil, protocol.NewErrWithMessage("1") 137 return nil, protocol.NewErrWithMessage("1")
136 } 138 }
137 - if !(ucenterReturn.Code == ResponseCode0) { 139 + if !(ucenterReturn.Code == ResponseCodeOk) {
138 return &ucenterReturn, protocol.NewErrWithMessage("10052") 140 return &ucenterReturn, protocol.NewErrWithMessage("10052")
139 } 141 }
140 return &ucenterReturn, nil 142 return &ucenterReturn, nil
@@ -148,7 +150,7 @@ func RequestUCenterSmsCodeCheck(phone string, captcha string) (*ResponseSmsCodeC @@ -148,7 +150,7 @@ func RequestUCenterSmsCodeCheck(phone string, captcha string) (*ResponseSmsCodeC
148 uclient := NewUCenterClient() 150 uclient := NewUCenterClient()
149 btBody, err := uclient.Call(param) 151 btBody, err := uclient.Call(param)
150 if err != nil { 152 if err != nil {
151 - log.Error("统一用户中心请求失败 err:%s", err) 153 + log.Error("向统一用户中心发送请求失败 err:%s", err)
152 return nil, protocol.NewErrWithMessage("1") 154 return nil, protocol.NewErrWithMessage("1")
153 } 155 }
154 var ucenterReturn ResponseSmsCodeCheck 156 var ucenterReturn ResponseSmsCodeCheck
@@ -157,7 +159,7 @@ func RequestUCenterSmsCodeCheck(phone string, captcha string) (*ResponseSmsCodeC @@ -157,7 +159,7 @@ func RequestUCenterSmsCodeCheck(phone string, captcha string) (*ResponseSmsCodeC
157 log.Error("解析统一用户中心响应失败 err:%s", err) 159 log.Error("解析统一用户中心响应失败 err:%s", err)
158 return nil, protocol.NewErrWithMessage("1") 160 return nil, protocol.NewErrWithMessage("1")
159 } 161 }
160 - if !(ucenterReturn.Code == ResponseCode0) { 162 + if !(ucenterReturn.Code == ResponseCodeOk) {
161 return &ucenterReturn, protocol.NewErrWithMessage("10026") 163 return &ucenterReturn, protocol.NewErrWithMessage("10026")
162 } 164 }
163 return &ucenterReturn, nil 165 return &ucenterReturn, nil
@@ -173,7 +175,7 @@ func RequestUCenterRestPassword(phone, newPwd, confirmPwd, certificate string) e @@ -173,7 +175,7 @@ func RequestUCenterRestPassword(phone, newPwd, confirmPwd, certificate string) e
173 uclient := NewUCenterClient() 175 uclient := NewUCenterClient()
174 btBody, err := uclient.Call(param) 176 btBody, err := uclient.Call(param)
175 if err != nil { 177 if err != nil {
176 - log.Error("统一用户中心请求失败 err:%s", err) 178 + log.Error("向统一用户中心发送请求失败 err:%s", err)
177 return protocol.NewErrWithMessage("1") 179 return protocol.NewErrWithMessage("1")
178 } 180 }
179 var ucenterReturn CommResponse 181 var ucenterReturn CommResponse
@@ -182,7 +184,7 @@ func RequestUCenterRestPassword(phone, newPwd, confirmPwd, certificate string) e @@ -182,7 +184,7 @@ func RequestUCenterRestPassword(phone, newPwd, confirmPwd, certificate string) e
182 log.Error("解析统一用户中心响应失败 err:%s", err) 184 log.Error("解析统一用户中心响应失败 err:%s", err)
183 return protocol.NewErrWithMessage("1") 185 return protocol.NewErrWithMessage("1")
184 } 186 }
185 - if !(ucenterReturn.Code == ResponseCode0) { 187 + if !(ucenterReturn.Code == ResponseCodeOk) {
186 return protocol.NewErrWithMessage("10052") 188 return protocol.NewErrWithMessage("10052")
187 } 189 }
188 190
@@ -12,7 +12,7 @@ import ( @@ -12,7 +12,7 @@ import (
12 ) 12 )
13 13
14 const ( 14 const (
15 - ResponseCode0 int = 0 15 + ResponseCodeOk int = 0
16 ) 16 )
17 17
18 type IUCenterParam interface { 18 type IUCenterParam interface {