forked from BIT_SCST_STIA/SmartMurphytt
222 lines
5.4 KiB
Go
222 lines
5.4 KiB
Go
package controller
|
|
|
|
import (
|
|
"chainmaker.org/chainmaker/pb-go/v2/common"
|
|
sdk "chainmaker.org/chainmaker/sdk-go/v2"
|
|
"chainmaker.org/chainmaker/sdk-go/v2/examples"
|
|
"chainmaker_go/database"
|
|
"chainmaker_go/model"
|
|
"fmt"
|
|
"github.com/gin-gonic/gin"
|
|
"log"
|
|
"strconv"
|
|
)
|
|
|
|
func TransferOwnership(ctx *gin.Context) {
|
|
DB := database.GetDB()
|
|
|
|
cookie, err := ctx.Cookie("username")
|
|
|
|
if err != nil {
|
|
cookie = "NotSet"
|
|
}
|
|
fmt.Printf("Cookie value: %s \n", cookie)
|
|
|
|
//获取参数
|
|
user_id = cookie
|
|
|
|
//查询该用户所有资源
|
|
var transferOwnerships []model.TransferOwnership
|
|
DB.Where("Authorized_userid = ? ", user_id).Find(&transferOwnerships)
|
|
|
|
//返回结果
|
|
ctx.JSON(200, gin.H{
|
|
"code": 200,
|
|
"msg": "登录成功",
|
|
"data": transferOwnerships,
|
|
})
|
|
}
|
|
|
|
func TransferOwnershipRow(ctx *gin.Context) {
|
|
DB := database.GetDB()
|
|
|
|
cookie, err := ctx.Cookie("username")
|
|
if err != nil {
|
|
cookie = "NotSet"
|
|
}
|
|
fmt.Printf("Cookie value: %s \n", cookie)
|
|
|
|
//获取参数
|
|
user_id = cookie
|
|
|
|
//获取参数
|
|
var requestRow = model.Row{}
|
|
ctx.Bind(&requestRow)
|
|
id := requestRow.Id
|
|
int_id, err := strconv.Atoi(id)
|
|
if err != nil {
|
|
fmt.Println("error")
|
|
}
|
|
|
|
//查询该用户所有资源
|
|
var resources []model.Resource
|
|
DB.Where("User_id = ? ", user_id).Find(&resources)
|
|
var resource model.Resource
|
|
resource = resources[int_id]
|
|
var transferOwnership model.TransferOwnership
|
|
transferOwnership.Resource_uniqueid = resource.Resource_uniqueid
|
|
transferOwnership.Origin_userid = user_id
|
|
transferOwnership.Authorized_userid = ""
|
|
transferOwnership.Right_type = resource.Right_type
|
|
transferOwnership.Resource_name = resource.Resource_name
|
|
transferOwnership.Resource_describe = resource.Resource_describe
|
|
transferOwnership.Contract_describe = ""
|
|
transferOwnership.Time = ""
|
|
transferOwnership.Data_sign = ""
|
|
|
|
//返回结果
|
|
ctx.JSON(200, gin.H{
|
|
"code": 200,
|
|
"msg": "登录成功",
|
|
"data": transferOwnership,
|
|
})
|
|
}
|
|
|
|
func TransferOwnershipYes(ctx *gin.Context) {
|
|
DB := database.GetDB()
|
|
|
|
var requestRow = model.Row{}
|
|
ctx.Bind(&requestRow)
|
|
//获取参数
|
|
|
|
id := requestRow.Id
|
|
|
|
int_id, err := strconv.Atoi(id)
|
|
if err != nil {
|
|
fmt.Println("error")
|
|
}
|
|
|
|
cookie, err := ctx.Cookie("username")
|
|
|
|
if err != nil {
|
|
cookie = "NotSet"
|
|
}
|
|
fmt.Printf("Cookie value: %s \n", cookie)
|
|
|
|
//获取参数
|
|
user_id = cookie
|
|
//查询该用户所有资源
|
|
var transferOwnerships []model.TransferOwnership
|
|
DB.Where("Authorized_userid = ? ", user_id).Find(&transferOwnerships)
|
|
var transferOwnership model.TransferOwnership
|
|
transferOwnership = transferOwnerships[int_id]
|
|
|
|
resource_id = transferOwnership.Resource_uniqueid
|
|
old_user_id = transferOwnership.Origin_userid
|
|
new_user_id = transferOwnership.Authorized_userid
|
|
right_type = transferOwnership.Right_type
|
|
contract = transferOwnership.Contract_describe
|
|
time = transferOwnership.Time
|
|
|
|
var resource model.Resource
|
|
DB.Where("resource_uniqueid = ?", resource_id).First(&resource)
|
|
newResource := model.Resource{
|
|
User_id: user_id,
|
|
Right_type: right_type,
|
|
Resource_name: resource.Resource_name,
|
|
Resource_describe: resource.Resource_describe,
|
|
Resource_data_hash: resource.Resource_data_hash,
|
|
Resource_refer: resource.Resource_refer,
|
|
Resource_root: resource.Resource_root,
|
|
Time: time,
|
|
Data_sign: "",
|
|
Resource_uniqueid: resource_id,
|
|
Is_right_confirmation: "是",
|
|
}
|
|
DB.Create(&newResource)
|
|
|
|
DB.Delete(&transferOwnership)
|
|
|
|
//注册信息上链
|
|
fmt.Println("====================== create client ======================")
|
|
client, err := examples.CreateChainClientWithSDKConf(sdkConfigOrg1Client1Path)
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
|
|
fmt.Println("====================== 调用合约 ======================")
|
|
err = testUserContractClaimInvoke3(client, "invoke_contract", true)
|
|
if err != nil {
|
|
log.Fatalln(err)
|
|
}
|
|
|
|
//返回结果
|
|
ctx.JSON(200, gin.H{
|
|
"code": 200,
|
|
"msg": "权属转让成功",
|
|
})
|
|
}
|
|
|
|
func testUserContractClaimInvoke3(client *sdk.ChainClient,
|
|
method string, withSyncResult bool) error {
|
|
|
|
kvs := []*common.KeyValuePair{
|
|
{
|
|
Key: "method",
|
|
Value: []byte("transfer_ownership"),
|
|
},
|
|
{
|
|
Key: "resource_id",
|
|
Value: []byte(resource_id),
|
|
},
|
|
{
|
|
Key: "old_user_id",
|
|
Value: []byte(old_user_id),
|
|
},
|
|
{
|
|
Key: "new_user_id",
|
|
Value: []byte(new_user_id),
|
|
},
|
|
{
|
|
Key: "right_type",
|
|
Value: []byte(right_type),
|
|
},
|
|
{
|
|
Key: "contract",
|
|
Value: []byte(contract),
|
|
},
|
|
{
|
|
Key: "time",
|
|
Value: []byte(time),
|
|
},
|
|
}
|
|
|
|
err := invokeUserContract3(client, claimContractName1, method, "", kvs, withSyncResult, &common.Limit{GasLimit: 200000})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func invokeUserContract3(client *sdk.ChainClient, contractName, method, txId string,
|
|
kvs []*common.KeyValuePair, withSyncResult bool, limit *common.Limit) error {
|
|
|
|
resp, err := client.InvokeContractWithLimit(contractName, method, txId, kvs, -1, withSyncResult, limit)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if resp.Code != common.TxStatusCode_SUCCESS {
|
|
return fmt.Errorf("invoke contract failed, [code:%d]/[msg:%s]\n", resp.Code, resp.Message)
|
|
}
|
|
|
|
if !withSyncResult {
|
|
fmt.Printf("invoke contract success, resp: [code:%d]/[msg:%s]/[txId:%s]\n", resp.Code, resp.Message, resp.ContractResult.Result)
|
|
} else {
|
|
fmt.Printf("invoke contract success, resp: [code:%d]/[msg:%s]/[contractResult:%s]\n", resp.Code, resp.Message, resp.ContractResult)
|
|
}
|
|
|
|
return nil
|
|
}
|