test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "github.com/gin-gonic/gin"
  6. "github.com/xuperchain/xuper-sdk-go/v2/account"
  7. "github.com/xuperchain/xuper-sdk-go/v2/xuper"
  8. )
  9. // 配置跨域函数
  10. func Cors() gin.HandlerFunc {
  11. return func(c *gin.Context) {
  12. method := c.Request.Method
  13. origin := c.Request.Header.Get("Origin")
  14. if origin != "" {
  15. c.Header("Access-Control-Allow-Origin", "*")
  16. c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
  17. c.Header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization")
  18. c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Cache-Control, Content-Language, Content-Type")
  19. c.Header("Access-Control-Allow-Credentials", "true")
  20. }
  21. if method == "OPTIONS" {
  22. c.AbortWithStatus(http.StatusNoContent)
  23. }
  24. c.Next()
  25. }
  26. }
  27. func main() {
  28. gin.SetMode(gin.ReleaseMode)
  29. // 1.创建路由
  30. r := gin.Default()
  31. // 开启跨域
  32. r.Use(Cors())
  33. // 2.绑定路由规则,执行的函数
  34. // gin.Context,封装了request和response
  35. r.POST("/query", func(c *gin.Context) {
  36. id := c.PostForm("id")
  37. resp:=queryRecords(id)
  38. c.JSON(200, gin.H{"resp": resp})
  39. })
  40. r.POST("/create", func(c *gin.Context) {
  41. id := c.PostForm("id")
  42. desc := c.PostForm("desc")
  43. account := c.PostForm("account")
  44. contractAccount := c.PostForm("contractAccount")
  45. resp:=addRecords(id,desc,account,contractAccount)
  46. c.JSON(200, gin.H{"resp": resp})
  47. })
  48. r.POST("/update", func(c *gin.Context) {
  49. id := c.PostForm("id")
  50. desc := c.PostForm("desc")
  51. account := c.PostForm("account")
  52. contractAccount := c.PostForm("contractAccount")
  53. resp:=updateRecords(id,desc,account,contractAccount)
  54. c.JSON(200, gin.H{"resp": resp})
  55. })
  56. r.POST("/addAdmin", func(c *gin.Context) {
  57. address := c.PostForm("address")
  58. account := c.PostForm("account")
  59. contractAccount := c.PostForm("contractAccount")
  60. resp:=addAdmin(address,account,contractAccount)
  61. c.JSON(200, gin.H{"resp": resp})
  62. })
  63. // 3.监听端口,默认在8080
  64. // Run("里面不指定端口号默认为8080")
  65. r.Run(":8000")
  66. }
  67. func queryRecords(id string) string {
  68. xuperClient, err := xuper.New("39.156.69.83:37100")
  69. if err != nil {
  70. resp:= fmt.Sprintf("new xuper Client error: %v\n", err)
  71. return resp
  72. }
  73. account, err := account.RetrieveAccount("铝 纵 殿 熙 何 云 关 棒 悟 信 析 造", 1)
  74. if err != nil {
  75. resp:= fmt.Sprintf("retrieveAccount err: %v\n", err)
  76. return resp
  77. }
  78. var contractName string="antifakecon"
  79. args := map[string]string{
  80. "id": id,
  81. }
  82. tx, err := xuperClient.QueryWasmContract(account, contractName, "queryRecords", args)
  83. if err != nil {
  84. resp:=fmt.Sprintf("query err: %v\n", err)
  85. return resp
  86. }
  87. resp:=fmt.Sprintf("data:%s", tx.ContractResponse.Body)
  88. return resp
  89. }
  90. func addRecords(id string,desc string,key string,contractAccount string) string {
  91. xuperClient, err := xuper.New("39.156.69.83:37100")
  92. if err != nil {
  93. resp:= fmt.Sprintf("new xuper Client error: %v\n", err)
  94. return resp
  95. }
  96. account, err := account.RetrieveAccount(key, 1)
  97. if err != nil {
  98. resp:= fmt.Sprintf("retrieveAccount err: %v\n", err)
  99. return resp
  100. }
  101. err = account.SetContractAccount(contractAccount)
  102. if err != nil {
  103. resp:= fmt.Sprintf("setContractAccount err: %v\n", err)
  104. return resp
  105. }
  106. var contractName string="antifakecon"
  107. args := map[string]string{
  108. "id": id,
  109. "desc":desc,
  110. }
  111. tx, err := xuperClient.InvokeWasmContract(account, contractName, "createGoods", args)
  112. if err != nil {
  113. resp:=fmt.Sprintf("create err: %v\n", err)
  114. return resp
  115. }
  116. resp:=fmt.Sprintf("data:%s TxID:%x", tx.ContractResponse.Body, tx.Tx.Txid)
  117. return resp
  118. }
  119. func updateRecords(id string,desc string,key string,contractAccount string) string {
  120. xuperClient, err := xuper.New("39.156.69.83:37100")
  121. if err != nil {
  122. resp:= fmt.Sprintf("new xuper Client error: %v\n", err)
  123. return resp
  124. }
  125. account, err := account.RetrieveAccount(key, 1)
  126. if err != nil {
  127. resp:= fmt.Sprintf("retrieveAccount err: %v\n", err)
  128. return resp
  129. }
  130. err = account.SetContractAccount(contractAccount)
  131. if err != nil {
  132. resp:= fmt.Sprintf("setContractAccount err: %v\n", err)
  133. return resp
  134. }
  135. var contractName string="antifakecon"
  136. args := map[string]string{
  137. "id": id,
  138. "reason":desc,
  139. }
  140. tx, err := xuperClient.InvokeWasmContract(account, contractName, "updateGoods", args)
  141. if err != nil {
  142. resp:=fmt.Sprintf("update err: %v\n", err)
  143. return resp
  144. }
  145. resp:=fmt.Sprintf("data:%s TxID:%x", tx.ContractResponse.Body, tx.Tx.Txid)
  146. return resp
  147. }
  148. func addAdmin(address string,key string,contractAccount string) string {
  149. xuperClient, err := xuper.New("39.156.69.83:37100")
  150. if err != nil {
  151. resp:= fmt.Sprintf("new xuper Client error: %v\n", err)
  152. return resp
  153. }
  154. account, err := account.RetrieveAccount(key, 1)
  155. if err != nil {
  156. resp:= fmt.Sprintf("retrieveAccount err: %v\n", err)
  157. return resp
  158. }
  159. err = account.SetContractAccount(contractAccount)
  160. if err != nil {
  161. resp:= fmt.Sprintf("setContractAccount err: %v\n", err)
  162. return resp
  163. }
  164. var contractName string="antifakecon"
  165. args := map[string]string{
  166. "admin": address,
  167. }
  168. tx, err := xuperClient.InvokeWasmContract(account, contractName, "addAdmin", args)
  169. if err != nil {
  170. resp:=fmt.Sprintf("update err: %v\n", err)
  171. return resp
  172. }
  173. resp:=fmt.Sprintf("data:%s TxID:%x", tx.ContractResponse.Body, tx.Tx.Txid)
  174. return resp
  175. }