rpc
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

240 lines
4.5 KiB

3 years ago
3 years ago
3 years ago
  1. package order
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "encoding/json"
  6. "errors"
  7. "log"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "git.tetele.net/tgo/crypter"
  12. "github.com/golang/protobuf/proto"
  13. )
  14. type OrderCreateRes struct {
  15. OrderSn string
  16. }
  17. func Create(dbname, site_id string, data map[string]string, url ...string) (*OrderCreateRes, error) {
  18. var order_rpc_url string = "127.0.0.1:7973"
  19. if len(url) > 0 && url[0] != "" {
  20. order_rpc_url = url[0]
  21. }
  22. conn, _, err := DialOrderService("tcp", order_rpc_url)
  23. if err != nil {
  24. return nil, err
  25. }
  26. defer conn.Close()
  27. data["dbname"] = dbname
  28. data["site_id"] = site_id
  29. data_json, err := json.Marshal(data)
  30. if err != nil {
  31. return nil, err
  32. }
  33. encryData := crypter.DesEn(string(data_json), "ordernew")
  34. now_int64 := time.Now().Unix()
  35. now := strconv.FormatInt(now_int64, 10)
  36. sign := Sign(encryData, now)
  37. req := &CreateRequest{
  38. proto.String(encryData),
  39. proto.String(now),
  40. proto.String(sign),
  41. nil}
  42. res := &CreateResponse{}
  43. err = conn.Create(req, res)
  44. if err != nil {
  45. return nil, err
  46. }
  47. return HandleRes(res)
  48. // return map[string]interface{}{
  49. // "code": res.GetCode(),
  50. // "msg": res.GetMsg(),
  51. // "order_sn": res.GetOrderSn()}, err
  52. }
  53. func CreateByChannel(dbname, site_id string, data map[string]string, url ...string) (*OrderCreateRes, error) {
  54. var order_rpc_url string = "127.0.0.1:7973"
  55. if len(url) > 0 && url[0] != "" {
  56. order_rpc_url = url[0]
  57. }
  58. conn, _, err := DialOrderService("tcp", order_rpc_url)
  59. if err != nil {
  60. return nil, err
  61. }
  62. defer conn.Close()
  63. data["dbname"] = dbname
  64. data["site_id"] = site_id
  65. data["source"] = "channel"
  66. data_json, err := json.Marshal(data)
  67. if err != nil {
  68. return nil, err
  69. }
  70. encryData := crypter.DesEn(string(data_json), "ordernew")
  71. now := strconv.FormatInt(time.Now().Unix(), 10)
  72. sign := Sign(encryData, now)
  73. req := &CreateRequest{
  74. proto.String(encryData),
  75. proto.String(now),
  76. proto.String(sign),
  77. nil}
  78. res := &CreateResponse{}
  79. err = conn.Create(req, res)
  80. if err != nil {
  81. return nil, err
  82. }
  83. return HandleRes(res)
  84. // return map[string]interface{}{
  85. // "code": res.GetCode(),
  86. // "msg": res.GetMsg(),
  87. // "order_sn": res.GetOrderSn()}, err
  88. }
  89. func CreateByCart(dbname, site_id string, data map[string]interface{}, url ...string) (*OrderCreateRes, error) {
  90. var order_rpc_url string = "127.0.0.1:7973"
  91. if len(url) > 0 && url[0] != "" {
  92. order_rpc_url = url[0]
  93. }
  94. conn, _, err := DialOrderService("tcp", order_rpc_url)
  95. if err != nil {
  96. return nil, err
  97. }
  98. defer conn.Close()
  99. data["dbname"] = dbname
  100. data["site_id"] = site_id
  101. data_json, err := json.Marshal(data)
  102. if err != nil {
  103. return nil, err
  104. }
  105. log.Println(string(data_json))
  106. encryData := crypter.DesEn(string(data_json), "cartsnew")
  107. now := strconv.FormatInt(time.Now().Unix(), 10)
  108. sign := Sign(encryData, now)
  109. req := &CreateRequest{
  110. proto.String(encryData),
  111. proto.String(now),
  112. proto.String(sign),
  113. nil}
  114. res := &CreateResponse{}
  115. err = conn.CreateByCart(req, res)
  116. if err != nil {
  117. return nil, err
  118. }
  119. return HandleRes(res)
  120. // return map[string]interface{}{
  121. // "code": res.GetCode(),
  122. // "msg": res.GetMsg(),
  123. // "order_sn": res.GetOrderSn()}, err
  124. }
  125. /**
  126. * 签名
  127. */
  128. func Sign(data string, salt string) string {
  129. var build strings.Builder
  130. build.WriteString(data)
  131. build.WriteString(salt)
  132. build.WriteString("neworder")
  133. data_str := build.String()
  134. h := md5.New()
  135. h.Write([]byte(data_str)) // 需要加密的字符串
  136. return hex.EncodeToString(h.Sum(nil)) // 输出加密结果
  137. }
  138. /**
  139. * 验证签名
  140. */
  141. func CheckSign(sign_str, data, salt string) bool {
  142. sign := Sign(data, salt)
  143. if strings.Compare(sign_str, sign) > -1 {
  144. return true
  145. }
  146. return false
  147. }
  148. /**
  149. * 处理返回结果
  150. */
  151. func HandleRes(res *CreateResponse) (*OrderCreateRes, error) {
  152. res_data := res.GetData()
  153. if res_data == "" {
  154. return nil, errors.New("未收到收据")
  155. }
  156. time_int64, err := strconv.ParseInt(res.GetTime(), 10, 64)
  157. if err != nil {
  158. return nil, err
  159. }
  160. now_int64 := time.Now().Unix()
  161. if now_int64-time_int64 > 10 || time_int64-now_int64 > 10 {
  162. //时间误差前后10秒,返回
  163. return nil, errors.New("返回时间错误")
  164. }
  165. check_sign := CheckSign(res.GetSign(), res_data, res.GetTime())
  166. if !check_sign {
  167. return nil, errors.New("返回数据签名错误")
  168. }
  169. //解密
  170. res_data_de := crypter.DesDe(res_data, "ordernew")
  171. var res_arr OrderCreateRes
  172. err = json.Unmarshal([]byte(res_data_de), &res_arr)
  173. if err != nil {
  174. return nil, err
  175. }
  176. return &res_arr, nil
  177. }