Browse Source

增加id取用户方法

master v0.4.3
guzeng 3 years ago
parent
commit
bf64e92a1e
3 changed files with 118 additions and 21 deletions
  1. +43
    -0
      sign.go
  2. +54
    -0
      user.client.go
  3. +21
    -21
      user.client_test.go

+ 43
- 0
sign.go View File

@ -84,3 +84,46 @@ func HandleRes(res *Response) (*Res, error) {
return &res_arr, nil
}
/**
* 处理返回结果
*/
func HandleUserRes(res *Response) (map[string]string, error) {
res_data := res.GetData()
if res_data == "" {
return nil, errors.New("未收到收据")
}
time_int64, err := strconv.ParseInt(res.GetTime(), 10, 64)
if err != nil {
return nil, err
}
now_int64 := time.Now().Unix()
if now_int64-time_int64 > 10 || time_int64-now_int64 > 10 {
//时间误差前后10秒,返回
return nil, errors.New("返回时间错误")
}
check_sign := CheckSign(res.GetSign(), res_data, res.GetTime())
if !check_sign {
return nil, errors.New("返回数据签名错误")
}
//解密
res_data_de := crypter.DesDe(res_data, DES_KEY)
var res_arr map[string]string
err = json.Unmarshal([]byte(res_data_de), &res_arr)
if err != nil {
return nil, err
}
return res_arr, nil
}

+ 54
- 0
user.client.go View File

@ -1,6 +1,12 @@
package userrpc
import (
"encoding/json"
"strconv"
"time"
"git.tetele.net/tgo/crypter"
"github.com/golang/protobuf/proto"
)
@ -149,3 +155,51 @@ func Login(dbname, account, password string, url ...string) (map[string]string,
return map[string]string{}, nil
}
/**
* 查找商户信息
* 2021/04/06
* gz
*/
func GetById(site_id, dbname, user_id string, url ...string) (map[string]string, error) {
conn, err := rpc_server_conn(url...)
if err != nil {
return nil, err
}
defer conn.Close()
data := GetUserReq{}
data.SiteId = site_id
data.Dbname = dbname
data.UserId = user_id
data_json, err := json.Marshal(data)
if err != nil {
return nil, err
}
encryData := crypter.DesEn(string(data_json), DES_KEY)
now_int64 := time.Now().Unix()
now := strconv.FormatInt(now_int64, 10)
sign := Sign(encryData, now)
req := &Request{
proto.String(encryData),
proto.String(now),
proto.String(sign),
nil}
res := &Response{}
err = conn.GetById(req, res)
if err != nil {
return nil, err
}
return HandleUserRes(res)
}

+ 21
- 21
user.client_test.go View File

@ -5,31 +5,31 @@ import (
)
func Test_GetUserByToken(t *testing.T) {
dbname := "dev_tetele_net"
token := "5536827c-36a5-4ec7-946d-49e4380c5103"
res, err := GetUserByToken(dbname, token, "111.229.34.252:7976")
t.Log(res)
t.Log(res["UserId"])
t.Log(err)
dbname := "shop_v2"
// token := "5536827c-36a5-4ec7-946d-49e4380c5103"
// res, err := GetUserByToken(dbname, token, "111.229.34.252:7976")
// t.Log(res)
// t.Log(res["UserId"])
// t.Log(err)
res2, err := GetUserByUsername(dbname, "tetele")
res2, err := GetById("1023", dbname, "3")
t.Log(res2)
t.Log(res2.GetUserId())
// t.Log(res2.GetUserId())
t.Log(err)
res3, err := GetUserByUsercode(dbname, "2233")
t.Log(res3)
t.Log(*res3.UserId)
t.Log(err)
// res3, err := GetUserByUsercode(dbname, "2233")
// t.Log(res3)
// t.Log(*res3.UserId)
// t.Log(err)
}
func Test_Login(t *testing.T) {
dbname := "dev_tetele_net"
account := "admin"
password := "e10adc3949ba59abbe56e057f20f883e"
res, err := Login(dbname, account, password)
t.Log(res)
t.Log(res["UserId"])
t.Log(err)
// func Test_Login(t *testing.T) {
// dbname := "dev_tetele_net"
// account := "admin"
// password := "e10adc3949ba59abbe56e057f20f883e"
// res, err := Login(dbname, account, password)
// t.Log(res)
// t.Log(res["UserId"])
// t.Log(err)
}
// }

Loading…
Cancel
Save