package orderrpc import ( "crypto/md5" "encoding/hex" "encoding/json" "errors" "log" "strconv" "strings" "time" "git.tetele.net/tgo/crypter" "github.com/golang/protobuf/proto" ) type OrderCreateRes struct { OrderSn string } func Create(dbname, site_id string, data map[string]string, url ...string) (*OrderCreateRes, error) { var order_rpc_url string = "127.0.0.1:7973" if len(url) > 0 && url[0] != "" { order_rpc_url = url[0] } conn, _, err := DialOrderService("tcp", order_rpc_url) if err != nil { return nil, err } defer conn.Close() data["dbname"] = dbname data["site_id"] = site_id data_json, err := json.Marshal(data) if err != nil { return nil, err } encryData := crypter.DesEn(string(data_json), "ordernew") now_int64 := time.Now().Unix() now := strconv.FormatInt(now_int64, 10) sign := Sign(encryData, now) req := &CreateRequest{ proto.String(encryData), proto.String(now), proto.String(sign), nil} res := &CreateResponse{} err = conn.Create(req, res) if err != nil { return nil, err } return HandleRes(res) // return map[string]interface{}{ // "code": res.GetCode(), // "msg": res.GetMsg(), // "order_sn": res.GetOrderSn()}, err } func CreateByChannel(dbname, site_id string, data map[string]string, url ...string) (*OrderCreateRes, error) { var order_rpc_url string = "127.0.0.1:7973" if len(url) > 0 && url[0] != "" { order_rpc_url = url[0] } conn, _, err := DialOrderService("tcp", order_rpc_url) if err != nil { return nil, err } defer conn.Close() data["dbname"] = dbname data["site_id"] = site_id data["source"] = "channel" data_json, err := json.Marshal(data) if err != nil { return nil, err } encryData := crypter.DesEn(string(data_json), "ordernew") now := strconv.FormatInt(time.Now().Unix(), 10) sign := Sign(encryData, now) req := &CreateRequest{ proto.String(encryData), proto.String(now), proto.String(sign), nil} res := &CreateResponse{} err = conn.Create(req, res) if err != nil { return nil, err } return HandleRes(res) // return map[string]interface{}{ // "code": res.GetCode(), // "msg": res.GetMsg(), // "order_sn": res.GetOrderSn()}, err } func CreateByCart(dbname, site_id string, data map[string]interface{}, url ...string) (*OrderCreateRes, error) { var order_rpc_url string = "127.0.0.1:7973" if len(url) > 0 && url[0] != "" { order_rpc_url = url[0] } conn, _, err := DialOrderService("tcp", order_rpc_url) if err != nil { return nil, err } defer conn.Close() data["dbname"] = dbname data["site_id"] = site_id data_json, err := json.Marshal(data) if err != nil { return nil, err } log.Println(string(data_json)) encryData := crypter.DesEn(string(data_json), "cartsnew") now := strconv.FormatInt(time.Now().Unix(), 10) sign := Sign(encryData, now) req := &CreateRequest{ proto.String(encryData), proto.String(now), proto.String(sign), nil} res := &CreateResponse{} err = conn.CreateByCart(req, res) if err != nil { return nil, err } return HandleRes(res) // return map[string]interface{}{ // "code": res.GetCode(), // "msg": res.GetMsg(), // "order_sn": res.GetOrderSn()}, err } /** * 签名 */ func Sign(data string, salt string) string { var build strings.Builder build.WriteString(data) build.WriteString(salt) build.WriteString("neworder") data_str := build.String() h := md5.New() h.Write([]byte(data_str)) // 需要加密的字符串 return hex.EncodeToString(h.Sum(nil)) // 输出加密结果 } /** * 验证签名 */ func CheckSign(sign_str, data, salt string) bool { sign := Sign(data, salt) if strings.Compare(sign_str, sign) > -1 { return true } return false } /** * 处理返回结果 */ func HandleRes(res *CreateResponse) (*OrderCreateRes, 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, "ordernew") var res_arr OrderCreateRes err = json.Unmarshal([]byte(res_data_de), &res_arr) if err != nil { return nil, err } return &res_arr, nil }